Skip to content

Commit

Permalink
lunaportal gui
Browse files Browse the repository at this point in the history
  • Loading branch information
manglemix committed Apr 17, 2024
1 parent 31f522a commit 55a93f4
Show file tree
Hide file tree
Showing 5 changed files with 247 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lunabase/lunabase.tscn
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ var connected := false


func _ready() -> void:
connected = Lunabot.is_lunabot_connected()
Lunabot.connected.connect(func(): connected = true)
Lunabot.disconnected.connect(func(): connected = false)
Lunabot.connected.connect($Connected.play)
Expand All @@ -35,6 +36,8 @@ script/source = "extends Label


func _ready() -> void:
if Lunabot.is_lunabot_connected():
text = \"Connected\"
Lunabot.disconnected.connect(func():
text = \"Not Connected\"
)
Expand Down
55 changes: 55 additions & 0 deletions lunabase/lunaportal.gd
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
extends Node


signal log_received(String)
signal portal_disconnected
signal portal_connected

var server_addr := "192.168.0.100"
var ws: WebSocketPeer
var logs: Array[String] = []
var connecting := false
var connected := false


func connect_to_lunaportal():
if ws != null:
disconnect_from_lunaportal()
connecting = true
connected = false
ws = WebSocketPeer.new()
var err := ws.connect_to_url("ws://" + server_addr)
if err != OK:
push_error("Failed to connect: %s" % err)


func disconnect_from_lunaportal():
if ws == null:
return
connecting = false
ws.close()


func _process(_delta: float) -> void:
if ws != null:
ws.poll()
for _i in range(ws.get_available_packet_count()):
var data := ws.get_packet().get_string_from_utf8()
log_received.emit(data)
logs.push_back(data)
if connecting and ws.get_ready_state() == WebSocketPeer.STATE_OPEN:
connecting = false
connected = true
portal_connected.emit()
if ws.get_ready_state() == WebSocketPeer.STATE_CLOSED:
ws = null
connected = false
portal_disconnected.emit()


func _exit_tree() -> void:
disconnect_from_lunaportal()

while connected:
_process(0)
OS.delay_msec(50)
172 changes: 172 additions & 0 deletions lunabase/lunaportal.tscn
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
[gd_scene load_steps=7 format=3 uid="uid://ba7ivbakwknsv"]

[sub_resource type="GDScript" id="GDScript_rcd5c"]
script/source = "extends VBoxContainer


func _ready() -> void:
if Lunaportal.connected:
$Connect.hide()
else:
$Connected.hide()

Lunaportal.portal_connected.connect(
func():
$Connect.hide()
$Connected.show()
)

Lunaportal.portal_disconnected.connect(
func():
$Connect.show()
$Connected.hide()
)
"

[sub_resource type="GDScript" id="GDScript_l668k"]
script/source = "extends LineEdit


func _ready() -> void:
text = Lunaportal.server_addr


func _on_text_changed(new_text: String) -> void:
Lunaportal.server_addr = new_text.strip_edges()
"

[sub_resource type="GDScript" id="GDScript_m0bdk"]
script/source = "extends Button


func _on_pressed() -> void:
Lunaportal.connect_to_lunaportal()
disabled = true
text = \"Connecting\"
$\"../LineEdit\".editable = false


func _ready() -> void:
Lunaportal.portal_disconnected.connect(
func():
disabled = false
$\"../LineEdit\".editable = true
text = \"Connect\"
)
Lunaportal.portal_connected.connect(
func():
disabled = false
$\"../LineEdit\".editable = true
text = \"Connect\"
)
"

[sub_resource type="GDScript" id="GDScript_waxm3"]
script/source = "extends Button


func _on_pressed() -> void:
Lunaportal.disconnect_from_lunaportal()
"

[sub_resource type="GDScript" id="GDScript_ixqcs"]
script/source = "extends VBoxContainer


func _ready() -> void:
for line in Lunaportal.logs:
add_line(line)
Lunaportal.log_received.connect(
func(line: String):
add_line(line)
)


func add_line(line: String):
var label := Label.new()
label.text = line
label.size_flags_horizontal = Control.SIZE_EXPAND_FILL
add_child(label)
"

[sub_resource type="GDScript" id="GDScript_lvxp7"]
script/source = "extends Button


