prompt navigation

This commit is contained in:
Nordup 2023-08-14 00:47:42 +04:00
parent 3985aece50
commit 93b47f288e
7 changed files with 113 additions and 37 deletions

View file

@ -1,16 +1,8 @@
[gd_resource type="Resource" script_class="Bookmarks" load_steps=4 format=3 uid="uid://bewhdj6jugt6q"]
[gd_resource type="Resource" script_class="Bookmarks" load_steps=2 format=3 uid="uid://bewhdj6jugt6q"]
[ext_resource type="Script" path="res://scripts/resources/bookmarks.gd" id="1_1h3wl"]
[ext_resource type="Script" path="res://scripts/resources/gate.gd" id="1_yi510"]
[sub_resource type="Resource" id="Resource_5ln6h"]
script = ExtResource("1_yi510")
url = "http://95.163.241.188:8000"
title = "Portals"
description = "Explore other worlds!"
image = ""
[resource]
script = ExtResource("1_1h3wl")
featured_gates = Array[ExtResource("1_yi510")]([SubResource("Resource_5ln6h")])
starred_gates = null
featured_gates = Array[Resource("res://scripts/resources/gate.gd")]([])
starred_gates = Array[Resource("res://scripts/resources/gate.gd")]([])

View file

@ -450,7 +450,6 @@ api = ExtResource("15_uafyh")
result_scene = ExtResource("16_lbcsd")
panel = NodePath("..")
[connection signal="text_changed" from="." to="." method="_on_text_changed"]
[connection signal="text_changed" from="." to="Go" method="_on_search_text_changed"]
[connection signal="text_changed" from="." to="Prompt/Panel/VBoxContainer" method="_on_search_text_changed"]
[connection signal="text_submitted" from="." to="." method="_on_text_submitted"]

View file

@ -14,4 +14,5 @@ static func to_alpha(text: String) -> String:
result += " "
last_is_alpha = false
result = result.strip_edges()
return result

View file

@ -2,12 +2,11 @@ extends LineEdit
class_name Search
signal on_release_focus
signal on_navigation(event: int)
@export var gate_events: GateEvents
@export var prompt_panel: Control
var url: String
func _ready() -> void:
gate_events.open_gate.connect(set_current_url)
@ -16,24 +15,11 @@ func _ready() -> void:
func set_current_url(_url: String) -> void:
url = _url
text = url
text = _url
on_release_focus.emit()
func _input(event: InputEvent) -> void:
if (has_focus() and event is InputEventMouseButton
and not get_global_rect().has_point(event.position)
and not prompt_panel.get_global_rect().has_point(event.position)):
release_focus()
on_release_focus.emit()
func _on_text_changed(_url: String) -> void:
url = _url
func _on_text_submitted(_url: String) -> void:
open_gate()
@ -43,12 +29,29 @@ func _on_go_pressed() -> void:
func open_gate() -> void:
if url.is_empty(): return
if text.is_empty(): return
if Url.is_valid(url):
gate_events.open_gate_emit(url)
if Url.is_valid(text):
gate_events.open_gate_emit(text)
else:
gate_events.search_emit(url)
gate_events.search_emit(text)
release_focus()
on_release_focus.emit()
func _input(event: InputEvent) -> void:
if not has_focus(): return
if (event is InputEventMouseButton
and not get_global_rect().has_point(event.position)
and not prompt_panel.get_global_rect().has_point(event.position)):
release_focus()
on_release_focus.emit()
if event.is_action_pressed("ui_text_caret_up"):
on_navigation.emit(PromptNavigation.UP)
get_viewport().set_input_as_handled()
elif event.is_action_pressed("ui_text_caret_down"):
on_navigation.emit(PromptNavigation.DOWN)
get_viewport().set_input_as_handled()

View file

@ -16,3 +16,11 @@ func fill(prompt: Dictionary) -> void:
func _on_button_pressed() -> void:
if prompt_text.text.is_empty(): return
gate_events.search_emit(prompt_text.text)
func focus() -> void:
print("focus: " + prompt_text.text)
func unfocus() -> void:
print("unfocus: " + prompt_text.text)

View file

@ -1,17 +1,90 @@
extends Node
class_name PromptNavigation
enum {
UP,
DOWN
}
@export var search: Search
@export var prompt_results: PromptResults
var current_prompt: int = -1
var actual_search_query: String
func _ready() -> void:
search.focus_entered.connect(_on_focus_entered)
search.on_release_focus.connect(_on_release_focus)
search.focus_entered.connect(on_focus_entered)
search.on_release_focus.connect(on_release_focus)
search.on_navigation.connect(on_navigation)
search.text_changed.connect(func(_text): reset())
func _on_focus_entered() -> void:
func on_focus_entered() -> void:
prompt_results._on_search_text_changed(search.text)
func _on_release_focus() -> void:
func on_release_focus() -> void:
prompt_results.clear()
reset()
func on_navigation(event: int) -> void:
match event:
UP:
prompt_up()
DOWN:
prompt_down()
_:
printerr("Unhandled navigation event")
func prompt_up() -> void:
var from: int = current_prompt
var to: int
if from == -1:
to = prompt_results.get_children().size() - 1
else:
to = from - 1
if from != to:
current_prompt = to
switch_prompt(from, to)
func prompt_down() -> void:
var from: int = current_prompt
var to: int
if from == prompt_results.get_children().size() - 1:
to = -1
else:
to = from + 1
if from != to:
current_prompt = to
switch_prompt(from, to)
func reset() -> void:
current_prompt = -1
actual_search_query = ""
func switch_prompt(from: int, to: int) -> void:
if from == -1:
actual_search_query = search.text
else:
var from_prompt: PromptResult = prompt_results.get_children()[from]
from_prompt.unfocus()
if to == -1:
search.text = actual_search_query
search.caret_column = search.text.length()
else:
var to_prompt: PromptResult = prompt_results.get_children()[to]
to_prompt.focus()
search.text = to_prompt.prompt_text.text
search.caret_column = search.text.length()

View file

@ -8,5 +8,5 @@ func _ready() -> void:
func _on_search_text_changed(_url: String) -> void:
# visible = true if Url.is_valid(_url) else false
#visible = true if not _url.is_empty() else false
pass