-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add gamepad input processing (left/right sticks so far)
- Loading branch information
Showing
6 changed files
with
285 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/input/GamepadAxisEvent.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/* | ||
* Copyright 2024, Emanuel Rabina (http://www.ultraq.net.nz/) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package nz.net.ultraq.redhorizon.engine.input | ||
|
||
import groovy.transform.TupleConstructor | ||
|
||
/** | ||
* Event emitted for gamepad axis controls. | ||
* | ||
* @author Emanuel Rabina | ||
*/ | ||
@TupleConstructor(defaults = false) | ||
class GamepadAxisEvent extends InputEvent { | ||
|
||
final int type | ||
final float value | ||
} |
65 changes: 65 additions & 0 deletions
65
redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/input/GamepadControl.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2024, Emanuel Rabina (http://www.ultraq.net.nz/) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package nz.net.ultraq.redhorizon.engine.input | ||
|
||
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_LEFT_X | ||
import static org.lwjgl.glfw.GLFW.GLFW_GAMEPAD_AXIS_LEFT_Y | ||
|
||
import groovy.transform.stc.ClosureParams | ||
import groovy.transform.stc.SimpleType | ||
|
||
/** | ||
* Gamepad-specific control class. | ||
* | ||
* @author Emanuel Rabina | ||
*/ | ||
class GamepadControl extends Control<GamepadAxisEvent> { | ||
|
||
private final int type | ||
private final Closure handler | ||
|
||
/** | ||
* Create a new gamepad control to act on the given gamepad axis events. | ||
*/ | ||
GamepadControl(int type, String name, | ||
@ClosureParams(value = SimpleType, options = 'float') Closure handler) { | ||
|
||
super(GamepadAxisEvent, name, determineBindingName(type)) | ||
this.type = type | ||
this.handler = handler | ||
} | ||
|
||
/** | ||
* Return a string representation of the axis binding. | ||
*/ | ||
private static String determineBindingName(int type) { | ||
|
||
return switch (type) { | ||
case GLFW_GAMEPAD_AXIS_LEFT_X -> 'Left stick X axis' | ||
case GLFW_GAMEPAD_AXIS_LEFT_Y -> 'Left stick Y axis' | ||
default -> '(unknown)' | ||
} | ||
} | ||
|
||
@Override | ||
void handleEvent(GamepadAxisEvent event) { | ||
|
||
if (event.type == type) { | ||
handler(event.value) | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
redhorizon-engine/source/nz/net/ultraq/redhorizon/engine/input/GamepadStateProcessor.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/* | ||
* Copyright 2024, Emanuel Rabina (http://www.ultraq.net.nz/) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package nz.net.ultraq.redhorizon.engine.input | ||
|
||
import nz.net.ultraq.redhorizon.engine.graphics.GraphicsSystem | ||
|
||
import org.lwjgl.glfw.GLFWGamepadState | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import static org.lwjgl.glfw.GLFW.* | ||
|
||
import groovy.transform.TupleConstructor | ||
import java.nio.FloatBuffer | ||
|
||
/** | ||
* A class for managing gamepad inputs and emitting them as events so that it | ||
* all acts the same for those on the other end of the {@link InputEventStream}. | ||
* <p> | ||
* GLFW currently doesn't have a callback system in place for joysticks, and so | ||
* you have to do DIY polling and handling. See https://github.com/glfw/glfw/issues/601, | ||
* which is unlikely to be solved any time soon. | ||
* <p> | ||
* Joystick/Gamepad processing is currently restricted to the same thread as the | ||
* GLFW context, so this class is only used from the {@link GraphicsSystem}. | ||
* | ||
* @author Emanuel Rabina | ||
*/ | ||
@TupleConstructor(defaults = false) | ||
class GamepadStateProcessor { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(GamepadStateProcessor) | ||
|
||
final InputEventStream inputEventStream | ||
|
||
@Lazy | ||
private GLFWGamepadState gamepadState = { GLFWGamepadState.create() }() | ||
|
||
/** | ||
* Check for any changes to the joystick/gamepad state and emit events for | ||
* them. Called after {@code glfwPollEvents}. | ||
*/ | ||
void process() { | ||
|
||
if (glfwJoystickIsGamepad(GLFW_JOYSTICK_1)) { | ||
glfwGetGamepadState(GLFW_JOYSTICK_1, gamepadState) | ||
|
||
var axes = gamepadState.axes() | ||
processAxis(axes, GLFW_GAMEPAD_AXIS_LEFT_X, 'Gamepad left stick X: {}') | ||
processAxis(axes, GLFW_GAMEPAD_AXIS_LEFT_Y, 'Gamepad left stick Y: {}') | ||
processAxis(axes, GLFW_GAMEPAD_AXIS_RIGHT_X, 'Gamepad right stick X: {}') | ||
processAxis(axes, GLFW_GAMEPAD_AXIS_RIGHT_Y, 'Gamepad right stick Y: {}') | ||
} | ||
} | ||
|
||
/** | ||
* Process a single axis. | ||
*/ | ||
private void processAxis(FloatBuffer axes, int type, String logMessage) { | ||
|
||
var value = axes.get(type) | ||
logger.debug(logMessage, sprintf('%.2f', value)) | ||
inputEventStream.trigger(new GamepadAxisEvent(type, value <= -0.2f || 0.2f <= value ? value : 0f)) | ||
} | ||
} |
Oops, something went wrong.