-
Notifications
You must be signed in to change notification settings - Fork 1
/
DisplayComponent.java
280 lines (218 loc) · 7.19 KB
/
DisplayComponent.java
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
package bleh;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.StringTokenizer;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.JTextField;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import processing.core.PApplet;
import toxi.geom.Vec3D;
public class DisplayComponent extends JComponent{
Rule r;
boolean hasChanged = false;
PApplet p;
JFrame f;
JPanel selectorPanel = new JPanel();
JLabel typeLabel = new JLabel("Current Type");
JComboBox typeSelector = new JComboBox();
JLabel axiomLabel = new JLabel("Axiom");
JComboBox axiomSelector = new JComboBox();
String currentType = "";
String axiom = "";
JPanel adPanel = new JPanel();
JButton add = new JButton("Add Type");
JButton remove = new JButton("Remove this Type");
JPanel xPanel = new JPanel();
JLabel xLabel, yLabel, zLabel;
JSlider x = new JSlider(JSlider.HORIZONTAL,-360,360,10);
JPanel yPanel = new JPanel();
JSlider y = new JSlider(JSlider.HORIZONTAL,-360,360,10);
JPanel zPanel = new JPanel();
JSlider z = new JSlider(JSlider.HORIZONTAL,-360,360,10);
JPanel spawnPanel = new JPanel();
JTextField spawnField = new JTextField();
@SuppressWarnings({ "unchecked", "rawtypes" })
public DisplayComponent(Rule r, PApplet p, JFrame f){
this.r = r;
this.p = p;
this.f = f;
// fill type selector with all different types
ArrayList<String> typesList = r.getTypes();
typeSelector = new JComboBox(typesList.toArray());
axiomSelector = new JComboBox(typesList.toArray());
axiom = r.getAxiom().getName();
if(currentType.equals(""))
currentType = typesList.get(0);
Type current = r.getTypesObj().get(r.getTypes().indexOf(currentType));
xLabel = new JLabel("X Rotation: " + current.getVelocity().x);
yLabel = new JLabel("Y Rotation: " + current.getVelocity().y);
zLabel = new JLabel("Z Rotation: " + current.getVelocity().z);
spawnField.setColumns(10);
resetSpawnField();
}
public void resetSpawnField(){
Type current = r.getTypesObj().get(r.getTypes().indexOf(currentType));
String text = "";
int i = 0;
for(Type n: current.getSpawn()){
if(i != current.getSpawn().size() -1)
text += n.getName() + ", ";
else text += n.getName();
i++;
}
spawnField.setText(text);
}
public void paintComponent(Graphics g){
if(hasChanged){
r.start(p);
p.draw();
//System.out.println("Current Type: " + currentType);
//System.out.println("Axiom: " + axiom);
Type current = r.getTypesObj().get(r.getTypes().indexOf(currentType));
xLabel.setText("X Rotation: " + current.getVelocity().x);
yLabel.setText("Y Rotation: " + current.getVelocity().y);
zLabel.setText("Z Rotation: " + current.getVelocity().z);
resetSpawnField();
hasChanged = false;
}
}
public void parseString(String s){
StringTokenizer stx = new StringTokenizer(s, " ");
while(stx.hasMoreTokens()){
String j = stx.nextToken();
}
}
public void init(){
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
// start with the selector panel
typeSelector.addActionListener(new ActionListener(){
// when type is selected, change current type
@SuppressWarnings("rawtypes")
public void actionPerformed(ActionEvent arg0) {
JComboBox cb = (JComboBox)arg0.getSource();
String type = (String)cb.getSelectedItem();
currentType = type;
hasChanged = true;
repaint();
}
});
// axiom selector
axiomSelector.addActionListener(new ActionListener(){
// when axiom is selected, change axiom string
public void actionPerformed(ActionEvent arg0) {
JComboBox cb = (JComboBox)arg0.getSource();
String ax = (String)cb.getSelectedItem();
axiom = ax;
r.setAxiom(axiom);
hasChanged = true;
repaint();
}
});
// add button
add.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e) {
ArrayList<Type> typesList = r.getTypesObj();
String str = JOptionPane.showInputDialog(f, "Enter name : ",
"Add new type", 1);
if(str != null){
typesList.add(new Type(p,str));
typeSelector.addItem(str);
axiomSelector.addItem(str);
}
}
});
// remove button
remove.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
ArrayList<Type> typesList = r.getTypesObj();
if(typesList.size() != 1 && !currentType.equals(axiom)){
typeSelector.removeItem(currentType);
axiomSelector.removeItem(currentType);
currentType = axiom;
typeSelector.setSelectedItem(axiom);
}
else{
JOptionPane.showMessageDialog(f,
"Cannot remove");
}
}
});
// x slider
x.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent arg0) {
int value = ((JSlider) arg0.getSource()).getValue();
Type current = r.getTypesObj().get(r.getTypes().indexOf(currentType));
Vec3D oldV = current.getVelocity();
Vec3D newV = new Vec3D(value,oldV.y,oldV.z);
current.setVelocity(newV);
xLabel.setText("X Rotation: " + current.getVelocity().x);
hasChanged = true;
repaint();
}
});
// y slider
y.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent arg0) {
int value = ((JSlider) arg0.getSource()).getValue();
Type current = r.getTypesObj().get(r.getTypes().indexOf(currentType));
Vec3D oldV = current.getVelocity();
Vec3D newV = new Vec3D(oldV.x,value,oldV.z);
yLabel.setText("Y Rotation: " + current.getVelocity().y);
current.setVelocity(newV);
hasChanged = true;
repaint();
}
});
// z slider
z.addChangeListener(new ChangeListener(){
public void stateChanged(ChangeEvent arg0) {
int value = ((JSlider) arg0.getSource()).getValue();
Type current = r.getTypesObj().get(r.getTypes().indexOf(currentType));
Vec3D oldV = current.getVelocity();
Vec3D newV = new Vec3D(oldV.x,oldV.y,value);
zLabel.setText("Z Rotation: " + current.getVelocity().z);
current.setVelocity(newV);
hasChanged = true;
repaint();
}
});
// spawnField editor
spawnField.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent arg0) {
String text = spawnField.getText();
}
});
selectorPanel.add(typeLabel);
selectorPanel.add(typeSelector);
selectorPanel.add(axiomLabel);
selectorPanel.add(axiomSelector);
adPanel.add(add);
//adPanel.add(remove);
xPanel.add(xLabel);
xPanel.add(x);
yPanel.add(yLabel);
yPanel.add(y);
zPanel.add(zLabel);
zPanel.add(z);
spawnPanel.add(spawnField);
this.add(selectorPanel);
this.add(adPanel);
this.add(xPanel);
this.add(yPanel);
this.add(zPanel);
this.add(spawnPanel);
}
}