Improved UI event system

This commit is contained in:
Eduard Urbach 2024-02-25 23:01:17 +01:00
parent 44fd041c60
commit 0a489e5d2f
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
16 changed files with 138 additions and 152 deletions

View File

@ -2,24 +2,41 @@ extends Node
@export var ui: UI @export var ui: UI
func _enter_tree():
connect_main()
connect_ui()
func _ready(): func _ready():
connect_events()
pause(true) pause(true)
var args := OS.get_cmdline_args() var args := OS.get_cmdline_args()
var offline := args.has("--offline") var offline := args.has("--offline")
if offline: if offline:
Global.account.id = "test" start_offline()
Global.account.name = "Local"
Global.account.auth_token = "test"
Global.player = %PlayerAdd.spawn_player(Global.account.id, Global.account.name, Vector3.ZERO)
%Login.success.emit()
else: else:
%Login.send_login() start_online()
func connect_events():
%Login.success.connect(on_login)
%Logout.success.connect(on_logout)
%Login.success.connect(ui.reconnect.hide)
%Logout.success.connect(ui.reconnect.show)
%Ping.changed.connect(ui.network.set_ping)
%Client.download_changed.connect(ui.network.set_download)
%Client.upload_changed.connect(ui.network.set_upload)
%Chat.message_received.connect(ui.chat.add_message)
ui.chat.message_submitted.connect(%Chat.send_message)
func start_offline():
Global.account.id = "test"
Global.account.name = "Local"
Global.account.auth_token = "test"
Global.player = %PlayerAdd.spawn_player(Global.account.id, Global.account.name, Vector3.ZERO)
%Login.success.emit()
func start_online():
%Login.send_login()
func _input(event): func _input(event):
if event.is_action_pressed("toggle_fullscreen"): if event.is_action_pressed("toggle_fullscreen"):
@ -47,19 +64,6 @@ func mute_audio(enabled: bool):
var master_sound = AudioServer.get_bus_index("Master") var master_sound = AudioServer.get_bus_index("Master")
AudioServer.set_bus_mute(master_sound, enabled) AudioServer.set_bus_mute(master_sound, enabled)
func connect_main():
%Login.success.connect(on_login)
%Logout.success.connect(on_logout)
func connect_ui():
ui.login = %Login.success
ui.logout = %Logout.success
ui.ping_changed = %Ping.changed
ui.download_changed = %Client.download_changed
ui.upload_changed = %Client.upload_changed
ui.message_received = %Chat.message_received
ui.chat_message_submitted.connect(%Chat.send_message)
func toggle_fullscreen(): func toggle_fullscreen():
var mode = DisplayServer.window_get_mode() var mode = DisplayServer.window_get_mode()
@ -74,4 +78,4 @@ func center_window():
var screen_center := DisplayServer.screen_get_position() + DisplayServer.screen_get_size() / 2 var screen_center := DisplayServer.screen_get_position() + DisplayServer.screen_get_size() / 2
var window := get_window() var window := get_window()
var window_size = window.get_size_with_decorations() var window_size = window.get_size_with_decorations()
window.set_position(screen_center - window_size / 2) window.set_position(screen_center - window_size / 2)

View File

@ -101,7 +101,7 @@ unique_name_in_owner = true
script = ExtResource("10_y3len") script = ExtResource("10_y3len")
packet_type = 20 packet_type = 20
[node name="Statistics" type="Timer" parent="Client"] [node name="BandwidthTimer" type="Timer" parent="Client"]
process_thread_group = 1 process_thread_group = 1
process_thread_group_order = 0 process_thread_group_order = 0
process_thread_messages = 0 process_thread_messages = 0
@ -177,4 +177,4 @@ process_mode = 3
[connection signal="timeout" from="Client/Ping/Timer" to="Client/Ping" method="send_ping"] [connection signal="timeout" from="Client/Ping/Timer" to="Client/Ping" method="send_ping"]
[connection signal="timeout" from="Client/Login/Timer" to="Client/Login" method="send_login"] [connection signal="timeout" from="Client/Login/Timer" to="Client/Login" method="send_login"]
[connection signal="timeout" from="Client/Statistics" to="Client" method="update_statistics"] [connection signal="timeout" from="Client/BandwidthTimer" to="Client" method="update_statistics"]

