From e1f99699863b2343b31b9b9a0efa1e9d12a3b2e4 Mon Sep 17 00:00:00 2001 From: Nitwel Date: Thu, 23 Nov 2023 19:19:30 +0100 Subject: [PATCH] Finish caret moving on input --- content/ui/components/input/input.gd | 41 ++++++++++++++++++++- content/ui/components/input/text_handler.gd | 13 ++++--- content/ui/menu/room/room_menu.gd | 6 +-- content/ui/menu/settings/settings_menu.tscn | 4 +- 4 files changed, 51 insertions(+), 13 deletions(-) diff --git a/content/ui/components/input/input.gd b/content/ui/components/input/input.gd index f380a83..e5df071 100644 --- a/content/ui/components/input/input.gd +++ b/content/ui/components/input/input.gd @@ -26,6 +26,7 @@ var text_handler = preload("res://content/ui/components/input/text_handler.gd"). var keyboard_input: bool = false +var input_plane = Plane(Vector3.UP, Vector3.ZERO) func _ready(): text_handler.label = label @@ -71,15 +72,51 @@ func set_width(value: float): collision.shape.size.x = value label.position.x = -value / 2 + 0.002 -func _on_focus_in(event): - var pos_x = label.to_local(event.ray.get_collision_point()).x +func _on_press_move(event): + var ray_pos = event.ray.global_position + var ray_dir = -event.ray.global_transform.basis.z + var local_pos = label.to_local(ray_pos) + var local_dir = label.global_transform.basis.inverse() * ray_dir + + var intersection_point = input_plane.intersects_ray(local_pos, local_dir) + + if intersection_point == null: + return + + var pos_x = intersection_point.x text_handler.update_caret_position(pos_x) caret.position.x = text_handler.get_caret_position() + label.text = text_handler.get_display_text() + +func _on_focus_in(event): + var pos_x = label.to_local(event.ray.get_collision_point()).x + text_handler.update_caret_position(pos_x) + + caret.position.x = text_handler.get_caret_position() + label.text = text_handler.get_display_text() caret.show() animation.play("blink") +func update_caret_position(event): + var ray_pos = event.ray.global_position + var ray_dir = -event.ray.global_transform.basis.z + + var local_pos = label.to_local(ray_pos) + var local_dir = label.global_transform.basis.inverse() * ray_dir + + var intersection_point = input_plane.intersects_ray(local_pos, local_dir) + + if intersection_point == null: + return + + var pos_x = intersection_point.x + text_handler.update_caret_position(pos_x) + + caret.position.x = text_handler.get_caret_position() + + func _on_focus_out(_event): animation.stop() caret.hide() diff --git a/content/ui/components/input/text_handler.gd b/content/ui/components/input/text_handler.gd index 9b75af1..8a8bf35 100644 --- a/content/ui/components/input/text_handler.gd +++ b/content/ui/components/input/text_handler.gd @@ -44,17 +44,21 @@ func focus_caret(): while caret_position > overflow_index: char_offset += caret_position - overflow_index overflow_index = _calculate_overflow_index() + if overflow_index == -1: + break while caret_position < char_offset: - char_offset = caret_position# + char_offset = caret_position overflow_index = _calculate_overflow_index() + if overflow_index == -1: + break func get_caret_position(): return gap_offsets[caret_position] - gap_offsets[char_offset] func update_caret_position(click_pos_x: float): caret_position = _calculate_caret_position(click_pos_x) - print("caret_position: ", caret_position) + focus_caret() func _calculate_caret_position(click_pos_x: float): for i in range(1, gap_offsets.size()): @@ -79,11 +83,8 @@ func _calculate_text_gaps(): return offsets -## Returns the index of the last character that fits in the width. -## If all characters fit, returns -1. func _calculate_overflow_index(): - for i in range(char_offset, gap_offsets.size()): if gap_offsets[i] - gap_offsets[char_offset] >= width: return i - 1 - return -1 + return gap_offsets.size() - 1 diff --git a/content/ui/menu/room/room_menu.gd b/content/ui/menu/room/room_menu.gd index 144b8f2..df3f0e3 100644 --- a/content/ui/menu/room/room_menu.gd +++ b/content/ui/menu/room/room_menu.gd @@ -25,8 +25,8 @@ func _ready(): add_corner(event.ray.get_collision_point()) ) - toggle_edit_button.get_node("Clickable").on_click.connect(func(event): - edit_enabled = event.active + toggle_edit_button.get_node("Clickable").on_press_up.connect(func(event): + edit_enabled = event.target.active if edit_enabled == false: wall_corners.visible = false @@ -131,7 +131,7 @@ func add_corner(position: Vector3): if moving == null: return - var direction = (event.ray.to_global(event.ray.target_position) - event.ray.global_position).normalized() + var direction = -event.ray.global_transform.basis.z var new_position = ground_plane.intersects_ray(event.ray.global_position, direction) if new_position == null: diff --git a/content/ui/menu/settings/settings_menu.tscn b/content/ui/menu/settings/settings_menu.tscn index f7aa1f9..2587855 100644 --- a/content/ui/menu/settings/settings_menu.tscn +++ b/content/ui/menu/settings/settings_menu.tscn @@ -44,7 +44,7 @@ horizontal_alignment = 0 [node name="InputURL" parent="Content" instance=ExtResource("4_q3x6k")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.18, 0, 0.03) width = 0.2 -text = "ws://192.168.33.33:8123" +text = "" [node name="LabelToken" type="Label3D" parent="Content"] transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0.01, 0, 0.07) @@ -56,7 +56,7 @@ horizontal_alignment = 0 [node name="InputToken" parent="Content" instance=ExtResource("4_q3x6k")] transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.18, 0, 0.07) width = 0.2 -text = "paste token here paste token here" +text = "" [node name="LabelConnect" type="Label3D" parent="Content"] transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0.15, 0, 0.12)