-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDrawState.cs
204 lines (179 loc) · 7.49 KB
/
DrawState.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
// Developed by Bulat Bagaviev (@sunnyyssh).
// This file is licensed to you under the MIT license.
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
namespace Sunnyyssh.ConsoleUI;
/// <summary>
/// Represents the draw state.
/// </summary>
public sealed class DrawState
{
/// <summary>
/// The empty draw state.
/// </summary>
public static DrawState Empty => new DrawState(ImmutableList<PixelLine>.Empty);
/// <summary>
/// Lines of the draw state. The state actually consists of them.
/// </summary>
public ImmutableList<PixelLine> Lines { get; }
/// <summary>
/// Tries to get pixel
/// </summary>
/// <param name="left">Left coordinate.</param>
/// <param name="top">Top coordinate.</param>
/// <param name="resultPixel">Result pixel if it's found. null otherwise.</param>
/// <returns>True if pixel was found. False otherwise.</returns>
public bool TryGetPixel(int left, int top, [NotNullWhen(true)] out PixelInfo? resultPixel)
{
foreach (PixelLine line in Lines)
{
if (line.Top != top)
continue;
if (left < line.Left || left >= line.Left + line.Left)
continue;
resultPixel = line[left - line.Left];
return true;
}
resultPixel = null;
return false;
}
/// <summary>
/// Overlaps this state with <see cref="state"/>. It respects not visible pixels and transparent colors. So, for example, if overlapping pixel is not visible underlying one is resolved.
/// </summary>
/// <param name="state">State to overlap over this one.</param>
/// <returns>Result state.</returns>
[Pure]
public DrawState OverlapWith(DrawState state)
{
ArgumentNullException.ThrowIfNull(state, nameof(state));
return Combine(this, state);
}
/// <summary>
/// Naively overlaps this state with <see cref="state"/>. It means that overlapping pixel always hides underlying.
/// It doesn't respect non-visibility and transparency.
/// </summary>
/// <param name="state">State to hide-overlap with.</param>
/// <returns>Result state.</returns>
[Pure]
public DrawState HideOverlapWith(DrawState state)
{
ArgumentNullException.ThrowIfNull(state, nameof(state));
return HideOverlap(this, state);
}
/// <summary>
/// Crops draw state to specified area.
/// </summary>
/// <param name="left">Left position of area to crop to.</param>
/// <param name="top">Top position of area to crop to.</param>
/// <param name="width">Width of area to crop to.</param>
/// <param name="height">Height of area to crop to.</param>
/// <returns>Cropped draw state.</returns>
[Pure]
public DrawState Crop(int left, int top, int width, int height)
{
var cropped = Lines
// Removing lines not matching vertical bounds
.Where(line => line.Top >= top && line.Top < top + height)
// Removing lines not matching horizntal bounds.
.Where(line => line.Left + line.Length > left && line.Left < left + width)
.Select(line => line.Crop(left - line.Left, left + width - line.Left))
.ToImmutableList();
return new DrawState(cropped);
}
/// <summary>
/// Shifts (or moves) state to the new area.
/// </summary>
/// <param name="leftShift">Horizontal shift. If it's positive state shifts to the right. If negative - to the left.</param>
/// <param name="topShift">Vertical shift. If it's positive state shifts down. If negative - up.</param>
/// <returns>Shifted state.</returns>
[Pure]
public DrawState Shift(int leftShift, int topShift)
{
return new DrawState(
Lines
.Select(l =>
new PixelLine(
l.Left + leftShift,
l.Top + topShift,
l.Pixels))
.ToImmutableList());
}
/// <summary>
/// Subtracts this state with <see cref="deductible"/>.
/// </summary>
/// <param name="deductible">The deductible state.</param>
/// <returns>The result of subtraction.</returns>
[Pure]
public DrawState SubtractWith(DrawState deductible)
{
ArgumentNullException.ThrowIfNull(deductible, nameof(deductible));
var newLines = Lines.ToArray();
for (int i = 0; i < newLines.Length; i++)
{
foreach (var deductibleLine in deductible.Lines)
{
if (deductibleLine.Top != newLines[i].Top)
continue;
newLines[i] = newLines[i].Subtract(deductibleLine);
}
}
return new DrawState(newLines.ToImmutableList());
}
/// <summary>
/// Combines ordered collection of <see cref="DrawState"/> instances. If state is earlier in collection then it is overlapped by later ones.
/// It respects not visible pixels and transparent colors. So, for example, if overlapping pixel is not visible underlying one is resolved.
/// </summary>
/// <param name="orderedDrawStates">Collection of combining states.</param>
/// <returns>Combined state.</returns>
public static DrawState Combine([Pure] params DrawState[] orderedDrawStates)
{
ArgumentNullException.ThrowIfNull(orderedDrawStates, nameof(orderedDrawStates));
var lines = orderedDrawStates
// making one sequence of lines from sequence of sequences of lines.
.SelectMany(
state => state.Lines,
(_, line) => line)
// Grouping lines by their Top value
// and making one line from each group using overlapping
.GroupBy(
line => line.Top,
line => line,
(_, lines) => PixelLine.Overlap(lines.ToArray()))
.ToImmutableList();
DrawState result = new(lines);
return result;
}
/// <summary>
/// Naively combines ordered collection of <see cref="DrawState"/> instances. If state is earlier in collection then it is naively overlapped (or hidden) by later ones.
/// It means that overlapping pixel always hides underlying. It doesn't respect non-visibility and transparency.
/// </summary>
/// <param name="orderedDrawStates">Collection of combining states.</param>
/// <returns>Combined state.</returns>
public static DrawState HideOverlap([Pure] params DrawState[] orderedDrawStates)
{
ArgumentNullException.ThrowIfNull(orderedDrawStates, nameof(orderedDrawStates));
var lines = orderedDrawStates
// making one sequence of lines from sequence of sequences of lines.
.SelectMany(
state => state.Lines,
(_, line) => line)
// Grouping lines by their Top value
// and making one line from each group using hide overlapping
.GroupBy(
line => line.Top,
line => line,
(_, lines) => PixelLine.HideOverlap(lines.ToArray()))
.ToImmutableList();
DrawState result = new(lines);
return result;
}
/// <summary>
/// Creates the instance of <see cref="DrawState"/> with given lines.
/// </summary>
/// <param name="lines">Lines that draw state'll consist of.</param>
public DrawState(ImmutableList<PixelLine> lines)
{
Lines = lines ?? throw new ArgumentNullException(nameof(lines));
}
}