View File

@ -35,4 +35,4 @@ func fade_out():
progress_bar.hide() progress_bar.hide()
var tween = get_tree().create_tween() var tween = get_tree().create_tween()
tween.tween_property(modulate, "color", Color(1.0, 1.0, 1.0, 0), fade_duration) tween.tween_property(modulate, "color", Color(1.0, 1.0, 1.0, 0), fade_duration)
tween.tween_callback(queue_free) tween.tween_callback(queue_free)

View File

@ -3,20 +3,14 @@ extends Node
static var focus: Control static var focus: Control
signal chat_message_submitted(message: String) var chat: Chat
var reconnect: Panel
@export var login: Signal var network: NetworkUI
@export var logout: Signal
@export var ping_changed: Signal
@export var download_changed: Signal
@export var upload_changed: Signal
@export var message_received: Signal
func _enter_tree(): func _enter_tree():
if get_node_or_null("/root/Main"): chat = %Chat
return reconnect = %Reconnect
network = %Network
connect_fake()
func _process(_delta): func _process(_delta):
var new_focus := get_viewport().gui_get_focus_owner() var new_focus := get_viewport().gui_get_focus_owner()
@ -26,14 +20,6 @@ func _process(_delta):
else: else:
UI.focus = null UI.focus = null
func connect_fake():
for property in get_property_list():
if property.type != TYPE_SIGNAL:
continue
add_user_signal(property.name)
set(property.name, Signal(self, property.name))
static func unfocus(): static func unfocus():
if UI.focus: if UI.focus:
UI.focus.release_focus() UI.focus.release_focus()

View File

