joystick_gremlin_axis_to_sc.../axis_to_scrollwheel.py

73 lines
1.9 KiB
Python
Raw Normal View History

2025-01-19 17:39:31 +00:00
from datetime import datetime, timedelta
import gremlin
from gremlin.user_plugin import *
# User-configurable variables; these appear in the Joystick Gremlin UI
2025-01-19 17:39:31 +00:00
axis = PhysicalInputVariable(
"Axis",
"The axis that will trigger scrolling.",
[gremlin.common.InputType.JoystickAxis],
)
mode = ModeVariable(
"Mode",
"The mode in which the axis will be mapped.",
)
scroll_speed = FloatVariable(
2025-01-19 17:39:31 +00:00
"Scroll Speed",
"Adjusts the rate at which the target button is pressed, relative to the axis strength.",
1.0,
)
deadzone = FloatVariable(
"Deadzone",
"Applies a deadzone to both sides of the axis.",
0.0,
)
# NB: Not a real boolean; invert.value returns 0 or 2
invert = BoolVariable(
"Invert",
"Reverse the direction that the axis scrolls.",
False,
)
# Constants
AXIS_SCALING_FACTOR = 250 # Value determined through testing.
2025-01-19 17:39:31 +00:00
# Debugging
ticks = 0
axis_decorator = axis.create_decorator(mode.value)
2025-01-19 17:39:31 +00:00
last_timestamp = datetime.now()
scroll_scaling = scroll_speed.value if scroll_speed.value != 0.0 else 1.0
2025-01-19 17:39:31 +00:00
# TODO: use this code instead when periodic callback is fixed
2025-01-19 17:39:31 +00:00
# @gremlin.input_devices.periodic(1)
@axis_decorator.axis(axis.input_id)
2025-01-19 17:39:31 +00:00
def handle_axis(event):
global last_timestamp
global damping
global ticks # debug
2025-01-19 17:39:31 +00:00
# TODO: use this code instead when periodic callback is fixed
2025-01-19 17:39:31 +00:00
# axis_value = joy[axis.device_guid].axis(axis.input_id).value
axis_value = event.value
if (abs(axis_value) < deadzone.value):
return
delta = (datetime.now() - last_timestamp) / timedelta(milliseconds=1)
# Scroll speed is *inversely proportional* to the right-hand side of this comparison
if delta >= ((1 - abs(axis_value)) * AXIS_SCALING_FACTOR) / scroll_scaling:
2025-01-19 17:39:31 +00:00
direction = 1 if axis_value > 0 else -1
if invert.value: direction = direction * -1
gremlin.sendinput.mouse_wheel(direction)
last_timestamp = datetime.now()
# debug
ticks += 1
gremlin.util.log(ticks)