00001 package org.hfbk.nubsi;
00002
00003 import java.io.IOException;
00004 import java.io.OutputStream;
00005 import java.io.PrintStream;
00006 import java.util.HashMap;
00007
00008 import net.java.games.input.Component;
00009 import net.java.games.input.Controller;
00010 import net.java.games.input.ControllerEnvironment;
00011 import net.java.games.input.Event;
00012
00013 import org.hfbk.util.Sleeper;
00014 import org.lwjgl.util.vector.Vector3f;
00015
00027 public class Spacemouse extends Thread {
00028
00029 public static Spacemouse instance;
00030
00031 public Vector3f trans = new Vector3f(), rot = new Vector3f();
00032
00033 final float transSpeed = 0.015f, rotSpeed = 0.003f;
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 final int DELAY = 100;
00050
00051 private HashMap<Component, Integer> mapping = new HashMap<Component, Integer>();
00052
00053 private Controller spacemouse = null;
00054
00055
00056 private Spacemouse() {
00057 super("Spacemouse");
00058 setDaemon(true);
00059
00060 int[] map=null;
00061
00062
00063 PrintStream out=System.out;
00064 System.setOut(new PrintStream(new OutputStream(){
00065 public void write(int b) throws IOException {}
00066 }));
00067 ControllerEnvironment ce = ControllerEnvironment.getDefaultEnvironment();
00068 for (Controller c : ce.getControllers())
00069 if (c.getComponents().length > 5)
00070 if (c.getName().matches("3Dconnexion.*")){
00071 spacemouse = c;
00072 map=new int[]{0,2,1,3,5,4};
00073 }else if (c.getName().matches("Space.*")){
00074 spacemouse = c;
00075 map=new int[]{1,2,0,4,5,3};
00076 }
00077
00078 System.setOut(out);
00079
00080 if (spacemouse != null) {
00081 System.out.println("Spacemouse found: " + spacemouse.getName());
00082
00083
00084
00085
00086
00087
00088
00089
00090 int i = 0;
00091 for (Component c : spacemouse.getComponents())
00092 if (c.isAnalog()) {
00093
00094 mapping.put(c, map[i++]);
00095 }
00096
00097 start();
00098 }
00099 }
00100
00101 public void run() {
00102 while (true) {
00103 spacemouse.poll();
00104
00105 float[] vals = new float[6];
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 Event e = new Event();
00119 while (spacemouse.getEventQueue().getNextEvent(e)) {
00120 Component c = e.getComponent();
00121 if (c.isAnalog())
00122 if (mapping.get(c) != null) {
00123
00124 int i = mapping.get(c);
00125 vals[i] = e.getValue();
00126 }
00127 }
00128
00129
00130 synchronized (this) {
00131 trans.set(vals[0] * transSpeed, -vals[1] * transSpeed, vals[2]* transSpeed);
00132 rot.set(vals[3] * rotSpeed, -vals[4] * rotSpeed, vals[5]* rotSpeed);
00133 }
00134 Sleeper.sleep(DELAY);
00135 }
00136 }
00137
00138 public boolean isReady(){
00139 return spacemouse!=null;
00140 }
00141
00142 public static Spacemouse getInstance() {
00143 if (instance==null) instance=new Spacemouse();
00144 return instance;
00145 }
00146 }