2023-11-22 02:44:07 +02:00
|
|
|
@tool
|
|
|
|
extends StaticBody3D
|
2023-11-21 12:40:06 +02:00
|
|
|
|
2024-03-07 16:49:33 +02:00
|
|
|
const button_scene = preload ("res://content/ui/components/button/button.tscn")
|
2023-11-21 12:40:06 +02:00
|
|
|
|
|
|
|
@onready var keys = $Keys
|
|
|
|
@onready var caps_button = $Caps
|
2023-11-22 02:44:07 +02:00
|
|
|
@onready var backspace_button = $Backspace
|
2023-11-23 00:59:46 +02:00
|
|
|
@onready var paste_button = $Paste
|
2023-11-21 12:40:06 +02:00
|
|
|
var key_list = [
|
2023-11-22 02:44:07 +02:00
|
|
|
[KEY_1, KEY_2, KEY_3, KEY_4, KEY_5, KEY_6, KEY_7, KEY_8, KEY_9, KEY_0, KEY_ASCIITILDE],
|
|
|
|
[KEY_Q, KEY_W, KEY_E, KEY_R, KEY_T, KEY_Y, KEY_U, KEY_I, KEY_O, KEY_P, KEY_SLASH],
|
|
|
|
[KEY_A, KEY_S, KEY_D, KEY_F, KEY_G, KEY_H, KEY_J, KEY_K, KEY_L, KEY_COLON, KEY_BACKSLASH],
|
2024-03-07 16:49:33 +02:00
|
|
|
[KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, KEY_COMMA, KEY_PERIOD, KEY_MINUS]
|
2023-11-21 12:40:06 +02:00
|
|
|
]
|
|
|
|
|
2024-03-07 16:49:33 +02:00
|
|
|
var caps = false:
|
2023-11-22 02:44:07 +02:00
|
|
|
set(value):
|
|
|
|
caps = value
|
|
|
|
update_labels()
|
2023-11-21 12:40:06 +02:00
|
|
|
|
|
|
|
func _ready():
|
2024-05-22 19:38:28 +03:00
|
|
|
get_parent().remove_child.call_deferred(self)
|
|
|
|
|
|
|
|
_create_keys()
|
|
|
|
_prepare_keyboard_spawn()
|
|
|
|
_connect_key_events()
|
|
|
|
|
|
|
|
func _prepare_keyboard_spawn():
|
|
|
|
if Engine.is_editor_hint():
|
|
|
|
return
|
|
|
|
|
|
|
|
EventSystem.on_focus_in.connect(func(event):
|
|
|
|
if is_inside_tree():
|
|
|
|
return
|
|
|
|
|
|
|
|
App.main.add_child(self)
|
|
|
|
global_transform=App.menu.get_node("AnimationContainer/KeyboardPlace").global_transform
|
|
|
|
)
|
|
|
|
|
|
|
|
EventSystem.on_focus_out.connect(func(event):
|
|
|
|
if !is_inside_tree():
|
|
|
|
return
|
|
|
|
|
|
|
|
App.main.remove_child(self)
|
|
|
|
)
|
|
|
|
|
|
|
|
func _create_keys():
|
2023-11-21 12:40:06 +02:00
|
|
|
for row in key_list:
|
|
|
|
for key in row:
|
2024-05-22 19:38:28 +03:00
|
|
|
var key_node = _create_key(key)
|
2023-11-25 02:46:41 +02:00
|
|
|
keys.add_child(key_node)
|
2023-11-22 18:07:28 +02:00
|
|
|
|
|
|
|
if Engine.is_editor_hint():
|
|
|
|
continue
|
|
|
|
|
2023-11-25 02:46:41 +02:00
|
|
|
key_node.on_button_down.connect(func():
|
2023-11-22 02:44:07 +02:00
|
|
|
_emit_event("key_down", key)
|
|
|
|
)
|
2023-11-25 02:46:41 +02:00
|
|
|
key_node.on_button_up.connect(func():
|
2023-11-22 02:44:07 +02:00
|
|
|
_emit_event("key_up", key)
|
|
|
|
)
|
2023-11-21 12:40:06 +02:00
|
|
|
|
|
|
|
keys.columns = key_list[0].size()
|
|
|
|
|
2024-05-22 19:38:28 +03:00
|
|
|
func _connect_key_events():
|
2023-11-22 18:07:28 +02:00
|
|
|
if Engine.is_editor_hint():
|
|
|
|
return
|
|
|
|
|
2023-11-22 02:44:07 +02:00
|
|
|
backspace_button.on_button_down.connect(func():
|
|
|
|
_emit_event("key_down", KEY_BACKSPACE)
|
|
|
|
)
|
2023-11-21 12:40:06 +02:00
|
|
|
|
2023-11-22 02:44:07 +02:00
|
|
|
backspace_button.on_button_up.connect(func():
|
|
|
|
_emit_event("key_up", KEY_BACKSPACE)
|
|
|
|
)
|
2023-11-21 12:40:06 +02:00
|
|
|
|
2023-11-22 02:44:07 +02:00
|
|
|
caps_button.on_button_down.connect(func():
|
2024-03-07 16:49:33 +02:00
|
|
|
caps=true
|
2023-11-22 02:44:07 +02:00
|
|
|
_emit_event("key_down", KEY_CAPSLOCK)
|
|
|
|
)
|
2023-11-21 12:40:06 +02:00
|
|
|
|
2023-11-22 02:44:07 +02:00
|
|
|
caps_button.on_button_up.connect(func():
|
2024-03-07 16:49:33 +02:00
|
|
|
caps=false
|
2023-11-22 02:44:07 +02:00
|
|
|
_emit_event("key_up", KEY_CAPSLOCK)
|
|
|
|
)
|
2023-11-21 12:40:06 +02:00
|
|
|
|
2023-11-23 00:59:46 +02:00
|
|
|
paste_button.on_button_down.connect(func():
|
|
|
|
# There is no KEY_PASTE obviously, so we use KEY_INSERT for now
|
|
|
|
_emit_event("key_down", KEY_INSERT)
|
|
|
|
)
|
|
|
|
|
|
|
|
paste_button.on_button_up.connect(func():
|
|
|
|
_emit_event("key_up", KEY_INSERT)
|
|
|
|
)
|
|
|
|
|
2024-05-22 19:38:28 +03:00
|
|
|
func _create_key(key: Key):
|
2023-11-26 01:02:03 +02:00
|
|
|
var key_node = button_scene.instantiate()
|
2023-11-22 02:44:07 +02:00
|
|
|
|
2023-11-26 01:02:03 +02:00
|
|
|
key_node.label = EventKey.key_to_string(key, caps)
|
2024-04-25 01:22:27 +03:00
|
|
|
key_node.size = Vector3(0.05, 0.05, 0.01)
|
2023-11-28 00:46:05 +02:00
|
|
|
key_node.focusable = false
|
|
|
|
key_node.font_size = 32
|
2024-03-07 16:49:33 +02:00
|
|
|
key_node.echo = true
|
2023-11-25 02:46:41 +02:00
|
|
|
key_node.set_meta("key", key)
|
2023-11-21 12:40:06 +02:00
|
|
|
|
2023-11-25 02:46:41 +02:00
|
|
|
return key_node
|
2023-11-22 02:44:07 +02:00
|
|
|
|
|
|
|
func update_labels():
|
|
|
|
for key_button in keys.get_children():
|
|
|
|
if caps:
|
2023-11-28 16:14:21 +02:00
|
|
|
key_button.label = key_button.label.to_upper()
|
2023-11-22 02:44:07 +02:00
|
|
|
else:
|
2023-11-28 16:14:21 +02:00
|
|
|
key_button.label = key_button.label.to_lower()
|
2023-11-22 02:44:07 +02:00
|
|
|
|
|
|
|
func _emit_event(type: String, key: Key):
|
|
|
|
var event = EventKey.new()
|
|
|
|
event.key = key
|
|
|
|
event.shift_pressed = caps
|
|
|
|
|
|
|
|
EventSystem.emit(type, event)
|