This repository has been archived by the owner on Aug 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControlFrameClass.pde
135 lines (100 loc) · 3.92 KB
/
ControlFrameClass.pde
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
/*
By Chris Eugene Mills
Based off an example by Andreas Schlegel, 2016
*/
import javax.swing.JFrame;
import processing.awt.PSurfaceAWT;
import com.jogamp.newt.opengl.GLWindow;
class ControlFrame extends PApplet {
int w, h;
PApplet parent;
ControlP5 cp5;
String windowTitle;
boolean visible = true;
public ControlFrame(PApplet _parent, int _w, int _h, String _name) {
super();
parent = _parent;
w=_w;
h=_h;
windowTitle = _name;
PApplet.runSketch(new String[]{this.getClass().getName()}, this);
}
public void settings() {
size(w, h, JAVA2D);
}
public void setup() {
surface.setTitle(windowTitle);
removeExitEvent(JAVA2D);
cp5 = new ControlP5(this);
cp5.enableShortcuts();
}
void draw() {
background(bg);
/*
TODO This window is already drawing when I want to add control elements, making
something in CP5 crap out, give a delay, and research later:
https://forum.processing.org/one/topic/controlp5-and-java-util-concurrentmodificationexception.html
*/
if (frameCount == 1) delay(1000);
}
// public void controlEvent(ControlEvent theEvent) {
//
// }
// INTERACTION ///////////////////////////////////////////////////////////////
// Use our button variables, as we are in another thread, and therefore these
// direct calls could kill. Replicating the interaction in KeyInteraction.pde
// for our second window.
void keyPressed(KeyEvent e) {
parent.keyPressed(e);
}
// PUBLIC ACCESS /////////////////////////////////////////////////////////////
public void setLocation( int x, int y ) {
surface.setLocation(x, y);
}
public void setResizable( boolean set ) {
surface.setResizable(set);
}
public void openClose() {
visible = !visible;
surface.setVisible(visible);
}
public int[] getCanvasSize() {
return new int[]{width, height};
}
// renderer = either JAVA2D (default), P2D or P3D
public int[] getWindowLocation(String renderer) {
if (renderer == P2D || renderer == P3D) {
// Get OpenGL window
com.jogamp.newt.opengl.GLWindow newtCanvas = (com.jogamp.newt.opengl.GLWindow) surface.getNative();
// Return X/Y
return new int[]{newtCanvas.getX(), newtCanvas.getY()};
} else if (renderer == JAVA2D) {
// Get JFrame window
javax.swing.JFrame awtCanvas = (javax.swing.JFrame)((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
// Return X/Y
return new int[]{awtCanvas.getX(), awtCanvas.getY()};
} else return new int[]{-1, -1};
}
// ADAPT WINDOW //////////////////////////////////////////////////////////////
@Override void exit() {
}
// renderer = either JAVA2D (default), P2D or P3D
public void removeExitEvent(String renderer) {
if (renderer == P2D || renderer == P3D) {
// Get OpenGL window
com.jogamp.newt.opengl.GLWindow newtCanvas = (com.jogamp.newt.opengl.GLWindow) surface.getNative();
// Remove listeners added by Processing
for (com.jogamp.newt.event.WindowListener l : newtCanvas.getWindowListeners())
if (l.getClass().getName().startsWith("processing"))
newtCanvas.removeWindowListener(l);
// Set on close action to do nothing i.e. keep the window open
newtCanvas.setDefaultCloseOperation(com.jogamp.nativewindow.WindowClosingProtocol.WindowClosingMode.DO_NOTHING_ON_CLOSE);
} else if (renderer == JAVA2D) {
javax.swing.JFrame awtCanvas = (javax.swing.JFrame)((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame();
//println(awtCanvas.getClass().getSimpleName());
for (java.awt.event.WindowListener l : awtCanvas.getWindowListeners())
awtCanvas.removeWindowListener(l);
awtCanvas.setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE);
}
}
}