Skip to content

Instantly share code, notes, and snippets.

@WhiteMagic
Created July 3, 2020 00:00
Show Gist options
  • Save WhiteMagic/3c7b8671f4e33ffe5ddc88c07f3a8271 to your computer and use it in GitHub Desktop.
Save WhiteMagic/3c7b8671f4e33ffe5ddc88c07f3a8271 to your computer and use it in GitHub Desktop.
center_virtual_button.py
import math
import gremlin
from gremlin.user_plugin import *
mode = ModeVariable("Mode", "Mode in which to use these settings")
pa_x = PhysicalInputVariable(
"Physical X Axis",
"Physical X axis input",
[gremlin.common.InputType.JoystickAxis]
)
pa_y = PhysicalInputVariable(
"Physical Y Axis",
"Physical Y axis input",
[gremlin.common.InputType.JoystickAxis]
)
#js_out = VirtualInputVariable(
# "Joystick button",
# "Joystick button pressed when the virtual button is active",
# [gremlin.common.InputType.JoystickButton]
#)
kb_out = StringVariable(
"Keyboard key",
"Key pressed when the virtual button is active",
"1"
)
radius = FloatVariable(
"Radius around center",
"Radius around the center in which the virtual button is activated",
0.0,
0.0,
1.0
)
# Decorators for the two physical axes
dec_x = pa_x.create_decorator(mode.value)
dec_y = pa_y.create_decorator(mode.value)
kb_press = gremlin.macro.Macro()
kb_press.press(kb_out.value)
kb_release = gremlin.macro.Macro()
kb_release.press(kb_out.value)
# Storage for the last known axis values
x_value = 0.0
y_value = 0.0
virtual_button_pressed = False
# Initialize object instances
macro_manager = gremlin.macro.MacroManager()
def update_vb(vjoy):
global virtual_button_pressed
alpha = math.atan2(abs(y_value), abs(x_value))
border_x = math.cos(alpha) * radius.value
border_y = math.sin(alpha) * radius.value
inside_vb_area = abs(x_value) <= border_x and abs(y_value) <= border_y
if virtual_button_pressed and not inside_vb_area:
virtual_button_pressed = False
#vjoy[js_out.value["device_id"]].button(js_out.value["input_id"]).is_pressed = False
macro_manager.queue_macro(kb_release)
if not virtual_button_pressed and inside_vb_area:
virtual_button_pressed = True
#vjoy[js_out.value["device_id"]].button(js_out.value["input_id"]).is_pressed = True
macro_manager.queue_macro(kb_press)
@dec_x.axis(pa_x.input_id)
def axis1(event, vjoy):
global x_value
x_value = event.value
update_vb(vjoy)
@dec_y.axis(pa_y.input_id)
def axis2(event, vjoy):
global y_value
y_value = event.value
update_vb(vjoy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment