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

@ -1,7 +1,7 @@
@tool
extends StaticBody3D
const button_scene = preload("res://content/ui/components/button/button.tscn")
const button_scene = preload ("res://content/ui/components/button/button.tscn")
@onready var keys = $Keys
@onready var caps_button = $Caps
@ -11,10 +11,10 @@ var key_list = [
[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],
[KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, KEY_COMMA , KEY_PERIOD, KEY_MINUS]
[KEY_Z, KEY_X, KEY_C, KEY_V, KEY_B, KEY_N, KEY_M, KEY_COMMA, KEY_PERIOD, KEY_MINUS]
]
var caps = false :
var caps = false:
set(value):
caps = value
update_labels()
@ -49,12 +49,12 @@ func _ready():
)
caps_button.on_button_down.connect(func():
caps = true
caps=true
_emit_event("key_down", KEY_CAPSLOCK)
)
caps_button.on_button_up.connect(func():
caps = false
caps=false
_emit_event("key_up", KEY_CAPSLOCK)
)
@ -73,6 +73,7 @@ func create_key(key: Key):
key_node.label = EventKey.key_to_string(key, caps)
key_node.focusable = false
key_node.font_size = 32
key_node.echo = true
key_node.set_meta("key", key)
return key_node
@ -90,4 +91,3 @@ func _emit_event(type: String, key: Key):
event.shift_pressed = caps
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
label = "backspace"
icon = true
echo = true
metadata/key = 4194308
[node name="Caps" parent="." instance=ExtResource("1_xdpwr")]
@ -32,12 +33,14 @@ focusable = false
label = "keyboard_capslock"
icon = true
toggleable = true
echo = false
[node name="Paste" parent="." instance=ExtResource("1_xdpwr")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.349964, 0, 0.089964)
focusable = false
label = "assignment"
icon = true
echo = false
[node name="Keys" type="Node3D" parent="."]
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()
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 finger_area: Area3D = $FingerArea
@ -47,6 +49,7 @@ const IconFont = preload ("res://assets/icons/icons.tres")
@export var toggleable: bool = false
@export var disabled: bool = false
@export var echo: bool = false
@export var initial_active: bool = false
var external_value: Proxy = null:
set(value):
@ -74,11 +77,26 @@ var active: bool = false:
@onready var animation_player: AnimationPlayer = $AnimationPlayer
@onready var console = get_node("/root/Main/Console")
var echo_timer: Timer = null
func _ready():
if initial_active:
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():
var length = animation_player.get_animation("down").length
@ -97,6 +115,9 @@ func _on_press_down(event):
if toggleable:
return
if echo:
echo_timer.start()
active = true
on_button_down.emit()
@ -113,6 +134,10 @@ func _on_press_up(event):
else:
on_button_up.emit()
else:
if echo:
echo_timer.stop()
echo_timer.wait_time = ECHO_WAIT_INITIAL
active = false
on_button_up.emit()

View File

@ -14,7 +14,7 @@ var caret_position: int = 3:
func set_width(value: float):
width = value
func set_text(value: String, insert: bool = false):
func set_text(value: String, insert: bool=false):
var old_text = text
text = value
@ -23,34 +23,39 @@ func set_text(value: String, insert: bool = false):
gap_offsets = _calculate_text_gaps()
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:
caret_position = 0
overflow_index = _calculate_overflow_index()
focus_caret()
print(overflow_index, " ", char_offset, " ", caret_position)
func get_display_text():
# In case all chars fit, return the whole text.
if overflow_index == -1:
if overflow_index == - 1:
return text.substr(char_offset)
return text.substr(char_offset, overflow_index - char_offset)
func focus_caret():
if overflow_index == -1:
if overflow_index == - 1:
char_offset = 0
return
while caret_position > overflow_index:
char_offset += caret_position - overflow_index
overflow_index = _calculate_overflow_index()
if overflow_index == -1:
if overflow_index == - 1:
break
while caret_position < char_offset:
char_offset = caret_position
overflow_index = _calculate_overflow_index()
if overflow_index == -1:
if overflow_index == - 1:
break
func get_caret_position():
@ -65,7 +70,7 @@ func _calculate_caret_position(click_pos_x: float):
var left = gap_offsets[i] - gap_offsets[char_offset]
if click_pos_x < left:
return i - 1
return i - 1
return gap_offsets.size() - 1