summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/TransformEditorDialog.java
blob: d7b5a21d6ec170023221312943381b70cb9e6178 (plain) (blame)
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
package NET.worlds.scape;

import NET.worlds.console.ConfirmDialog;
import NET.worlds.console.Console;
import NET.worlds.console.DialogReceiver;
import NET.worlds.console.PolledDialog;
import java.awt.Button;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Label;
import java.awt.TextField;
import java.text.MessageFormat;
import java.util.StringTokenizer;
import java.util.Vector;

class TransformEditorDialog extends PolledDialog implements DialogReceiver {
   Property property;
   Transform transform;
   int undoCount;
   Button undoButton = new Button(Console.message("Undo"));
   Button okButton = new Button(Console.message("OK"));
   Button cancelButton = new Button(Console.message("Cancel"));
   Button pitch = new MyButton(Console.message("Pitch-N"), 0, 1);
   Button roll = new MyButton(Console.message("Roll-N"), 0, 1);
   Button yaw = new MyButton(Console.message("Yaw-N"), 0, 1);
   Button spin = new MyButton(Console.message("Spin-by-XYZN"), 3, 1);
   Button spinTo = new MyButton(Console.message("Spin-to-XYZN"), 3, 1);
   Button moveX = new MyButton(Console.message("Move-x-by-N"), 0, 1);
   Button moveY = new MyButton(Console.message("Move-y-by-N"), 0, 1);
   Button moveZ = new MyButton(Console.message("Move-z-by-N"), 0, 1);
   Button moveBy = new MyButton(Console.message("Move-by-XYZ"), 3, 0);
   Button moveTo = new MyButton(Console.message("Move-to-XYZ"), 3, 0);
   Button scaleX = new MyButton(Console.message("Scale-x-by-N"), 0, 1);
   Button scaleY = new MyButton(Console.message("Scale-y-by-N"), 0, 1);
   Button scaleZ = new MyButton(Console.message("Scale-z-by-N"), 0, 1);
   Button scaleBy = new MyButton(Console.message("Scale-by-N"), 0, 1);
   Button scaleTo = new MyButton(Console.message("Scale-to-N"), 0, 1);
   Label position = new Label();
   Label rotation = new Label();
   Label scale = new Label();
   TextField editXYZ = new TextField();
   TextField editN = new TextField();
   GridBagLayout gbag = new GridBagLayout();
   GridBagConstraints c = new GridBagConstraints();
   Font normalFont;
   Font selectedFont;
   Button selectedButton;
   TextField lastEdit;
   EditTile parent;
   Object queuedFunc;
   private static Object lastPosAndSize;

   TransformEditorDialog(EditTile parent, String title, Property property) {
      super(Console.getFrame(), parent, title, false);
      this.property = property;
      this.parent = parent;
      this.ready();
   }

   @Override
   protected void build() {
      this.transform = (Transform)this.property.get();
      this.setLayout(this.gbag);
      this.add2(new Label("(X,Y,Z):"), this.editXYZ);
      this.add2(new Label("(N):"), this.editN);
      this.add3(this.moveX, this.pitch, this.scaleX);
      this.add3(this.moveY, this.roll, this.scaleY);
      this.add3(this.moveZ, this.yaw, this.scaleZ);
      this.add3(this.moveBy, this.spin, this.scaleBy);
      this.add3(this.moveTo, this.spinTo, this.scaleTo);
      this.add2(new Label(Console.message("Position")), this.position);
      this.add2(new Label(Console.message("Rotation:")), this.rotation);
      this.add2(new Label(Console.message("Scale:")), this.scale);
      this.addButtons(this.undoButton, this.okButton, this.cancelButton);
      this.normalFont = this.pitch.getFont();
      this.selectedFont = new Font(this.normalFont.getName(), this.normalFont.isBold() ? 0 : 1, this.normalFont.getSize());
      this.updateInfo();
   }

   private void add2(Component c1, Component c2) {
      this.c.fill = 0;
      this.c.anchor = 13;
      this.c.gridheight = 1;
      this.c.weightx = 1.0;
      this.c.weighty = 1.0;
      this.c.gridwidth = 1;
      this.add(this.gbag, c1, this.c);
      this.c.anchor = 17;
      this.c.fill = 2;
      this.c.gridwidth = 0;
      this.add(this.gbag, c2, this.c);
   }

