-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawerPal.cs
276 lines (209 loc) · 8.55 KB
/
DrawerPal.cs
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
// Developed by Bulat Bagaviev (@sunnyyssh).
// This file is licensed to you under the MIT license.
using System.Diagnostics.CodeAnalysis;
using System.Text;
namespace Sunnyyssh.ConsoleUI;
// The type is expected to be inherited
// because it's possible that different platforms should have different console drawing.
// This type is not thread-safe. But it's used only in thread-safe context.
internal class DrawerPal
{
// Indicates if it's expected to throw an exception on trying to draw outside the buffer.
private readonly bool _borderConflictsAllowed;
// The default background color
protected readonly Color DefaultBackground;
// The default foreground color
protected readonly Color DefaultForeground;
/// <summary>
/// Stores the previous (latest) state that is drawn in console.
/// </summary>
protected PartLine[] PreviousState = Array.Empty<PartLine>();
private ConsoleColor? _lastBackground;
private ConsoleColor? _lastForeground;
private int? _currentCursorLeft;
private int? _currentCursorTop;
public int BufferWidth => Console.WindowWidth;
public int BufferHeight => Console.WindowHeight;
public static int WindowWidth => Console.WindowWidth;
public static int WindowHeight => Console.WindowHeight;
public void Clear() => Console.Clear();
/// <summary>
/// Handles start of drawing.
/// It may be implemented by platfrom-specific DrawerPal inheritors.
/// </summary>
public virtual void OnStart()
{
Console.OutputEncoding = Encoding.Unicode;
}
// /// <summary>
// /// Redraws the latest state of console.
// /// </summary>
//public void Redraw(CancellationToken cancellationToken) => DrawSingleRequest(PreviousState, cancellationToken);
/// <summary>
/// Draws the request directly in console.
/// </summary>
/// <param name="drawState">The state to draw.</param>
/// <param name="cancellationToken"></param>
public virtual void DrawSingleRequest(DrawState drawState, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
return;
ArgumentNullException.ThrowIfNull(drawState, nameof(drawState));
// Checking if state is outside of buffer size.
ValidateBorderConflicts(drawState);
// If exception on border violation is not thrown
// I should crop the DrawState instance to the actual buffer size.
drawState = drawState.Crop(0, 0, BufferWidth, BufferHeight);
var parts = drawState.Lines
.SelectMany(LineToParts)
.ToArray();
foreach (var part in parts)
{
DrawPart(part, cancellationToken);
}
// Reseting cursor position to (0, 0) point.
Console.SetCursorPosition(0, 0);
// Storing actual state as latest.
PreviousState = parts;
}
// Splits single line to same-colored parts.
protected PartLine[] LineToParts(PixelLine line)
{
// Optimizing the drawing is mainly in minimizing the count of Console methods invocations.
// So the less parts line's splitted, the faster drawing is.
List<PartLine> resultParts = new();
for (int startIndex = 0; startIndex < line.Length;)
{
// If part can be extracted it's added to the result collection.
if (TryExtractPart(line, ref startIndex, out var newPart))
{
resultParts.Add(newPart);
}
}
return resultParts.ToArray();
}
// Tries to extract part starting at given index.
private bool TryExtractPart(PixelLine sourceLine, ref int position, [NotNullWhen((true))] out PartLine? result)
{
int startPosition = position;
ConsoleColor? background = null;
ConsoleColor? foreground = null;
StringBuilder resultBuilder = new();
for ( ; position < sourceLine.Length; position++)
{
var current = sourceLine[position];
if (current.IsVisible)
{
var currentBackground = ToConsoleBackgroundColor(current.Background);
background ??= currentBackground;
if (background != currentBackground)
break;
if (current.Foreground == Color.Transparent)
{
resultBuilder.Append(' ');
continue;
}
var currentForeground = ToConsoleForegroundColor(current.Foreground);
foreground ??= currentForeground;
if (foreground != currentForeground)
break;
resultBuilder.Append(current.Char);
continue;
}
var defaultBackground = ToConsoleBackgroundColor(Color.Default);
background ??= defaultBackground;
if (background != defaultBackground)
break;
resultBuilder.Append(' ');
}
background ??= ToConsoleBackgroundColor(Color.Default);
foreground ??= background;
result = new PartLine(
sourceLine.Left + startPosition,
sourceLine.Top,
background.Value,
foreground.Value,
resultBuilder.ToString());
return true;
}
protected virtual void DrawPart(PartLine part, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
return;
SetCursorPosition(part.Left, part.Top, cancellationToken);
SetBackgroundColor(part.Background, cancellationToken);
SetForegroundColor(part.Foreground, cancellationToken);
Write(part.Part, cancellationToken);
}
private void SetCursorPosition(int left, int top, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
return;
if (left == _currentCursorLeft && top == _currentCursorTop)
return;
Console.SetCursorPosition(left, top);
_currentCursorLeft = left;
_currentCursorTop = top;
}
private void SetBackgroundColor(ConsoleColor color, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
return;
if (_lastBackground == color)
return;
Console.BackgroundColor = color;
_lastBackground = color;
}
private void SetForegroundColor(ConsoleColor color, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
return;
if (_lastForeground == color)
return;
Console.ForegroundColor = color;
_lastForeground = color;
}
private void Write(string line, CancellationToken cancellationToken)
{
if (cancellationToken.IsCancellationRequested)
return;
Console.Write(line);
}
protected ConsoleColor ToConsoleBackgroundColor(Color color)
{
if (color == Color.Transparent || color == Color.Default)
return ColorHelper.ToConsoleColor(DefaultBackground);
return (ConsoleColor)(color - 2);
}
protected ConsoleColor ToConsoleForegroundColor(Color color)
{
if (color == Color.Transparent)
return ColorHelper.ToConsoleColor(DefaultBackground);
if (color == Color.Default)
return ColorHelper.ToConsoleColor(DefaultForeground);
return ColorHelper.ToConsoleColor(color);
}
protected void ValidateBorderConflicts(DrawState drawState)
{
if (_borderConflictsAllowed)
return;
foreach (PixelLine line in drawState.Lines)
{
if (line.Top >= BufferHeight)
{
throw new DrawingException("Buffer height is less than it's needed. ");
}
if (line.Left + line.Length > BufferWidth)
{
throw new DrawingException("Buffer width is less than it's needed. ");
}
}
}
public DrawerPal(Color defaultBackground, Color defaultForeground, bool borderConflictsAllowed)
{
DefaultBackground = defaultBackground;
DefaultForeground = defaultForeground;
_borderConflictsAllowed = borderConflictsAllowed;
}
protected record PartLine(int Left, int Top, ConsoleColor Background, ConsoleColor Foreground, string Part);
}