From 9b867c66521ea609e8053bb59d4f15f25bc5101a Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Fri, 8 Jul 2022 00:57:46 -0400 Subject: [PATCH] add GUI to get player info --- godot/monitor/EnterButton.gd | 16 +++ godot/monitor/GUI.tscn | 238 +++++++++++++++++++++++++++++++++++ godot/monitor/Monitor.gd | 64 +++++++++- godot/monitor/Monitor.tscn | 5 +- godot/monitor/Rut.gd | 13 ++ godot/monitor/Text.gd | 14 +++ 6 files changed, 344 insertions(+), 6 deletions(-) create mode 100644 godot/monitor/EnterButton.gd create mode 100644 godot/monitor/GUI.tscn create mode 100644 godot/monitor/Rut.gd create mode 100644 godot/monitor/Text.gd diff --git a/godot/monitor/EnterButton.gd b/godot/monitor/EnterButton.gd new file mode 100644 index 0000000..2430822 --- /dev/null +++ b/godot/monitor/EnterButton.gd @@ -0,0 +1,16 @@ +extends Button + + +signal input_validated(player) + + +func _on_Button_pressed() -> void: + var name = get_node("%Name") + var rut = get_node("%Rut") + var email = get_node("%Email") + var player: Dictionary = { + "name" : name.text, + "rut" : rut.text, + "email" : email.text + } + emit_signal("input_validated", player) diff --git a/godot/monitor/GUI.tscn b/godot/monitor/GUI.tscn new file mode 100644 index 0000000..85a6faa --- /dev/null +++ b/godot/monitor/GUI.tscn @@ -0,0 +1,238 @@ +[gd_scene load_steps=18 format=2] + +[ext_resource path="res://assets/fonts/data/PixelOperator.tres" type="DynamicFontData" id=1] +[ext_resource path="res://assets/fonts/data/PixelOperator-Bold.tres" type="DynamicFontData" id=2] +[ext_resource path="res://monitor/Rut.gd" type="Script" id=3] +[ext_resource path="res://monitor/Text.gd" type="Script" id=4] +[ext_resource path="res://monitor/EnterButton.gd" type="Script" id=5] + +[sub_resource type="DynamicFont" id=1] +font_data = ExtResource( 1 ) + +[sub_resource type="DynamicFont" id=2] +size = 12 +font_data = ExtResource( 2 ) + +[sub_resource type="DynamicFont" id=3] +size = 12 +font_data = ExtResource( 2 ) + +[sub_resource type="DynamicFont" id=4] +size = 12 +font_data = ExtResource( 2 ) + +[sub_resource type="DynamicFont" id=9] +size = 8 +extra_spacing_top = 2 +font_data = ExtResource( 1 ) + +[sub_resource type="StyleBoxFlat" id=5] +content_margin_left = 5.0 +content_margin_right = 5.0 +bg_color = Color( 0.313726, 0.290196, 0.290196, 0.752941 ) +border_blend = true +corner_radius_top_left = 5 +corner_radius_top_right = 5 +corner_radius_bottom_right = 5 +corner_radius_bottom_left = 5 + +[sub_resource type="DynamicFont" id=10] +size = 8 +extra_spacing_top = 2 +font_data = ExtResource( 1 ) + +[sub_resource type="StyleBoxFlat" id=6] +content_margin_left = 5.0 +content_margin_right = 5.0 +bg_color = Color( 0.313726, 0.290196, 0.290196, 0.752941 ) +corner_radius_top_left = 5 +corner_radius_top_right = 5 +corner_radius_bottom_right = 5 +corner_radius_bottom_left = 5 + +[sub_resource type="DynamicFont" id=8] +size = 8 +extra_spacing_top = 2 +font_data = ExtResource( 1 ) + +[sub_resource type="StyleBoxFlat" id=7] +content_margin_left = 5.0 +content_margin_right = 5.0 +bg_color = Color( 0.313726, 0.290196, 0.290196, 0.752941 ) +corner_radius_top_left = 5 +corner_radius_top_right = 5 +corner_radius_bottom_right = 5 +corner_radius_bottom_left = 5 + +[sub_resource type="DynamicFont" id=11] +size = 12 +extra_spacing_top = 2 +extra_spacing_bottom = 2 +extra_spacing_char = 2 +extra_spacing_space = 2 +font_data = ExtResource( 2 ) + +[sub_resource type="StyleBoxFlat" id=12] +content_margin_left = 5.0 +content_margin_right = 5.0 +content_margin_top = 5.0 +content_margin_bottom = 5.0 +bg_color = Color( 0.313726, 0.290196, 0.290196, 0.752941 ) +corner_radius_top_left = 5 +corner_radius_top_right = 5 +corner_radius_bottom_right = 5 +corner_radius_bottom_left = 5 + +[node name="MonitorGUI" type="CanvasLayer"] +pause_mode = 2 + +[node name="ColorRect" type="ColorRect" parent="."] +margin_right = 40.0 +margin_bottom = 40.0 +rect_min_size = Vector2( 512, 288 ) +color = Color( 0.0117647, 0.00784314, 0.00784314, 0.376471 ) + +[node name="GUI" type="Control" parent="."] +anchor_right = 1.0 +anchor_bottom = 1.0 +rect_min_size = Vector2( 512, 0 ) + +[node name="VBoxContainer" type="VBoxContainer" parent="GUI"] +anchor_right = 1.0 +anchor_bottom = 1.0 +size_flags_horizontal = 3 +size_flags_vertical = 3 + +[node name="CenterContainer" type="CenterContainer" parent="GUI/VBoxContainer"] +margin_right = 512.0 +margin_bottom = 96.0 +rect_min_size = Vector2( 0, 96 ) +size_flags_horizontal = 3 + +[node name="Label" type="Label" parent="GUI/VBoxContainer/CenterContainer"] +margin_top = 30.0 +margin_right = 512.0 +margin_bottom = 65.0 +rect_min_size = Vector2( 512, 0 ) +custom_fonts/font = SubResource( 1 ) +text = "Ingresa su nombre completo, RUT y email por favor!" +align = 1 +autowrap = true + +[node name="CenterContainer2" type="CenterContainer" parent="GUI/VBoxContainer"] +margin_top = 100.0 +margin_right = 512.0 +margin_bottom = 196.0 +rect_min_size = Vector2( 0, 96 ) +size_flags_horizontal = 3 +size_flags_vertical = 3 + +[node name="HBoxContainer" type="HBoxContainer" parent="GUI/VBoxContainer/CenterContainer2"] +margin_left = 6.0 +margin_top = 14.0 +margin_right = 506.0 +margin_bottom = 82.0 +rect_min_size = Vector2( 500, 0 ) +size_flags_horizontal = 13 +size_flags_vertical = 13 + +[node name="VBoxContainer" type="VBoxContainer" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer"] +margin_right = 74.0 +margin_bottom = 68.0 +size_flags_vertical = 3 + +[node name="Label" type="Label" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer"] +margin_top = 3.0 +margin_right = 74.0 +margin_bottom = 16.0 +size_flags_vertical = 6 +custom_fonts/font = SubResource( 2 ) +text = "Nombre" +align = 2 + +[node name="Label2" type="Label" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer"] +margin_top = 27.0 +margin_right = 74.0 +margin_bottom = 40.0 +size_flags_vertical = 6 +custom_fonts/font = SubResource( 3 ) +text = "RUT" +align = 2 + +[node name="Label3" type="Label" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer"] +margin_top = 51.0 +margin_right = 74.0 +margin_bottom = 64.0 +size_flags_vertical = 6 +custom_fonts/font = SubResource( 4 ) +text = "Email" +align = 2 + +[node name="VBoxContainer2" type="VBoxContainer" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer"] +margin_left = 78.0 +margin_right = 500.0 +margin_bottom = 68.0 +size_flags_horizontal = 3 + +[node name="Name" type="TextEdit" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2"] +unique_name_in_owner = true +margin_right = 422.0 +margin_bottom = 20.0 +rect_min_size = Vector2( 300, 20 ) +size_flags_horizontal = 3 +size_flags_vertical = 3 +custom_fonts/font = SubResource( 9 ) +custom_styles/normal = SubResource( 5 ) +text = "Christopher Barry Cromer" +caret_blink = true +script = ExtResource( 4 ) + +[node name="Rut" type="TextEdit" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2"] +unique_name_in_owner = true +margin_top = 24.0 +margin_right = 422.0 +margin_bottom = 44.0 +rect_min_size = Vector2( 0, 20 ) +custom_fonts/font = SubResource( 10 ) +custom_styles/normal = SubResource( 6 ) +text = "23.660.457-8" +caret_blink = true +script = ExtResource( 3 ) + +[node name="Email" type="TextEdit" parent="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2"] +unique_name_in_owner = true +margin_top = 48.0 +margin_right = 422.0 +margin_bottom = 68.0 +rect_min_size = Vector2( 0, 20 ) +custom_fonts/font = SubResource( 8 ) +custom_styles/normal = SubResource( 7 ) +text = "christohper.cromer1501@alumnos.ubiobio.cl" +caret_blink = true +script = ExtResource( 4 ) + +[node name="CenterContainer3" type="CenterContainer" parent="GUI/VBoxContainer"] +margin_top = 200.0 +margin_right = 512.0 +margin_bottom = 296.0 +rect_min_size = Vector2( 0, 96 ) +size_flags_horizontal = 3 +size_flags_vertical = 3 + +[node name="Button" type="Button" parent="GUI/VBoxContainer/CenterContainer3"] +margin_left = 197.0 +margin_top = 34.0 +margin_right = 314.0 +margin_bottom = 61.0 +focus_mode = 0 +custom_fonts/font = SubResource( 11 ) +custom_styles/normal = SubResource( 12 ) +enabled_focus_mode = 0 +text = "Ingresar" +script = ExtResource( 5 ) + +[connection signal="text_changed" from="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Name" to="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Name" method="_on_text_changed"] +[connection signal="focus_exited" from="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Rut" to="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Rut" method="_on_Rut_focus_exited"] +[connection signal="text_changed" from="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Rut" to="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Rut" method="_on_Rut_text_changed"] +[connection signal="text_changed" from="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Email" to="GUI/VBoxContainer/CenterContainer2/HBoxContainer/VBoxContainer2/Email" method="_on_text_changed"] +[connection signal="pressed" from="GUI/VBoxContainer/CenterContainer3/Button" to="GUI/VBoxContainer/CenterContainer3/Button" method="_on_Button_pressed"] diff --git a/godot/monitor/Monitor.gd b/godot/monitor/Monitor.gd index 391df9b..9a4e6f8 100644 --- a/godot/monitor/Monitor.gd +++ b/godot/monitor/Monitor.gd @@ -49,9 +49,9 @@ var game: Dictionary = {} func _ready() -> void: game_version = get_parent().game_version - player["rut"] = "23.660.457-8" - player["name"] = "Chris Cromer" - player["email"] = "chris@cromer.cl" + player["rut"] = "" + player["name"] = "" + player["email"] = "" var os_name = OS.get_name() if os_name == "Android": @@ -94,6 +94,10 @@ func _ready() -> void: if err != OK: print(err) + err = $MonitorGUI.find_node("Button").connect("input_validated", self, "_on_input_validated") + if err != OK: + print(err) + func _physics_process(_delta: float) -> void: if enabled: @@ -117,6 +121,12 @@ func _physics_process(_delta: float) -> void: start_monitor() +func _on_input_validated(validated_player: Dictionary) -> void: + $MonitorGUI.queue_free() + get_tree().paused = false + player = validated_player.duplicate(true) + + func _object_created(name: String, state: String, position: Vector2, velocity: Vector2) -> void: if enabled and started: add_object(name, state, position, velocity) @@ -211,9 +221,57 @@ func clean_rut(rut: String) -> String: rut = rut.to_lower() rut = rut.replace(".", "") rut = rut.replace("-", "") + + var rutTemp: String = rut.substr(0, rut.length() - 1) + var verifier: String = rut.substr(rut.length() - 1, 1) + + var regex = RegEx.new() + regex.compile("\\D") + rutTemp = regex.sub(rutTemp, "", true) + + regex.compile("[^kK\\d]") + verifier = regex.sub(verifier, "", true) + + rut = rutTemp + verifier + return rut +func pretty_rut(rut: String) -> String: + rut = clean_rut(rut) + + var rutTemp: String = rut.substr(0, rut.length() - 1) + var verifier: String = rut.substr(rut.length() - 1, 1) + + var regex = RegEx.new() + regex.compile("[^kK\\d]") + verifier = regex.sub(verifier, "", true) + + var byteArray = rutTemp.to_utf8() + byteArray.invert() + + var newByteArray: PoolByteArray = PoolByteArray() + var i = 1 + for symbol in byteArray: + newByteArray.append(symbol) + if i == 3: + newByteArray.append(".".to_utf8()[0]) + i = 0 + i = i + 1 + if newByteArray.size() > 0 and newByteArray[newByteArray.size() - 1] == ".".to_utf8()[0]: + newByteArray.resize(newByteArray.size() - 1) + + newByteArray.invert() + rutTemp = newByteArray.get_string_from_utf8() + + if rutTemp.length() == 0 and verifier.length() > 0: + rutTemp = verifier + elif rutTemp.length() > 0 and verifier.length() > 0: + rutTemp = rutTemp + "-" + verifier + + return rutTemp + + func is_rut_valid(rut: String) -> bool: rut = clean_rut(rut) if rut.length() < 8 or rut.length() > 9: diff --git a/godot/monitor/Monitor.tscn b/godot/monitor/Monitor.tscn index c2280ca..a50b176 100644 --- a/godot/monitor/Monitor.tscn +++ b/godot/monitor/Monitor.tscn @@ -1,13 +1,12 @@ [gd_scene load_steps=3 format=2] [ext_resource path="res://monitor/Monitor.gd" type="Script" id=1] -[ext_resource path="res://monitor/Player.gd" type="Script" id=2] +[ext_resource path="res://monitor/GUI.tscn" type="PackedScene" id=3] [node name="Monitor" type="Node"] script = ExtResource( 1 ) use_development_url = true -[node name="Player" type="Node" parent="."] -script = ExtResource( 2 ) +[node name="MonitorGUI" parent="." instance=ExtResource( 3 )] [node name="HTTPRequest" type="HTTPRequest" parent="."] diff --git a/godot/monitor/Rut.gd b/godot/monitor/Rut.gd new file mode 100644 index 0000000..ea7339c --- /dev/null +++ b/godot/monitor/Rut.gd @@ -0,0 +1,13 @@ +extends TextEdit + + +func _on_Rut_text_changed() -> void: + _on_Rut_focus_exited() + + +func _on_Rut_focus_exited() -> void: + var monitor = get_tree().get_current_scene().get_node("Monitor") + if text.length() > 12: + text = text.substr(0, 12) + text = monitor.pretty_rut(text) + cursor_set_column(text.length()) diff --git a/godot/monitor/Text.gd b/godot/monitor/Text.gd new file mode 100644 index 0000000..7109c78 --- /dev/null +++ b/godot/monitor/Text.gd @@ -0,0 +1,14 @@ +extends TextEdit + + +onready var previousText: String = text + + +func _on_text_changed() -> void: + var col = cursor_get_column() + # if a scroll bar appears, reset to the previous good string + if text.length() > 50: + text = previousText + col = col - 1 + previousText = text + cursor_set_column(col)