   private void add3(Component c1, Component c2, Component c3) {
      this.c.fill = 2;
      this.c.anchor = 17;
      this.c.gridheight = 1;
      this.c.gridwidth = 3;
      this.c.weightx = 1.0;
      this.c.weighty = 1.0;
      this.add(this.gbag, c1, this.c);
      this.c.weightx = 0.0;
      this.c.weighty = 0.0;
      this.add(this.gbag, c2, this.c);
      this.c.gridwidth = 0;
      this.add(this.gbag, c3, this.c);
   }

   private void addButtons(Component c1, Component c2, Component c3) {
      this.c.fill = 0;
      this.c.anchor = 13;
      this.c.gridheight = 1;
      this.c.gridwidth = 3;
      this.c.weightx = 1.0;
      this.c.weighty = 1.0;
      this.add(this.gbag, c1, this.c);
      this.c.anchor = 10;
      this.add(this.gbag, c2, this.c);
      this.c.anchor = 17;
      this.add(this.gbag, c3, this.c);
   }

   private void selectButton(Button button) {
      if (this.selectedButton != null) {
         this.selectedButton.setFont(this.normalFont);
      }

      this.selectedButton = button;
      this.selectedButton.setFont(this.selectedFont);
   }

   private void updateInfo() {
      this.position.setText(this.transform.getPosition().toString());
      this.scale.setText(this.transform.getScale().toString());
      Point3Temp axis = Point3Temp.make();
      float angle = this.transform.getSpin(axis);
      Object[] arguments = new Object[]{new String("" + axis), new String("" + angle)};
      this.rotation.setText(MessageFormat.format(Console.message("angle"), arguments));
      this.undoButton.enable(this.undoCount != 0);
   }

   private void set(Transform t) {
      this.parent.addUndoableSet(this.property, t);
      this.undoCount++;
      this.transform = (Transform)this.property.get();
   }

   @Override
   protected synchronized void activeCallback() {
      if (this.queuedFunc == this.undoButton) {
         if (this.undoCount != 0) {
            this.parent.undo();
            this.undoCount--;
            this.transform = (Transform)this.property.get();
            this.updateInfo();
         }
      } else if (this.queuedFunc instanceof MyButton) {
         this.apply((MyButton)this.queuedFunc);
      }

      this.queuedFunc = null;
   }

   public boolean undoAll() {
      if (this.undoCount == 0) {
         return this.done(false);
      } else {
         new ConfirmDialog(this, Console.message("Cancel-Transform"), Console.message("undo-changes"));
         return true;
      }
   }

   @Override
   public synchronized void dialogDone(Object who, boolean confirmed) {
      if (confirmed) {
         while (this.undoCount != 0) {
            this.parent.undo();
            this.undoCount--;
         }

         this.done(false);
      }
   }

   @Override
   public synchronized boolean action(java.awt.Event event, Object what) {
      Object target = event.target;
      if (target == this.okButton) {
         return this.done(true);
      } else if (target == this.cancelButton) {
         return this.undoAll();
      } else if ((target == this.undoButton || target instanceof MyButton) && this.queuedFunc == null) {
         this.queuedFunc = target;
         return true;
      } else {
         return false;
      }
   }

   private float[] parseNumbers(TextField edit) {
      Vector vec = new Vector();
      StringTokenizer e = new StringTokenizer(edit.getText().trim(), ", \t", false);

      while (e.hasMoreElements()) {
         try {
            vec.addElement(Float.valueOf(e.nextToken()));
         } catch (NumberFormatException var6) {
            vec.removeAllElements();
            break;
         }
      }

      float[] ret = new float[vec.size()];

      for (int i = 0; i < ret.length; i++) {
         ret[i] = (Float)vec.elementAt(i);
      }

      return ret;
   }