@ -1,18 +1,15 @@
[gd_scene load_steps=16 format=3 uid="uid://dagn5bf7ou3sd"] [gd_scene load_steps=13 format=3 uid="uid://dagn5bf7ou3sd"]
[ext_resource type="PackedScene" uid="uid://cch67vqpsmtej" path="res://ui/debug/DebugLabel.tscn" id="1_7s8uu"] [ext_resource type="PackedScene" uid="uid://cch67vqpsmtej" path="res://ui/debug/DebugLabel.tscn" id="1_7s8uu"]
[ext_resource type="Script" path="res://ui/UI.gd" id="1_l5b6o"] [ext_resource type="Script" path="res://ui/UI.gd" id="1_l5b6o"]
[ext_resource type="Script" path="res://ui/debug/FPSLabel.gd" id="2_i200p"] [ext_resource type="Script" path="res://ui/debug/FPSLabel.gd" id="2_i200p"]
[ext_resource type="Script" path="res://ui/debug/PingLabel.gd" id="3_xjdws"]
[ext_resource type="Script" path="res://ui/debug/PositionLabel.gd" id="4_beqf6"] [ext_resource type="Script" path="res://ui/debug/PositionLabel.gd" id="4_beqf6"]
[ext_resource type="Script" path="res://ui/debug/VelocityLabel.gd" id="5_8lm6a"] [ext_resource type="Script" path="res://ui/debug/VelocityLabel.gd" id="5_8lm6a"]
[ext_resource type="Script" path="res://ui/chat/ChatBox.gd" id="5_43juw"]
[ext_resource type="Script" path="res://ui/debug/PerformanceLabel.gd" id="5_ab7ln"] [ext_resource type="Script" path="res://ui/debug/PerformanceLabel.gd" id="5_ab7ln"]
[ext_resource type="Script" path="res://ui/chat/ChatInput.gd" id="6_cg2h5"] [ext_resource type="PackedScene" uid="uid://dvfy6k5h51x7w" path="res://ui/chat/Chat.tscn" id="5_jygpp"]
[ext_resource type="Script" path="res://ui/debug/UploadLabel.gd" id="7_cfnpx"] [ext_resource type="PackedScene" uid="uid://bigr3od0vmicg" path="res://ui/network/Network.tscn" id="6_asn0f"]
[ext_resource type="Script" path="res://ui/debug/DownloadLabel.gd" id="8_ogt38"]
[ext_resource type="Script" path="res://ui/connect/ConnectPanel.gd" id="11_cwl0t"]
[ext_resource type="PackedScene" uid="uid://bqpbrju7mc7d5" path="res://ui/settings/Settings.tscn" id="11_rt7sl"] [ext_resource type="PackedScene" uid="uid://bqpbrju7mc7d5" path="res://ui/settings/Settings.tscn" id="11_rt7sl"]
[ext_resource type="PackedScene" uid="uid://c6jhsk76pk5k4" path="res://ui/reconnect/Reconnect.tscn" id="12_plqhr"]
[ext_resource type="Script" path="res://ui/debug/StateLabel.gd" id="12_rr0mv"] [ext_resource type="Script" path="res://ui/debug/StateLabel.gd" id="12_rr0mv"]
[ext_resource type="PackedScene" uid="uid://y6kdpmp5glv0" path="res://ui/inventory/Inventory.tscn" id="13_1fc2b"] [ext_resource type="PackedScene" uid="uid://y6kdpmp5glv0" path="res://ui/inventory/Inventory.tscn" id="13_1fc2b"]
@ -122,26 +119,9 @@ offset_right = 50.5
grow_horizontal = 2 grow_horizontal = 2
grow_vertical = 0 grow_vertical = 0
[node name="Chat" type="VBoxContainer" parent="Bottom"] [node name="Chat" parent="Bottom" instance=ExtResource("5_jygpp")]
custom_minimum_size = Vector2(320, 180) unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
size_flags_vertical = 3
alignment = 2
[node name="ChatBox" type="RichTextLabel" parent="Bottom/Chat"]
layout_mode = 2
size_flags_vertical = 3
focus_mode = 2
bbcode_enabled = true
scroll_active = false
scroll_following = true
context_menu_enabled = true
selection_enabled = true
script = ExtResource("5_43juw")
[node name="ChatInput" type="LineEdit" parent="Bottom/Chat"]
layout_mode = 2
script = ExtResource("6_cg2h5")
[node name="BottomRight" type="MarginContainer" parent="."] [node name="BottomRight" type="MarginContainer" parent="."]
anchors_preset = 3 anchors_preset = 3
@ -154,24 +134,9 @@ offset_top = -54.0
grow_horizontal = 0 grow_horizontal = 0
grow_vertical = 0 grow_vertical = 0
[node name="VBoxContainer" type="VBoxContainer" parent="BottomRight"] [node name="Network" parent="BottomRight" instance=ExtResource("6_asn0f")]
unique_name_in_owner = true
layout_mode = 2 layout_mode = 2
alignment = 2
[node name="Ping" parent="BottomRight/VBoxContainer" instance=ExtResource("1_7s8uu")]
layout_mode = 2
alignment = 2
script = ExtResource("3_xjdws")
[node name="Send" parent="BottomRight/VBoxContainer" instance=ExtResource("1_7s8uu")]
layout_mode = 2
alignment = 2
script = ExtResource("7_cfnpx")
[node name="Receive" parent="BottomRight/VBoxContainer" instance=ExtResource("1_7s8uu")]
layout_mode = 2
alignment = 2
script = ExtResource("8_ogt38")
[node name="TopRight" type="MarginContainer" parent="."] [node name="TopRight" type="MarginContainer" parent="."]
anchors_preset = 1 anchors_preset = 1
@ -199,29 +164,9 @@ layout_mode = 2
alignment = 2 alignment = 2
script = ExtResource("5_8lm6a") script = ExtResource("5_8lm6a")
[node name="ConnectPanel" type="Panel" parent="."] [node name="Reconnect" parent="." instance=ExtResource("12_plqhr")]
unique_name_in_owner = true
visible = false visible = false
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
script = ExtResource("11_cwl0t")
[node name="ConnectLabel" type="Label" parent="ConnectPanel"]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -47.5
offset_top = -10.0
offset_right = 47.5
offset_bottom = 10.0
grow_horizontal = 2
grow_vertical = 2
text = "Connecting..."
[node name="Settings" parent="." instance=ExtResource("11_rt7sl")] [node name="Settings" parent="." instance=ExtResource("11_rt7sl")]
visible = false visible = false

7
client/ui/chat/Chat.gd Normal file
View File

@ -0,0 +1,7 @@
class_name Chat
extends VBoxContainer
signal message_submitted(message: String)
func add_message(message: String):
%Messages.append_text(message + "\n")

