From b2ac12c4ed5be44bdbdd7016a8fbbb4ec83a5d4f Mon Sep 17 00:00:00 2001 From: Sebastian Morr Date: Mon, 21 Sep 2020 15:36:09 +0200 Subject: [PATCH] Pull out TextEditor scene, it handles the TCP server --- main.gd | 37 ------------------------------------- main.tscn | 29 ++--------------------------- terminal.gd | 2 -- text_editor.gd | 38 ++++++++++++++++++++++++++++++++++++++ text_editor.tscn | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 72 insertions(+), 66 deletions(-) create mode 100644 text_editor.tscn diff --git a/main.gd b/main.gd index ea4e2b2..7be5fde 100644 --- a/main.gd +++ b/main.gd @@ -2,8 +2,6 @@ extends Control var dragged = null -var server -var client_connection var current_level = 0 export(NodePath) var terminal_path @@ -39,10 +37,6 @@ func _ready(): level_select.theme = Theme.new() level_select.theme.default_font = load("res://fonts/default.tres") - # Initialize TCP server for fake editor. - server = TCP_Server.new() - server.listen(1234) - # Load first level. load_level(0) input.grab_focus() @@ -169,37 +163,6 @@ func construct_repo(script_content, path): game.global_shell.run("git symbolic-ref HEAD refs/heads/main") game.global_shell.run("sh "+script_path) -func _process(_delta): - if server.is_connection_available(): - client_connection = server.take_connection() - var length = client_connection.get_u8() - var filename = client_connection.get_string(length) - var regex = RegEx.new() - regex.compile("(\\.git\\/.*)") - filename = regex.search(filename).get_string() - read_message(filename) - -func read_message(filename): - $TextEditor.show() - input.editable = false - var fixme_path = game.tmp_prefix+"/active/" - var content = game.read_file(fixme_path+filename, "[ERROR_FAKE_EDITOR]") - if content == "[ERROR_FAKE_EDITOR]": - push_error("file specified by fake-editor could not be read.") - get_tree().quit() - $TextEditor.text = content - $TextEditor.path = filename - $TextEditor.grab_focus() - -func save_message(): - var fixme_path = game.tmp_prefix+"/active/" - game.write_file(fixme_path+$TextEditor.path, $TextEditor.text) - client_connection.disconnect_from_host() - input.editable = true - $TextEditor.text = "" - $TextEditor.hide() - input.grab_focus() - func show_win_status(): next_level_button.show() level_description.hide() diff --git a/main.tscn b/main.tscn index ef4e565..ea60b8f 100644 --- a/main.tscn +++ b/main.tscn @@ -5,7 +5,7 @@ [ext_resource path="res://repository.tscn" type="PackedScene" id=3] [ext_resource path="res://styles/alert_button.tres" type="StyleBox" id=4] [ext_resource path="res://fonts/default.tres" type="DynamicFont" id=5] -[ext_resource path="res://text_editor.gd" type="Script" id=6] +[ext_resource path="res://text_editor.tscn" type="PackedScene" id=6] [ext_resource path="res://fonts/big.tres" type="DynamicFont" id=7] [ext_resource path="res://tcp_server.gd" type="Script" id=8] [ext_resource path="res://test.gd" type="Script" id=9] @@ -180,32 +180,8 @@ size_flags_vertical = 3 [node name="Terminal" parent="HBoxContainer/RightSide/InputArea" instance=ExtResource( 1 )] repository_path = NodePath("../../../Repositories/ActiveRepository") -[node name="TextEditor" type="TextEdit" parent="HBoxContainer/RightSide/InputArea"] +[node name="TextEditor" parent="HBoxContainer/RightSide/InputArea" instance=ExtResource( 6 )] visible = false -anchor_right = 1.0 -anchor_bottom = 1.0 -custom_fonts/font = ExtResource( 5 ) -custom_colors/background_color = Color( 0, 0, 0, 1 ) -syntax_highlighting = true -script = ExtResource( 6 ) -__meta__ = { -"_edit_use_anchors_": false -} - -[node name="SaveButton" type="Button" parent="HBoxContainer/RightSide/InputArea/TextEditor"] -anchor_left = 1.0 -anchor_top = 1.0 -anchor_right = 1.0 -anchor_bottom = 1.0 -margin_left = -117.279 -margin_top = -59.399 -margin_right = -17.2788 -margin_bottom = -14.399 -custom_fonts/font = ExtResource( 5 ) -text = "Save" -__meta__ = { -"_edit_use_anchors_": false -} [node name="Test" type="Node2D" parent="."] visible = false @@ -227,6 +203,5 @@ __meta__ = { [connection signal="button_down" from="HBoxContainer/RightSide/Menu/LevelSelect" to="." method="repopulate_levels"] [connection signal="pressed" from="HBoxContainer/RightSide/Menu/ReloadButton" to="." method="reload_level"] [connection signal="pressed" from="HBoxContainer/RightSide/Menu/NextLevelButton" to="." method="load_next_level"] -[connection signal="pressed" from="HBoxContainer/RightSide/InputArea/TextEditor/SaveButton" to="." method="save_message"] [connection signal="data_received" from="Test/TCPServer" to="Test" method="data"] [connection signal="text_entered" from="Test/LineEdit" to="Test" method="send"] diff --git a/terminal.gd b/terminal.gd index e6e405c..ad9a569 100644 --- a/terminal.gd +++ b/terminal.gd @@ -27,8 +27,6 @@ func _ready(): command_dropdown.get_popup().connect("id_pressed", self, "load_command") command_dropdown.theme = Theme.new() command_dropdown.theme.default_font = load("res://fonts/default.tres") - - print(main) func _input(event): if event is InputEventKey and not text_editor.visible: diff --git a/text_editor.gd b/text_editor.gd index db6fa16..56e1ad1 100644 --- a/text_editor.gd +++ b/text_editor.gd @@ -1,3 +1,41 @@ extends TextEdit var path + +var _server +var _client_connection + +func _ready(): + # Initialize TCP server for fake editor. + _server = TCP_Server.new() + _server.listen(1234) + +func _process(_delta): + if _server.is_connection_available(): + _client_connection = _server.take_connection() + var length = _client_connection.get_u8() + var filename = _client_connection.get_string(length) + var regex = RegEx.new() + regex.compile("(\\.git\\/.*)") + filename = regex.search(filename).get_string() + open(filename) + +func open(filename): + path = filename + + var fixme_path = game.tmp_prefix+"/active/" + var content = game.read_file(fixme_path+filename, "[ERROR_FAKE_EDITOR]") + if content == "[ERROR_FAKE_EDITOR]": + push_error("Specified file could not be read.") + get_tree().quit() + text = content + + show() + grab_focus() + +func save(): + var fixme_path = game.tmp_prefix+"/active/" + game.write_file(fixme_path+path, text) + _client_connection.disconnect_from_host() + text = "" + hide() diff --git a/text_editor.tscn b/text_editor.tscn new file mode 100644 index 0000000..f75acaa --- /dev/null +++ b/text_editor.tscn @@ -0,0 +1,32 @@ +[gd_scene load_steps=3 format=2] + +[ext_resource path="res://fonts/default.tres" type="DynamicFont" id=1] +[ext_resource path="res://text_editor.gd" type="Script" id=2] + +[node name="TextEditor" type="TextEdit"] +anchor_right = 1.0 +anchor_bottom = 1.0 +custom_fonts/font = ExtResource( 1 ) +custom_colors/background_color = Color( 0, 0, 0, 1 ) +text = "Text here" +syntax_highlighting = true +script = ExtResource( 2 ) +__meta__ = { +"_edit_use_anchors_": false +} + +[node name="SaveButton" type="Button" parent="."] +anchor_left = 1.0 +anchor_top = 1.0 +anchor_right = 1.0 +anchor_bottom = 1.0 +margin_left = -117.279 +margin_top = -59.399 +margin_right = -17.2788 +margin_bottom = -14.399 +custom_fonts/font = ExtResource( 1 ) +text = "Save" +__meta__ = { +"_edit_use_anchors_": false +} +[connection signal="pressed" from="SaveButton" to="." method="save"]