gamo-gd/controller/joystick/joystick.gd

43 lines
1.2 KiB
GDScript

extends Panel
const DRIFT_DEADZONE := 0.03;
const REST_DEADZONE := 0.05;
const DEFAULT_COLOR := Color(0x2a2a2aff)
const ACTIVE_COLOR := Color(0x4a8e53ff)
const ERROR_COLOR := Color(0xbe2037ff)
@onready var pos: Panel = $Pos
@onready var label_node: RichTextLabel = $Label
@onready var box_theme: StyleBoxFlat = preload("uid://bgb7rlf1chjnd") as StyleBoxFlat
@export var axis_x: JoyAxis
@export var axis_y: JoyAxis
var _dot_style: StyleBoxFlat
func _ready() -> void:
self._dot_style = box_theme.duplicate()
self.pos.add_theme_stylebox_override("panel", self._dot_style)
self._dot_style.bg_color = DEFAULT_COLOR
func _process(_delta: float) -> void:
var x := Input.get_joy_axis(0, self.axis_x)
var y := Input.get_joy_axis(0, self.axis_y)
self.pos.position.x = (x * 128.0) + 104.0
self.pos.position.y = (y * 128.0) + 104.0
var is_moving := x > REST_DEADZONE || y > REST_DEADZONE \
|| x < -REST_DEADZONE || y < -REST_DEADZONE
var is_drifting := x >= DRIFT_DEADZONE || y >= DRIFT_DEADZONE \
|| x <= -DRIFT_DEADZONE || y <= -DRIFT_DEADZONE
if is_moving:
self._dot_style.bg_color = ACTIVE_COLOR
elif is_drifting:
self._dot_style.bg_color = ERROR_COLOR
else:
self._dot_style.bg_color = DEFAULT_COLOR
self.label_node.text = "x:%0.2f y:%0.2f" % [x, y]