-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGUI.pde
More file actions
469 lines (375 loc) · 12.6 KB
/
Copy pathGUI.pde
File metadata and controls
469 lines (375 loc) · 12.6 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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
import controlP5.*;
ControlP5 cp5;
StrategyPoint selected = null;
Textlabel labelInfo;
Textfield fieldX, fieldY;
boolean updatingFromGUI = false;
Toggle toggleAddEnabled;
boolean addPointEnabled = true;
Toggle toggleShowOverlay;
Toggle toggleUseAlign;
int lastAutoSave = 0;
int autoSaveInterval = 30 * 1000; // xx seconds
public class StrategyEditorGUI extends PApplet {
StrategyEditor mainApp;
public void settings() {
size(400, 800);
}
public void setup() {
surface.setTitle("StrategyEditor - GUI");
surface.setLocation(1320, 100);
cp5 = new ControlP5(this);
// Bloc 1 : Infos point sélectionné
labelInfo = cp5.addTextlabel("labelInfo")
.setText("No point selected")
.setPosition(20, 20)
.setSize(360, 100)
.setColorValue(color(0, 102, 153));
fieldX = cp5.addTextfield("x_mm")
.setPosition(20, 100)
.setSize(100, 30)
.setAutoClear(false)
.setLabel("X (mm)")
.setText("0");
fieldX.getCaptionLabel().setColor(color(0, 102, 153));
fieldY = cp5.addTextfield("y_mm")
.setPosition(140, 100)
.setSize(100, 30)
.setAutoClear(false)
.setLabel("Y (mm)")
.setText("0");
fieldY.getCaptionLabel().setColor(color(0, 102, 153));
// Bloc 2 : Activation
toggleAddEnabled = cp5.addToggle("addPointEnabled")
.setPosition(20, 220)
.setSize(20, 20)
.setLabel("Add points enabled")
.setValue(true);
toggleAddEnabled.getCaptionLabel().setColor(color(0, 102, 153));
// Bloc 3 : Navigation
cp5.addButton("selectPrevPoint")
.setLabel("Previous point")
.setPosition(20, 270)
.setSize(120, 30);
cp5.addButton("selectNextPoint")
.setLabel("Next point")
.setPosition(160, 270)
.setSize(120, 30);
// Bloc 4 : Sauvegarde / chargement
cp5.addButton("saveStrategy")
.setLabel("Save strategy")
.setPosition(20, 320)
.setSize(120, 30);
cp5.addButton("loadStrategy")
.setLabel("Load strategy")
.setPosition(160, 320)
.setSize(120, 30);
cp5.addButton("resetStrategy")
.setLabel("Reset strategy")
.setPosition(20, 370)
.setSize(120, 30);
cp5.addButton("reloadTempStrategy")
.setLabel("Reload temp strategy")
.setPosition(20, 410)
.setSize(260, 30);
// Bloc 5 : Simulation / overlay
cp5.addButton("startSimulation")
.setLabel("Start simulation")
.setPosition(20, 470)
.setSize(120, 30);
toggleShowOverlay = cp5.addToggle("showOverlay")
.setPosition(20, 520)
.setSize(20, 20)
.setValue(false)
.setLabel("Show table overlay");
toggleShowOverlay.getCaptionLabel().setColor(color(0, 102, 153));
// Bloc de commande pour les orientations
toggleUseAlign = cp5.addToggle("useAlign")
.setPosition(20, 160)
.setSize(20, 20)
.setLabel("Enable align")
.setValue(false);
toggleUseAlign.getCaptionLabel().setColor(color(0, 102, 153));
cp5.addScrollableList("compassZone")
.setPosition(60, 160)
.setSize(100, 100)
.setBarHeight(20)
.setItemHeight(20)
.addItems(new String[] { "A", "AB", "B", "BC", "C", "CA" })
.setValue(0);
cp5.addScrollableList("orientationMode")
.setPosition(180, 160)
.setSize(100, 100)
.setBarHeight(20)
.setItemHeight(20)
.addItems(new String[] { "CUSTOM", "NORTH", "EAST", "SOUTH", "WEST" })
.setValue(0);
cp5.addTextfield("customAngle")
.setLabel("°")
.setPosition(300, 160)
.setSize(60, 20)
.setAutoClear(false)
.setText("0");
// Chargement automatique au démarrage
reloadTempStrategy();
}
public void draw() {
background(220);
if (millis() - lastAutoSave > autoSaveInterval) {
println("auto-save triggered");
savePointsToFile("strategy_temp.json");
lastAutoSave = millis();
}
}
public void updateLabelInfo(){
labelInfo.setText(
"Point P" + selected.id +
"\nX: " + nf(selected.x_mm, 0, 0) + " mm" +
"\nY: " + nf(selected.y_mm, 0, 0) + " mm" +
"\nPOI: " + selected.poiName +
"\nAlign: " + selected.useAlign +
"\nCompass: " + selected.compass +
"\norientation: " + selected.orientation +
"\ncustomAngle: " + selected.customAngle
);
}
public void setSelectedPoint(StrategyPoint p) {
selected = p;
if (selected != null) {
updatingFromGUI = true;
updateLabelInfo();
fieldX.setText(nf(selected.x_mm, 0, 0));
fieldY.setText(nf(selected.y_mm, 0, 0));
// mettre à jour les composants d'alignement
toggleUseAlign.setValue(selected.useAlign);
cp5.get(ScrollableList.class, "compassZone").setStringValue(selected.compass);
cp5.get(ScrollableList.class, "orientationMode").setStringValue(selected.orientation);
cp5.get(Textfield.class, "customAngle").setText(str(selected.customAngle));
updatingFromGUI = false;
} else {
labelInfo.setText("No point selected");
fieldX.setText("");
fieldY.setText("");
}
}
public int compassIndexOf(String value) {
String[] options = { "A", "AB", "B", "BC", "C", "CA" };
for (int i = 0; i < options.length; i++) {
if (options[i].equals(value)) return i;
}
return 0; // défaut si non trouvé
}
public int orientationIndexOf(String value) {
String[] options = { "CUSTOM", "NORTH", "SOUTH", "WEST", "EAST" };
for (int i = 0; i < options.length; i++) {
if (options[i].equals(value)) return i;
}
return 0; // défaut si non trouvé
}
public void controlEvent(ControlEvent e) {
if (updatingFromGUI) return;
println("[DEBUG] controlEvent: " + e.getName());
if (e.getName().equals("addPointEnabled")) {
addPointEnabled = e.getValue() == 1;
println("[GUI] Add point: " + (addPointEnabled ? "enabled" : "disabled"));
toggleAddEnabled.setLabel(addPointEnabled ? "Add points enabled" : "Add points disabled");
return;
}
if (selected != null) {
switch (e.getName()) {
case "x_mm":
try {
float newX = Float.parseFloat(e.getStringValue());
selected.x_mm = constrain(newX, 0, 3000);
}
catch (Exception ex) {
println("[GUI] Invalid X value");
}
break;
case "y_mm":
try {
float newY = Float.parseFloat(e.getStringValue());
selected.y_mm = constrain(newY, 0, 2000);
}
catch (Exception ex) {
println("[GUI] Invalid Y value");
}
break;
case "useAlign":
selected.useAlign = (e.getValue() == 1);
break;
case "compassZone":
int index = (int) e.getValue(); // getValue() retourne l’index sélectionné
String[] optionsCompass = { "A", "AB", "B", "BC", "C", "CA" };
if (index >= 0 && index < optionsCompass.length) {
selected.compass = optionsCompass[index];
println("[DEBUG] compass set to: " + selected.compass);
}
break;
case "orientationMode":
int i = (int) e.getValue();
String[] orientations = { "CUSTOM", "NORTH", "EAST", "SOUTH", "WEST" };
if (i >= 0 && i < orientations.length) {
selected.orientation = orientations[i];
println("[DEBUG] orientation set to: " + selected.orientation);
}
break;
case "customAngle":
try {
selected.customAngle = constrain(int(e.getStringValue()), 0, 359);
}
catch (Exception ex) {
println("[GUI] Invalid angle");
}
break;
}
}
// Update le label d'info
updateLabelInfo();
}
public boolean isAddPointEnabled() {
return addPointEnabled;
}
public void saveStrategy() {
// 1. Save temp file
savePointsToFile("strategy_temp.json");
// 2. Prompt for custom location
selectOutput("Save strategy to...", "saveStrategyToFile");
}
public void saveStrategyToFile(File selection) {
if (selection == null) {
println("[GUI] Save cancelled.");
return;
}
String path = selection.getAbsolutePath();
if (!path.toLowerCase().endsWith(".json")) {
path += ".json";
}
savePointsToFile(path);
println("[GUI] Strategy saved to: " + path);
}
public void savePointsToFile(String path) {
JSONObject data = exportPointsToJSON();
// Si c'est un chemin absolu (ex: vient de selectOutput), on le garde tel quel
if (path.startsWith("/") || path.contains(":")) {
saveJSONObject(data, path);
} else {
// Sinon, on utilise le chemin relatif depuis le sketch
saveJSONObject(data, mainApp.getDataPath(path));
}
}
public void reloadTempStrategy() {
println("[GUI] Reloading strategy_temp.json...");
File f = new File(mainApp.getDataPath("strategy_temp.json"));
loadStrategyFromFile(f);
}
public JSONObject exportPointsToJSON() {
JSONObject data = new JSONObject();
JSONArray list = new JSONArray();
for (StrategyPoint p : StrategyEditor.points) {
JSONObject entry = new JSONObject();
entry.setInt("id", p.id);
entry.setFloat("x_mm", p.x_mm);
entry.setFloat("y_mm", p.y_mm);
if (p.poiName != null) {
entry.setString("poi", p.poiName);
}
// Ajout des paramètres d'alignement
entry.setBoolean("useAlign", p.useAlign);
entry.setString("compass", p.compass);
entry.setString("orientation", p.orientation);
entry.setInt("customAngle", p.customAngle);
list.append(entry);
}
data.setJSONArray("strategy", list);
return data;
}
public void setMainApp(StrategyEditor app) {
this.mainApp = app;
}
public void loadStrategy() {
selectInput("Select a strategy file to load...", "loadStrategyFromFile");
}
public void loadStrategyFromFile(File selection) {
if (selection == null) {
println("[GUI] Load cancelled.");
return;
}
String path = selection.getAbsolutePath();
JSONObject data = loadJSONObject(path);
JSONArray list = data.getJSONArray("strategy");
StrategyEditor.points.clear();
for (int i = 0; i < list.size(); i++) {
JSONObject entry = list.getJSONObject(i);
int id = entry.getInt("id");
float x = entry.getFloat("x_mm");
float y = entry.getFloat("y_mm");
StrategyPoint p = new StrategyPoint(id, x, y);
if (entry.hasKey("poi")) p.poiName = entry.getString("poi");
if (entry.hasKey("useAlign")) p.useAlign = entry.getBoolean("useAlign");
if (entry.hasKey("compass")) p.compass = entry.getString("compass");
if (entry.hasKey("orientation")) p.orientation = entry.getString("orientation");
if (entry.hasKey("customAngle")) p.customAngle = entry.getInt("customAngle");
StrategyEditor.points.add(p);
}
mainApp.renumerotePoints();
println("[GUI] Loaded " + StrategyEditor.points.size() + " points from: " + path);
}
public void resetStrategy() {
int confirm = javax.swing.JOptionPane.showConfirmDialog(null,
"Are you sure you want to delete all points?",
"Confirm Reset",
javax.swing.JOptionPane.YES_NO_OPTION);
if (confirm == javax.swing.JOptionPane.YES_OPTION) {
StrategyEditor.points.clear();
mainApp.renumerotePoints();
setSelectedPoint(null);
println("[GUI] Strategy reset.");
} else {
println("[GUI] Reset cancelled.");
}
}
public void selectPrevPoint() {
if (StrategyEditor.points.size() == 0) return;
if (selected == null) {
selected = StrategyEditor.points.get(0);
} else {
int index = StrategyEditor.points.indexOf(selected);
if (index > 0) {
selected = StrategyEditor.points.get(index - 1);
}
}
setSelectedPoint(selected);
StrategyEditor.selectedPoint = selected;
}
public void selectNextPoint() {
if (StrategyEditor.points.size() == 0) return;
if (selected == null) {
selected = StrategyEditor.points.get(0);
} else {
int index = StrategyEditor.points.indexOf(selected);
if (index < StrategyEditor.points.size() - 1) {
selected = StrategyEditor.points.get(index + 1);
}
}
setSelectedPoint(selected);
StrategyEditor.selectedPoint = selected;
}
public void startSimulation() {
if (StrategyEditor.points.size() >= 2) {
StrategyEditor.currentSegment = 0;
StrategyEditor.t = 0.0;
StrategyEditor.robotPos = new PVector(
StrategyEditor.points.get(0).x_mm,
StrategyEditor.points.get(0).y_mm
);
StrategyEditor.isSimulating = true;
println("[GUI] Simulation started.");
} else {
println("[GUI] Not enough points to simulate.");
}
}
public void showOverlay(boolean val) {
showOverlay = val;
}
}