Skip to content

Commit 45a6519

Browse files
author
Joshua
committed
Add joy axis input map binding
1 parent 641b351 commit 45a6519

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

plugin/addons/godot_ai/handlers/input_handler.gd

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,8 +194,16 @@ func _create_event(event_type: String, params: Dictionary):
194194
var ev := InputEventJoypadButton.new()
195195
ev.button_index = int(params.get("button", 0))
196196
return ev
197+
"joy_axis":
198+
if not params.has("axis"):
199+
return ErrorCodes.make(ErrorCodes.MISSING_REQUIRED_PARAM,
200+
"event_type='joy_axis' requires axis (JoyAxis index, e.g. 0=left stick X, 1=left stick Y).")
201+
var ev := InputEventJoypadMotion.new()
202+
ev.axis = int(params.get("axis", 0))
203+
ev.axis_value = float(params.get("axis_value", 1.0))
204+
return ev
197205
return ErrorCodes.make(ErrorCodes.VALUE_OUT_OF_RANGE,
198-
"Unsupported event_type: '%s'. Use 'key', 'mouse_button', or 'joy_button'." % event_type)
206+
"Unsupported event_type: '%s'. Use 'key', 'mouse_button', 'joy_button', or 'joy_axis'." % event_type)
199207

200208

201209
func _serialize_event(event: InputEvent) -> Dictionary:

src/godot_ai/tools/input_map.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@
3030
• remove_action(action)
3131
Remove an action and all its event bindings.
3232
• bind_event(action, event_type, keycode="", ctrl=False, alt=False,
33-
shift=False, meta=False, button=None)
33+
shift=False, meta=False, button=None, axis=None,
34+
axis_value=1.0)
3435
Bind a key/mouse/gamepad event to an action. The action must
3536
already exist (call ``add_action`` first). ``event_type`` is
36-
``"key"`` | ``"mouse_button"`` | ``"joy_button"``.
37+
``"key"`` | ``"mouse_button"`` | ``"joy_button"`` |
38+
``"joy_axis"``.
3739
- ``key``: ``keycode`` is a Godot keycode *name string* like
3840
``"A"``, ``"Space"``, ``"Enter"``, ``"Escape"``, ``"F1"``,
3941
``"Left"`` — not an integer and not ``KEY_*``. Modifier
@@ -42,6 +44,8 @@
4244
3=middle, 4=wheel up, 5=wheel down.
4345
- ``joy_button``: ``button`` is the ``JoyButton`` index
4446
(e.g. 0=A/Cross, 1=B/Circle).
47+
- ``joy_axis``: ``axis`` is the ``JoyAxis`` index and
48+
``axis_value`` is the direction/value, usually -1.0 or 1.0.
4549
"""
4650

4751

test_project/tests/test_input.gd

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,16 @@ func test_bind_event_mouse_button_zero_button() -> void:
209209
_handler.remove_action({"action": TEST_ACTION})
210210

211211

212+
func test_bind_event_joy_axis_missing_axis() -> void:
213+
_handler.add_action({"action": TEST_ACTION})
214+
var result := _handler.bind_event({
215+
"action": TEST_ACTION,
216+
"event_type": "joy_axis",
217+
})
218+
assert_is_error(result, ErrorCodes.MISSING_REQUIRED_PARAM)
219+
_handler.remove_action({"action": TEST_ACTION})
220+
221+
212222
func test_bind_event_unknown_action_message_suggests_add_action() -> void:
213223
## The error string should point the caller at the fix so they don't loop.
214224
var result := _handler.bind_event({
@@ -232,3 +242,20 @@ func test_bind_key_event() -> void:
232242
assert_has_key(result.data, "event")
233243
assert_eq(result.data.event.type, "key")
234244
_handler.remove_action({"action": TEST_ACTION})
245+
246+
247+
func test_bind_joy_axis_event() -> void:
248+
_handler.add_action({"action": TEST_ACTION})
249+
var result := _handler.bind_event({
250+
"action": TEST_ACTION,
251+
"event_type": "joy_axis",
252+
"axis": JOY_AXIS_LEFT_X,
253+
"axis_value": -1.0,
254+
})
255+
assert_has_key(result, "data")
256+
assert_eq(result.data.action, TEST_ACTION)
257+
assert_has_key(result.data, "event")
258+
assert_eq(result.data.event.type, "joy_axis")
259+
assert_eq(result.data.event.axis, JOY_AXIS_LEFT_X)
260+
assert_eq(result.data.event.axis_value, -1.0)
261+
_handler.remove_action({"action": TEST_ACTION})

0 commit comments

Comments
 (0)