Skip to content

Instantly share code, notes, and snippets.

@WhiteMagic
Created December 30, 2023 14:44
Show Gist options
  • Save WhiteMagic/2ab62717408bbfeda32b48f3c726f8f3 to your computer and use it in GitHub Desktop.
Save WhiteMagic/2ab62717408bbfeda32b48f3c726f8f3 to your computer and use it in GitHub Desktop.
Merges a stick and throttle axis, using the throttle axis as the neutral point for the stick axis.
import gremlin
from gremlin.user_plugin import *
# UI variable definitions
mode = ModeVariable("Mode", "Mode in which to use these settings")
input_joystick = PhysicalInputVariable(
"Joystick axis",
"Axis corresponding to the joystick axis to use",
[gremlin.common.InputType.JoystickAxis]
)
invert_joystick = BoolVariable(
"Invert joystick axis",
"Whether or not to invert the joystick axis",
False
)
input_throttle = PhysicalInputVariable(
"Throttle axis",
"Axis corresponding to the throttle slider to use",
[gremlin.common.InputType.JoystickAxis]
)
invert_throttle = BoolVariable(
"Invert throttle axis",
"Whether or not to invert the throttle axis",
False
)
output = VirtualInputVariable(
"Output axis",
"Axis to which the combined output is sent",
[gremlin.common.InputType.JoystickAxis]
)
# Device decorators
dec_joystick = input_joystick.create_decorator(mode.value)
dec_throttle = input_throttle.create_decorator(mode.value)
# Global state variables
g_throttle = 0.0
g_joystick = 0.0
def update_vjoy(vjoy):
"""Computes the merged output value and sets it.
Args:
vjoy: vJoy device instance to use
"""
device = vjoy[output.value["device_id"]]
device.axis(output.value["input_id"]).value = \
max(-1.0, min(1.0, g_throttle + g_joystick))
# Callback handlers
@dec_joystick.axis(input_joystick.input_id)
def axis_joystick(event, vjoy):
global g_joystick
g_joystick = event.value if not invert_joystick.value else -event.value
update_vjoy(vjoy)
@dec_throttle.axis(input_throttle.input_id)
def axis_throttle(event, vjoy):
global g_throttle
g_throttle = event.value if not invert_throttle.value else -event.value
g_throttle = (g_throttle + 1.0) / 2.0
update_vjoy(vjoy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment