-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsoleGrid.cs
More file actions
135 lines (122 loc) · 4.01 KB
/
Copy pathConsoleGrid.cs
File metadata and controls
135 lines (122 loc) · 4.01 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
using Opal.ConsoleHandlers;
namespace Opal.Rendering;
/// <summary>
/// Represents a grid of <c><see cref="ConsoleChar"/></c> that can be printed to the console.
/// </summary>
public class ConsoleGrid : BaseConsoleGrid
{
public Lock Lock { get; }
internal Memory<ConsoleChar> Buffer { get; private set; }
public override ConsoleChar this[int x, int y]
{
get
{
lock (Lock)
{
return IsCoordinateWithinGrid(x, y)
? Buffer.Span[x + y * Width]
: default;
}
}
set
{
lock (Lock)
{
if (IsCoordinateWithinGrid(x, y))
{
Buffer.Span[x + y * Width] = value;
}
}
}
}
public ConsoleGrid(int width, int height) : base(width, height)
{
Lock = new Lock();
SetSize(width, height, true);
}
/// <summary>
/// Update the size of the console grid, if <see cref="IConsoleHandler.Width"/> or <see cref="IConsoleHandler.Height"/> of <paramref name="handler"/> are different from the current dimensions of the console grid.
/// </summary>
/// <param name="handler"></param>
public void SetSize(IConsoleHandler handler)
{
SetSize(handler.Width, handler.Height);
}
/// <summary>
/// Updates the size of the console grid, if <paramref name="width"/> or <paramref name="height"/> are different from the current dimensions of the console grid, or if <paramref name="force"/> is set to <c>true</c>.
/// </summary>
/// <param name="width">The new width of the console grid.</param>
/// <param name="height">The new height of the console grid.</param>
/// <param name="force">If true, the console grid will be set (and, in doing so, cleared), even if the dimensions of the console grid won't change. This means the method will always return <c>true</c>.</param>
/// <returns>Returns <c>true</c> if the console grid was cleared, otherwise returns <c>false</c>.</returns>
public bool SetSize(int width, int height, bool force = false)
{
lock (Lock)
{
if (force || Width != width || Height != height)
{
Width = width;
Height = height;
Buffer = new ConsoleChar[Width * Height];
return true;
}
return false;
}
}
/// <summary>
/// Creates a new <see cref="ConsoleGrid"/> with the same dimensions and buffer content as this grid.
/// </summary>
/// <returns></returns>
public ConsoleGrid MakeClone()
{
lock (Lock)
{
ConsoleGrid clone = new ConsoleGrid(Width, Height);
Buffer.CopyTo(clone.Buffer);
return clone;
}
}
/// <summary>
/// Attempt to copy the buffer of this grid into <paramref name="grid"/>, if they have the same size.
/// </summary>
/// <returns></returns>
public bool TryCopyTo(ConsoleGrid? grid)
{
if (grid == null)
{
return false;
}
lock (Lock)
{
if (Width != grid.Width || Height != grid.Height)
{
return false;
}
try
{
Buffer.CopyTo(grid.Buffer);
return true;
}
catch
{
return false;
}
}
}
/// <summary>
/// Determines if <paramref name="other"/> has a different size or content that this grid.
/// </summary>
/// <param name="other"></param>
/// <returns></returns>
public bool IsDifferentFrom(ConsoleGrid other)
{
return Width != other.Width
|| Height != other.Height
|| !Buffer.Span.SequenceEqual(other.Buffer.Span);
}
public override (int PosX, int PosY) GetAbsolutePosition()
{
// This is the outermost grid, so the position is always [0, 0].
return (0, 0);
}
}