Pull out TextEditor scene, it handles the TCP server

This commit is contained in:
Sebastian Morr 2020-09-21 15:36:09 +02:00
parent 260beefbe9
commit b2ac12c4ed
5 changed files with 72 additions and 66 deletions

37
main.gd
View file

@ -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()

View file

@ -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"]

View file

@ -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:

View file

@ -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()

32
text_editor.tscn Normal file
View file

@ -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"]