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; } }