26
client/ui/chat/Chat.tscn Normal file
View File

@ -0,0 +1,26 @@
[gd_scene load_steps=3 format=3 uid="uid://dvfy6k5h51x7w"]
[ext_resource type="Script" path="res://ui/chat/Chat.gd" id="1_sxxey"]
[ext_resource type="Script" path="res://ui/chat/ChatInput.gd" id="2_yjq4f"]
[node name="Chat" type="VBoxContainer"]
custom_minimum_size = Vector2(320, 180)
size_flags_vertical = 3
alignment = 2
script = ExtResource("1_sxxey")
[node name="Messages" type="RichTextLabel" parent="."]
unique_name_in_owner = true
layout_mode = 2
size_flags_vertical = 3
focus_mode = 2
bbcode_enabled = true
scroll_active = false
scroll_following = true
context_menu_enabled = true
selection_enabled = true
[node name="Input" type="LineEdit" parent="."]
unique_name_in_owner = true
layout_mode = 2
script = ExtResource("2_yjq4f")

View File

@ -1,7 +0,0 @@
extends RichTextLabel
func _ready():
owner.message_received.connect(on_message_received)
func on_message_received(bbcode: String):
append_text(bbcode + "\n")

View File

@ -23,6 +23,6 @@ func on_focus_exited():
tween.tween_property(self, "modulate", Color(1, 1, 1, 0), 0.2) tween.tween_property(self, "modulate", Color(1, 1, 1, 0), 0.2)
func on_text_submitted(message: String): func on_text_submitted(message: String):
(owner as UI).chat_message_submitted.emit(message)
text = "" text = ""
release_focus() release_focus()
(owner as Chat).message_submitted.emit(message)

View File

@ -1,11 +0,0 @@
extends Panel
func _ready():
owner.login.connect(on_login)
owner.logout.connect(on_logout)
func on_login():
hide()
func on_logout():
show()

View File

@ -1,7 +0,0 @@
extends DebugLabel
func _ready():
owner.download_changed.connect(on_download_changed)
func on_download_changed(download):
text = "%d bytes" % download

View File

@ -1,7 +0,0 @@
extends DebugLabel
func _ready():
owner.ping_changed.connect(on_ping_changed)
func on_ping_changed(ping):
text = str(snapped(ping * 1000, 1)) + " ms"

View File

@ -1,7 +0,0 @@
extends DebugLabel
func _ready():
owner.upload_changed.connect(on_upload_changed)
func on_upload_changed(upload):
text = "%d bytes" % upload

View File

@ -0,0 +1,11 @@
class_name NetworkUI
extends VBoxContainer
func set_ping(ping: float):
%Ping.text = str(snapped(ping * 1000, 1)) + " ms"
func set_download(download: int):
%Receive.text = "%d bytes" % download
func set_upload(upload: int):
%Send.text = "%d bytes" % upload

View File

@ -0,0 +1,23 @@
[gd_scene load_steps=3 format=3 uid="uid://bigr3od0vmicg"]
[ext_resource type="PackedScene" uid="uid://cch67vqpsmtej" path="res://ui/debug/DebugLabel.tscn" id="1_d3f0b"]
[ext_resource type="Script" path="res://ui/network/Network.gd" id="1_dnvs4"]
[node name="Network" type="VBoxContainer"]
alignment = 2
script = ExtResource("1_dnvs4")
[node name="Ping" parent="." instance=ExtResource("1_d3f0b")]
unique_name_in_owner = true
layout_mode = 2
alignment = 2
[node name="Send" parent="." instance=ExtResource("1_d3f0b")]
unique_name_in_owner = true
layout_mode = 2
alignment = 2
[node name="Receive" parent="." instance=ExtResource("1_d3f0b")]
unique_name_in_owner = true
layout_mode = 2
alignment = 2

View File

@ -0,0 +1,23 @@
[gd_scene format=3 uid="uid://c6jhsk76pk5k4"]
[node name="Reconnect" type="Panel"]
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
[node name="Label" type="Label" parent="."]
layout_mode = 1
anchors_preset = 8
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
offset_left = -47.5
offset_top = -10.0
offset_right = 47.5
offset_bottom = 10.0
grow_horizontal = 2
grow_vertical = 2
text = "Connecting..."