From 550a1c80a4b517974a01cdcb3c819d8e00cbcf54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lera=20Elvo=C3=A9?= <24468819+Yagich@users.noreply.github.com> Date: Sat, 25 Jun 2022 19:37:09 +0300 Subject: [PATCH] add server start functionality to ServerUI --- Main.gd | 21 ++++++++++++++++++--- Main.tscn | 3 +++ ServerUI.gd | 30 +++++++++++++++++++++++++++--- ServerUI.tscn | 2 ++ server_files/files.txt | 5 ++--- server_files/second.md | 2 +- 6 files changed, 53 insertions(+), 10 deletions(-) diff --git a/Main.gd b/Main.gd index 6b60a2d..ec5159f 100644 --- a/Main.gd +++ b/Main.gd @@ -4,11 +4,12 @@ var mime_types := MimeTypeHelper.generate_db() var _server: HTTPServer = null -var files: Array = [] +onready var server_ui := $ServerUI +onready var server_dir = server_ui.server_dir func _ready() -> void: - _start_server() + pass func _start_server(port: int = 3001) -> void: @@ -17,7 +18,7 @@ func _start_server(port: int = 3001) -> void: _server = HTTPServer.new() var dir := Directory.new() - if dir.open("res://server_files") == OK: + if dir.open(server_dir) == OK: if dir.list_dir_begin() != OK: # TODO: show error to user here return @@ -41,11 +42,16 @@ func _start_server(port: int = 3001) -> void: # TODO: show error to user here return + server_ui.is_server_running = true + func _stop_server() -> void: if _server: + _server.stop() _server = null + server_ui.is_server_running = false + func _process(_delta: float) -> void: if _server == null: @@ -77,3 +83,12 @@ func _serve_file(_request: HTTPServer.Request, response: HTTPServer.Response, bi else: response.header("content-type", "text/plain") response.data("500 - Read Error") + + +func _on_ServerUI_start_server_button_pressed(port: int, new_dir: String) -> void: + server_dir = new_dir + _start_server(port) + + +func _on_ServerUI_stop_server_button_pressed() -> void: + _stop_server() diff --git a/Main.tscn b/Main.tscn index d8a00da..b8e1662 100644 --- a/Main.tscn +++ b/Main.tscn @@ -9,3 +9,6 @@ anchor_bottom = 1.0 script = ExtResource( 1 ) [node name="ServerUI" parent="." instance=ExtResource( 2 )] + +[connection signal="start_server_button_pressed" from="ServerUI" to="." method="_on_ServerUI_start_server_button_pressed"] +[connection signal="stop_server_button_pressed" from="ServerUI" to="." method="_on_ServerUI_stop_server_button_pressed"] diff --git a/ServerUI.gd b/ServerUI.gd index fb1f7de..a4dc477 100644 --- a/ServerUI.gd +++ b/ServerUI.gd @@ -17,6 +17,7 @@ enum ContextMenuOptions { onready var server_path_label := $"%ServerPathLabel" onready var port_spin_box := $"%PortSpinBox" +onready var start_server_button := $"%StartServerButton" onready var server_status_label := $"%ServerStatusLabel" onready var document_title_lineedit := $"%DocTitleLineEdit" @@ -32,7 +33,8 @@ onready var server_folder_dialog := $"%ServerFolderDialog" signal server_folder_changed(new_path) # new_path: String signal server_port_changed(new_port) # new_port: int -signal start_server_button_pressed(port) # port: int +signal start_server_button_pressed(port, path) # port: int, path: String +signal stop_server_button_pressed() # emit from %StartServerButton, when the server is not running. signal open_browser_button_pressed(port) # port: int signal files_selection_changed(new_files) # new_files: Array @@ -147,7 +149,21 @@ func _on_OpenServerFolderButton_pressed() -> void: func set_server_running(running: bool) -> void: - is_server_running = running # TODO: logic for disabling and enabling certain nodes + is_server_running = running + if is_server_running: + server_status_label.text = SERVER_STATUS_TEXT.RUNNING + start_server_button.text = "Stop server" + port_spin_box.editable = false + document_date_lineedit.editable = false + document_title_lineedit.editable = false + document_input_textedit.readonly = true + else: + server_status_label.text = SERVER_STATUS_TEXT.NOT_RUNNING + start_server_button.text = "Start server" + port_spin_box.editable = !false + document_date_lineedit.editable = !false + document_title_lineedit.editable = !false + document_input_textedit.readonly = !true func _generate_filestxt(): @@ -163,7 +179,15 @@ func _generate_filestxt(): f.store_string(files) f.close() else: - push_error("File.txt open for save error!") + push_error("File.txt open for save error!") # TODO: show a user-facing error + + +func _on_StartServerButton_pressed() -> void: + if is_server_running: + emit_signal("stop_server_button_pressed") + else: + _generate_filestxt() + emit_signal("start_server_button_pressed", port_spin_box.value, server_dir) func _is_file_in_filestxt(path: String) -> bool: diff --git a/ServerUI.tscn b/ServerUI.tscn index 31d8177..c36a60e 100644 --- a/ServerUI.tscn +++ b/ServerUI.tscn @@ -62,6 +62,7 @@ max_value = 8000.0 value = 3001.0 [node name="StartServerButton" type="Button" parent="VBoxContainer/HBoxContainer"] +unique_name_in_owner = true margin_left = 693.0 margin_right = 778.0 margin_bottom = 32.0 @@ -246,6 +247,7 @@ rect_min_size = Vector2( 95, 56 ) items = [ "Move Up", null, 0, false, false, 0, 0, null, "", false, "Move Down", null, 0, false, false, 1, 0, null, "", false ] [connection signal="pressed" from="VBoxContainer/HBoxContainer/OpenServerFolderButton" to="." method="_on_OpenServerFolderButton_pressed"] +[connection signal="pressed" from="VBoxContainer/HBoxContainer/StartServerButton" to="." method="_on_StartServerButton_pressed"] [connection signal="item_edited" from="VBoxContainer/HSplitContainer/VBoxContainer2/FileTree" to="." method="_on_FileTree_item_edited"] [connection signal="item_rmb_selected" from="VBoxContainer/HSplitContainer/VBoxContainer2/FileTree" to="." method="_on_FileTree_item_rmb_selected"] [connection signal="item_selected" from="VBoxContainer/HSplitContainer/VBoxContainer2/FileTree" to="." method="_on_FileTree_item_selected"] diff --git a/server_files/files.txt b/server_files/files.txt index 1092b00..25e7634 100644 --- a/server_files/files.txt +++ b/server_files/files.txt @@ -1,3 +1,2 @@ -first.md 2022-01-06 first article -second.md 2021-05-03 second article -second.md +first.md 2022-03-03 first title +second.md 2022-03-05 second title diff --git a/server_files/second.md b/server_files/second.md index 32735f0..35db2d7 100644 --- a/server_files/second.md +++ b/server_files/second.md @@ -17,4 +17,4 @@ Image: code_block! ``` -`code not block` +`code not block` \ No newline at end of file