From 9eda30c3d96f6d465891421a4e649a5906bcdf4a Mon Sep 17 00:00:00 2001 From: Julien Gouesse Date: Wed, 28 May 2014 21:58:58 +0200 Subject: Copies the controller based on JInput 2.0.6 into the JogAmp backend --- ardor3d-jogl/pom.xml | 4 + .../input/jinput/JInputControllerWrapper.java | 127 +++++++++++++++++++++ 2 files changed, 131 insertions(+) create mode 100644 ardor3d-jogl/src/main/java/com/ardor3d/input/jinput/JInputControllerWrapper.java (limited to 'ardor3d-jogl') diff --git a/ardor3d-jogl/pom.xml b/ardor3d-jogl/pom.xml index f0d90cc..e14ff2a 100644 --- a/ardor3d-jogl/pom.xml +++ b/ardor3d-jogl/pom.xml @@ -46,6 +46,10 @@ org.jogamp.jocl jocl-main + + net.java.jinput + jinput + org.eclipse.swt ${swt.artifactId} 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 . + */ + +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 _events = Collections.synchronizedList(new ArrayList()); + protected JInputControllerEventIterator _eventsIt = new JInputControllerEventIterator(); + protected final List _controllers = Lists.newArrayList(); + protected static boolean _inited = false; + + @Override + public PeekingIterator 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 axisNames = Lists.newArrayList(); + final List 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 implements + PeekingIterator { + + @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 -- cgit v1.2.3