forked from FrozenAssassine/TextControlBox-WinUI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionRenderer.cs
More file actions
211 lines (184 loc) · 8.55 KB
/
Copy pathSelectionRenderer.cs
File metadata and controls
211 lines (184 loc) · 8.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
using Microsoft.Graphics.Canvas;
using Microsoft.Graphics.Canvas.Text;
using Microsoft.Graphics.Canvas.UI.Xaml;
using System;
using System.Diagnostics;
using TextControlBoxNS.Core.Selection;
using TextControlBoxNS.Core.Text;
using TextControlBoxNS.Helper;
using Windows.Foundation;
using Windows.UI;
namespace TextControlBoxNS.Core.Renderer
{
internal class SelectionRenderer
{
public int renderedSelectionLength = 0;
public int renderedSelectionStart = 0;
private SelectionManager selectionManager;
private TextRenderer textRenderer;
private EventsManager eventsManager;
private ScrollManager scrollManager;
private ZoomManager zoomManager;
private DesignHelper designHelper;
private TextManager textManager;
public void Init(
SelectionManager selectionManager,
TextRenderer textRenderer,
EventsManager eventsManager,
ScrollManager scrollManager,
ZoomManager zoomManager,
DesignHelper designHelper,
TextManager textManager
)
{
this.selectionManager = selectionManager;
this.textRenderer = textRenderer;
this.eventsManager = eventsManager;
this.scrollManager = scrollManager;
this.zoomManager = zoomManager;
this.designHelper = designHelper;
this.textManager = textManager;
}
public void DrawSelection(
CanvasTextLayout textLayout,
CanvasDrawEventArgs args,
float marginLeft,
float marginTop,
int unrenderedLinesToRenderStart,
int numberOfRenderedLines,
float fontSize,
Color selectionColor
)
{
int selStartIndex = 0;
int selEndIndex = 0;
int characterPosStart = selectionManager.selectionStart.CharacterPosition;
int characterPosEnd = selectionManager.selectionEnd.CharacterPosition;
int startLine = selectionManager.selectionStart.LineNumber;
int endLine = selectionManager.selectionEnd.LineNumber;
int lineEndingLength = textManager.NewLineCharacter.Length;
if (endLine > textManager.totalLines.Count)
endLine = textManager.totalLines.Count - 1;
if (characterPosStart > textManager.totalLines.Span[startLine].Length)
characterPosStart = textManager.totalLines.Span[startLine].Length;
if (characterPosEnd > textManager.totalLines.Span[endLine].Length)
characterPosEnd = textManager.totalLines.Span[endLine].Length;
//Render the selection on position 0 if the user scrolled the start away
if (startLine < unrenderedLinesToRenderStart)
{
startLine = unrenderedLinesToRenderStart;
characterPosStart = 0;
}
if (endLine < unrenderedLinesToRenderStart)
{
endLine = unrenderedLinesToRenderStart;
characterPosEnd = 0;
}
// If start is beyond visible area, clamp it to the end of the visible region
int lastRenderedLine = unrenderedLinesToRenderStart + numberOfRenderedLines - 1;
if (startLine > lastRenderedLine)
{
startLine = lastRenderedLine;
characterPosStart = textManager.totalLines.Span[startLine].Length;
}
if (endLine > lastRenderedLine)
{
endLine = lastRenderedLine;
characterPosEnd = textManager.totalLines.Span[endLine].Length;
}
if (startLine == endLine)
{
int lenghtToLine = 0;
for (int i = 0; i < startLine - unrenderedLinesToRenderStart; i++)
{
if (i < numberOfRenderedLines)
{
lenghtToLine += textManager.totalLines.Span[textRenderer.NumberOfStartLine + i].Length + lineEndingLength;
}
}
selStartIndex = characterPosStart + lenghtToLine;
selEndIndex = characterPosEnd + lenghtToLine;
}
else
{
for (int i = 0; i < startLine - unrenderedLinesToRenderStart; i++)
{
if (i >= numberOfRenderedLines) //Out of range of the List (do nothing)
break;
selStartIndex += textManager.totalLines.Span[textRenderer.NumberOfStartLine + i].Length + lineEndingLength;
}
selStartIndex += characterPosStart;
for (int i = 0; i < endLine - unrenderedLinesToRenderStart; i++)
{
if (i >= numberOfRenderedLines) //Out of range of the List (do nothing)
break;
selEndIndex += textManager.totalLines.Span[textRenderer.NumberOfStartLine + i].Length + lineEndingLength;
}
selEndIndex += characterPosEnd;
}
renderedSelectionStart = Math.Max(0, Math.Min(selStartIndex, selEndIndex));
renderedSelectionLength = selEndIndex > selStartIndex ?
selEndIndex - selStartIndex :
selStartIndex - selEndIndex;
//no selection can be rendered.
//GetCharacterRegions(0,0) still returns a "ghost" region, so stop rendering here
if(renderedSelectionLength == 0)
{
selectionManager.currentTextSelection.renderedIndex = 0;
selectionManager.currentTextSelection.renderedLength = 0;
return;
}
CanvasCommandList canvasCommandList = new CanvasCommandList(args.DrawingSession);
using (var ccls = canvasCommandList.CreateDrawingSession())
{
CanvasTextLayoutRegion[] regions = textLayout.GetCharacterRegions(renderedSelectionStart, renderedSelectionLength);
for (int i = 0; i < regions.Length; i++)
{
//Change the width if selection in an empty line or starts at a line end
if (regions[i].LayoutBounds.Width == 0)
{
var bounds = regions[i].LayoutBounds;
regions[i].LayoutBounds = new Rect
{
Width = fontSize / 4,
Height = bounds.Height,
X = bounds.X,
Y = bounds.Y
};
}
ccls.FillRectangle(Utils.CreateRect(regions[i].LayoutBounds, marginLeft, marginTop), selectionColor);
}
}
args.DrawingSession.DrawImage(canvasCommandList);
selectionManager.currentTextSelection.renderedIndex = renderedSelectionStart;
selectionManager.currentTextSelection.renderedLength = renderedSelectionLength;
}
public void Draw(CanvasControl canvasSelection, CanvasDrawEventArgs args)
{
if (!selectionManager.selectionStart.IsNull && !selectionManager.selectionEnd.IsNull)
selectionManager.HasSelection = SelectionHelper.TextIsSelected(selectionManager.selectionStart, selectionManager.selectionEnd);
else
selectionManager.HasSelection = false;
if (selectionManager.HasSelection)
{
DrawSelection(
textRenderer.DrawnTextLayout,
args,
(float)-scrollManager.HorizontalScroll,
textRenderer.SingleLineHeight / scrollManager.DefaultVerticalScrollSensitivity + textRenderer.VerticalDrawOffset,
textRenderer.NumberOfStartLine,
textRenderer.NumberOfRenderedLines,
zoomManager.ZoomedFontSize,
designHelper._Design.SelectionColor
);
}
if (selectionManager.HasSelection && !selectionManager.Equals(selectionManager.OldTextSelection, selectionManager.currentTextSelection))
{
//Update the variables
selectionManager.OldTextSelection.EndPosition.SetChangeValues(selectionManager.currentTextSelection.EndPosition);
selectionManager.OldTextSelection.StartPosition.SetChangeValues(selectionManager.currentTextSelection.StartPosition);
eventsManager.CallSelectionChanged();
}
}
}
}