diff options
author | Julien Gouesse <[email protected]> | 2014-05-28 21:58:58 +0200 |
---|---|---|
committer | Julien Gouesse <[email protected]> | 2014-05-28 21:58:58 +0200 |
commit | 9eda30c3d96f6d465891421a4e649a5906bcdf4a (patch) | |
tree | c211b65ae02e2adf0e30c94bb9927ce77ef55c1c /ardor3d-jogl/src/main/java/com | |
parent | e5cbd7ab638f4e0186f682769166c19a56e45f6c (diff) |
Copies the controller based on JInput 2.0.6 into the JogAmp backend
Diffstat (limited to 'ardor3d-jogl/src/main/java/com')
-rw-r--r-- | ardor3d-jogl/src/main/java/com/ardor3d/input/jinput/JInputControllerWrapper.java | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/ardor3d-jogl/src/main/java/com/ardor3d/input/jinput/JInputControllerWrapper.java b/ardor3d-jogl/src/main/java/com/ardor3d/input/jinput/JInputControllerWrapper.java new file mode 100644 index 0000000..45e062a --- /dev/null +++ b/ardor3d-jogl/src/main/java/com/ardor3d/input/jinput/JInputControllerWrapper.java @@ -0,0 +1,127 @@ +/** + * Copyright (c) 2008-2012 Ardor Labs, Inc. + * + * This file is part of Ardor3D. + * + * Ardor3D is free software: you can redistribute it and/or modify it + * under the terms of its license which may be found in the accompanying + * LICENSE file or at <http://www.ardor3d.com/LICENSE>. + */ + +package com.ardor3d.input.jinput; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +import net.java.games.input.Component; +import net.java.games.input.Component.Identifier; +import net.java.games.input.Controller; +import net.java.games.input.Controller.Type; +import net.java.games.input.ControllerEnvironment; +import net.java.games.input.Event; + +import com.ardor3d.input.ControllerEvent; +import com.ardor3d.input.ControllerInfo; +import com.ardor3d.input.ControllerState; +import com.ardor3d.input.ControllerWrapper; +import com.google.common.collect.AbstractIterator; +import com.google.common.collect.Lists; +import com.google.common.collect.PeekingIterator; + +public class JInputControllerWrapper implements ControllerWrapper { + + protected final Event _event = new Event(); + protected final List<ControllerEvent> _events = Collections.synchronizedList(new ArrayList<ControllerEvent>()); + protected JInputControllerEventIterator _eventsIt = new JInputControllerEventIterator(); + protected final List<ControllerInfo> _controllers = Lists.newArrayList(); + protected static boolean _inited = false; + + @Override + public PeekingIterator<ControllerEvent> getEvents() { + init(); + if (!_eventsIt.hasNext()) { + _eventsIt = new JInputControllerEventIterator(); + } + for (final Controller controller : ControllerEnvironment.getDefaultEnvironment().getControllers()) { + controller.poll(); + while (controller.getEventQueue().getNextEvent(_event)) { + if (controller.getType() != Type.KEYBOARD && controller.getType() != Type.MOUSE) { + _events.add(createControllerEvent(controller, _event)); + } + } + } + + return _eventsIt; + } + + @Override + public int getControllerCount() { + init(); + return _controllers.size(); + } + + @Override + public ControllerInfo getControllerInfo(final int controllerIndex) { + init(); + return _controllers.get(controllerIndex); + } + + @Override + public synchronized void init() { + if (_inited) { + return; + } + + try { + ControllerEnvironment.getDefaultEnvironment(); + + for (final Controller controller : ControllerEnvironment.getDefaultEnvironment().getControllers()) { + if (controller.getType() != Type.KEYBOARD && controller.getType() != Type.MOUSE) { + _controllers.add(getControllerInfo(controller)); + for (final Component component : controller.getComponents()) { + ControllerState.NOTHING.set(controller.getName(), component.getIdentifier().getName(), 0); + } + } + } + } catch (final Exception e) { + e.printStackTrace(); + } finally { + _inited = true; + } + } + + protected ControllerInfo getControllerInfo(final Controller controller) { + final List<String> axisNames = Lists.newArrayList(); + final List<String> buttonNames = Lists.newArrayList(); + + for (final Component comp : controller.getComponents()) { + if (comp.getIdentifier() instanceof Identifier.Axis) { + axisNames.add(comp.getName()); + } else if (comp.getIdentifier() instanceof Identifier.Button) { + buttonNames.add(comp.getName()); + } + } + + return new ControllerInfo(controller.getName(), axisNames, buttonNames); + } + + protected ControllerEvent createControllerEvent(final Controller controller, final Event event) { + return new ControllerEvent(event.getNanos(), controller.getName(), event.getComponent().getIdentifier() + .getName(), event.getValue()); + } + + protected class JInputControllerEventIterator extends AbstractIterator<ControllerEvent> implements + PeekingIterator<ControllerEvent> { + + @Override + protected ControllerEvent computeNext() { + if (_events.size() > 0) { + final ControllerEvent controllerEvent = _events.remove(0); + return controllerEvent; + } else { + return endOfData(); + } + } + } +}
\ No newline at end of file |