Finish caret moving on input

This commit is contained in:
Nitwel 2023-11-23 19:19:30 +01:00
parent c1ba32552d
commit e1f9969986
4 changed files with 51 additions and 13 deletions

View File

@ -26,6 +26,7 @@ var text_handler = preload("res://content/ui/components/input/text_handler.gd").
var keyboard_input: bool = false var keyboard_input: bool = false
var input_plane = Plane(Vector3.UP, Vector3.ZERO)
func _ready(): func _ready():
text_handler.label = label text_handler.label = label
@ -71,15 +72,51 @@ func set_width(value: float):
collision.shape.size.x = value collision.shape.size.x = value
label.position.x = -value / 2 + 0.002 label.position.x = -value / 2 + 0.002
func _on_focus_in(event): func _on_press_move(event):
var pos_x = label.to_local(event.ray.get_collision_point()).x 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) text_handler.update_caret_position(pos_x)
caret.position.x = text_handler.get_caret_position() 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() caret.show()
animation.play("blink") 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): func _on_focus_out(_event):
animation.stop() animation.stop()
caret.hide() caret.hide()

View File

@ -44,17 +44,21 @@ func focus_caret():
while caret_position > overflow_index: while caret_position > overflow_index:
char_offset += caret_position - overflow_index char_offset += caret_position - overflow_index
overflow_index = _calculate_overflow_index() overflow_index = _calculate_overflow_index()
if overflow_index == -1:
break
while caret_position < char_offset: while caret_position < char_offset:
char_offset = caret_position# char_offset = caret_position
overflow_index = _calculate_overflow_index() overflow_index = _calculate_overflow_index()
if overflow_index == -1:
break
func get_caret_position(): func get_caret_position():
return gap_offsets[caret_position] - gap_offsets[char_offset] return gap_offsets[caret_position] - gap_offsets[char_offset]
func update_caret_position(click_pos_x: float): func update_caret_position(click_pos_x: float):
caret_position = _calculate_caret_position(click_pos_x) caret_position = _calculate_caret_position(click_pos_x)
print("caret_position: ", caret_position) focus_caret()
func _calculate_caret_position(click_pos_x: float): func _calculate_caret_position(click_pos_x: float):
for i in range(1, gap_offsets.size()): for i in range(1, gap_offsets.size()):
@ -79,11 +83,8 @@ func _calculate_text_gaps():
return offsets return offsets
## Returns the index of the last character that fits in the width.
## If all characters fit, returns -1.
func _calculate_overflow_index(): func _calculate_overflow_index():
for i in range(char_offset, gap_offsets.size()): for i in range(char_offset, gap_offsets.size()):
if gap_offsets[i] - gap_offsets[char_offset] >= width: if gap_offsets[i] - gap_offsets[char_offset] >= width:
return i - 1 return i - 1
return -1 return gap_offsets.size() - 1

View File

@ -25,8 +25,8 @@ func _ready():
add_corner(event.ray.get_collision_point()) add_corner(event.ray.get_collision_point())
) )
toggle_edit_button.get_node("Clickable").on_click.connect(func(event): toggle_edit_button.get_node("Clickable").on_press_up.connect(func(event):
edit_enabled = event.active edit_enabled = event.target.active
if edit_enabled == false: if edit_enabled == false:
wall_corners.visible = false wall_corners.visible = false
@ -131,7 +131,7 @@ func add_corner(position: Vector3):
if moving == null: if moving == null:
return 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) var new_position = ground_plane.intersects_ray(event.ray.global_position, direction)
if new_position == null: if new_position == null:

View File

@ -44,7 +44,7 @@ horizontal_alignment = 0
[node name="InputURL" parent="Content" instance=ExtResource("4_q3x6k")] [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) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.18, 0, 0.03)
width = 0.2 width = 0.2
text = "ws://192.168.33.33:8123" text = ""
[node name="LabelToken" type="Label3D" parent="Content"] [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) 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")] [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) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.18, 0, 0.07)
width = 0.2 width = 0.2
text = "paste token here paste token here" text = ""
[node name="LabelConnect" type="Label3D" parent="Content"] [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) transform = Transform3D(1, 0, 0, 0, -4.37114e-08, 1, 0, -1, -4.37114e-08, 0.15, 0, 0.12)