summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/Camera.java
blob: 61addea892af3dd94bcda742538a03dd943d1a72 (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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
package NET.worlds.scape;

import NET.worlds.console.Console;
import NET.worlds.console.Gamma;
import NET.worlds.console.RenderCanvas;
import NET.worlds.console.Window;
import NET.worlds.network.URL;
import java.io.IOException;

public class Camera extends WObject implements MouseButtonHandler, MouseEnterHandler, MouseExitHandler, MouseMoveHandler, IncrementalRestorer, URLSelf {
   private RenderCanvas canvas;
   static final int CAMERA_SIZE = 20;
   private boolean mouseIsOverClickable = false;
   private boolean validMouseCoordinates = false;
   private float validMouseX = -1.0F;
   private float validMouseY = -1.0F;
   private static WObject cachedWObject = null;
   private static float cachedMouseX = -1.0F;
   private static float cachedMouseY = -1.0F;
   private int cameraID;
   private int cameraMode;
   private boolean alwaysClearBackground;
   private float xPick;
   private float yPick;
   private Point3Temp unpickSpot;
   private WObject pickedObj;
   public Transform lookAround = Transform.make();
   private static final int isBotMode = 0;
   private static final int isPickingMode = 1;
   private static final int isDrawingMode = 2;
   private static final int isThroughMode = 4;
   static WObject[] downWentTo = new WObject[3];
   static Point3 downAt = new Point3();
   private static Object classCookie = new Object();
   public float xForRoomPreAndPostRender;
   public float yForRoomPreAndPostRender;
   public float zForRoomPreAndPostRender;
   public float dForRoomPreAndPostRender;

   static {
      cameraNativeInit();
   }

   public Camera() {
      this.setVisible(false);
   }

   public static native void cameraNativeInit();

   public Window getWindow() {
      return this.canvas == null ? null : this.canvas.getWindow();
   }

   @Override
   public BoundBoxTemp getBoundBox() {
      Transform t = this.getObjectToWorldMatrix();
      Point3Temp p = t.getPosition();
      t.recycle();
      Point3Temp q = Point3Temp.make(p).minus(20.0F);
      return BoundBoxTemp.make(q, p.plus(20.0F));
   }

   public static WObject getMousePickWObject() {
      return cachedWObject;
   }

   public static float getMousePickX() {
      return cachedMouseX;
   }

   public static float getMousePickY() {
      return cachedMouseY;
   }

   protected WObject updateView(Window w, int mode) {
      Room r = this.getRoom();
      if (!this.hasClump() && r != null) {
         r.aboutToDraw();
      }

      Transform t = null;
      SuperRoot o = this.getOwner();
      Pilot pilot = null;
      if (o instanceof Pilot && o.getOwner() == r) {
         pilot = (Pilot)o;
         if (r != null) {
            pilot.aboutToDraw();
            this.detach();
            r.add(this);
            t = this.getTransform();
            Point3Temp motion = t.getPosition().times(pilot).minus(pilot.getPosition());
            this.post(pilot);
            if (motion.squaredLength() > 1.0F) {
               this.moveBy(Point3Temp.make().minus(motion));
               boolean pBumpable = pilot.getBumpable();
               pilot.setBumpable(false);
               this.moveThrough(motion);
               pilot.setBumpable(pBumpable);
               Room r2 = this.getRoom();
               if (!this.hasClump() && r2 != null && r2 != r) {
                  r2.aboutToDraw();
               }
            }
         }
      }

      WObject result = this.renderScene(w.getHwnd(), w.fullWidth(), w.fullHeight(), mode, pilot);
      if (t != null) {
         this.detach();
         pilot.add(this);
         this.setTransform(t);
         t.recycle();
      }

      return result;
   }

   public void renderToCanvas() {
      assert this.cameraID == 0;

      if (this.isActive()) {
         Window w = this.getWindow();
         if (w != null) {
            if (this.validMouseCoordinates) {
               this.xPick = this.validMouseX;
               this.yPick = this.validMouseY;
               WObject obj = this.updateView(w, 7);
               boolean toggleMouseCursor = false;
               cachedWObject = obj;
               cachedMouseX = this.validMouseX;
               cachedMouseY = this.validMouseY;
               if (obj != null && obj.acceptsLeftClicks()) {
                  if (!this.mouseIsOverClickable) {
                     this.mouseIsOverClickable = true;
                     toggleMouseCursor = true;
                  }
               } else if (this.mouseIsOverClickable) {
                  this.mouseIsOverClickable = false;
                  toggleMouseCursor = true;
               }

               if (toggleMouseCursor) {
                  URL newURL = null;
                  if (this.mouseIsOverClickable) {
                     newURL = URL.make("home:HAND-M.CUR");
                  } else {
                     newURL = URL.make("system:DEFAULT_CURSOR");
                  }

                  Console.getActive().getCursor().setURL(newURL);
               }
            } else {
               this.updateView(w, 6);
            }
         }
      }
   }

   public native void nDrawText(String var1, int var2, int var3, int var4, int var5);

   public RenderCanvas getCanvas() {
      return this.canvas;
   }

   public void setCanvas(RenderCanvas c) {
      this.canvas = c;
   }

   public void setAlwaysClearBackground(boolean in) {
      this.alwaysClearBackground = in;
   }

   public Point3Temp lastPickSpot() {
      return this.unpickSpot;
   }

   public void transferFrom(Camera target) {
      this.lookAround.setTransform(target.lookAround);
   }

   synchronized native WObject renderScene(int var1, int var2, int var3, int var4, Pilot var5);

   public WObject getObjectAt(float x, float y, boolean pickingThrough, Point3Temp pt) {
      Window w = this.getWindow();
      if (w == null) {
         return null;
      } else {
         this.xPick = x;
         this.yPick = y;
         this.unpickSpot = null;
         WObject obj = this.updateView(w, 1 + (pickingThrough ? 4 : 0));
         if (pt != null && this.unpickSpot != null) {
            pt.copy(this.unpickSpot);
         }

         return obj;
      }
   }

   private void sendClick(WObject obj, MouseButtonEvent e) {
      WObject oldTarget = e.target;
      e.target = obj;
      obj.deliver(e);
      e.target = oldTarget;
   }

   @Override
   public boolean handle(MouseButtonEvent e) {
      WObject obj = null;
      int which = 3;
      if (e.key == '\ue301') {
         which = 0;
      } else if (e.key == '\ue302') {
         which = 1;
      } else if (e.key == '\ue304') {
         which = 2;
      }

      assert which != 3;

      if (e instanceof MouseUpEvent) {
         obj = downWentTo[which];
         downWentTo[which] = null;
      } else {
         Window w = this.getWindow();
         if (w != null && !w.getDeltaMode()) {
            if (which == 1 && Gamma.getShaper() != null) {
               obj = this.getObjectAt(e.x, e.y, false, null);
               if (obj != null) {
                  obj.rightMenu();
               }
            } else {
               obj = this.getObjectAt(e.x, e.y, true, downAt);
            }

            downWentTo[which] = obj;
         }
      }

      if (obj != null) {
         if (which == 0 && e instanceof MouseUpEvent) {
            obj.doAction(Console.getActive().getDefaultAction(), e);
         }

         this.sendClick(obj, e);
      }

      return false;
   }

   @Override
   public boolean handle(MouseEnterEvent e) {
      if (e != null) {
         URL defaultCursor = URL.make("system:DEFAULT_CURSOR");
         Console.getActive().getCursor().setURL(defaultCursor);
         this.mouseIsOverClickable = false;
         this.validMouseCoordinates = true;
         this.validMouseX = e.x;
         this.validMouseY = e.y;
         cachedWObject = null;
         cachedMouseX = -1.0F;
         cachedMouseY = -1.0F;
      }

      return false;
   }

   @Override
   public boolean handle(MouseExitEvent e) {
      if (e != null) {
         URL defaultCursor = URL.make("system:DEFAULT_CURSOR");
         Console.getActive().getCursor().setURL(defaultCursor);
         this.mouseIsOverClickable = false;
         this.validMouseCoordinates = false;
         this.validMouseX = -1.0F;
         this.validMouseY = -1.0F;
         cachedWObject = null;
         cachedMouseX = -1.0F;
         cachedMouseY = -1.0F;
      }

      return false;
   }

   @Override
   public boolean handle(MouseMoveEvent e) {
      if (e != null) {
         this.validMouseX = e.x;
         this.validMouseY = e.y;
      }

      return false;
   }

   @Override
   public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException {
      Object ret = null;
      int var10000 = index - offset;
      return super.properties(index, offset + 0, mode, value);
   }

   @Override
   public void saveState(Saver s) throws IOException {
      s.saveVersion(2, classCookie);
      super.saveState(s);
   }

   @Override
   public void restoreState(Restorer r) throws IOException, TooNewException {
      switch (r.restoreVersion(classCookie)) {
         case 0:
         case 2:
            super.restoreState(r);
            break;
         case 1:
            super.restoreState(r);
            r.restoreFloat();
            r.restore();
            r.restore();
            break;
         default:
            throw new TooNewException();
      }
   }

   @Override
   public int incRestore(int state, Restorer r, URLSelfLoader p) throws Exception {
      if (state == 0) {
         this.restoreState(r);
      }

      if (r.version() != 3) {
         return -1;
      } else {
         if (state == 0) {
            p.otemp1 = r.restore(false);
         }

         World w = (World)p.otemp1;
         if (state == 0) {
            w.setSourceURL(this.getSourceURL());
         }

         return w.incRestore(state, r, p);
      }
   }

   @Override
   public void incRef() {
      assert false;
   }

   @Override
   public void decRef() {
      assert false;
   }
}