Skip to content

Commit

Permalink
fix(ManagedGamepad): Fix denormalize axis for axes that start at 0
Browse files Browse the repository at this point in the history
  • Loading branch information
pastaq authored and Derek J. Clark committed Oct 19, 2023
1 parent 012b363 commit dd7ba11
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 38 deletions.
77 changes: 39 additions & 38 deletions core/systems/input/managed_gamepad.gd
Original file line number Diff line number Diff line change
Expand Up @@ -787,24 +787,24 @@ func _is_axis_pressed(event: InputDeviceEvent, is_positive: bool) -> bool:
# reflecting how far the axis has been pushed from the center
func _normalize_axis(event: InputDeviceEvent) -> float:
match event.get_code():
event.ABS_Y:
var event_value := event.value - abs_y_mid
event.ABS_X:
var event_value := event.value - abs_x_mid
if event_value >= 0:
var maximum := abs_y_max - abs_y_mid
var maximum := abs_x_max - abs_x_mid
var value := event_value / float(maximum)
return value
if event_value < 0:
var minimum := abs_y_min - abs_y_mid
var minimum := abs_x_min - abs_x_mid
var value := event_value / float(minimum)
return -value
event.ABS_X:
var event_value := event.value - abs_x_mid
event.ABS_Y:
var event_value := event.value - abs_y_mid
if event_value >= 0:
var maximum := abs_x_max - abs_x_mid
var maximum := abs_y_max - abs_y_mid
var value := event_value / float(maximum)
return value
if event_value < 0:
var minimum := abs_x_min - abs_x_mid
var minimum := abs_y_min - abs_y_mid
var value := event_value / float(minimum)
return -value
event.ABS_Z:
Expand All @@ -816,24 +816,24 @@ func _normalize_axis(event: InputDeviceEvent) -> float:
var minimum := abs_z_min
var value := event.value / float(minimum)
return -value
event.ABS_RY:
var event_value := event.value - abs_ry_mid
event.ABS_RX:
var event_value := event.value - abs_rx_mid
if event_value >= 0:
var maximum := abs_ry_max - abs_ry_mid
var maximum := abs_rx_max - abs_rx_mid
var value := event_value / float(maximum)
return value
if event_value < 0:
var minimum := abs_ry_min - abs_ry_mid
var minimum := abs_rx_min - abs_rx_mid
var value := event_value / float(minimum)
return -value
event.ABS_RX:
var event_value := event.value - abs_rx_mid
event.ABS_RY:
var event_value := event.value - abs_ry_mid
if event_value >= 0:
var maximum := abs_rx_max - abs_rx_mid
var maximum := abs_ry_max - abs_ry_mid
var value := event_value / float(maximum)
return value
if event_value < 0:
var minimum := abs_rx_min - abs_rx_mid
var minimum := abs_ry_min - abs_ry_mid
var value := event_value / float(minimum)
return -value
event.ABS_RZ:
Expand All @@ -853,25 +853,26 @@ func _normalize_axis(event: InputDeviceEvent) -> float:
# and maximum values for the given axis. This does the opposite of _normalize_axis().
# E.g. _denormalize_axis(event.get_code(), 0.75)
func _denormalize_axis(axis_code: int, normalized_value: float) -> float:
var normalized_value_abs: float = abs(normalized_value)
match axis_code:
InputDeviceEvent.ABS_X:
if normalized_value >= 0:
var maximum := abs_x_max - abs_x_mid
var value := normalized_value * float(maximum) + abs_x_mid
return value
if normalized_value < 0:
var minimum := abs_x_min - abs_x_mid
var value: float = (normalized_value_abs * float(minimum)) + abs_x_mid
return value
InputDeviceEvent.ABS_Y:
if normalized_value >= 0:
var maximum := abs_y_max - abs_y_mid
var value := (normalized_value * float(maximum)) + abs_y_mid
return value
if normalized_value < 0:
var minimum := abs_y_min - abs_y_mid
var value := normalized_value * float(minimum) + abs_y_mid
return -value
InputDeviceEvent.ABS_X:
if normalized_value >= 0:
var maximum := abs_x_max - abs_x_mid
var value := normalized_value * float(maximum) + abs_x_mid
var value: float = (normalized_value_abs * float(minimum)) + abs_y_mid
return value
if normalized_value < 0:
var minimum := abs_x_min - abs_x_mid
var value := normalized_value * float(minimum) + abs_x_mid
return -value
InputDeviceEvent.ABS_Z:
if normalized_value >= 0:
var maximum := abs_z_max
Expand All @@ -881,24 +882,24 @@ func _denormalize_axis(axis_code: int, normalized_value: float) -> float:
var minimum := abs_z_min
var value := normalized_value * float(minimum)
return -value
InputDeviceEvent.ABS_RX:
if normalized_value >= 0:
var maximum := abs_rx_max - abs_rx_mid
var value := normalized_value * float(maximum) + abs_rx_mid
return value
if normalized_value < 0:
var minimum := abs_rx_min - abs_rx_mid
var value := (normalized_value_abs * float(minimum)) + abs_rx_mid
return value
InputDeviceEvent.ABS_RY:
if normalized_value >= 0:
var maximum := abs_ry_max - abs_ry_mid
var value := normalized_value * float(maximum) + abs_ry_mid
return value
if normalized_value < abs_ry_mid:
var minimum := abs_ry_min - - abs_ry_mid
var value := normalized_value * float(minimum) + abs_ry_mid
return -value
InputDeviceEvent.ABS_RX:
if normalized_value >= abs_rx_mid:
var maximum := abs_rx_max - abs_rx_mid
var value := normalized_value * float(maximum) + abs_rx_mid
if normalized_value < 0:
var minimum := abs_ry_min - abs_ry_mid
var value := (normalized_value_abs * float(minimum)) + abs_ry_mid
return value
if normalized_value < abs_rx_mid:
var minimum := abs_rx_min - abs_rx_mid
var value := normalized_value * float(minimum) + abs_rx_mid
return -value
InputDeviceEvent.ABS_RZ:
if normalized_value >= 0:
var maximum := abs_rz_max
Expand Down
2 changes: 2 additions & 0 deletions core/systems/input/managed_gamepad_test.gd
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func test_denormalize_axis(params=use_parameters(normalize_axis_params)) -> void

# Try to denormalize the input events
for event in events:
gut.p("Testing params: " + str(event.code) + " " + str(expected))
var denormalized := gamepad._denormalize_axis(event.code, expected)
assert_eq(denormalized, event.value)

Expand All @@ -102,6 +103,7 @@ func create_events(type: int, codes: Array[int], value: int) -> Array[InputDevic

func set_gamepad_min_mid_max(gamepad: ManagedGamepad, axis_min: int, axis_max: int) -> void:
var axis_mid := (axis_max + axis_min)/2
gut.p("Mid point for axis: " + str(axis_mid))
gamepad.abs_x_min = axis_min
gamepad.abs_x_max = axis_max
gamepad.abs_x_mid = axis_mid
Expand Down

0 comments on commit dd7ba11

Please sign in to comment.