Skip to content

Instantly share code, notes, and snippets.

@WhiteMagic
Created May 4, 2020 11:54
Show Gist options
  • Save WhiteMagic/f6dab456dc0cc3b7496b768ab735f4ba to your computer and use it in GitHub Desktop.
Save WhiteMagic/f6dab456dc0cc3b7496b768ab735f4ba to your computer and use it in GitHub Desktop.
import threading
import gremlin
from gremlin.user_plugin import *
mode = ModeVariable("Mode", "The mode in which to use this mapping")
joy_axis = PhysicalInputVariable(
"Axis to refresh",
"The physical input axis being refreshed.",
[gremlin.common.InputType.JoystickAxis]
)
trigger_button = PhysicalInputVariable(
"Trigger button",
"The button which will trigger the refresh",
[gremlin.common.InputType.JoystickButton]
)
delay = IntegerVariable(
"Delay (ms)",
"Time elapsed before the refresh happens",
100,
1,
5000
)
on_press = BoolVariable(
"Update on press",
"Updates the axis state when the button is pressed",
True
)
on_release = BoolVariable(
"Update on release",
"Updates the axis state when the button is released",
True
)
# Decorator for the button triggering the update
d_button = trigger_button.create_decorator(mode.value)
# Event structure being emitted to force an update of linked actions
g_joystick_event = gremlin.event_handler.Event(
gremlin.common.InputType.JoystickAxis,
joy_axis.input_id,
joy_axis.device_guid
)
# EventListener instance used to emit the joystick event
g_event_listener = gremlin.event_handler.EventListener()
# Timer object ensuring the update event is sent with a delay
g_timer = None
def emit_axis():
"""Emits the joystick event populated with data in the button callback."""
g_event_listener.joystick_event.emit(g_joystick_event)
@d_button.button(trigger_button.input_id)
def trigger_button_cb(event, joy):
"""Updates the value of the joystick axis event with the current axis' state."""
global g_joystick_event
global g_timer
if (event.is_pressed and on_press.value) or (not event.is_pressed and on_release.value):
g_joystick_event.value = joy[joy_axis.device_guid].axis(joy_axis.input_id).value
g_timer = threading.Timer(delay.value / 1000.0, emit_axis)
g_timer.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment