keebie/scripts/game_key.gd

135 lines
3.4 KiB
GDScript

class_name GameKey extends Node3D
static var _scene := preload("res://scenes/game_key.tscn")
@export var _mesh: MeshInstance3D
@export var _upper_left_label: Label3D
@export var _upper_right_label: Label3D
@export var _lower_left_label: Label3D
@export var _lower_right_label: Label3D
@export var _center_label: Label3D
var props: KeyProps
var keyboard: GameKeyboard
var input_event_init: InputEventKey
var _is_pressed: bool
var _input_event_main: InputEventKey
var _input_event_shift: InputEventKey
var _input_event_alt: InputEventKey
var _input_event_alt_shift: InputEventKey
@onready var _default_position: Vector3 = position
static func instantiate_with_props(
_props: KeyProps, _keyboard: GameKeyboard
) -> GameKey:
var input_event := InputEventKey.new()
input_event.physical_keycode = _props.physical_keycode
input_event.location = _props.location
var node := _scene.instantiate() as GameKey
node.keyboard = _keyboard
node.props = _props
node.input_event_init = input_event
node.name = node.name + " " + node.input_event_init.as_text_physical_keycode()
if _props.location != KEY_LOCATION_UNSPECIFIED:
node.name += " " + node.input_event_init.as_text_location()
return node
func _ready() -> void:
_mesh.scale.x = props.width_ratio
_clear_labels()
func _process(_delta: float) -> void:
position.y = _default_position.y - 0.1 if _is_pressed else _default_position.y
func _unhandled_input(event: InputEvent) -> void:
if event is not InputEventKey:
return
var event_key := event as InputEventKey
if (
event_key.physical_keycode != props.physical_keycode
or event_key.location != props.location
):
return
_is_pressed = event_key.is_pressed()
if keyboard.configuring and _is_pressed:
if not keyboard.alt_layout:
if not event_key.shift_pressed:
_input_event_main = event_key
else:
_input_event_shift = event_key
else:
if not event_key.shift_pressed:
_input_event_alt = event_key
else:
_input_event_alt_shift = event_key
_set_labels()
func _clear_labels() -> void:
_upper_left_label.text = ""
_upper_right_label.text = ""
_lower_left_label.text = ""
_lower_right_label.text = ""
_center_label.text = ""
func _set_labels() -> void:
_clear_labels()
var main_char := (
char(_input_event_main.unicode).to_upper() if _input_event_main else ""
)
var shift_char := (
char(_input_event_shift.unicode).to_upper() if _input_event_shift else ""
)
var alt_char := (
char(_input_event_alt.unicode).to_upper() if _input_event_alt else ""
)
var alt_shift_char := (
char(_input_event_alt_shift.unicode).to_upper()
if _input_event_alt_shift
else ""
)
props.main_char = main_char
props.shift_char = shift_char
props.alt_char = alt_char
props.alt_shift_char = alt_shift_char
if (
props.physical_keycode == KEY_SPACE
or (!main_char and !shift_char and !alt_char and !alt_shift_char)
):
_center_label.text = input_event_init.as_text_physical_keycode()
return
if (main_char and not shift_char) or main_char == shift_char:
_upper_left_label.text = main_char
if (main_char and shift_char) and main_char != shift_char:
_upper_left_label.text = shift_char
_lower_left_label.text = main_char
if (not main_char and shift_char) and main_char != shift_char:
_upper_left_label.text = shift_char
if alt_char and alt_char != main_char:
_lower_right_label.text = alt_char
if alt_shift_char and alt_shift_char != alt_char and alt_shift_char != shift_char:
_upper_right_label.text = alt_shift_char