use of org.terasology.input.device.ControllerAction in project Terasology by MovingBlocks.
the class InputSystem method processControllerInput.
private void processControllerInput(float delta) {
if (!isCapturingMouse()) {
return;
}
for (ControllerAction action : controllers.getInputQueue()) {
// TODO: send event to entity system
boolean consumed = false;
Input input = action.getInput();
if (input.getType() == InputType.CONTROLLER_BUTTON) {
processControllerButtonInput(delta, action, consumed, input);
} else if (input.getType() == InputType.CONTROLLER_AXIS) {
processControllerAxisInput(action, input);
}
}
}
use of org.terasology.input.device.ControllerAction in project Terasology by MovingBlocks.
the class JInputControllerDevice method getInputQueue.
@Override
public Queue<ControllerAction> getInputQueue() {
Queue<ControllerAction> result = new ArrayDeque<>();
Event event = new Event();
Iterator<Controller> it = controllers.iterator();
while (it.hasNext()) {
Controller c = it.next();
if (c.poll()) {
EventQueue queue = c.getEventQueue();
while (queue.getNextEvent(event)) {
ControllerAction action = convertEvent(c, event);
if (action != null) {
result.add(action);
}
}
} else {
removeController(c);
}
}
return result;
}
use of org.terasology.input.device.ControllerAction in project Terasology by MovingBlocks.
the class JInputControllerDevice method convertEvent.
private ControllerAction convertEvent(Controller c, Event event) {
Component comp = event.getComponent();
Identifier id = comp.getIdentifier();
float axisValue = comp.getPollData();
Input input;
ButtonState state = ButtonState.UP;
if (id instanceof Identifier.Button) {
state = event.getValue() != 0 ? ButtonState.DOWN : ButtonState.UP;
Integer buttonId = buttonMap.get(id);
if (buttonId == null) {
// button not registered
return null;
}
input = InputType.CONTROLLER_BUTTON.getInput(buttonId);
} else if (id instanceof Identifier.Axis) {
ControllerInfo info = config.getController(c.getName());
if (id.equals(Identifier.Axis.X)) {
if (Math.abs(axisValue) < info.getMovementDeadZone()) {
axisValue = 0;
}
input = InputType.CONTROLLER_AXIS.getInput(ControllerId.X_AXIS);
} else if (id.equals(Identifier.Axis.Y)) {
if (Math.abs(axisValue) < info.getMovementDeadZone()) {
axisValue = 0;
}
input = InputType.CONTROLLER_AXIS.getInput(ControllerId.Y_AXIS);
} else if (id.equals(Identifier.Axis.Z)) {
if (Math.abs(axisValue) < info.getMovementDeadZone()) {
axisValue = 0;
}
input = InputType.CONTROLLER_AXIS.getInput(ControllerId.Z_AXIS);
} else if (id.equals(Identifier.Axis.RX)) {
if (Math.abs(axisValue) < info.getRotationDeadZone()) {
axisValue = 0;
}
input = InputType.CONTROLLER_AXIS.getInput(ControllerId.RX_AXIS);
} else if (id.equals(Identifier.Axis.RY)) {
if (Math.abs(axisValue) < info.getRotationDeadZone()) {
axisValue = 0;
}
input = InputType.CONTROLLER_AXIS.getInput(ControllerId.RY_AXIS);
} else if (id.equals(Identifier.Axis.POV)) {
// the poll data float value is actually an ID in this case
boolean isX = (axisValue == Component.POV.LEFT) || (axisValue == Component.POV.RIGHT);
boolean isY = (axisValue == Component.POV.UP) || (axisValue == Component.POV.DOWN);
if (isX || isY) {
input = InputType.CONTROLLER_AXIS.getInput(isX ? ControllerId.POVX_AXIS : ControllerId.POVY_AXIS);
if ((axisValue == Component.POV.UP) || (axisValue == Component.POV.LEFT)) {
axisValue = -1;
}
if ((axisValue == Component.POV.DOWN) || (axisValue == Component.POV.RIGHT)) {
axisValue = 1;
}
} else {
// TODO: handle 8-button POVs
return null;
}
} else {
// unrecognized axis
return null;
}
} else {
// unrecognized id (e.g. Identifier.Key)
return null;
}
return new ControllerAction(input, c.getName(), state, axisValue);
}
use of org.terasology.input.device.ControllerAction in project Terasology by MovingBlocks.
the class InputSystem method processControllerInput.
/**
* Processes the current input state of any connected controllers, and updates bind buttons.
* <p>
* Controller button and axis events are both handled in {@link #processControllerButtonInput(float,
* ControllerAction, boolean, Input)} and {@link #processControllerAxisInput(ControllerAction, Input)} accordingly.
*
* @param delta The length of the current frame.
*/
private void processControllerInput(float delta) {
if (!isCapturingMouse()) {
return;
}
for (ControllerAction action : controllers.getInputQueue()) {
// TODO: send event to entity system
boolean consumed = false;
Input input = action.getInput();
if (input.getType() == InputType.CONTROLLER_BUTTON) {
processControllerButtonInput(delta, action, consumed, input);
} else if (input.getType() == InputType.CONTROLLER_AXIS) {
processControllerAxisInput(action, input);
}
}
}
use of org.terasology.input.device.ControllerAction in project Terasology by MovingBlocks.
the class LwjglControllerDevice method getInputQueue.
@Override
public Queue<ControllerAction> getInputQueue() {
Queue<ControllerAction> controllerActions = Lists.newLinkedList();
for (int jid : gamepadIds.toArray()) {
if (GLFW.glfwJoystickPresent(jid)) {
// callback remove it later, if you are here
continue;
}
String controllerName = GLFW.glfwGetGamepadName(jid);
GLFWGamepadState gamepadState = GLFWGamepadState.create();
if (GLFW.glfwGetGamepadState(jid, gamepadState)) {
FloatBuffer axes = gamepadState.axes();
int axesIndex = 0;
while (axes.hasRemaining()) {
int teraId = axisMapping.get(axesIndex);
Input input = InputType.CONTROLLER_AXIS.getInput(teraId);
float axisValue = axes.get();
if (Math.abs(axisValue) < getDeadzoneForInput(controllerName, input)) {
axisValue = 0;
}
controllerActions.add(new ControllerAction(input, controllerName, ButtonState.UP, axisValue));
axesIndex++;
}
ByteBuffer buttonsStates = gamepadState.buttons();
int buttonIndex = 0;
while (buttonsStates.hasRemaining()) {
int teraButtonId = buttonMap.get(buttonIndex);
if (teraButtonId == -1) {
if (GLFW.GLFW_GAMEPAD_BUTTON_DPAD_UP <= buttonIndex && buttonIndex <= GLFW.GLFW_GAMEPAD_BUTTON_DPAD_LEFT) {
boolean isX = (buttonIndex == GLFW.GLFW_GAMEPAD_BUTTON_DPAD_LEFT) || (buttonIndex == GLFW.GLFW_GAMEPAD_BUTTON_DPAD_RIGHT);
Input input = InputType.CONTROLLER_AXIS.getInput(isX ? ControllerId.POVX_AXIS : ControllerId.POVY_AXIS);
float axisValue;
if (buttonIndex == GLFW.GLFW_GAMEPAD_BUTTON_DPAD_RIGHT || buttonIndex == GLFW.GLFW_GAMEPAD_BUTTON_DPAD_DOWN) {
axisValue = -1;
} else {
axisValue = 1;
}
controllerActions.add(new ControllerAction(input, controllerName, ButtonState.UP, axisValue));
}
logger.error("Received unknown/unhandled buttonId for GLFW: {}", buttonIndex);
} else {
Input input = InputType.CONTROLLER_BUTTON.getInput(teraButtonId);
short btnState = buttonsStates.get();
ButtonState buttonState = btnState == GLFW.GLFW_RELEASE ? ButtonState.UP : ButtonState.DOWN;
controllerActions.add(new ControllerAction(input, controllerName, buttonState, 0.0F));
}
buttonIndex++;
}
} else {
logger.error("Cannot get states for {}", GLFW.glfwGetGamepadName(jid));
}
}
return controllerActions;
}
Aggregations