   private boolean apply(MyButton func) {
      float[] n = this.parseNumbers(this.editN);
      float[] xyz = this.parseNumbers(this.editXYZ);
      if (func.numParamsXYZ != 0 && func.numParamsXYZ != xyz.length) {
         return this.setEdit(this.editXYZ);
      } else if (func.numParamsN != 0 && func.numParamsN != n.length) {
         return this.setEdit(this.editN);
      } else {
         if (func == this.pitch) {
            this.transform.worldSpin(1.0F, 0.0F, 0.0F, n[0]);
         } else if (func == this.roll) {
            this.transform.worldSpin(0.0F, 1.0F, 0.0F, n[0]);
         } else if (func == this.yaw) {
            this.transform.worldSpin(0.0F, 0.0F, 1.0F, n[0]);
         } else if (func == this.spin) {
            this.transform.worldSpin(xyz[0], xyz[1], xyz[2], n[0]);
         } else if (func == this.spinTo) {
            Point3Temp scale = this.transform.getScale();
            Point3Temp pos = this.transform.getPosition();
            this.transform.makeIdentity();
            this.transform.scale(scale);
            this.transform.worldSpin(xyz[0], xyz[1], xyz[2], n[0]);
            this.transform.moveTo(pos.x, pos.y, pos.z);
         } else if (func == this.moveX) {
            this.transform.moveBy(n[0], 0.0F, 0.0F);
         } else if (func == this.moveY) {
            this.transform.moveBy(0.0F, n[0], 0.0F);
         } else if (func == this.moveZ) {
            this.transform.moveBy(0.0F, 0.0F, n[0]);
         } else if (func == this.moveBy) {
            this.transform.moveBy(xyz[0], xyz[1], xyz[2]);
         } else if (func == this.moveTo) {
            this.transform.moveTo(xyz[0], xyz[1], xyz[2]);
         } else if (func == this.scaleX && n[0] != 0.0F && this.checkScale(n[0], this.transform.getScaleX())) {
            this.transform.scale(n[0], 1.0F, 1.0F);
         } else if (func == this.scaleY && n[0] != 0.0F && this.checkScale(n[0], this.transform.getScaleY())) {
            this.transform.scale(1.0F, n[0], 1.0F);
         } else if (func == this.scaleZ && n[0] != 0.0F && this.checkScale(n[0], this.transform.getScaleZ())) {
            this.transform.scale(1.0F, 1.0F, n[0]);
         } else if (func == this.scaleBy
            && n[0] != 0.0F
            && this.checkScale(n[0], this.transform.getScaleX())
            && this.checkScale(n[0], this.transform.getScaleY())
            && this.checkScale(n[0], this.transform.getScaleZ())) {
            this.transform.scale(n[0]);
         } else {
            if (func != this.scaleTo || n[0] == 0.0F) {
               return true;
            }

            Point3Temp scale = this.transform.getScale();
            this.transform.scale(n[0] / scale.x, n[0] / scale.y, n[0] / scale.z);
         }

         this.set(this.transform);
         this.selectButton(func);
         this.updateInfo();
         return this.setEdit(this.lastEdit);
      }
   }

   private boolean checkScale(float oldScale, float factor) {
      double newScale = (double)oldScale * factor;
      if (newScale < 2.938736052218037E-39) {
         Console.println(Console.message("exceed-minimum"));
      } else {
         if (!(newScale > Float.MAX_VALUE)) {
            return true;
         }

         Console.println(Console.message("exceed-maximum"));
      }

      return false;
   }

   private boolean toggleEdit() {
      return this.lastEdit == this.editN ? this.setEdit(this.editXYZ) : this.setEdit(this.editN);
   }

   private boolean setEdit(TextField edit) {
      edit.requestFocus();
      edit.selectAll();
      this.lastEdit = edit;
      return true;
   }

   @Override
   public boolean keyDown(java.awt.Event event, int key) {
      if (event.target == this.editXYZ || event.target == this.editN) {
         this.lastEdit = (TextField)event.target;
      }

      if (key == 27) {
         return this.undoAll();
      } else if (key == 10) {
         return this.done(true);
      } else {
         return key == 9 ? this.toggleEdit() : super.keyDown(event, key);
      }
   }

   @Override
   public void show() {
      super.show();
      this.setEdit(this.editXYZ);
   }

   @Override
   public void savePosAndSize(Object state) {
      lastPosAndSize = state;
   }

   @Override
   public Object restorePosAndSize() {
      return lastPosAndSize;
   }
}