Last active
April 18, 2021 23:47
-
-
Save WhiteMagic/63b8cd650f91035e2caeb9dc226d0c3f to your computer and use it in GitHub Desktop.
Combines two or three axes into a single one
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import math | |
import gremlin | |
from gremlin.user_plugin import * | |
mode = ModeVariable("Mode", "Mode in which to use these settings") | |
input_1 = PhysicalInputVariable( | |
"1st axis", | |
"First physical input axis", | |
[gremlin.common.InputType.JoystickAxis] | |
) | |
input_2 = PhysicalInputVariable( | |
"2nd axis", | |
"Second physical input axis", | |
[gremlin.common.InputType.JoystickAxis] | |
) | |
input_3 = PhysicalInputVariable( | |
"3rd axis", | |
"Third physical input axis", | |
[gremlin.common.InputType.JoystickAxis] | |
) | |
output = VirtualInputVariable( | |
"Output axis", | |
"Virtual combined output axis", | |
[gremlin.common.InputType.JoystickAxis] | |
) | |
use_norm = BoolVariable( | |
"Use vector norm", | |
"Uses the norm of the vector if set, otherwise maximum element", | |
False | |
) | |
use_third = BoolVariable( | |
"Use third axis", | |
"Whether or not to use the third axis", | |
False | |
) | |
invert_output = BoolVariable( | |
"Invert output axis", | |
"Whether or not to invert the output axis", | |
False | |
) | |
# Decorators | |
dec_1 = input_1.create_decorator(mode.value) | |
dec_2 = input_2.create_decorator(mode.value) | |
dec_3 = input_3.create_decorator(mode.value) | |
class Vector3: | |
"""Simple 3D vector class.""" | |
def __init__(self, x, y, z): | |
"""Creates a new instance. | |
Parameters | |
========== | |
x : float | |
X component of the vector | |
y : float | |
Y component of the vector | |
z : float | |
Z component of the vector | |
""" | |
self.x = x | |
self.y = y | |
self.z = z | |
def max_abs_element(self): | |
"""Returns the element with the largest absolute value. | |
Returns | |
======= | |
float | |
Value of the element with the largest absolute value | |
""" | |
return max([abs(self.x), abs(self.y), abs(self.z)]) | |
def norm(self): | |
"""Returns the norm of the vector. | |
Returns | |
======= | |
float | |
Norm of the vector | |
""" | |
return math.sqrt(self.x ** 2 + self.y ** 2 + self.z ** 2) | |
def __str__(self): | |
"""Returns a string representation of the vector. | |
Returns | |
======= | |
str | |
String representation of the vector | |
""" | |
return "[{:.2f}, {:.2f}, {:.2f}]".format(self.x, self.y, self.z) | |
# Last axis value storage | |
g_last_values = Vector3(0.0, 0.0, 0.0) | |
# Updates the vJoy device | |
def update_vjoy(vjoy): | |
device = vjoy[output.value["device_id"]] | |
if use_norm.value: | |
value = -1.0 + 2 * min(1.0, g_last_values.norm()) | |
else: | |
value = -1.0 + 2 * g_last_values.max_abs_element() | |
if invert_output.value: | |
value *= -1 | |
device.axis(output.value["input_id"]).value = value | |
# Callbacks for the individual axes | |
@dec_1.axis(input_1.input_id) | |
def axis1(event, vjoy): | |
global g_last_values | |
g_last_values.x = event.value | |
update_vjoy(vjoy) | |
@dec_2.axis(input_2.input_id) | |
def axis2(event, vjoy): | |
global g_last_values | |
g_last_values.y = event.value | |
update_vjoy(vjoy) | |
if use_third.value: | |
@dec_3.axis(input_3.input_id) | |
def axis3(event, vjoy): | |
global g_last_values | |
g_last_values.z = event.value | |
update_vjoy(vjoy) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment