-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRubiksCubeRenderer.cs
236 lines (201 loc) · 8.18 KB
/
RubiksCubeRenderer.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
using System.Diagnostics;
using System.Drawing.Drawing2D;
namespace VirtualRubiksCube
{
public class RubiksCubeRenderer
{
#region Fields
private RubiksCube rubiksCube;
private CancellationTokenSource cancellationTokenSource;
private Task? renderTask;
private List<double> frameTimes;
private RenderInfo currentRenderInfo;
// flags
private bool isPaused;
private bool isTerminating;
#endregion
#region Properties
public bool IsRunning { get; private set; }
public double FrameRate { get; private set; } // fps
#endregion
#region Event
public delegate void RenderHandler(object sender, RenderEventArgs e);
public event RenderHandler? OnRender;
#endregion
#region Constructor
public RubiksCubeRenderer(RubiksCube rubiksCube, RenderInfo initialCommand)
{
this.rubiksCube = rubiksCube;
rubiksCube.Renderer = this;
frameTimes = new List<double>();
IsRunning = false;
currentRenderInfo = initialCommand;
cancellationTokenSource = new CancellationTokenSource();
}
#endregion
#region Methods
public void Start()
{
if (!IsRunning)
{
IsRunning = true;
renderTask = Task.Run(async () => await RenderLoop(cancellationTokenSource.Token));
}
}
public void Stop()
{
if (IsRunning)
{
IsRunning = false;
isPaused = true;
}
}
public void Resume()
{
if (!IsRunning)
{
IsRunning = true;
isPaused = false;
}
}
public void Abort()
{
if (IsRunning)
{
IsRunning = false;
isTerminating = true;
cancellationTokenSource?.Cancel();
Thread.Sleep(500);
if (renderTask?.IsCanceled == false)
{
renderTask?.Wait();
}
}
}
private async Task RenderLoop(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
// Check for termination request
if (isTerminating)
return;
// Check for pausing
while (isPaused)
{
await Task.Delay(100, cancellationToken);
if (isTerminating)
return;
}
// Rendering work
Stopwatch stopwatch = new();
stopwatch.Start();
// Animate move
if (rubiksCube.Controller != null)
{
RotationInfo currentRotationInfo = rubiksCube.Controller.CurrentRotationInfo;
if (currentRotationInfo.IsRotating)
{
rubiksCube.Controller.SetRotationInfo(currentRotationInfo);
rubiksCube.Controller.RotateStep();
}
}
// Raise the event
OnRender?.Invoke(this, new RenderEventArgs(currentRenderInfo));
if (stopwatch.ElapsedMilliseconds < 15)
await Task.Delay(Math.Max(15 - (int)stopwatch.ElapsedMilliseconds, 0), cancellationToken);
while (stopwatch.Elapsed.TotalMilliseconds < 1000.0 / 60) { }
stopwatch.Stop();
frameTimes.Add(stopwatch.Elapsed.TotalMilliseconds);
int counter = 0;
int index = frameTimes.Count - 1;
double ms = 0;
while (index >= 0 && ms + frameTimes[index] <= 1000)
{
ms += frameTimes[index];
counter++;
index--;
}
if (index > 0) frameTimes.RemoveRange(0, index);
FrameRate = counter + ((1000 - ms) / frameTimes[0]);
// Check for cancellation and exit the loop if requested
if (cancellationToken.IsCancellationRequested)
break;
}
}
private Face3D? GetMouseHoveredFace(List<Face3D> projectedFaces)
{
Face3D? mouseHoverFace = null;
for (int i = projectedFaces.Count - 1; i >= 0; i--)
{
PointF[] vertices = projectedFaces[i].Vertices.Select(p => new PointF((float)p.X, (float)p.Y)).ToArray();
GraphicsPath graphicsPath = new();
graphicsPath.AddPolygon(vertices);
if (graphicsPath.IsVisible(currentRenderInfo.MousePosition))
{
mouseHoverFace = projectedFaces[i];
break;
}
}
return mouseHoverFace;
}
public Face3D? RenderFaces(Graphics graphics, RenderInfo renderInfo)
{
currentRenderInfo = renderInfo;
graphics.SmoothingMode = SmoothingMode.AntiAlias;
// Rotate faces
List<Face3D> rotatedFaces = new();
foreach (Face3D face in rubiksCube.Faces)
rotatedFaces.Add(face.Rotate(renderInfo.RotationX, renderInfo.RotationY, renderInfo.RotationZ));
// Project faces
List<Face3D> projectedFaces = new();
foreach (Face3D face in rotatedFaces)
projectedFaces.Add(face.Project(currentRenderInfo.ViewWidth, currentRenderInfo.ViewHeight, renderInfo.ImageDistance, currentRenderInfo.ViewDistance));
projectedFaces = projectedFaces.OrderBy(p => p.MinZ).ToList();
// Render
Face3D? mouseHoveredFace = GetMouseHoveredFace(projectedFaces);
foreach (Face3D projectedFace in projectedFaces)
{
if (projectedFace != mouseHoveredFace) // render last
{
PointF[] vertices = projectedFace.Vertices.Select(p => new PointF((float)p.X, (float)p.Y)).ToArray();
if (projectedFace.SelectionStatus == Face3D.SelectionMode.Selected)
graphics.FillPolygon(new HatchBrush(HatchStyle.Percent90, Color.Black, GetFaceColor(projectedFace.ColorIndex)), vertices);
else if (projectedFace.SelectionStatus == Face3D.SelectionMode.SecondarySelection)
graphics.FillPolygon(new HatchBrush(HatchStyle.Percent60, Color.Black, GetFaceColor(projectedFace.ColorIndex)), vertices);
else
graphics.FillPolygon(new SolidBrush(GetFaceColor(projectedFace.ColorIndex)), vertices);
graphics.DrawPolygon(Pens.Black, vertices);
}
}
if (mouseHoveredFace != null)
{
PointF[] vertices = mouseHoveredFace.Vertices.Select(p => new PointF((float)p.X, (float)p.Y)).ToArray();
graphics.FillPolygon(new HatchBrush(HatchStyle.Percent25, Color.Gold, GetFaceColor(mouseHoveredFace.ColorIndex)), vertices);
graphics.DrawPolygon(new Pen(Color.Gold, 3f), vertices);
}
return mouseHoveredFace;
}
public void SetRenderInfo(RenderInfo renderInfo)
{
currentRenderInfo = renderInfo;
}
private Color GetFaceColor(byte colorIndex)
{
Color faceColor = Color.Black;
if (colorIndex == 1)
faceColor = currentRenderInfo.TopFaceColor;
if (colorIndex == 2)
faceColor = currentRenderInfo.BottomFaceColor;
if (colorIndex == 3)
faceColor = currentRenderInfo.LeftFaceColor;
if (colorIndex == 4)
faceColor = currentRenderInfo.RightFaceColor;
if (colorIndex == 5)
faceColor = currentRenderInfo.FrontFaceColor;
if (colorIndex == 6)
faceColor = currentRenderInfo.BackFaceColor;
return faceColor;
}
#endregion
}
}