69 lines
1.5 KiB
GDScript
69 lines
1.5 KiB
GDScript
extends Button
|
|
|
|
|
|
const BLINK_SPEED: int = 150
|
|
const GAMEPAD_BUTTONS: AtlasTexture = preload("res://objects/hud/gamepad_buttons.atlastex")
|
|
|
|
export var action: String
|
|
export var options_screen: NodePath
|
|
|
|
|
|
var listen := false
|
|
var gamepad_buttons := GAMEPAD_BUTTONS.duplicate()
|
|
|
|
|
|
func _init() -> void:
|
|
icon_align = Button.ALIGN_CENTER
|
|
connect("pressed", self, "_on_pressed")
|
|
add_to_group("controls_buttons")
|
|
|
|
|
|
func _ready() -> void:
|
|
_update_text()
|
|
|
|
|
|
func _process(delta: float) -> void:
|
|
if listen:
|
|
if Time.get_ticks_msec() % (BLINK_SPEED * 2) < BLINK_SPEED:
|
|
modulate.a = 0.0
|
|
else:
|
|
modulate.a = 1.0
|
|
|
|
|
|
func _update_text() -> void:
|
|
var button = Controls.get_button(action)
|
|
if button >= 0:
|
|
gamepad_buttons.region.position.x = float(button % 8) * 12.0
|
|
gamepad_buttons.region.position.y = float(button / 8) * 10.0
|
|
icon = gamepad_buttons
|
|
text = ""
|
|
else:
|
|
text = "-"
|
|
modulate.a = 1.0
|
|
|
|
|
|
func _on_pressed() -> void:
|
|
if not listen:
|
|
listen = true
|
|
disabled = true
|
|
text = ""
|
|
get_node(options_screen).can_exit = false
|
|
get_tree().create_timer(2.0, true).connect("timeout", self, "_on_timeout")
|
|
|
|
|
|
func _on_timeout() -> void:
|
|
listen = false
|
|
disabled = false
|
|
_update_text()
|
|
get_node(options_screen).set_deferred("can_exit", true)
|
|
|
|
|
|
func _gui_input(event: InputEvent) -> void:
|
|
if listen:
|
|
if event is InputEventJoypadButton and event.pressed:
|
|
Controls.set_button(action, event.button_index)
|
|
listen = false
|
|
get_node(options_screen).set_deferred("can_exit", true)
|
|
set_deferred("disabled", false)
|
|
_update_text()
|
|
accept_event()
|