From 93b47f288e9e03427239ca7a42550e2761841800 Mon Sep 17 00:00:00 2001 From: Nordup Date: Mon, 14 Aug 2023 00:47:42 +0400 Subject: [PATCH] prompt navigation --- project/resources/bookmarks.tres | 14 +--- project/scenes/components/search.tscn | 1 - project/scripts/string_tools.gd | 1 + project/scripts/ui/search.gd | 43 +++++----- project/scripts/ui/search/prompt.gd | 8 ++ .../scripts/ui/search/prompt_navigation.gd | 81 ++++++++++++++++++- project/scripts/ui/search_go.gd | 2 +- 7 files changed, 113 insertions(+), 37 deletions(-) diff --git a/project/resources/bookmarks.tres b/project/resources/bookmarks.tres index 8532331..3922f9d 100644 --- a/project/resources/bookmarks.tres +++ b/project/resources/bookmarks.tres @@ -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")]([]) diff --git a/project/scenes/components/search.tscn b/project/scenes/components/search.tscn index 90b6a52..127d004 100644 --- a/project/scenes/components/search.tscn +++ b/project/scenes/components/search.tscn @@ -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"] diff --git a/project/scripts/string_tools.gd b/project/scripts/string_tools.gd index 76612ac..1b4d742 100644 --- a/project/scripts/string_tools.gd +++ b/project/scripts/string_tools.gd @@ -14,4 +14,5 @@ static func to_alpha(text: String) -> String: result += " " last_is_alpha = false + result = result.strip_edges() return result diff --git a/project/scripts/ui/search.gd b/project/scripts/ui/search.gd index 716065c..987cb0b 100644 --- a/project/scripts/ui/search.gd +++ b/project/scripts/ui/search.gd @@ -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() diff --git a/project/scripts/ui/search/prompt.gd b/project/scripts/ui/search/prompt.gd index 8dd951b..64dc895 100644 --- a/project/scripts/ui/search/prompt.gd +++ b/project/scripts/ui/search/prompt.gd @@ -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) diff --git a/project/scripts/ui/search/prompt_navigation.gd b/project/scripts/ui/search/prompt_navigation.gd index 2e3fa99..37e77d9 100644 --- a/project/scripts/ui/search/prompt_navigation.gd +++ b/project/scripts/ui/search/prompt_navigation.gd @@ -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() diff --git a/project/scripts/ui/search_go.gd b/project/scripts/ui/search_go.gd index 2651b3a..2b4c035 100644 --- a/project/scripts/ui/search_go.gd +++ b/project/scripts/ui/search_go.gd @@ -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