diff options
author | endolf <[email protected]> | 2007-06-10 10:51:31 +0000 |
---|---|---|
committer | endolf <[email protected]> | 2007-06-10 10:51:31 +0000 |
commit | f066a36502cb89bef96d1f32ea99eb81ae0fb7ec (patch) | |
tree | e60fe420348974827c4f29c9a030058b2d9bc591 /coreAPI | |
parent | c57d5cd72ac3b086857a1e15a1ff4cafd3cb06f0 (diff) |
Added commented examples
git-svn-id: file:///home/sven/projects/JOGL/git-svn/svn-server-sync/jinput/trunk@185 e343933a-64c8-49c5-92b1-88f2ce3e89e8
Diffstat (limited to 'coreAPI')
-rw-r--r-- | coreAPI/src/java/net/java/games/input/example/ReadAllEvents.java | 92 | ||||
-rw-r--r-- | coreAPI/src/java/net/java/games/input/example/ReadFirstMouse.java | 86 |
2 files changed, 178 insertions, 0 deletions
diff --git a/coreAPI/src/java/net/java/games/input/example/ReadAllEvents.java b/coreAPI/src/java/net/java/games/input/example/ReadAllEvents.java new file mode 100644 index 0000000..f792e66 --- /dev/null +++ b/coreAPI/src/java/net/java/games/input/example/ReadAllEvents.java @@ -0,0 +1,92 @@ +package net.java.games.input.example; + +import net.java.games.input.Component; +import net.java.games.input.Controller; +import net.java.games.input.ControllerEnvironment; +import net.java.games.input.Event; +import net.java.games.input.EventQueue; + +/** + * This class shows how to use the event queue system in JInput. It will show + * how to get the controllers, how to get the event queue for a controller, and + * how to read and process events from the queue. + * + * @author Endolf + */ +public class ReadAllEvents { + + public ReadAllEvents() { + while (true) { + /* Get the available controllers */ + Controller[] controllers = ControllerEnvironment + .getDefaultEnvironment().getControllers(); + if (controllers.length == 0) { + System.out.println("Found no controllers."); + System.exit(0); + } + + for (int i = 0; i < controllers.length; i++) { + /* Remember to poll each one */ + controllers[i].poll(); + + /* Get the controllers event queue */ + EventQueue queue = controllers[i].getEventQueue(); + + /* Create an event object for the underlying plugin to populate */ + Event event = new Event(); + + /* For each object in the queue */ + while (queue.getNextEvent(event)) { + + /* + * Create a strug buffer and put in it, the controller name, + * the time stamp of the event, the name of the component + * that changed and the new value. + * + * Note that the timestamp is a relative thing, not + * absolute, we can tell what order events happened in + * across controllers this way. We can not use it to tell + * exactly *when* an event happened just the order. + */ + StringBuffer buffer = new StringBuffer(controllers[i] + .getName()); + buffer.append(" at "); + buffer.append(event.getNanos()).append(", "); + Component comp = event.getComponent(); + buffer.append(comp.getName()).append(" changed to "); + float value = event.getValue(); + + /* + * Check the type of the component and display an + * appropriate value + */ + if (comp.isAnalog()) { + buffer.append(value); + } else { + if (value == 1.0f) { + buffer.append("On"); + } else { + buffer.append("Off"); + } + } + System.out.println(buffer.toString()); + } + } + + /* + * Sleep for 20 milliseconds, in here only so the example doesn't + * thrash the system. + */ + try { + Thread.sleep(20); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + public static void main(String[] args) { + new ReadAllEvents(); + } +} diff --git a/coreAPI/src/java/net/java/games/input/example/ReadFirstMouse.java b/coreAPI/src/java/net/java/games/input/example/ReadFirstMouse.java new file mode 100644 index 0000000..83788ec --- /dev/null +++ b/coreAPI/src/java/net/java/games/input/example/ReadFirstMouse.java @@ -0,0 +1,86 @@ +package net.java.games.input.example; + +import net.java.games.input.Component; +import net.java.games.input.Controller; +import net.java.games.input.ControllerEnvironment; +import net.java.games.input.test.ControllerTextTest; + +/** + * This class shows how to read the values in a polling loop for the first mouse + * detected. It will show how to get the available controllers, how to check the + * type of the controller, how to read the components of the controller, and how + * to get the data from the component. + * + * @author Endolf + */ +public class ReadFirstMouse { + + public ReadFirstMouse() { + /* Get the available controllers */ + Controller[] controllers = ControllerEnvironment + .getDefaultEnvironment().getControllers(); + + /* + * Loop through the controllers, check the type of each one, and save + * the first mouse we find. + */ + Controller firstMouse = null; + for (int i = 0; i < controllers.length && firstMouse == null; i++) { + if (controllers[i].getType() == Controller.Type.MOUSE) { + // Found a mouse + firstMouse = controllers[i]; + } + } + if (firstMouse == null) { + // Couldn't find a mouse + System.out.println("Found no mouse"); + System.exit(0); + } + + System.out.println("First mouse is: " + firstMouse.getName()); + + while (true) { + /* Poll the controller */ + firstMouse.poll(); + + /* Get all the axis and buttons */ + Component[] components = firstMouse.getComponents(); + StringBuffer buffer = new StringBuffer(); + + /* For each component, get it's name, and it's current value */ + for (int i = 0; i < components.length; i++) { + if (i > 0) { + buffer.append(", "); + } + buffer.append(components[i].getName()); + buffer.append(": "); + if (components[i].isAnalog()) { + /* Get the value at the last poll of this component */ + buffer.append(components[i].getPollData()); + } else { + if (components[i].getPollData() == 1.0f) { + buffer.append("On"); + } else { + buffer.append("Off"); + } + } + } + System.out.println(buffer.toString()); + + /* + * Sleep for 20 millis, this is just so the example doesn't thrash + * the system. + */ + try { + Thread.sleep(20); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + } + + public static void main(String[] args) { + new ReadFirstMouse(); + } +} |