diff options
author | Renanse <[email protected]> | 2012-10-30 18:03:15 -0500 |
---|---|---|
committer | Renanse <[email protected]> | 2012-10-30 18:03:15 -0500 |
commit | 645eae0beeeeb0cc4bb422b14c80de26af793a03 (patch) | |
tree | c0eb91d46cf14e208ebd079809cc38574ee375ab /trunk/ardor3d-core | |
parent | 502690720cdd4276796ab2fa3897d51af13a3e1e (diff) |
Updates to make controllers more useful. New controller condition type.
(originally accidentally committed to old google code repo.)
Diffstat (limited to 'trunk/ardor3d-core')
7 files changed, 187 insertions, 50 deletions
diff --git a/trunk/ardor3d-core/src/main/java/com/ardor3d/input/ControllerInfo.java b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/ControllerInfo.java new file mode 100644 index 0000000..5f6f205 --- /dev/null +++ b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/ControllerInfo.java @@ -0,0 +1,54 @@ +/**
+ * 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;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import com.google.common.collect.Lists;
+
+public class ControllerInfo {
+
+ private final String _controllerName;
+ private final ArrayList<String> _axisNames;
+ private final ArrayList<String> _buttonNames;
+
+ public ControllerInfo(final String controllerName, final List<String> axisNames, final List<String> buttonNames) {
+ _controllerName = controllerName;
+ _axisNames = Lists.newArrayList(axisNames);
+ _buttonNames = Lists.newArrayList(buttonNames);
+ }
+
+ public String getControllerName() {
+ return _controllerName;
+ }
+
+ public ArrayList<String> getAxisNames() {
+ return _axisNames;
+ }
+
+ public ArrayList<String> getButtonNames() {
+ return _buttonNames;
+ }
+
+ public int getAxisCount() {
+ return _axisNames.size();
+ }
+
+ public int getButtonCount() {
+ return _buttonNames.size();
+ }
+
+ @Override
+ public String toString() {
+ return "Controller '" + _controllerName + "' with Axis " + _axisNames + " and Buttons " + _buttonNames;
+ }
+}
\ No newline at end of file diff --git a/trunk/ardor3d-core/src/main/java/com/ardor3d/input/ControllerState.java b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/ControllerState.java index a65032d..dca8689 100644 --- a/trunk/ardor3d-core/src/main/java/com/ardor3d/input/ControllerState.java +++ b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/ControllerState.java @@ -19,26 +19,41 @@ import java.util.Map; import java.util.Map.Entry;
import com.ardor3d.annotation.Immutable;
+import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
@Immutable
public class ControllerState {
- public static final ControllerState NOTHING = new ControllerState();
+ public static final ControllerState NOTHING = new ControllerState(0);
- private final Map<String, Map<String, Float>> controllerStates = new LinkedHashMap<String, Map<String, Float>>();
- private final List<ControllerEvent> eventsSinceLastState = new ArrayList<ControllerEvent>();
+ protected final Map<String, Map<String, Float>> _controllerStates = Maps.newLinkedHashMap();
+ protected final List<ControllerEvent> _eventsSinceLastState = Lists.newArrayList();
+
+ protected ControllerState(final int ignore) {}
+
+ public ControllerState() {
+ ControllerState.NOTHING.duplicateStates(_controllerStates);
+ }
+
+ public ControllerState(final ControllerState previous) {
+ if (previous != null) {
+ previous.duplicateStates(_controllerStates);
+ } else {
+ ControllerState.NOTHING.duplicateStates(_controllerStates);
+ }
+ }
/**
* Sets a components state
*/
public void set(final String controllerName, final String componentName, final float value) {
Map<String, Float> controllerState = null;
- if (controllerStates.containsKey(controllerName)) {
- controllerState = controllerStates.get(controllerName);
+ if (_controllerStates.containsKey(controllerName)) {
+ controllerState = _controllerStates.get(controllerName);
} else {
controllerState = new LinkedHashMap<String, Float>();
- controllerStates.put(controllerName, controllerState);
+ _controllerStates.put(controllerName, controllerState);
}
controllerState.put(componentName, value);
@@ -48,7 +63,7 @@ public class ControllerState { public boolean equals(final Object obj) {
if (obj instanceof ControllerState) {
final ControllerState other = (ControllerState) obj;
- return other.controllerStates.equals(controllerStates);
+ return other._controllerStates.equals(_controllerStates);
}
return false;
@@ -58,8 +73,8 @@ public class ControllerState { public String toString() {
final StringBuilder stateString = new StringBuilder("ControllerState: ");
- for (final String controllerStateKey : controllerStates.keySet()) {
- final Map<String, Float> state = controllerStates.get(controllerStateKey);
+ for (final String controllerStateKey : _controllerStates.keySet()) {
+ final Map<String, Float> state = _controllerStates.get(controllerStateKey);
stateString.append("[").append(controllerStateKey);
for (final String stateKey : state.keySet()) {
stateString.append("[").append(stateKey).append(":").append(state.get(stateKey)).append("]");
@@ -72,43 +87,54 @@ public class ControllerState { public ControllerState snapshot() {
final ControllerState snapshot = new ControllerState();
- duplicateStates(snapshot.controllerStates);
- snapshot.eventsSinceLastState.addAll(eventsSinceLastState);
+ duplicateStates(snapshot._controllerStates);
+ snapshot._eventsSinceLastState.addAll(_eventsSinceLastState);
return snapshot;
}
private void duplicateStates(final Map<String, Map<String, Float>> store) {
store.clear();
- for (final Entry<String, Map<String, Float>> entry : controllerStates.entrySet()) {
+ for (final Entry<String, Map<String, Float>> entry : _controllerStates.entrySet()) {
store.put(entry.getKey(), Maps.newLinkedHashMap(entry.getValue()));
}
}
public void addEvent(final ControllerEvent event) {
- eventsSinceLastState.add(event);
+ _eventsSinceLastState.add(event);
set(event.getControllerName(), event.getComponentName(), event.getValue());
}
public List<ControllerEvent> getEvents() {
- Collections.sort(eventsSinceLastState, new Comparator<ControllerEvent>() {
+ Collections.sort(_eventsSinceLastState, new Comparator<ControllerEvent>() {
public int compare(final ControllerEvent o1, final ControllerEvent o2) {
return (int) (o2.getNanos() - o1.getNanos());
}
});
- return Collections.unmodifiableList(eventsSinceLastState);
+ return Collections.unmodifiableList(_eventsSinceLastState);
}
public void clearEvents() {
- eventsSinceLastState.clear();
+ _eventsSinceLastState.clear();
}
public List<String> getControllerNames() {
- return new ArrayList<String>(controllerStates.keySet());
+ return new ArrayList<String>(_controllerStates.keySet());
}
public List<String> getControllerComponentNames(final String controller) {
- return new ArrayList<String>(controllerStates.get(controller).keySet());
+ return new ArrayList<String>(_controllerStates.get(controller).keySet());
+ }
+
+ public Map<String, Float> getControllerComponentValues(final String controller) {
+ return _controllerStates.get(controller);
+ }
+
+ public float getComponentValue(final String controller, final String component) {
+ if (!_controllerStates.containsKey(controller) || !_controllerStates.get(controller).containsKey(component)) {
+ return 0;
+ }
+ return _controllerStates.get(controller).get(component);
}
-}
+}
\ No newline at end of file diff --git a/trunk/ardor3d-core/src/main/java/com/ardor3d/input/ControllerWrapper.java b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/ControllerWrapper.java index f718c1b..e348ec7 100644 --- a/trunk/ardor3d-core/src/main/java/com/ardor3d/input/ControllerWrapper.java +++ b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/ControllerWrapper.java @@ -14,17 +14,19 @@ import com.google.common.collect.PeekingIterator; public interface ControllerWrapper {
/**
- * Allows the keyboard wrapper implementation to initialise itself.
+ * Allows the controller wrapper implementation to initialize itself.
*/
public void init();
/**
- * Returns a peeking iterator that allows the client to loop through all keyboard events that have not yet been
+ * Returns a peeking iterator that allows the client to loop through all controller events that have not yet been
* handled.
*
* @return an iterator that allows the client to check which events have still not been handled
*/
public PeekingIterator<ControllerEvent> getEvents();
- public ControllerState getBlankState();
-}
+ public int getControllerCount();
+
+ public ControllerInfo getControllerInfo(int controllerIndex);
+}
\ No newline at end of file diff --git a/trunk/ardor3d-core/src/main/java/com/ardor3d/input/PhysicalLayer.java b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/PhysicalLayer.java index fa125f9..5948094 100644 --- a/trunk/ardor3d-core/src/main/java/com/ardor3d/input/PhysicalLayer.java +++ b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/PhysicalLayer.java @@ -71,6 +71,7 @@ public class PhysicalLayer { _currentKeyboardState = KeyboardState.NOTHING; _currentMouseState = MouseState.NOTHING; + _currentControllerState = ControllerState.NOTHING; } /** @@ -131,7 +132,7 @@ public class PhysicalLayer { final PeekingIterator<ControllerEvent> eventIterator = _controllerWrapper.getEvents(); if (eventIterator.hasNext()) { - _currentControllerState = new ControllerState(); + _currentControllerState = new ControllerState(_currentControllerState); while (eventIterator.hasNext()) { final ControllerEvent event = eventIterator.next(); _currentControllerState.addEvent(event); @@ -199,7 +200,7 @@ public class PhysicalLayer { _stateQueue.add(InputState.LOST_FOCUS); _currentKeyboardState = KeyboardState.NOTHING; _currentMouseState = MouseState.NOTHING; - _currentControllerState = _controllerWrapper.getBlankState(); + _currentControllerState = ControllerState.NOTHING; } private void init() { @@ -209,7 +210,17 @@ public class PhysicalLayer { _mouseWrapper.init(); _focusWrapper.init(); _controllerWrapper.init(); + } + + public ControllerWrapper getControllerWrapper() { + return _controllerWrapper; + } + + public KeyboardWrapper getKeyboardWrapper() { + return _keyboardWrapper; + } - _currentControllerState = _controllerWrapper.getBlankState(); + public MouseWrapper getMouseWrapper() { + return _mouseWrapper; } -} +}
\ No newline at end of file diff --git a/trunk/ardor3d-core/src/main/java/com/ardor3d/input/logical/ControllerComponentActiveCondition.java b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/logical/ControllerComponentActiveCondition.java new file mode 100644 index 0000000..82b1771 --- /dev/null +++ b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/logical/ControllerComponentActiveCondition.java @@ -0,0 +1,55 @@ +/**
+ * 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.logical;
+
+import java.util.Map;
+
+import com.google.common.base.Predicate;
+
+public final class ControllerComponentActiveCondition implements Predicate<TwoInputStates> {
+
+ private final String controllerName;
+ private final String[] componentNames;
+
+ public ControllerComponentActiveCondition(final String controller, final String... components) {
+ controllerName = controller;
+ componentNames = components;
+ }
+
+ public boolean apply(final TwoInputStates states) {
+ final Map<String, Float> currentStates = states.getCurrent().getControllerState()
+ .getControllerComponentValues(controllerName);
+ final Map<String, Float> previousStates = states.getPrevious().getControllerState()
+ .getControllerComponentValues(controllerName);
+
+ if (currentStates == null) {
+ return false;
+ }
+
+ float prev, curr;
+ for (final String component : componentNames) {
+ curr = currentStates.get(component);
+ if (curr != 0) {
+ return true;
+ }
+
+ if (previousStates != null) {
+ prev = previousStates.get(component);
+
+ if (curr != prev) {
+ return true;
+ }
+ }
+ }
+
+ return false;
+ }
+}
\ No newline at end of file diff --git a/trunk/ardor3d-core/src/main/java/com/ardor3d/input/logical/DummyControllerWrapper.java b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/logical/DummyControllerWrapper.java index aba7f9d..eda4351 100644 --- a/trunk/ardor3d-core/src/main/java/com/ardor3d/input/logical/DummyControllerWrapper.java +++ b/trunk/ardor3d-core/src/main/java/com/ardor3d/input/logical/DummyControllerWrapper.java @@ -11,7 +11,7 @@ package com.ardor3d.input.logical;
import com.ardor3d.input.ControllerEvent;
-import com.ardor3d.input.ControllerState;
+import com.ardor3d.input.ControllerInfo;
import com.ardor3d.input.ControllerWrapper;
import com.google.common.collect.PeekingIterator;
@@ -34,10 +34,6 @@ public class DummyControllerWrapper implements ControllerWrapper { }
};
- public ControllerState getBlankState() {
- return new ControllerState();
- }
-
public PeekingIterator<ControllerEvent> getEvents() {
return empty;
}
@@ -46,4 +42,13 @@ public class DummyControllerWrapper implements ControllerWrapper { ; // ignore, does nothing
}
-}
+ @Override
+ public int getControllerCount() {
+ return 0;
+ }
+
+ @Override
+ public ControllerInfo getControllerInfo(final int controllerIndex) {
+ return null;
+ }
+}
\ No newline at end of file diff --git a/trunk/ardor3d-core/src/test/java/com/ardor3d/input/TestPhysicalLayer.java b/trunk/ardor3d-core/src/test/java/com/ardor3d/input/TestPhysicalLayer.java index 2b769ea..6502656 100644 --- a/trunk/ardor3d-core/src/test/java/com/ardor3d/input/TestPhysicalLayer.java +++ b/trunk/ardor3d-core/src/test/java/com/ardor3d/input/TestPhysicalLayer.java @@ -87,8 +87,6 @@ public class TestPhysicalLayer { controllerWrapper.init();
focusWrapper.init();
- expect(controllerWrapper.getBlankState()).andReturn(new ControllerState()).anyTimes();
-
expect(keyboardWrapper.getEvents()).andReturn(Iterators.peekingIterator(Adown.iterator()));
expect(keyboardWrapper.getEvents()).andReturn(Iterators.peekingIterator(noKeys.iterator()));
expect(keyboardWrapper.getEvents()).andReturn(Iterators.peekingIterator(noKeys.iterator()));
@@ -126,8 +124,6 @@ public class TestPhysicalLayer { controllerWrapper.init();
focusWrapper.init();
- expect(controllerWrapper.getBlankState()).andReturn(new ControllerState()).anyTimes();
-
expect(keyboardWrapper.getEvents()).andReturn(adau);
expect(keyboardWrapper.getEvents()).andReturn(adau);
expect(keyboardWrapper.getEvents()).andReturn(Iterators.peekingIterator(noKeys.iterator()));
@@ -170,8 +166,6 @@ public class TestPhysicalLayer { final PeekingIterator<KeyEvent> keyIterator = Iterators.peekingIterator(AdownBdown.iterator());
- expect(controllerWrapper.getBlankState()).andReturn(new ControllerState()).anyTimes();
-
expect(keyboardWrapper.getEvents()).andReturn(keyIterator).times(4);
expect(mouseWrapper.getEvents()).andReturn(Iterators.peekingIterator(noMice.iterator())).times(4);
expect(controllerWrapper.getEvents()).andReturn(Iterators.peekingIterator(nothing.iterator())).times(4);
@@ -209,8 +203,6 @@ public class TestPhysicalLayer { focusWrapper.init();
controllerWrapper.init();
- expect(controllerWrapper.getBlankState()).andReturn(new ControllerState()).anyTimes();
-
expect(keyboardWrapper.getEvents()).andReturn(iter).atLeastOnce();
expect(mouseWrapper.getEvents()).andReturn(Iterators.peekingIterator(noMice.iterator())).atLeastOnce();
expect(controllerWrapper.getEvents()).andReturn(Iterators.peekingIterator(nothing.iterator())).atLeastOnce();
@@ -230,8 +222,6 @@ public class TestPhysicalLayer { focusWrapper.init();
controllerWrapper.init();
- expect(controllerWrapper.getBlankState()).andReturn(new ControllerState()).anyTimes();
-
expect(keyboardWrapper.getEvents()).andReturn(Iterators.peekingIterator(noKeys.iterator())).atLeastOnce();
expect(mouseWrapper.getEvents()).andReturn(iter).atLeastOnce();
expect(controllerWrapper.getEvents()).andReturn(Iterators.peekingIterator(nothing.iterator())).atLeastOnce();
@@ -249,8 +239,6 @@ public class TestPhysicalLayer { focusWrapper.init();
controllerWrapper.init();
- expect(controllerWrapper.getBlankState()).andReturn(new ControllerState()).anyTimes();
-
expect(keyboardWrapper.getEvents()).andReturn(Iterators.peekingIterator(Adown.iterator()));
expect(keyboardWrapper.getEvents()).andReturn(Iterators.peekingIterator(noKeys.iterator()));
expect(mouseWrapper.getEvents()).andReturn(Iterators.peekingIterator(noMice.iterator())).times(2);
@@ -280,8 +268,6 @@ public class TestPhysicalLayer { focusWrapper.init();
controllerWrapper.init();
- expect(controllerWrapper.getBlankState()).andReturn(new ControllerState()).anyTimes();
-
expect(keyboardWrapper.getEvents()).andReturn(Iterators.peekingIterator(Adown.iterator()));
expect(keyboardWrapper.getEvents()).andReturn(Iterators.peekingIterator(noKeys.iterator()));
expect(keyboardWrapper.getEvents()).andReturn(Iterators.peekingIterator(noKeys.iterator()));
@@ -318,8 +304,6 @@ public class TestPhysicalLayer { focusWrapper.init();
controllerWrapper.init();
- expect(controllerWrapper.getBlankState()).andReturn(new ControllerState()).anyTimes();
-
expect(keyboardWrapper.getEvents()).andReturn(Iterators.peekingIterator(noKeys.iterator())).times(3);
expect(mouseWrapper.getEvents()).andReturn(Iterators.peekingIterator(buttonDown.iterator()));
expect(mouseWrapper.getEvents()).andReturn(Iterators.peekingIterator(noMice.iterator())).times(2);
@@ -386,4 +370,4 @@ public class TestPhysicalLayer { return m2;
}
}
-}
+}
\ No newline at end of file |