allow for keyboard echoing and improve deleting in input.

This commit is contained in:
Nitwel 2024-03-07 15:49:33 +01:00
parent 8bf3c5ce3d
commit 75ca169876
4 changed files with 46 additions and 13 deletions

View File

@ -73,6 +73,7 @@ func create_key(key: Key):
key_node.label = EventKey.key_to_string(key, caps) key_node.label = EventKey.key_to_string(key, caps)
key_node.focusable = false key_node.focusable = false
key_node.font_size = 32 key_node.font_size = 32
key_node.echo = true
key_node.set_meta("key", key) key_node.set_meta("key", key)
return key_node return key_node
@ -90,4 +91,3 @@ func _emit_event(type: String, key: Key):
event.shift_pressed = caps event.shift_pressed = caps
EventSystem.emit(type, event) EventSystem.emit(type, event)

View File

@ -24,6 +24,7 @@ transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.349964, 0, -0.0700361)
focusable = false focusable = false
label = "backspace" label = "backspace"
icon = true icon = true
echo = true
metadata/key = 4194308 metadata/key = 4194308
[node name="Caps" parent="." instance=ExtResource("1_xdpwr")] [node name="Caps" parent="." instance=ExtResource("1_xdpwr")]
@ -32,12 +33,14 @@ focusable = false
label = "keyboard_capslock" label = "keyboard_capslock"
icon = true icon = true
toggleable = true toggleable = true
echo = false
[node name="Paste" parent="." instance=ExtResource("1_xdpwr")] [node name="Paste" parent="." instance=ExtResource("1_xdpwr")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.349964, 0, 0.089964) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.349964, 0, 0.089964)
focusable = false focusable = false
label = "assignment" label = "assignment"
icon = true icon = true
echo = false
[node name="Keys" type="Node3D" parent="."] [node name="Keys" type="Node3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.310036, 0, -0.090036) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.310036, 0, -0.090036)

View File

@ -7,6 +7,8 @@ signal on_button_down()
signal on_button_up() signal on_button_up()
const IconFont = preload ("res://assets/icons/icons.tres") const IconFont = preload ("res://assets/icons/icons.tres")
const ECHO_WAIT_INITIAL = 0.5
const ECHO_WAIT_REPEAT = 0.1
@onready var label_node: Label3D = $Body/Label @onready var label_node: Label3D = $Body/Label
@onready var finger_area: Area3D = $FingerArea @onready var finger_area: Area3D = $FingerArea
@ -47,6 +49,7 @@ const IconFont = preload ("res://assets/icons/icons.tres")
@export var toggleable: bool = false @export var toggleable: bool = false
@export var disabled: bool = false @export var disabled: bool = false
@export var echo: bool = false
@export var initial_active: bool = false @export var initial_active: bool = false
var external_value: Proxy = null: var external_value: Proxy = null:
set(value): set(value):
@ -74,11 +77,26 @@ var active: bool = false:
@onready var animation_player: AnimationPlayer = $AnimationPlayer @onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var console = get_node("/root/Main/Console") @onready var console = get_node("/root/Main/Console")
var echo_timer: Timer = null
func _ready(): func _ready():
if initial_active: if initial_active:
active = true active = true
if echo:
echo_timer = Timer.new()
echo_timer.wait_time = ECHO_WAIT_INITIAL
echo_timer.one_shot = false
echo_timer.timeout.connect(func():
echo_timer.stop()
echo_timer.wait_time=ECHO_WAIT_REPEAT
echo_timer.start()
on_button_down.emit()
)
add_child(echo_timer)
func update_animation(): func update_animation():
var length = animation_player.get_animation("down").length var length = animation_player.get_animation("down").length
@ -97,6 +115,9 @@ func _on_press_down(event):
if toggleable: if toggleable:
return return
if echo:
echo_timer.start()
active = true active = true
on_button_down.emit() on_button_down.emit()
@ -113,6 +134,10 @@ func _on_press_up(event):
else: else:
on_button_up.emit() on_button_up.emit()
else: else:
if echo:
echo_timer.stop()
echo_timer.wait_time = ECHO_WAIT_INITIAL
active = false active = false
on_button_up.emit() on_button_up.emit()

View File

@ -23,13 +23,18 @@ func set_text(value: String, insert: bool = false):
gap_offsets = _calculate_text_gaps() gap_offsets = _calculate_text_gaps()
if insert == false: if insert == false:
caret_position += text.length() - old_text.length() var text_diff = text.length() - old_text.length()
caret_position += text_diff
if text_diff < 0:
char_offset = max(0, char_offset + text_diff)
else: else:
caret_position = 0 caret_position = 0
overflow_index = _calculate_overflow_index() overflow_index = _calculate_overflow_index()
focus_caret() focus_caret()
print(overflow_index, " ", char_offset, " ", caret_position)
func get_display_text(): func get_display_text():
# In case all chars fit, return the whole text. # In case all chars fit, return the whole text.
if overflow_index == - 1: if overflow_index == - 1: