From bffe8a330f64551019b340050eeaa3fc5693f55c Mon Sep 17 00:00:00 2001 From: Nordup Date: Sun, 13 Aug 2023 12:44:51 +0400 Subject: [PATCH] search prompt (wip) --- project/resources/api_settings.tres | 2 +- project/scenes/components/prompt.tscn | 84 +++++++++++++++++++++ project/scenes/components/search.tscn | 66 +++++++++++++++- project/scripts/resources/api_settings.gd | 3 + project/scripts/string_tools.gd | 17 +++++ project/scripts/ui/search/prompt.gd | 18 +++++ project/scripts/ui/search/prompt_results.gd | 44 +++++++++++ project/scripts/ui/search/search_results.gd | 2 +- project/scripts/ui/top_level.gd | 6 ++ 9 files changed, 237 insertions(+), 5 deletions(-) create mode 100644 project/scenes/components/prompt.tscn create mode 100644 project/scripts/string_tools.gd create mode 100644 project/scripts/ui/search/prompt.gd create mode 100644 project/scripts/ui/search/prompt_results.gd create mode 100644 project/scripts/ui/top_level.gd diff --git a/project/resources/api_settings.tres b/project/resources/api_settings.tres index 19e975a..608528f 100644 --- a/project/resources/api_settings.tres +++ b/project/resources/api_settings.tres @@ -5,5 +5,5 @@ [resource] script = ExtResource("1_oiju7") local_url = "http://127.0.0.1:8000" -remote_url = "http://95.163.241.188:8000" +remote_url = "http://95.163.240.240:8000" host_type = 1 diff --git a/project/scenes/components/prompt.tscn b/project/scenes/components/prompt.tscn new file mode 100644 index 0000000..cc1607e --- /dev/null +++ b/project/scenes/components/prompt.tscn @@ -0,0 +1,84 @@ +[gd_scene load_steps=8 format=3 uid="uid://b57n6cvtqn5b7"] + +[ext_resource type="Script" path="res://scripts/ui/search/prompt.gd" id="1_7xv44"] +[ext_resource type="Resource" uid="uid://b1xvdym0qh6td" path="res://resources/gate_events.res" id="2_33m26"] +[ext_resource type="Texture2D" uid="uid://c28surtqjf4st" path="res://textures/search.svg" id="2_tk8hn"] + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_wbjg7"] +bg_color = Color(0.25098, 0.25098, 0.25098, 0) +corner_radius_top_left = 6 +corner_radius_top_right = 6 +corner_radius_bottom_right = 6 +corner_radius_bottom_left = 6 + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_srt8r"] +bg_color = Color(0, 0, 0, 0.25098) +corner_radius_top_left = 6 +corner_radius_top_right = 6 +corner_radius_bottom_right = 6 +corner_radius_bottom_left = 6 + +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_awixv"] +bg_color = Color(0, 0, 0, 0.25098) +corner_radius_top_left = 6 +corner_radius_top_right = 6 +corner_radius_bottom_right = 6 +corner_radius_bottom_left = 6 + +[sub_resource type="StyleBoxEmpty" id="StyleBoxEmpty_6fg0o"] +content_margin_left = 40.0 + +[node name="prompt" type="Button" node_paths=PackedStringArray("prompt_text")] +custom_minimum_size = Vector2(0, 40) +anchors_preset = 14 +anchor_top = 0.5 +anchor_right = 1.0 +anchor_bottom = 0.5 +offset_top = -20.0 +offset_bottom = 20.0 +grow_horizontal = 2 +grow_vertical = 2 +theme_override_styles/normal = SubResource("StyleBoxFlat_wbjg7") +theme_override_styles/hover = SubResource("StyleBoxFlat_srt8r") +theme_override_styles/pressed = SubResource("StyleBoxFlat_awixv") +script = ExtResource("1_7xv44") +gate_events = ExtResource("2_33m26") +prompt_text = NodePath("Label") + +[node name="Label" type="Label" parent="."] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +theme_override_font_sizes/font_size = 20 +theme_override_styles/normal = SubResource("StyleBoxEmpty_6fg0o") +text = "prompt" +vertical_alignment = 1 + +[node name="SearchStatus" type="Control" parent="."] +self_modulate = Color(1, 1, 1, 0.8) +layout_mode = 1 +anchors_preset = 4 +anchor_top = 0.5 +anchor_bottom = 0.5 +offset_left = 12.0 +offset_top = -8.0 +offset_right = 28.0 +offset_bottom = 8.0 +grow_vertical = 2 +mouse_filter = 1 + +[node name="Search" type="TextureRect" parent="SearchStatus"] +self_modulate = Color(1, 1, 1, 0.8) +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +texture = ExtResource("2_tk8hn") +expand_mode = 1 + +[connection signal="pressed" from="." to="." method="_on_button_pressed"] diff --git a/project/scenes/components/search.tscn b/project/scenes/components/search.tscn index aa0f104..b6e5767 100644 --- a/project/scenes/components/search.tscn +++ b/project/scenes/components/search.tscn @@ -1,4 +1,4 @@ -[gd_scene load_steps=15 format=3 uid="uid://ctam0fxigbefk"] +[gd_scene load_steps=20 format=3 uid="uid://ctam0fxigbefk"] [ext_resource type="Script" path="res://scripts/ui/search.gd" id="1_7ivk2"] [ext_resource type="Resource" uid="uid://b1xvdym0qh6td" path="res://resources/gate_events.res" id="2_13io8"] @@ -13,6 +13,10 @@ [ext_resource type="Texture2D" uid="uid://dystd8vvbqwo2" path="res://textures/exit.svg" id="7_jllve"] [ext_resource type="Texture2D" uid="uid://btn6ytky782a2" path="res://textures/star.svg" id="8_b00p6"] [ext_resource type="Texture2D" uid="uid://lxx36xkotvh1" path="res://textures/star_filled.svg" id="9_2en0l"] +[ext_resource type="Script" path="res://scripts/ui/search/prompt_results.gd" id="14_0lofv"] +[ext_resource type="Script" path="res://scripts/ui/top_level.gd" id="14_5k27g"] +[ext_resource type="Resource" uid="uid://cjcdum6fm4ta0" path="res://resources/api_settings.tres" id="15_uafyh"] +[ext_resource type="PackedScene" uid="uid://b57n6cvtqn5b7" path="res://scenes/components/prompt.tscn" id="16_lbcsd"] [sub_resource type="StyleBoxFlat" id="StyleBoxFlat_hyysn"] content_margin_left = 40.0 @@ -29,6 +33,21 @@ corner_radius_bottom_right = 6 corner_radius_bottom_left = 6 corner_detail = 10 +[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_mh73y"] +content_margin_left = 40.0 +content_margin_right = 40.0 +bg_color = Color(0.207843, 0.164706, 0.47451, 1) +border_width_left = 1 +border_width_top = 1 +border_width_right = 1 +border_width_bottom = 1 +border_color = Color(0.176471, 0.14902, 0.352941, 1) +corner_radius_top_left = 6 +corner_radius_top_right = 6 +corner_radius_bottom_right = 6 +corner_radius_bottom_left = 6 +corner_detail = 10 + [node name="Search" type="LineEdit"] custom_minimum_size = Vector2(1000, 40) anchors_preset = 8 @@ -37,13 +56,14 @@ anchor_top = 0.5 anchor_right = 0.5 anchor_bottom = 0.5 offset_left = -500.0 -offset_top = -24.5 +offset_top = -20.0 offset_right = 500.0 -offset_bottom = 15.5 +offset_bottom = 20.0 grow_horizontal = 2 grow_vertical = 2 auto_translate = false localize_numeral_system = false +focus_next = NodePath("Prompt") focus_mode = 1 theme_override_colors/font_color = Color(1, 1, 1, 0.8) theme_override_colors/font_placeholder_color = Color(1, 1, 1, 0.498039) @@ -131,6 +151,8 @@ _import_path = NodePath("") unique_name_in_owner = false process_mode = 0 process_priority = 0 +process_physics_priority = 0 +process_thread_group = 0 editor_description = "" visible = false modulate = Color(1, 1, 1, 1) @@ -230,6 +252,8 @@ _import_path = NodePath("") unique_name_in_owner = false process_mode = 0 process_priority = 0 +process_physics_priority = 0 +process_thread_group = 0 editor_description = "" visible = true modulate = Color(1, 1, 1, 1) @@ -309,6 +333,8 @@ _import_path = NodePath("") unique_name_in_owner = false process_mode = 0 process_priority = 0 +process_physics_priority = 0 +process_thread_group = 0 editor_description = "" visible = false modulate = Color(1, 1, 1, 1) @@ -383,8 +409,42 @@ grow_vertical = 2 texture = ExtResource("9_2en0l") expand_mode = 1 +[node name="Prompt" type="Control" parent="."] +layout_mode = 1 +anchors_preset = 2 +anchor_top = 1.0 +anchor_bottom = 1.0 +grow_vertical = 0 + +[node name="Panel" type="Panel" parent="Prompt"] +top_level = true +layout_mode = 1 +anchors_preset = 5 +anchor_left = 0.5 +anchor_right = 0.5 +offset_left = -500.0 +offset_top = 560.0 +offset_right = 500.0 +offset_bottom = 1060.0 +grow_horizontal = 2 +theme_override_styles/panel = SubResource("StyleBoxFlat_mh73y") +script = ExtResource("14_5k27g") + +[node name="VBoxContainer" type="VBoxContainer" parent="Prompt/Panel"] +layout_mode = 1 +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +script = ExtResource("14_0lofv") +gate_events = ExtResource("2_13io8") +api = ExtResource("15_uafyh") +result_scene = ExtResource("16_lbcsd") + [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"] [connection signal="pressed" from="Go" to="." method="_on_go_pressed"] [connection signal="pressed" from="Bookmark/Star" to="Bookmark" method="_on_star_pressed"] diff --git a/project/scripts/resources/api_settings.gd b/project/scripts/resources/api_settings.gd index cb7aef0..1dce1a5 100644 --- a/project/scripts/resources/api_settings.gd +++ b/project/scripts/resources/api_settings.gd @@ -24,3 +24,6 @@ var discover_gate: String : var search: String : get: return url + "/api/search?query=" + +var prompt: String : + get: return url + "/api/prompt?query=" diff --git a/project/scripts/string_tools.gd b/project/scripts/string_tools.gd new file mode 100644 index 0000000..f4d4240 --- /dev/null +++ b/project/scripts/string_tools.gd @@ -0,0 +1,17 @@ +extends Node +class_name StringTools + + +static func to_alpha(text: String) -> String: + var clean_text = "" + var last_is_char = false + + for char in text: + if (char >= 'a' and char <= 'z') or (char >= 'A' and char <= 'Z'): + clean_text += char + last_is_char = true + elif last_is_char: + clean_text += " " + last_is_char = false + + return clean_text diff --git a/project/scripts/ui/search/prompt.gd b/project/scripts/ui/search/prompt.gd new file mode 100644 index 0000000..8dd951b --- /dev/null +++ b/project/scripts/ui/search/prompt.gd @@ -0,0 +1,18 @@ +extends Control +class_name PromptResult + +@export var gate_events: GateEvents +@export var prompt_text: Label + + +func fill(prompt: Dictionary) -> void: + if prompt == null: return + + var text: String = prompt["prompt"].to_lower() + text = StringTools.to_alpha(text) + prompt_text.text = text + + +func _on_button_pressed() -> void: + if prompt_text.text.is_empty(): return + gate_events.search_emit(prompt_text.text) diff --git a/project/scripts/ui/search/prompt_results.gd b/project/scripts/ui/search/prompt_results.gd new file mode 100644 index 0000000..eee9fea --- /dev/null +++ b/project/scripts/ui/search/prompt_results.gd @@ -0,0 +1,44 @@ +extends VBoxContainer + +@export var gate_events: GateEvents +@export var api: ApiSettings +@export var result_scene: PackedScene + +var result_str: String + + +func _ready() -> void: + pass + + +func _on_search_text_changed(query: String) -> void: + clear() + if query.is_empty(): return + + await prompt_request(query) + + var prompts = JSON.parse_string(result_str) + if prompts == null or prompts.is_empty(): + return + + for prompt in prompts: + var result: PromptResult = result_scene.instantiate() + result.fill(prompt) + add_child(result) + + +func prompt_request(query: String) -> void: + var url = api.prompt + query.uri_encode() + var callback = func(_result, code, _headers, body): + if code == 200: + result_str = body.get_string_from_utf8() + else: Debug.logclr("Request prompt failed. Code " + str(code), Color.RED) + + var err = await Backend.request(url, callback) + if err != HTTPRequest.RESULT_SUCCESS: Debug.logclr("Cannot send request prompt", Color.RED) + + +func clear() -> void: + for child in get_children(): + child.queue_free() + remove_child(child) diff --git a/project/scripts/ui/search/search_results.gd b/project/scripts/ui/search/search_results.gd index 9b40fbe..cec6904 100644 --- a/project/scripts/ui/search/search_results.gd +++ b/project/scripts/ui/search/search_results.gd @@ -27,7 +27,7 @@ func search(query: String) -> void: Debug.logr(gate["url"]) -func search_request(query: String): +func search_request(query: String) -> void: var url = api.search + query.uri_encode() var callback = func(_result, code, _headers, body): if code == 200: diff --git a/project/scripts/ui/top_level.gd b/project/scripts/ui/top_level.gd new file mode 100644 index 0000000..87ad307 --- /dev/null +++ b/project/scripts/ui/top_level.gd @@ -0,0 +1,6 @@ +extends Control + + +func _ready() -> void: + await get_tree().process_frame + global_position = get_parent().global_position