-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCubeViewer.cs
239 lines (190 loc) · 6.41 KB
/
CubeViewer.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp.WinForms;
using System.IO;
using CefSharp;
using Ballcuber.Modele;
using RevengeCube;
namespace Ballcuber
{
public partial class CubeViewer : UserControl
{
private ChromiumWebBrowser viewer;
public CubeViewer()
{
InitializeComponent();
var url = Path.Combine(Path.GetDirectoryName(this.GetType().Assembly.Location), "twisty.js", "fgViewer.html");
viewer = new ChromiumWebBrowser(url);
viewer.Dock = DockStyle.Fill;
Controls.Add(this.viewer);
cbAxe.Text = "X";
}
public bool ShowDebugPanel
{
get
{
return pnlDebug.Visible;
}
set
{
pnlDebug.Visible = value;
}
}
public void PeriodicUpdate(GlobalState formerState, GlobalState currentState)
{
if (formerState.InitialCube != currentState.InitialCube)
{
RefreshCube(currentState.InitialCube);
}
}
private void ExecuteJS(string js)
{
viewer.GetBrowser().MainFrame.ExecuteJavaScriptAsync(js);
}
private void btnDebug_Click(object sender, EventArgs e)
{
viewer.ShowDevTools();
}
private void btnTestMoves_Click(object sender, EventArgs e)
{
ExecuteJS("move(\"" + txtMove.Text + "\", " + udStartLayer.Value + ", " + udEndLayer.Value + ", " + udAmount.Value + ")");
}
public void RefreshCube()
{
using (var state = GlobalState.GetState())
{
RefreshCube(state.InitialCube);
}
}
public void RefreshCube(ColorCube cube)
{
var faces = cube?.FaceColors;
var sb = new StringBuilder();
sb.Append("reloadWithColors([");
for (int i = 0; i < 6; i++)
{
sb.Append("[");
for (int j = 0; j < 16; j++)
{
sb.Append(faces == null ? 7 : (int)faces[i * 16 + j] + 1);
sb.Append(",");
}
sb.Append("],");
}
sb.Append("])");
ExecuteJS(sb.ToString());
}
public void setColors(IEnumerable<Color> colors)
{
if (colors == null)
{
RefreshCube(null);
return;
}
var sb = new StringBuilder();
sb.Append("setColors([");
sb.Append(string.Join(",", colors.Select((color) => string.Format("0x{0:X2}{1:X2}{2:X2}", color.R, color.G, color.B))));
sb.Append("])");
ExecuteJS(sb.ToString());
}
public void setCameraPosition(double theta, double height)
{
var sb = new StringBuilder();
sb.Append("setCameraPosition(");
sb.Append(theta.ToString().Replace(",","."));
sb.Append(",");
sb.Append(height.ToString().Replace(",", "."));
sb.Append(")");
ExecuteJS(sb.ToString());
}
public void ExecuteMachineMove(MotorMove mv)
{
int[] moves = { 0, mv.MaxMovesCount, mv.MidMaxMovesCount, mv.MidMinMovesCount };
int[,] indexes = { { 1, 3 }, { 1, 2 }, { 1, 1 }, { 2, 3 }, { 2, 2 }, { 3, 3 } };
while (moves[1] != 0 || moves[2] != 0 || moves[3] != 0)
{
for (int i = 0; i < indexes.Length / 2; i++)
{
int startLayer = indexes[i, 0];
int endLayer = indexes[i, 1];
int amount = moves[startLayer];
for (int j = startLayer+1; j <= endLayer; j++)
{
if(Math.Sign(moves[j]) != Math.Sign(amount))
{
amount = 0; ;
break;
}
else if(amount > 0 && moves[j] < amount || amount < 0 && moves[j] > amount)
{
amount = moves[j];
}
}
if (amount!=0) {
for (int j = startLayer; j <= endLayer; j++)
{
moves[j] -= amount;
}
Move(mv.Axe,startLayer, endLayer,amount);
break;
}
}
}
}
public void Move(Axe axe, int startLayer, int endLayer, int amount)
{
string axeName;
switch (axe)
{
case Axe.X:
axeName = "b";
break;
case Axe.Y:
axeName = "l";
break;
case Axe.Z:
axeName = "u";
break;
default:
return;
}
ExecuteJS("move(\"" + axeName + "\", " + startLayer + ", " + endLayer + ", " + -amount + ")");
}
public void RotateCube(Axe axe, int amount = 1)
{
SetSpeed(1);
Move(axe, 1, 4, amount);
}
public void SetCameraInclination(double radian)
{
ExecuteJS("setCameraInclination(" + radian.ToString().Replace(',','.') + ")");
}
private void btnTestMotorMoves_Click(object sender, EventArgs e)
{
Axe axe;
if(Enum.TryParse<Axe>(cbAxe.Text, out axe))
{
var mv = new MotorMove(axe);
mv.MaxMovesCount = (int)udCourronneMax.Value;
mv.MidMaxMovesCount = (int)udCourronneMidMax.Value;
mv.MidMinMovesCount = (int)udCourronneMidMin.Value;
ExecuteMachineMove(mv);
}
}
public void SetSpeed(int speed)
{
ExecuteJS("twistyScene.setSpeed(" + speed + ")");
}
private void udSpeed_ValueChanged(object sender, EventArgs e)
{
SetSpeed((int)udSpeed.Value);
}
}
}