func _on_pressed() -> void:
get_tree().change_scene_to_file(\"res://main.tscn\")
"
[node name="Lunaportal" type="VBoxContainer"]
anchors_preset = -1
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 10.0
offset_top = 10.0
offset_right = -10.0
offset_bottom = -10.0
grow_horizontal = 2
grow_vertical = 2
script = SubResource("GDScript_rcd5c")
[node name="Label" type="Label" parent="."]
layout_mode = 2
theme_override_font_sizes/font_size = 48
text = "LunaPortal"
horizontal_alignment = 1
[node name="Connect" type="VBoxContainer" parent="."]
layout_mode = 2
size_flags_vertical = 3
[node name="Label" type="Label" parent="Connect"]
layout_mode = 2
text = "LunaServer Address"
horizontal_alignment = 1
[node name="LineEdit" type="LineEdit" parent="Connect"]
custom_minimum_size = Vector2(120, 0)
layout_mode = 2
size_flags_horizontal = 4
text = "192.168.0.100"
expand_to_text_length = true
script = SubResource("GDScript_l668k")
[node name="Button" type="Button" parent="Connect"]
layout_mode = 2
size_flags_horizontal = 4
text = "Connect"
script = SubResource("GDScript_m0bdk")
[node name="Connected" type="VBoxContainer" parent="."]
layout_mode = 2
size_flags_vertical = 3
[node name="Button" type="Button" parent="Connected"]
layout_mode = 2
size_flags_horizontal = 4
text = "Disconnect"
script = SubResource("GDScript_waxm3")
[node name="PanelContainer" type="PanelContainer" parent="Connected"]
layout_mode = 2
size_flags_vertical = 3
[node name="ScrollContainer" type="ScrollContainer" parent="Connected/PanelContainer"]
layout_mode = 2
[node name="VBoxContainer" type="VBoxContainer" parent="Connected/PanelContainer/ScrollContainer"]
layout_mode = 2
size_flags_horizontal = 3
size_flags_vertical = 3
script = SubResource("GDScript_ixqcs")
[node name="Button" type="Button" parent="."]
layout_mode = 2
text = "Back to Menu"
script = SubResource("GDScript_lvxp7")
[connection signal="text_changed" from="Connect/LineEdit" to="Connect/LineEdit" method="_on_text_changed"]
[connection signal="pressed" from="Connect/Button" to="Connect/Button" method="_on_pressed"]
[connection signal="pressed" from="Connected/Button" to="Connected/Button" method="_on_pressed"]
[connection signal="pressed" from="Button" to="Button" method="_on_pressed"]
17 changes: 16 additions & 1 deletion lunabase/main.tscn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[gd_scene load_steps=5 format=3 uid="uid://b8qgudi6vkflb"]
[gd_scene load_steps=6 format=3 uid="uid://b8qgudi6vkflb"]

[ext_resource type="Texture2D" uid="uid://d4it3tpx2xqxq" path="res://icon.png" id="1_1xyvq"]

Expand All @@ -21,6 +21,14 @@ func _on_pressed() -> void:
get_tree().change_scene_to_file(\"res://lunasim.tscn\")
"
[sub_resource type="GDScript" id="GDScript_7iehl"]
script/source = "extends Button


func _on_pressed() -> void:
get_tree().change_scene_to_file(\"res://lunaportal.tscn\")
"
[node name="VBoxContainer" type="VBoxContainer"]
anchors_preset = 15
anchor_right = 1.0
Expand Down Expand Up @@ -53,5 +61,12 @@ size_flags_horizontal = 4
text = "Lunasim"
script = SubResource("GDScript_gxj40")
[node name="Button3" type="Button" parent="."]
layout_mode = 2
size_flags_horizontal = 4
text = "Lunaportal"
script = SubResource("GDScript_7iehl")
[connection signal="pressed" from="Button" to="Button" method="_on_pressed"]
[connection signal="pressed" from="Button2" to="Button2" method="_on_pressed"]
[connection signal="pressed" from="Button3" to="Button3" method="_on_pressed"]
1 change: 1 addition & 0 deletions lunabase/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ config/icon="res://icon.png"

Lunabot="*res://lunabot.gd"
UI="*res://ui.gd"
Lunaportal="*res://lunaportal.gd"

[display]

Expand Down

0 comments on commit 55a93f4

Please sign in to comment.