2014-08-06 08:29:14 +02:00
|
|
|
|
|
|
|
extends Node2D
|
|
|
|
|
|
|
|
# Joysticks demo, written by Dana Olson <dana@shineuponthee.com>
|
|
|
|
#
|
|
|
|
# This is a demo of joystick support, and doubles as a testing application
|
|
|
|
# inspired by and similar to jstest-gtk.
|
|
|
|
#
|
|
|
|
# Licensed under the MIT license
|
|
|
|
|
2015-11-21 13:31:25 +01:00
|
|
|
# Member variables
|
2014-08-06 08:29:14 +02:00
|
|
|
var joy_num
|
|
|
|
var cur_joy
|
|
|
|
var axis_value
|
|
|
|
|
2016-01-06 01:01:50 +01:00
|
|
|
const DEADZONE = 0.2
|
2014-08-06 08:29:14 +02:00
|
|
|
|
2016-01-06 01:01:50 +01:00
|
|
|
func _fixed_process(delta):
|
2015-11-21 13:31:25 +01:00
|
|
|
# Get the joystick device number from the spinbox
|
2014-08-06 08:29:14 +02:00
|
|
|
joy_num = get_node("joy_num").get_value()
|
2015-12-09 08:38:23 +01:00
|
|
|
|
2015-11-21 13:31:25 +01:00
|
|
|
# Display the name of the joystick if we haven't already
|
2014-08-06 08:29:14 +02:00
|
|
|
if joy_num != cur_joy:
|
|
|
|
cur_joy = joy_num
|
2015-12-09 08:38:23 +01:00
|
|
|
get_node("joy_name").set_text(Input.get_joy_name(joy_num))
|
|
|
|
|
2015-11-21 13:31:25 +01:00
|
|
|
# Loop through the axes and show their current values
|
2016-05-10 20:32:12 +02:00
|
|
|
for axis in range(JOY_ANALOG_0_X, JOY_AXIS_MAX):
|
2015-12-09 08:38:23 +01:00
|
|
|
axis_value = Input.get_joy_axis(joy_num, axis)
|
|
|
|
get_node("axis_prog" + str(axis)).set_value(100*axis_value)
|
|
|
|
get_node("axis_val" + str(axis)).set_text(str(axis_value))
|
2016-05-10 20:32:12 +02:00
|
|
|
# Show joystick direction indicators
|
|
|
|
if (axis <= JOY_ANALOG_1_Y):
|
2016-01-06 01:01:50 +01:00
|
|
|
if (abs(axis_value) < DEADZONE):
|
|
|
|
get_node("diagram/axes/" + str(axis) + "+").hide()
|
|
|
|
get_node("diagram/axes/" + str(axis) + "-").hide()
|
|
|
|
elif (axis_value > 0):
|
|
|
|
get_node("diagram/axes/" + str(axis) + "+").show()
|
|
|
|
else:
|
|
|
|
get_node("diagram/axes/" + str(axis) + "-").show()
|
2015-12-09 08:38:23 +01:00
|
|
|
|
2015-11-21 13:31:25 +01:00
|
|
|
# Loop through the buttons and highlight the ones that are pressed
|
2016-05-10 20:32:12 +02:00
|
|
|
for btn in range(JOY_BUTTON_0, JOY_BUTTON_MAX):
|
2014-08-06 08:29:14 +02:00
|
|
|
if (Input.is_joy_button_pressed(joy_num, btn)):
|
2015-12-09 08:38:23 +01:00
|
|
|
get_node("btn" + str(btn)).add_color_override("font_color", Color(1, 1, 1, 1))
|
2016-01-06 01:01:50 +01:00
|
|
|
get_node("diagram/buttons/" + str(btn)).show()
|
2014-08-06 08:29:14 +02:00
|
|
|
else:
|
2015-12-09 08:38:23 +01:00
|
|
|
get_node("btn" + str(btn)).add_color_override("font_color", Color(0.2, 0.1, 0.3, 1))
|
2016-01-06 01:01:50 +01:00
|
|
|
get_node("diagram/buttons/" + str(btn)).hide()
|
2015-12-09 08:38:23 +01:00
|
|
|
|
|
|
|
func _ready():
|
2016-01-06 01:01:50 +01:00
|
|
|
set_fixed_process(true)
|
2016-01-26 14:26:11 +01:00
|
|
|
Input.connect("joy_connection_changed", self, "_on_joy_connection_changed")
|
|
|
|
|
|
|
|
#Called whenever a joystick has been connected or disconnected.
|
|
|
|
func _on_joy_connection_changed(device_id, connected):
|
|
|
|
if device_id == cur_joy:
|
|
|
|
if connected:
|
|
|
|
get_node("joy_name").set_text(Input.get_joy_name(device_id))
|
|
|
|
else:
|
|
|
|
get_node("joy_name").set_text("")
|