Improve documentation and make Scroll Speed work intuitively.

This commit is contained in:
Anna Rose Wiggins 2025-01-19 13:06:46 -05:00
parent c4244d325d
commit 754b4107b3
2 changed files with 37 additions and 13 deletions

View file

@ -2,6 +2,8 @@ from datetime import datetime, timedelta
import gremlin
from gremlin.user_plugin import *
# User-configurable variables; these appear in the Joystick Gremlin UI
axis = PhysicalInputVariable(
"Axis",
"The axis that will trigger scrolling.",
@ -13,7 +15,7 @@ mode = ModeVariable(
"The mode in which the axis will be mapped.",
)
response_coefficient = FloatVariable(
scroll_speed = FloatVariable(
"Scroll Speed",
"Adjusts the rate at which the target button is pressed, relative to the axis strength.",
1.0,
@ -32,16 +34,25 @@ invert = BoolVariable(
False,
)
# Constants
AXIS_SCALING_FACTOR = 250 # Value determined through testing.
# Debugging
ticks = 0
axis_decorator = axis.create_decorator(mode.value)
# Stateful data
last_timestamp = datetime.now()
scroll_scaling = scroll_speed.value if scroll_speed.value != 0.0 else 1.0
@axis_decorator.axis(axis.input_id)
# TODO: use this code instead when periodic callback is fixed
# @gremlin.input_devices.periodic(1)
@axis_decorator.axis(axis.input_id)
def handle_axis(event):
global last_timestamp
global damping
global ticks # debug
# TODO: use this code instead when periodic callback is fixed
# axis_value = joy[axis.device_guid].axis(axis.input_id).value
axis_value = event.value
@ -50,8 +61,13 @@ def handle_axis(event):
delta = (datetime.now() - last_timestamp) / timedelta(milliseconds=1)
if delta >= (1 - abs(axis_value)) * 250 * response_coefficient.value:
# Scroll speed is *inversely proportional* to the right-hand side of this comparison
if delta >= ((1 - abs(axis_value)) * AXIS_SCALING_FACTOR) / scroll_scaling:
direction = 1 if axis_value > 0 else -1
if invert.value: direction = direction * -1
gremlin.sendinput.mouse_wheel(direction)
last_timestamp = datetime.now()
last_timestamp = datetime.now()
# debug
ticks += 1
gremlin.util.log(ticks)