-
Notifications
You must be signed in to change notification settings - Fork 6
/
GuiController.pde
229 lines (190 loc) · 6.08 KB
/
GuiController.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
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
import processing.core.*;
import processing.event.*;
import java.awt.datatransfer.*;
import java.awt.Toolkit;
public class GUIController extends GUIComponent implements ClipboardOwner {
private GUIComponent[] contents;
private int numItems = 0;
private int focusIndex = -1;
private boolean visible;
private IFLookAndFeel lookAndFeel;
public IFPGraphicsState userState;
private Clipboard clipboard;
public PApplet parent;
public boolean showBounds = false;
public GUIController (PApplet newParent) {
this(newParent, true);
}
public GUIController (PApplet newParent, int x, int y, int width, int height) {
this(newParent, true);
setPosition(x, y);
setSize(width, height);
}
public GUIController (PApplet newParent, boolean newVisible) {
setParent(newParent);
setVisible(newVisible);
contents = new GUIComponent[5];
lookAndFeel = new IFLookAndFeel(parent, IFLookAndFeel.DEFAULT);
userState = new IFPGraphicsState();
SecurityManager security = System.getSecurityManager();
if (security != null) {
try {
security.checkSystemClipboardAccess();
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
} catch (SecurityException e) {
clipboard = new Clipboard("Interfascia Clipboard");
}
} else {
try {
clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
} catch (Exception e) {
// THIS IS DUMB
}
}
newParent.registerMethod("keyEvent", this);
newParent.registerMethod("show", this);
}
public void setLookAndFeel(IFLookAndFeel lf) {
lookAndFeel = lf;
}
public IFLookAndFeel getLookAndFeel() {
return lookAndFeel;
}
public void add (GUIComponent component) {
if (numItems == contents.length) {
GUIComponent[] temp = contents;
contents = new GUIComponent[contents.length * 2];
System.arraycopy(temp, 0, contents, 0, numItems);
}
component.setController(this);
component.setLookAndFeel(lookAndFeel);
//component.setIndex(numItems);
contents[numItems++] = component;
component.initWithParent();
}
public void remove (GUIComponent component) {
int componentIndex = -1;
for (int i = 0; i < numItems; i++) {
if (component == contents[i]){
componentIndex = i;
break;
}
}
if (componentIndex != -1) {
contents[componentIndex] = null;
if (componentIndex < numItems - 1) {
System.arraycopy(contents, componentIndex + 1, contents, componentIndex, numItems - (componentIndex + 1));
}
numItems--;
}
}
public void setParent (PApplet argParent) {
parent = argParent;
}
public PApplet getParent () {
return parent;
}
public void setVisible (boolean newVisible) {
visible = newVisible;
}
public boolean getVisible() {
return visible;
}
public void requestFocus(GUIComponent c) {
for (int i = 0; i < numItems; i++) {
if (c == contents[i])
focusIndex = i;
}
}
// ****** LOOK AT THIS, I DON'T THINK IT'S RIGHT ******
public void yieldFocus(GUIComponent c) {
if (focusIndex > -1 && focusIndex < numItems && contents[focusIndex] == c) {
focusIndex = -1;
}
}
public GUIComponent getComponentWithFocus() {
return contents[focusIndex];
}
public boolean getFocusStatusForComponent(GUIComponent c) {
if (focusIndex >= 0 && focusIndex < numItems)
return c == contents[focusIndex];
else
return false;
}
public void lostOwnership (Clipboard parClipboard, Transferable parTransferable) {
}
public void copy(String v)
{
StringSelection fieldContent = new StringSelection (v);
clipboard.setContents (fieldContent, this);
}
public String paste()
{
Transferable clipboardContent = clipboard.getContents (this);
if ((clipboardContent != null) &&
(clipboardContent.isDataFlavorSupported (DataFlavor.stringFlavor))) {
try {
String tempString;
tempString = (String) clipboardContent.getTransferData(DataFlavor.stringFlavor);
return tempString;
}
catch (Exception e) {
e.printStackTrace ();
}
}
return "";
}
public void keyEvent(KeyEvent e) {
if (visible) {
if (e.getAction() == KeyEvent.PRESS && e.getKeyCode() == java.awt.event.KeyEvent.VK_TAB) {
if (focusIndex != -1 && contents[focusIndex] != null) {
contents[focusIndex].actionPerformed(
new GUIEvent(contents[focusIndex], "Lost Focus")
);
}
if (e.isShiftDown())
giveFocusToPreviousComponent();
else
giveFocusToNextComponent();
if (focusIndex != -1 && contents[focusIndex] != null) {
contents[focusIndex].actionPerformed(
new GUIEvent(contents[focusIndex], "Received Focus")
);
}
} else if (e.getKeyCode() != java.awt.event.KeyEvent.VK_TAB) {
if (focusIndex >= 0 && focusIndex < contents.length)
contents[focusIndex].keyEvent(e);
}
}
}
private void giveFocusToPreviousComponent() {
int oldFocus = focusIndex;
focusIndex = (focusIndex - 1) % numItems;
while (!contents[focusIndex].canReceiveFocus() && focusIndex != oldFocus) {
focusIndex = (focusIndex - 1) % numItems;
}
}
private void giveFocusToNextComponent() {
int oldFocus = focusIndex;
focusIndex = (focusIndex + 1) % numItems;
while (!contents[focusIndex].canReceiveFocus() && focusIndex != oldFocus) {
focusIndex = (focusIndex + 1) % numItems;
}
}
public void show() {
if (visible) {
userState.saveSettingsForApplet(parent);
lookAndFeel.defaultGraphicsState.restoreSettingsToApplet(parent);
//parent.background(parent.g.backgroundColor);
parent.fill(color(0));
parent.rect(getX(), getY(), getWidth(), getHeight());
for(int i = 0; i < contents.length; i++){
if(contents[i] != null){
//parent.smooth();
contents[i].show();
}
}
userState.restoreSettingsToApplet(parent);
}
}
}