-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCanvas.cs
225 lines (209 loc) · 7.48 KB
/
Canvas.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
using System.Runtime.Versioning;
namespace TerminalEngine
{
public struct Position(int x, int y)
{
public int x = x;
public int y = y;
public override readonly string ToString()
{
return $"Position: {{ x:\"{x}\", y:\"{y}\" }}";
}
}
public static class Canvas
{
public static int Width { get; set; }
public static int Height { get; set; }
public static int Left { get; set; }
public static int Top { get; set; }
public static ConsoleColor ForegroundColor { get; set; }
public static ConsoleColor BackgroundColor { get; set; }
[SupportedOSPlatform("windows")]
public static bool CursorVisible { get => Console.CursorVisible; set => CursorVisible = value; }
public static Pixel[] Pixels { get; set; } = [];
public static IDrawable[] Drawable { get; set; } = [];
public static uint PixelCharWidth { get; set; }
public static void Initialize(
int width,
int height,
ConsoleColor foreground = ConsoleColor.White,
ConsoleColor background = ConsoleColor.Black,
int? left = null,
int? top = null,
uint pixelCharWidth = 2)
{
Console.WindowWidth = width;
Width = Console.WindowWidth;
Console.WindowHeight = height;
Height = Console.WindowHeight;
ForegroundColor = foreground;
BackgroundColor = background;
Console.ForegroundColor = foreground;
Console.BackgroundColor = background;
if (left != null && OperatingSystem.IsWindows())
{
Console.WindowLeft = (int)left;
Left = Console.WindowLeft;
}
if (top != null && OperatingSystem.IsWindows())
{
Console.WindowTop = (int)top;
Top = Console.WindowTop;
}
Pixels = GeneratePixelArray(Width, Height);
PixelCharWidth = pixelCharWidth;
}
public static Position GetCursorPosition()
{
return new Position(Console.CursorLeft, Console.CursorTop);
}
public static void SetCursorPosition(int x, int y)
{
Console.SetCursorPosition(x, y);
}
public static void SetCursorPosition(Position position)
{
Console.SetCursorPosition(position.x, position.y);
}
public static int GetCursorSize()
{
return Console.CursorSize;
}
public static void SetCursorSize(int s)
{
if (s < 1 || s >= 100)
{
throw new ArgumentOutOfRangeException(nameof(s));
}
else if (OperatingSystem.IsWindows())
{
Console.CursorSize = s;
}
else
{
throw new Exception("Error, unsupported platform. 'Console.CursorSize' can only be modified on a Windows system");
}
}
private static Pixel[] GeneratePixelArray(int w, int h)
{
return new Pixel[w * h].Select((pixel, index) => pixel = new Pixel(Pixel.DEFAULT_EMPTY, index, ConsoleColor.White)).ToArray();
}
public static Pixel GetPixelFromPosition(int x, int y)
{
int index = x + y * Width;
return Pixels[index];
}
public static Pixel GetPixelFromPosition(Position pos)
{
int index = pos.x + pos.y * Width;
return Pixels[index];
}
public static Pixel GetCenterPixel()
{
Position center = GetCenterPosition();
return GetPixelFromPosition(center);
}
public static Position GetCenterPosition()
{
return new(Width / 2, Height / 2);
}
public static void Clear()
{
for (int i = 0; i < Pixels.Length; i++)
{
Pixels[i].c = Pixel.DEFAULT_EMPTY;
}
Console.Clear();
}
public static Queue<Pixel> PixelActions { get; set; } = [];
public static Queue<DrawableAction> DrawableActions { get; set; } = [];
public static void Draw()
{
if (Pixels.Length != Width * Height)
{
throw new ArgumentException("The number of pixels does not match the canvas size.");
}
foreach (Pixel pixel in PixelActions)
{
DrawPixel(pixel);
}
foreach (DrawableAction drawing in DrawableActions)
{
if (drawing.LastDraw is not null)
{
EraseDrawing(drawing.LastDraw);
}
DrawDrawing(drawing.NewDraw);
}
PixelActions.Clear();
DrawableActions.Clear();
}
public static void DrawPixel(Pixel pixel)
{
Console.ForegroundColor = pixel.color;
Console.SetCursorPosition(pixel.column, pixel.row);
Console.Write(new string(pixel.c, (int)PixelCharWidth));
Console.ForegroundColor = ForegroundColor;
}
public static void DrawPixel(Position position, char c)
{
Console.SetCursorPosition(position.x, position.y);
Console.Write(new string(c, (int)PixelCharWidth));
}
public static void DrawPixel(Position position, char c, ConsoleColor color, uint charWidth)
{
Console.ForegroundColor = color;
Console.SetCursorPosition(position.x, position.y);
Console.Write(new string(c, (int)charWidth));
Console.ForegroundColor = ForegroundColor;
}
public static void EraseDrawing(IDrawable draw)
{
for (int y = 0; y < draw.Height; y++)
{
for (int x = 0; x < draw.Width; x++)
{
DrawPixel(new(x + draw.Left, y + draw.Top), Pixel.DEFAULT_FULL, BackgroundColor, PixelCharWidth);
}
}
}
public static void DrawDrawing(Drawable draw)
{
Console.ForegroundColor = draw.Color;
for (int y = 0; y < draw.Height; y++)
{
for (int x = 0; x < draw.Width; x++)
{
char c;
if (draw.FillChar != null)
{
c = (char)draw.FillChar;
}
else if (draw.SpanChars != null)
{
int index = x + y * draw.Width;
if (index >= draw.SpanChars.Length)
{
c = Pixel.DEFAULT_EMPTY;
}
else
{
c = draw.SpanChars[index];
}
}
else
{
throw new Exception("The 'Drawable' object's 'FillChar' and 'SpanChars' are both null. Can't draw pixel char to canvas.");
}
DrawPixel(new(x + draw.Left, y + draw.Top), c);
}
}
Console.ForegroundColor = ForegroundColor;
}
}
public struct DrawableAction(IDrawable? lastDraw, Drawable newDraw)
{
public IDrawable? LastDraw = lastDraw;
public Drawable NewDraw = newDraw;
}
}