Repository navigation
Expand file tree
/
Copy pathControlP5Util.java
More file actions
606 lines (540 loc) · 15.4 KB
/
Copy pathControlP5Util.java
File metadata and controls
606 lines (540 loc) · 15.4 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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
import java.awt.Frame;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import processing.core.PApplet;
import sojamo.drop.SDrop;
import controlP5.Accordion;
import controlP5.Button;
import controlP5.ControlEvent;
import controlP5.ControlP5;
import controlP5.ControllerGroup;
import controlP5.ControllerList;
import controlP5.Group;
import controlP5.Numberbox;
import controlP5.RadioButton;
import controlP5.Slider;
import controlP5.Tab;
import controlP5.Textfield;
import controlP5.Textlabel;
import controlP5.Toggle;
public abstract class ControlP5Util implements Controller {
protected ControlP5 controlP5;
protected int windowH = 580;
protected int windowW = 820;
protected boolean ready = false;
protected int wMargin = 4;
protected int hMargin = wMargin;
protected int wNum = 23;
protected int toggleActiveColor=-1;
protected int selectorActiveColor=-1;
protected boolean visible = true;
protected SDrop drop;
protected int lastGuiUpdate = 0;
protected int refresh = 100;//milliseconds between gui refresh
protected Application app;
protected ControlFrame controlFrame;
protected int lastGuiDraw=0;
protected List<Textfield> textFields = new ArrayList<Textfield>();
protected int w = 100;
protected int h = 10;
public abstract void update();
public abstract void setup();
public abstract void mousePress(int x,int y, int buttons);
public abstract void mouseClick(int x,int y, int buttons);
public abstract void mouseRelease(int x,int y, int buttons);
public abstract void drawCustomUI(PApplet theApplet);
public abstract void controlEvent(ControlEvent theEvent);
@SuppressWarnings("deprecation")
public ControlP5Util(Application app,boolean window,boolean visible){
toggleActiveColor=app.color(40,240,40);
selectorActiveColor=app.color(240,40,40);
System.out.println("GUI Controller.");
this.app=app;
if(this.app!=null){
if(Config.GUI_WINDOW){
controlFrame = new ControlFrame(this,"patterns controller (#"+app.id+")",this.windowH,this.windowW);
if(app.presetManager!=null || "".equals(drop)){
drop = new SDrop(controlFrame, app);
}
controlFrame.addMouseListener(new MyMouseListener() );
controlFrame.addKeyListener(this.app);
while(controlFrame.cp5==null){
try {Thread.sleep(50);} catch (Exception e) {}
}
this.controlP5 = controlFrame.cp5;
}else{
this.controlP5 = new ControlP5(app);
app.frame.addMouseListener(new MyMouseListener());
}
this.hide();
//this.controlP5.setFont(this.app.createFont("", 8));
this.controlP5.setMoveable(false);
this.controlP5.disableShortcuts();
this.controlP5.blockDraw = true;
setup();
this.controlP5.blockDraw = false;
if(Config.GUI_VISIBLE){
this.show();
}else{
this.hide();
}
this.ready=true;
}
}
public void control(){}
public void hide(){
if(controlFrame!=null){
this.controlFrame.setVisible(false);
}
this.controlP5.hide();
visible=false;
}
public void show(){
if(controlFrame!=null){
this.controlFrame.setVisible(true);
}
this.controlP5.show();
visible=true;
}
public boolean isVisible(){
return(this.visible);
}
public void toggleVisiblity(){
if(visible){
hide();
}else{
show();
}
}
class MyMouseListener implements MouseListener{
public void mouseClicked(MouseEvent e) {
mouseClick(e.getX(),e.getY(),e.getButton());
}
public void mousePressed(MouseEvent e) {
mousePress(e.getX(),e.getY(),e.getButton());
}
public void mouseReleased(MouseEvent e) {
mouseRelease(e.getX(),e.getY(),e.getButton());
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
}
public class ControlFrame extends PApplet {
private static final long serialVersionUID = -8996418478036660977L;
volatile ControlP5 cp5;
ControlP5Util parent;
int w, h;
Frame f;
ControlFrame(ControlP5Util parent,String name,int h, int w) {
f = new Frame(name);
this.h=h;
this.w=w;
f.add(this);
this.init();
f.setTitle(name);
f.setSize(this.w, this.h);
f.setLocation(120, 120);
f.setResizable(false);
f.setVisible(true);
f.setVisible(false);
this.parent=parent;
}
public void keyPressed(){
if(key==27){
key=0;//disable ESC
}
}
public void setup() {
size(w, h);
frameRate(25);
cp5 = new ControlP5(this);
}
public void draw() {
background(0,0,0);
if(ready){
drawCustomUI(this);
}
}
public void setVisible(boolean visible){
f.setVisible(visible);
super.setVisible(visible);
}
public void controlEvent(ControlEvent theEvent) {
parent.controlEvent(theEvent);
}
}
/**
* switch between gui tabs
*/
public void switchTab(){
ControllerList tabs = controlP5.controlWindow.getTabs();
String currentTabName=controlP5.controlWindow.getCurrentTab().getName();
for(int i=0;i<tabs.size();i++){
if(tabs.get(i).getName().equals(currentTabName)){
if(i==tabs.size()-1){
controlP5.controlWindow.activateTab(tabs.get(1).getName());
}else{
controlP5.controlWindow.activateTab(tabs.get(i+1).getName());
}
}
}
}
public void autoSelectTab(){
Layer layer = app.layers[app.currentLayer];
if(layer!=null){
ControllerList tabs = controlP5.controlWindow.getTabs();
String layerType = layer.getType();
for(int i=0;i<tabs.size();i++){
if(tabs.get(i).getName().equals(layerType)){
controlP5.controlWindow.activateTab(tabs.get(i).getName());
}
}
}
}
public boolean textFieldIsFocus(){
for(Textfield textField : textFields){
if(textField.isFocus()){
return(true);
}
}
return(false);
}
public Textfield addTextfield(String layerType, String name, String value, int x, int y, Integer w, Integer h, Tab tab, Group group){
Textfield c = this.controlP5.addTextfield(getName(layerType, name));
c.setWidth(getW(w));
c.setHeight(getH(h));
moveTo(c, tab, group, x, y);
textFields.add(c);
return(c);
}
public Slider addSlider(String layerType, String name, float def,float min, float max, int x, int y, Integer w, Integer h, Tab tab, Group group){
Slider c = this.controlP5.addSlider(getName(layerType, name));
c.setDefaultValue(def);
c.setMax(max);
c.setMin(min);
c.setWidth(getW(w));
c.setHeight(getH(h));
moveTo(c, tab, group, x, y);
return(c);
}
public Toggle addToggle(String layerType, String name, boolean def, int x, int y, Tab tab, Group group){
return(addToggle(layerType, name, def, x, y, null, null, tab, group));
}
public Toggle addToggle(String layerType, String name, boolean def, int x, int y, Integer w, Integer h, Tab tab, Group group){
Toggle c = this.controlP5.addToggle(getName(layerType, name));
c.setDefaultValue(def?1:0);
c.setWidth(w!=null?w:this.h);
c.setHeight(getH(h));
moveTo(c, tab, group, x, y);
c.setColorActive(toggleActiveColor);
return(c);
}
public Button addButton(String layerType, String name, int x, int y, Integer w, Integer h, Tab tab, Group group){
Button c = this.controlP5.addButton(getName(layerType, name));
c.setWidth(w!=null?w:this.h);
c.setHeight(getH(h));
moveTo(c, tab, group, x, y);
c.setColorActive(toggleActiveColor);
return(c);
}
public Numberbox addNumber(String layerType, String name, float def, int x, int y, Tab tab, Group group){
return addNumber(layerType, name, def, null, null, null, x, y, tab, group);
}
public Numberbox addNumber(String layerType, String name, float def, Integer precision, int x, int y, Tab tab, Group group){
return addNumber(layerType, name, def, null, null, precision, x, y, tab, group);
}
public Numberbox addNumber(String layerType, String name, float def,Float min, Float max, Integer precision, int x, int y, Tab tab, Group group){
Numberbox c = this.controlP5.addNumberbox(getName(layerType, name));
c.setDefaultValue(def);
c.setWidth(w/4);
c.setHeight(h);
//nb.setId(id);
if(max!=null){
c.setMax(max);
}
if(min!=null){
c.setMin(min);
}
if(precision!=null){
c.setDecimalPrecision(precision);
}else{
c.setDecimalPrecision(0);
}
//c.setWidth(getW(w));
//c.setHeight(getH(h));
moveTo(c, tab, group, x, y);
return(c);
}
public RadioButton addRadio(String layerType, String name, String[] names, int x, int y, Tab tab, Group group){
return(addRadio(layerType, name, names, x, y, null, null, tab, group));
}
public RadioButton addRadio(String layerType, String name, String[] names, int x, int y, Integer w, Integer h, Tab tab, Group group){
RadioButton c = this.controlP5.addRadio(getName(layerType, name));
c.setWidth(getW(w));
c.setHeight(getH(h));
int hh=this.h;
if(h!=null){
hh=h;
}
c.setSize(hh,hh+1)
.setColorActive(selectorActiveColor)
.setItemsPerRow(1)
.setSpacingColumn(50)
.setNoneSelectedAllowed(false)
.setSpacingRow(this.hMargin-1);
for(int i=0;i<names.length;i++){
c.addItem(name+"_"+i+"_"+names[i], i);
c.getItem(i).getCaptionLabel().set(names[i]);
//c.getItem(i).setInternalValue(values[i]);
}
moveTo(c, tab, group, x, y);
return(c);
}
public Textlabel addLabel(String layerType, String name,String value,int x, int y, Tab tab, Group group){
Textlabel c = this.controlP5.addTextlabel(getName(layerType, name));
c.setText(value);
//c.setWidth(getW(w));
//c.setHeight(getH(h));
moveTo(c, tab, group, x, y);
return(c);
}
public Group addGroup(String layerType, String name, int x, int y, int w, int h, Tab tab, Group group, Accordion accordion){
Group c = this.controlP5.addGroup(getName(layerType, name));
c.setWidth(w);
c.setHeight(h);
moveTo(c, tab, group, accordion, x, y);
return(c);
}
public Accordion addAccordion(String layerType, String name, int x, int y, Integer w, Integer h, Tab tab, Group group){
Accordion c = this.controlP5.addAccordion(getName(layerType, name));
if(w!=null){
c.setWidth(w);
}
if(h!=null){
c.setHeight(h);
}
moveTo(c, tab, group, x, y);
return(c);
}
private String getName(String layerType, String name){
if(layerType!=null){
return(layerType+"."+name);
}
return(name);
}
/*private void moveTo(controlP5.Controller<?> c, Tab tab, Group group, Accordion accordion){
moveTo(c, tab, group, accordion,null,null);
}*/
private void moveTo(controlP5.Controller<?> c, Tab tab, Group group, Integer x, Integer y){
if(x!=null && y !=null){
c.setPosition(x, y);
}
if(tab!=null){
c.moveTo(tab);
}
if(group!=null){
c.moveTo(group);
}
}
private void moveTo(RadioButton c, Tab tab, Group group, Integer x, Integer y){
if(x!=null && y !=null){
c.setPosition(x, y);
}
if(tab!=null){
c.moveTo(tab);
}
if(group!=null){
c.moveTo(group);
}
}
private void moveTo(Accordion c, Tab tab, Group group, Integer x, Integer y){
if(x!=null && y !=null){
c.setPosition(x, y);
}
if(tab!=null){
c.moveTo(tab);
}
if(group!=null){
c.moveTo(group);
}
}
private void moveTo(Group c, Tab tab, Group group, Accordion accordion, Integer x, Integer y){
if(x!=null && y !=null){
c.setPosition(x, y);
}
if(tab!=null){
c.moveTo(tab);
}
if(group!=null){
c.moveTo(group);
}
if(accordion!=null){
accordion.addItem(c);
}
}
private int getW(Integer w){
if(w!=null){
return(w);
}
return(this.w);
}
private int getH(Integer h){
if(h!=null){
return(h);
}
return(this.h);
}
Map<String,controlP5.Controller<?>> controllers = new HashMap<String,controlP5.Controller<?>>();
protected controlP5.Controller<?> getController(String layerType, String name){
name = getName(layerType,name);
controlP5.Controller<?> c = controllers.get(name);
if(c==null){
c=controlP5.getController(name);
controllers.put(name,c);
}
return(c);
}
protected ControllerGroup<?> getControllerGroup(String layerType, String name){
if(layerType==null){
return(controlP5.getGroup(name));
}else{
return(controlP5.getGroup(getName(layerType,name)));
}
}
protected void updateController(String layerType,String name, float value){
controlP5.Controller<?> c = getController(layerType,name);
if(c!=null){
updateController(getController(layerType,name),value);
}else{
ControllerGroup<?> cg = getControllerGroup(layerType,name);
if(cg instanceof RadioButton){
updateController((RadioButton)cg,value);
}
}
}
protected void updateController(controlP5.Controller<?> c, float value){
if(c!=null && c.getValue()!=value){
c.setBroadcast(false);
c.setValue(value);
c.setBroadcast(true);
}
}
protected void updateController(RadioButton r, float value){
updateControllerRadioHack(r,value);
/*
if(r!=null && !r.getState((int)value)){
for (Toggle item : r.getItems()) {
item.setBroadcast(false);
}
r.deactivateAll();
//r.activate((int)value);
r.getItem((int)value).setValue(true);
System.out.println(r);
for (Toggle item : r.getItems()) {
item.setBroadcast(true);
}
}*/
}
Field f =null;
Field f2 =null;
public void updateControllerRadioHack(RadioButton r, float value){
try{
if(f==null){
Field fieldlist[] = Toggle.class.getDeclaredFields();
for(Field fieldd : fieldlist){
//System.out.println(fieldd.getName());
if(fieldd.getName().equals("isOn")){
if("boolean".equals(fieldd.getType().getName())){
f = fieldd;
f.setAccessible( true);
}
}
}
}
if(f2==null){
Field fieldlist[] = Controller.class.getDeclaredFields();
for(Field fieldd : fieldlist){
if(fieldd.getName().equals("_myValue")){
if("float".equals(fieldd.getType().getName())){
f2 = fieldd;
f2.setAccessible(true);
}
}
}
}
int n = r.getItems().size();
//if (value < n) {
for (int i = 0; i < n; i++) {
r.getItem(i).setBroadcast(false);
}
for (int i = 0; i < n; i++) {
Toggle t = r.getItem(i);
//t.setState(true);
try{
//r.getItem(i).setValue(0);
f.setBoolean(t, false);
if(f2!=null){
f2.setFloat(t, 0);
}
}catch(Exception e){
e.printStackTrace();
}
}
for (int i = 0; i < n; i++) {
Toggle t = r.getItem(i);
//t.setState(true);
try{
if(r.getItem(i).internalValue()==value && !r.getItem(i).getState()){
f.setBoolean(t, true);
if(f2!=null){
f2.setFloat(t, 1);
}
}else{ //if( r.getItem(i).getValue()!=0){
//r.getItem(i).setValue(0);
//f.setBoolean(t, false);
//f2.setFloat(t, 0);
}
}catch(Exception e){
e.printStackTrace();
}
}
//r.update();
for (int i = 0; i < n; i++) {
r.getItem(i).setBroadcast(true);
}
//}
}catch(Exception e){
System.out.println(r+" "+value);
e.printStackTrace();
}
}
protected void updateController(String layerType,String name, String value){
updateController(getController(layerType,name),value);
}
protected void updateController(controlP5.Controller<?> c, String value){
if(c instanceof controlP5.Textfield){
Textfield textField = ((controlP5.Textfield)c);
if(!textField.isFocus() && !textField.getText().equals(value)){
textField.setBroadcast(false);
textField.setText(value);
textField.setBroadcast(true);
}
}else if(c instanceof controlP5.Textlabel){
String val = ((controlP5.Textlabel)c).getValueLabel().getText();
if(!val.equals(value)){
c.setBroadcast(false);
((controlP5.Textlabel)c).setValue(value);
c.setBroadcast(true);
}
}else{
if(c!=null){
System.err.println("unspported controller type"+c.getClass());
}
}
}
}