-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
247 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters