-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSettings.java
More file actions
234 lines (207 loc) · 9.62 KB
/
Copy pathSettings.java
File metadata and controls
234 lines (207 loc) · 9.62 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
package com.panels;
import com.javagamemaker.javagameengine.components.Component;
import com.components.Vector2;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
public class Settings extends JPanel {
com.javagamemaker.javagameengine.components.Component selectedComponent;
public Settings(){
setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
}
public static List<Field> getFieldsUpTo( Class<?> startClass,
Class<?> exclusiveParent) {
if(startClass == exclusiveParent){
return Arrays.asList(startClass.getDeclaredFields());
}else{
List<Field> my = new java.util.ArrayList<>(Arrays.asList(startClass.getDeclaredFields()));
my.addAll(getFieldsUpTo(startClass.getSuperclass(),exclusiveParent));
return my;
}
}
/*
plan is to scan fields and create swing elements and bide the data togheter so boolean -> checkbox
*/
public void loadFields() throws IllegalAccessException {
List<Field> fields = getFieldsUpTo(selectedComponent.getClass(), com.javagamemaker.javagameengine.components.Component.class);
int nField = 1;
for (Field field : fields) {
// System.out.println(field);
// type field.getAnnotatedType()
boolean accessible = field.isAccessible();
field.setAccessible(true);
if (field.getModifiers() == 1 || field.getModifiers() == 4) {
if (field.getType() == Color.class)
add(new JLabel("color"));
if (field.getType() == com.javagamemaker.javagameengine.msc.Vector2.class){
field.setAccessible(true);
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p,BoxLayout.Y_AXIS));
p.add(new JLabel(field.getName()));
p.add(new Vector2(((com.javagamemaker.javagameengine.msc.Vector2) field.get(selectedComponent))){
@Override
public void update() {
super.update();
if(field.getName() == "position") getSelectedComponent().setPosition(vector2);
if(field.getName() == "scale") getSelectedComponent().setScale(vector2);
}
});
add(p);
field.setAccessible(false);
}
if (field.getType() == float.class) {
JPanel p = new JPanel();
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(new JLabel(field.getName()));
JSpinner spinner = new JSpinner();
spinner.setValue(field.get(selectedComponent));
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
try {
field.set(selectedComponent, spinner.getValue());
} catch (IllegalAccessException ex) {
throw new RuntimeException(ex);
}
}
});
p.setMaximumSize(new Dimension(1000, 20));
spinner.setMinimumSize(new Dimension(100, 20));
p.add(spinner);
add(p);
}
if (field.getType() == boolean.class) {
JCheckBox box = new JCheckBox(field.getName());
field.setAccessible(true);
box.setSelected((Boolean) field.get(selectedComponent));
field.setAccessible(false);
box.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
boolean accessible = field.isAccessible();
field.setAccessible(true);
com.javagamemaker.javagameengine.msc.Debug.log(box.isSelected());
field.set(selectedComponent, box.isSelected());
field.setAccessible(accessible);
}catch (Exception ee){}
}
});
add(box);
}
if (field.getType() == int.class) {
JPanel p = new JPanel();
p.setMaximumSize(new Dimension(1000, 20));
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(new JLabel(field.getName()));
JSpinner spinner = new JSpinner();
field.setAccessible(true);
spinner.setValue(field.get(selectedComponent));
field.setAccessible(false);
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
try {
boolean accessible = field.isAccessible();
field.setAccessible(true);
field.set(selectedComponent, spinner.getValue());
field.setAccessible(accessible);
}catch (Exception ee){}
}
});
p.setMaximumSize(new Dimension(1000, 20));
p.add(spinner);
add(p);
}
if(field.getType() == String.class){
JPanel p = new JPanel();
p.setMaximumSize(new Dimension(1000, 20));
p.setLayout(new BoxLayout(p, BoxLayout.X_AXIS));
p.add(new JLabel(field.getName()));
JTextField text = new JTextField();
field.setAccessible(true);
text.setText(String.valueOf(field.get(selectedComponent)));
field.setAccessible(false);
text.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
try {
boolean accessible = field.isAccessible();
field.setAccessible(true);
field.set(selectedComponent, text.getText());
field.setAccessible(accessible);
}catch (Exception ee){}
}
});
if(field.getType() == Component.class){
JButton b = new JButton("Open "+field.getName());
b.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
}
});
add(b);
}
p.add(text);
add(p);
}
field.setAccessible(accessible);
}
validate();
repaint();
}
}
public void setSelectedComponent(com.javagamemaker.javagameengine.components.Component selectedComponent) {
this.selectedComponent = selectedComponent;
removeAll();
try {
if(selectedComponent!=null)
loadFields();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
/*
pos = new Vector2(selectedComponent.getPosition()){
@Override
public void update() {
super.update();
if(selectedComponent.getParent() == null)
selectedComponent.setPosition(getVector2());
else
selectedComponent.setParentOffset(getVector2());
}
};
scale = new Vector2(selectedComponent.getScale()){
@Override
public void update() {
super.update();
selectedComponent.setScale(getVector2());
}
};
rotation = new Vector2(new javagameengine.msc.Vector2(selectedComponent.getAngle(),selectedComponent.getAngle()));
add(pos);
add(scale);
add(rotation);
repaint();
validate();*/
}
public void update(){
removeAll();
try {
loadFields();
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
validate();
repaint();
}
public com.javagamemaker.javagameengine.components.Component getSelectedComponent() {
return selectedComponent;
}
}