Skip to content

Commit 1c9d167

Browse files
committed
modify TextHistory and readme
1 parent 5315d08 commit 1c9d167

File tree

7 files changed

+81
-44
lines changed

7 files changed

+81
-44
lines changed

README.CN.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
[![.Net35](https://img.shields.io/badge/DotNet-3.5-blue)](https://www.microsoft.com/zh-cn/download/details.aspx?id=25150)
2+
[![LICENSE](https://img.shields.io/badge/License-MIT-green)](https://github.com/DebugST/STNodeEditor/blob/main/LICENSE)
3+
4+
# STTextBox
5+
`STTextBox`是我和我的朋友[netero](https://github.com/0x54164)使用`GDI`构建的一个`WinForm`控件,可以支持`Emoji`
6+
7+
我们采用MIT开源协议,使用`.Net3.5` + `vs2010`构建的项目,所以几乎可以兼容任何VS版本,
8+
在开发的过程中我们还衍生出两个额外的开源项目:
9+
10+
[emoji-svg-render](https://github.com/DebugST/emoji-svg-render),
11+
[STGraphemeSplitter](https://github.com/DebugST/STGraphemeSplitter)
12+
13+
因为能力有限,很多功能我们都无法很好的实现,所以将一些有代表性的功能独立成了接口。
14+
如果有能力且愿意去修改它们的开发者可以直接继承接口实现,不用在项目中到处找代码了。
15+
16+
![Emoji](https://s3.bmp.ovh/imgs/2022/08/01/870c128600fcaf5b.png)
17+
18+
你可以在`STTextBox`中使用`Emoji`,在`demos_bin`的样例我们使用的是推特的表情符[twemoji](https://github.com/twitter/twemoji).
19+
更多信息可以查看: [emoji-svg-render](https://github.com/DebugST/emoji-svg-render)
20+
21+
![Alpha](https://s3.bmp.ovh/imgs/2022/08/01/9adb88ed6966ba5b.png)
22+
23+
`STTextBox`中的所有颜色你都可以使用`alpha`通道,当然代价就是渲染的速度会变慢。
24+
25+
![Style](https://s3.bmp.ovh/imgs/2022/08/01/d18e93176e4a4e48.png)
26+
27+
你可以自定义文本样式,通过实现`ITextStyleMonitor`接口,在`STTextBox`中内置了4个样式监视器。
28+
`KeyWorldStyleMonitor` `CSharpStyleMonitor` `LinkStyleMonitor` `SelectionStyleMonitor`
29+
30+
注意:`STTextBox`的渲染速度和内容没太大关系,而是和当前UI中需要绘制的字符个数有关,因为一些原因我们采用的是逐个字符绘制。
31+
为了让颜色可以使用`Alpha`通道,所以我们并没有对绘制的内容做缓存处理,因为背景可能是变化的。而且要做缓存也是一个麻烦事情。
32+
所以我们暂时没有考虑极端情况的渲染速度。
33+

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[![.Net35](https://img.shields.io/badge/DotNet-3.5-blue)](https://www.microsoft.com/zh-cn/download/details.aspx?id=25150)
22
[![LICENSE](https://img.shields.io/badge/License-MIT-green)](https://github.com/DebugST/STNodeEditor/blob/main/LICENSE)
33

4+
[简体中文](./README.CN.md)
5+
46
# STTextBox
57
STTextBox is a pure GDI-drawn WinForm control, which can support transparent colors and Emoji, etc., and can customize the display style of text through the text style monitor.
68

@@ -32,3 +34,10 @@ In `STTextBox` all the color you can use `alpha`, Of course his efficiency is al
3234
You can also customize text styles, by implementing the `ITextStyleMonitor` interface, we have four realities built in.
3335
`KeyWorldStyleMonitor` `CSharpStyleMonitor` `LinkStyleMonitor` `SelectionStyleMonitor`.
3436

37+
Note: The rendering speed of `STTextBox` has nothing to do with the content,
38+
but is related to the number of characters that need to be drawn in the current UI.
39+
For some reasons, we use character-by-character drawing.
40+
In order for the colors to use the `Alpha` channel, we don't cache what's being drawn,
41+
since the background may change. And doing caching is also a hassle.
42+
So we have not considered the rendering speed of extreme cases for the time being.
43+

ST.Library.UI.STTextBox/Implementation/TextHistory.cs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,13 @@ public class TextHistory : ITextHistory
1010
private int m_nIndex;
1111
private int m_nCount;
1212
private int m_nMax;
13-
private TextManager m_textManager;
1413
private TextHistoryRecord[][] m_arr;
1514

1615
public TextHistory(int nCount) {
1716
m_nMax = nCount;
1817
m_arr = new TextHistoryRecord[nCount][];
1918
}
2019

21-
internal void SetTextManager(TextManager textManager) {
22-
m_textManager = textManager;
23-
}
24-
2520
public void SetHistory(TextHistoryRecord[] histories) {
2621
if (m_nIndex == m_nMax) {
2722
for (int i = 1; i < m_arr.Length; i++) {
@@ -37,15 +32,7 @@ public TextHistoryRecord[] GetUndo() {
3732
if (m_nIndex == 0) { //not have history
3833
return null;
3934
}
40-
var old = m_arr[--m_nIndex];
41-
var ret = new TextHistoryRecord[old.Length];
42-
for (int i = old.Length - 1, j = 0; i >= 0; i--, j++) {
43-
ret[j] = old[i];
44-
var temp = old[i].NewText;
45-
ret[i].NewText = ret[i].OldText;
46-
ret[i].OldText = temp;
47-
}
48-
return ret;
35+
return m_arr[--m_nIndex];
4936
}
5037

5138
public TextHistoryRecord[] GetRedo() {

ST.Library.UI.STTextBox/Implementation/TextView/NoWrapTextView.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,6 @@ protected override int OnDrawLine(ISTTextBoxRender render, TextLine line, int nX
225225
int nCurrentCharIndex = line.IndexOfFirstChar + nIndexEach;
226226

227227
bool bIsEmoji = false;
228-
TextStyleRange tsi = TextStyleRange.Empty;
229228
c.IGraphemeSplitter.Each(line.RawString, nIndexEach, (str, nStart, nLen) => {
230229
string strChar = str.Substring(nStart, nLen);
231230
if (c.IsEmoji(strChar)) {

ST.Library.UI.STTextBox/Implementation/TextView/WrapTextView.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ public override FindInfo FindFromPoint(Point pt) {
401401
fi.Location = new Point((int)nX + base.TextRectangle.X + subLine.X, base.TextRectangle.Y + nYOffset * c.LineHeight);
402402
}
403403
fi.Find = true;
404-
Console.WriteLine("->>>>>>>>>>>>>>>>>>>>>" + line.RawString.Substring(subLine.Index, subLine.Length) + " -- " + fi.IndexOfChar);
404+
//Console.WriteLine("->>>>>>>>>>>>>>>>>>>>>" + line.RawString.Substring(subLine.Index, subLine.Length) + " -- " + fi.IndexOfChar);
405405
return fi;
406406
}
407407

ST.Library.UI.STTextBox/STTextBox.MouseEvent.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ protected override void OnMouseMove(MouseEventArgs e) {
7979
return;
8080
}
8181
if (e.Button == System.Windows.Forms.MouseButtons.Left) {
82-
var sw = new System.Diagnostics.Stopwatch();
83-
sw.Start();
82+
//var sw = new System.Diagnostics.Stopwatch();
83+
//sw.Start();
8484
var fi = c.ITextView.FindFromPoint(e.Location);
8585
if (!fi.Find) return;
8686
c.Caret.CopyFromFindInfo(fi);
@@ -97,8 +97,8 @@ protected override void OnMouseMove(MouseEventArgs e) {
9797
c.Selection.SetSelection(c.Selection.AnchorIndex, nIndex);
9898
c.ITextView.SetCaretPostion(fi.IndexOfChar);
9999
c.ITextView.ScrollToCaret();
100-
sw.Stop();
101-
Console.WriteLine("CheckSelection: - " + sw.ElapsedMilliseconds);
100+
//sw.Stop();
101+
//Console.WriteLine("CheckSelection: - " + sw.ElapsedMilliseconds);
102102
this.Invalidate();
103103
return;
104104
}
@@ -141,7 +141,7 @@ protected override void OnMouseLeave(EventArgs e) {
141141
protected override void OnMouseWheel(MouseEventArgs e) {
142142
base.OnMouseWheel(e);
143143
DateTime dt_now = DateTime.Now;
144-
Console.WriteLine("Scroll: --------------------- " + dt_now.Subtract(m_dt_last_scroll_v).TotalMilliseconds);
144+
//Console.WriteLine("Scroll: --------------------- " + dt_now.Subtract(m_dt_last_scroll_v).TotalMilliseconds);
145145
int nIncrement = 1;
146146
int nTemp = (int)dt_now.Subtract(m_dt_last_scroll_v).TotalMilliseconds;
147147
foreach (var v in m_arr_si) {
@@ -178,7 +178,7 @@ protected override void OnMouseWheel(MouseEventArgs e) {
178178

179179
protected virtual void OnMouseHWheel(MouseEventArgs e) {
180180
DateTime dt_now = DateTime.Now;
181-
Console.WriteLine("Scroll: --------------------- " + dt_now.Subtract(m_dt_last_scroll_h).TotalMilliseconds);
181+
//Console.WriteLine("Scroll: --------------------- " + dt_now.Subtract(m_dt_last_scroll_h).TotalMilliseconds);
182182
int nIncrement = 1;
183183
int nTemp = (int)dt_now.Subtract(m_dt_last_scroll_h).TotalMilliseconds;
184184
foreach (var v in m_arr_si) {

ST.Library.UI.STTextBox/STTextBox.cs

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -185,32 +185,32 @@ protected override void WndProc(ref Message m) {
185185

186186
protected override void OnPaint(PaintEventArgs e) {
187187
base.OnPaint(e);
188-
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
189-
sw.Start();
188+
//System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
189+
//sw.Start();
190190
this.XDPIZoom = e.Graphics.DpiX / 96;
191191
this.YDPIZoom = e.Graphics.DpiY / 96;
192192
var render = m_core.ITextBoxRender;
193-
try {
194-
render.OnBeginPaint(e.Graphics);
195-
m_core.ITextView.OnDrawView(render);
196-
if (this._AllowScrollBar && m_core.Scroll.CountDown != 0) {
197-
this.OnCalcScrollRectangle(m_core.Scroll);
198-
bool bFlag = true;
199-
if (m_core.Scroll.HBackRect != m_core.Scroll.HThumbRect) {
200-
this.OnDrawHScrollBar(render, m_core.Scroll);
201-
} else { bFlag = false; }
202-
if (m_core.Scroll.VBackRect != m_core.Scroll.VThumbRect) {
203-
this.OnDrawVScrollBar(render, m_core.Scroll);
204-
} else { bFlag = false; }
205-
if (bFlag) this.OnDrawScrollBarCorner(render, m_core.Scroll);
206-
}
207-
this.OnDrawBorder(render);
208-
render.OnEndPaint(e.Graphics);
209-
} catch (Exception ex) {
210-
MessageBox.Show(ex.Message + "\r\n\r\n" + ex.StackTrace);
193+
//try {
194+
render.OnBeginPaint(e.Graphics);
195+
m_core.ITextView.OnDrawView(render);
196+
if (this._AllowScrollBar && m_core.Scroll.CountDown != 0) {
197+
this.OnCalcScrollRectangle(m_core.Scroll);
198+
bool bFlag = true;
199+
if (m_core.Scroll.HBackRect != m_core.Scroll.HThumbRect) {
200+
this.OnDrawHScrollBar(render, m_core.Scroll);
201+
} else { bFlag = false; }
202+
if (m_core.Scroll.VBackRect != m_core.Scroll.VThumbRect) {
203+
this.OnDrawVScrollBar(render, m_core.Scroll);
204+
} else { bFlag = false; }
205+
if (bFlag) this.OnDrawScrollBarCorner(render, m_core.Scroll);
211206
}
212-
sw.Stop();
213-
Console.WriteLine("OnPaint - " + sw.ElapsedMilliseconds + "ms");
207+
this.OnDrawBorder(render);
208+
render.OnEndPaint(e.Graphics);
209+
//} catch (Exception ex) {
210+
// MessageBox.Show(ex.Message + "\r\n\r\n" + ex.StackTrace);
211+
//}
212+
//sw.Stop();
213+
//Console.WriteLine("OnPaint - " + sw.ElapsedMilliseconds + "ms");
214214
}
215215

216216
protected virtual void OnDrawBorder(ISTTextBoxRender render) {
@@ -338,6 +338,15 @@ private void RunHistory(bool isUndo) {
338338
if (histories == null || histories.Length == 0) {
339339
return;
340340
}
341+
if (isUndo) {
342+
var temp = new TextHistoryRecord[histories.Length];
343+
for (int i = 0; i < histories.Length; i++) {
344+
temp[i] = histories[i];
345+
temp[i].NewText = histories[i].OldText;
346+
temp[i].OldText = histories[i].NewText;
347+
}
348+
histories = temp;
349+
}
341350
histories = c.TextManager.RunHistory(histories);
342351
var last = histories[histories.Length - 1];
343352
c.Selection.SetSelection(last.Index, last.Index + last.NewText.Length);

0 commit comments

Comments
 (0)