import com.blaxxun.x3d.*; import com.blaxxun.bx3d.blaxxun3d; /** * hud * * implements a blaxxun3d hud * * @author marc kaufmann (mk), blaxxun interactive * @version 0.1 * **/ public class hud extends java.applet.Applet implements EventObserver { private Browser browser=null; private boolean initialized=false; private Node hud=null; private Field hudpos=null; private Field hudrot=null; private Node camred=null; private Node camgreen=null; private Node camblue=null; private Field bindred=null; private Field bindblue=null; private Field bindgreen=null; private Node tsred=null; private Node tsgreen=null; private Node tsblue=null; private Field clickred=null; private Field clickgreen=null; private Field clickblue=null; public void start() { // connect to blaxxun3d while (browser==null) { browser=blaxxun3d.getBrowser("browser"); hesitate(); } System.out.println("Got browser."); // wait till everything is loaded while (browser.getWorld()==null) { hesitate(); } System.out.println("World loaded."); browser.addAWTObserver(this); browser.addBrowserObserver(this); initialized=connectNodes(); setHud(); } /** * cleanup **/ public void stop() { browser.removeAWTObserver(this); browser.removeBrowserObserver(this); super.destroy(); } /** * Handles awt events sent from the x3d browser * * @param e The java.awt.Event * **/ public boolean onEvent(java.awt.Event event) { return false; } /** * Handles render events sent from the x3d browser * * @param type The type of the event * @param object The object that is associated with the event, Field or Node. object might be * null for certain events, e.g. SHUTDOWN. * @param userdata The userdata that was specified when registering for the event. **/ public boolean onEvent(int type, Object object, Object userData){ switch (type) { case FIELD_CHANGED: { if (((String)userData).equals("red")) { bindred.setValueBool(true); } if (((String)userData).equals("blue")) { bindblue.setValueBool(true); } if (((String)userData).equals("green")) { bindgreen.setValueBool(true); } } case START_RENDERING: { if (initialized) setHud(); break; } } return false; } /** * Establishes the connection to the nodes * * @return false if connection has not been established, else true **/ private boolean connectNodes() { try { hud=browser.getNode("hud"); hudpos=hud.getField("translation"); hudrot=hud.getField("rotation"); // get touch sensor active flags tsred=browser.getNode("t1"); tsgreen=browser.getNode("t2"); tsblue=browser.getNode("t3"); clickred=tsred.getField("touchTime"); clickgreen=tsgreen.getField("touchTime"); clickblue=tsblue.getField("touchTime"); //register observer clickred.addObserver(this,"red"); clickgreen.addObserver(this,"green"); clickblue.addObserver(this,"blue"); // get camera set bind events camred=browser.getNode("camred"); bindred=camred.getField("set_bind"); camblue=browser.getNode("camblue"); bindblue=camblue.getField("set_bind"); camgreen=browser.getNode("camgreen"); bindgreen=camgreen.getField("set_bind"); } catch (Exception e) { System.out.println(e+": Problems getting nodes!"); stop(); } System.out.println("Got nodes."); return true; } /** * Update the hud's coordinates * **/ private void setHud() { Node vp=null; float [] pos=new float[3]; float [] rot=new float[4]; // get current cam coords vp=(Node)browser.getProperty(browser.PROPERTY_BOUNDVIEWPOINT); pos=(vp.getField("position")).getValueFloatArray(0,-1); rot=(vp.getField("orientation")).getValueFloatArray(0,-1); // sync with renderer browser.beginUpdate(); // update hud's coords hudpos.setValueFloatArray(0,-1,pos); hudrot.setValueFloatArray(0,-1,rot); // sync browser.endUpdate(); } /** * waits 300 millis */ private void hesitate() { try { Thread.sleep(300); } catch(InterruptedException ignored) {} } }