From b117caed189c99e780f819555868371d557f8ccc Mon Sep 17 00:00:00 2001 From: Benjamin Palko Date: Thu, 22 May 2025 09:03:10 -0400 Subject: [PATCH] cleanup --- client/.editorconfig | 5 ++ client/chat/ChatInput.cs | 39 ++++++++++ client/chat/ChatInput.cs.uid | 1 + client/chat/ChatLog.cs | 93 ++++++++++++++++++---- client/chat/ChatOptions.cs | 60 +++++++++++++++ client/chat/ChatOptions.cs.uid | 1 + client/chat/ChatWindow.cs | 136 ++------------------------------- client/chat/ChatWindow.tscn | 8 +- 8 files changed, 200 insertions(+), 143 deletions(-) create mode 100644 client/.editorconfig create mode 100644 client/chat/ChatInput.cs create mode 100644 client/chat/ChatInput.cs.uid create mode 100644 client/chat/ChatOptions.cs create mode 100644 client/chat/ChatOptions.cs.uid diff --git a/client/.editorconfig b/client/.editorconfig new file mode 100644 index 0000000..0b3779e --- /dev/null +++ b/client/.editorconfig @@ -0,0 +1,5 @@ +root = true + +[*] +end_of_line = lf +insert_final_newline = true diff --git a/client/chat/ChatInput.cs b/client/chat/ChatInput.cs new file mode 100644 index 0000000..652a055 --- /dev/null +++ b/client/chat/ChatInput.cs @@ -0,0 +1,39 @@ +using Godot; +using SpacetimeDB; +using SpacetimeDB.Types; + +public partial class ChatInput : LineEdit +{ + public override void _EnterTree() + { + TextSubmitted += OnMessageInput; + } + + // Called when the node enters the scene tree for the first time. + public override void _Ready() + { + DbConnection conn = Spacetime.Instance.Connection; + RegisterSubscriptions(conn); + } + + void OnMessageInput(string text) + { + Spacetime.Instance.Connection.Reducers.SendMessage(text); + Text = ""; + } + + void RegisterSubscriptions(DbConnection conn) + { + conn.Reducers.OnSendMessage += Reducer_OnSendMessageEvent; + } + + /// Our `OnSendMessageEvent` callback: print a warning if the reducer failed. + void Reducer_OnSendMessageEvent(ReducerEventContext ctx, string text) + { + var e = ctx.Event; + if (e.CallerIdentity == Spacetime.Instance.Identity && e.Status is Status.Failed(var error)) + { + GD.PrintErr($"Failed to send message {text}: {error}"); + } + } +} diff --git a/client/chat/ChatInput.cs.uid b/client/chat/ChatInput.cs.uid new file mode 100644 index 0000000..1eaa0c5 --- /dev/null +++ b/client/chat/ChatInput.cs.uid @@ -0,0 +1 @@ +uid://115ssbk8epio diff --git a/client/chat/ChatLog.cs b/client/chat/ChatLog.cs index 83a2bd7..4f55792 100644 --- a/client/chat/ChatLog.cs +++ b/client/chat/ChatLog.cs @@ -1,20 +1,87 @@ using Godot; +using SpacetimeDB; +using SpacetimeDB.Types; public partial class ChatLog : RichTextLabel { - // Called when the node enters the scene tree for the first time. - public override void _Ready() - { - } + const string SystemColor = "#747474"; - // Called every frame. 'delta' is the elapsed time since the previous frame. - public override void _Process(double delta) - { - } + public override void _EnterTree() + { + ClearLog(); + } - public void PushMessage(string name, string message, string color) - { - string entry = $"[color={color}]{name}:[/color] {message}"; - this.Text += $"{entry}\n"; - } + public override void _Ready() + { + DbConnection conn = Spacetime.Instance.Connection; + RegisterSubscriptions(conn); + } + + string UserNameOrIdentity(User user) + { + return user != null ? user.Name ?? user.Identity.ToString()[..8] : "unknown"; + } + + void PushMessage(string name, string message, string color) + { + string entry = $"[color={color}]{name}:[/color] {message}"; + this.Text += $"{entry}\n"; + } + + void ClearLog() + { + Text = ""; + } + + void RegisterSubscriptions(DbConnection conn) + { + conn.Db.User.OnInsert += User_OnInsert; + conn.Db.User.OnUpdate += User_OnUpdate; + + conn.Db.Message.OnInsert += Message_OnInsert; + } + + void User_OnInsert(EventContext ctx, User insertedValue) + { + if (ctx.Event is Event.SubscribeApplied) + { + return; + } + PushMessage("System", $"{UserNameOrIdentity(insertedValue)} connected", SystemColor); + } + + void User_OnUpdate(EventContext ctx, User oldValue, User newValue) + { + if (oldValue.Name != newValue.Name) + { + PushMessage( + "System", + $"{UserNameOrIdentity(oldValue)} renamed to {UserNameOrIdentity(newValue)}", + SystemColor + ); + } + if (oldValue.Online != newValue.Online) + { + if (newValue.Online) + { + PushMessage("System", $"{UserNameOrIdentity(newValue)} connected", SystemColor); + } + else + { + PushMessage("System", $"{UserNameOrIdentity(newValue)} disconnected", SystemColor); + } + } + } + + void Message_OnInsert(EventContext ctx, Message insertedValue) + { + User sender = ctx.Db.User.Identity.Find(insertedValue.Sender); + string color = sender.Identity.Equals(Spacetime.Instance.Identity) ? "blue" : "red"; + if (ctx.Event is Event.SubscribeApplied) + { + PushMessage(UserNameOrIdentity(sender), insertedValue.Text, color); + return; + } + PushMessage(UserNameOrIdentity(sender), insertedValue.Text, color); + } } diff --git a/client/chat/ChatOptions.cs b/client/chat/ChatOptions.cs new file mode 100644 index 0000000..041dd75 --- /dev/null +++ b/client/chat/ChatOptions.cs @@ -0,0 +1,60 @@ +using Godot; +using SpacetimeDB; +using SpacetimeDB.Types; + +public partial class ChatOptions : HBoxContainer +{ + public LineEdit Username { get; private set; } + public ColorPickerButton ColorPicker { get; private set; } + + public override void _EnterTree() + { + Username = GetNode("Username"); + ColorPicker = GetNode("ColorPicker"); + + Username.TextSubmitted += OnUsernameInput; + Username.FocusExited += ResetUsername; + } + + public override void _Ready() + { + DbConnection conn = Spacetime.Instance.Connection; + RegisterSubscriptions(conn); + } + + string UserNameOrIdentity(User user) + { + return user != null ? user.Name ?? user.Identity.ToString()[..8] : "unknown"; + } + + void ResetUsername() + { + Username.Text = UserNameOrIdentity(Spacetime.Instance.Me); + } + + void OnUsernameInput(string text) + { + Spacetime.Instance.Connection.Reducers.SetName(text); + } + + void RegisterSubscriptions(DbConnection conn) + { + conn.Reducers.OnSetName += Reducer_OnSetNameEvent; + } + + /// Our `OnSetNameEvent` callback: print a warning if the reducer failed. + void Reducer_OnSetNameEvent(ReducerEventContext ctx, string name) + { + var e = ctx.Event; + if (e.CallerIdentity != Spacetime.Instance.Identity) + { + // Not me + return; + } + if (e.Status is Status.Failed(var error)) + { + GD.PrintErr($"Failed to change name to {name}: {error}"); + return; + } + } +} diff --git a/client/chat/ChatOptions.cs.uid b/client/chat/ChatOptions.cs.uid new file mode 100644 index 0000000..e04a848 --- /dev/null +++ b/client/chat/ChatOptions.cs.uid @@ -0,0 +1 @@ +uid://tff5u6blrc1d diff --git a/client/chat/ChatWindow.cs b/client/chat/ChatWindow.cs index 0b58d20..6f387c7 100644 --- a/client/chat/ChatWindow.cs +++ b/client/chat/ChatWindow.cs @@ -1,136 +1,16 @@ using Godot; -using SpacetimeDB; -using SpacetimeDB.Types; public partial class ChatWindow : VBoxContainer { - const string SystemColor = "#747474"; + private ChatOptions _options; + private ChatLog _log; + private LineEdit _input; - private LineEdit _userNameInput; - private ChatLog _log; - private LineEdit _input; - - // Called when the node enters the scene tree for the first time. - public override void _EnterTree() - { - _userNameInput = GetNode("Options/Username"); - _log = GetNode("ChatLog"); - _input = GetNode("ChatInput"); - - _log.Text = ""; - } - - string UserNameOrIdentity(User user) - { - return user != null ? user.Name ?? user.Identity.ToString()[..8] : "unknown"; - } - - // Called when the node enters the scene tree for the first time. - public override void _Ready() - { - DbConnection conn = Spacetime.Instance.Connection; - - _userNameInput.TextSubmitted += OnUsernameInput; - _userNameInput.FocusExited += ResetUsername; - _input.TextSubmitted += OnMessageInput; - - conn.Db.User.OnInsert += User_OnInsert; - conn.Db.User.OnUpdate += User_OnUpdate; - - conn.Db.Message.OnInsert += Message_OnInsert; - - conn.Reducers.OnSetName += Reducer_OnSetNameEvent; - conn.Reducers.OnSendMessage += Reducer_OnSendMessageEvent; - } - - void ResetUsername() - { - _userNameInput.Text = UserNameOrIdentity(Spacetime.Instance.Me); - } - - // Called every frame. 'delta' is the elapsed time since the previous frame. - public override void _Process(double delta) - { - } - - private void OnUsernameInput(string text) - { - Spacetime.Instance.Connection.Reducers.SetName(text); - } - - private void OnMessageInput(string text) - { - Spacetime.Instance.Connection.Reducers.SendMessage(text); - _input.Text = ""; - } - - void User_OnInsert(EventContext ctx, User insertedValue) - { - if (ctx.Event is Event.SubscribeApplied) + // Called when the node enters the scene tree for the first time. + public override void _EnterTree() { - if (insertedValue.Identity == Spacetime.Instance.Identity) - { - ResetUsername(); - } - return; + _options = GetNode("Options"); + _log = GetNode("ChatLog"); + _input = GetNode("ChatInput"); } - _log.PushMessage("System", $"{UserNameOrIdentity(insertedValue)} connected", SystemColor); - } - - void User_OnUpdate(EventContext ctx, User oldValue, User newValue) - { - if (oldValue.Name != newValue.Name) - { - _log.PushMessage("System", $"{UserNameOrIdentity(oldValue)} renamed to {UserNameOrIdentity(newValue)}", SystemColor); - } - if (oldValue.Online != newValue.Online) - { - if (newValue.Online) - { - _log.PushMessage("System", $"{UserNameOrIdentity(newValue)} connected", SystemColor); - } - else - { - _log.PushMessage("System", $"{UserNameOrIdentity(newValue)} disconnected", SystemColor); - } - } - } - - void Message_OnInsert(EventContext ctx, Message insertedValue) - { - User sender = ctx.Db.User.Identity.Find(insertedValue.Sender); - string color = sender.Identity.Equals(Spacetime.Instance.Identity) ? "blue" : "red"; - if (ctx.Event is Event.SubscribeApplied) - { - _log.PushMessage(UserNameOrIdentity(sender), insertedValue.Text, color); - return; - } - _log.PushMessage(UserNameOrIdentity(sender), insertedValue.Text, color); - } - - /// Our `OnSetNameEvent` callback: print a warning if the reducer failed. - void Reducer_OnSetNameEvent(ReducerEventContext ctx, string name) - { - var e = ctx.Event; - if (e.CallerIdentity != Spacetime.Instance.Identity) - { - // Not me - return; - } - if (e.Status is Status.Failed(var error)) - { - GD.PrintErr($"Failed to change name to {name}: {error}"); - return; - } - } - - /// Our `OnSendMessageEvent` callback: print a warning if the reducer failed. - void Reducer_OnSendMessageEvent(ReducerEventContext ctx, string text) - { - var e = ctx.Event; - if (e.CallerIdentity == Spacetime.Instance.Identity && e.Status is Status.Failed(var error)) - { - GD.PrintErr($"Failed to send message {text}: {error}"); - } - } } diff --git a/client/chat/ChatWindow.tscn b/client/chat/ChatWindow.tscn index be31869..05f7b26 100644 --- a/client/chat/ChatWindow.tscn +++ b/client/chat/ChatWindow.tscn @@ -1,7 +1,9 @@ -[gd_scene load_steps=3 format=3 uid="uid://cqmy41vtnqd6f"] +[gd_scene load_steps=5 format=3 uid="uid://cqmy41vtnqd6f"] [ext_resource type="Script" uid="uid://c3s41dv7hv4md" path="res://chat/ChatWindow.cs" id="1_d8jvm"] [ext_resource type="Script" uid="uid://cgn6wa7td0ekp" path="res://chat/ChatLog.cs" id="2_fkxbv"] +[ext_resource type="Script" uid="uid://tff5u6blrc1d" path="res://chat/ChatOptions.cs" id="2_lvlsn"] +[ext_resource type="Script" uid="uid://115ssbk8epio" path="res://chat/ChatInput.cs" id="4_yd183"] [node name="Chat Window" type="VBoxContainer"] anchors_preset = 15 @@ -14,6 +16,7 @@ script = ExtResource("1_d8jvm") [node name="Options" type="HBoxContainer" parent="."] layout_mode = 2 alignment = 2 +script = ExtResource("2_lvlsn") [node name="Label" type="Label" parent="Options"] layout_mode = 2 @@ -23,7 +26,7 @@ text = "Username:" custom_minimum_size = Vector2(120, 0) layout_mode = 2 -[node name="ColorPickerButton" type="ColorPickerButton" parent="Options"] +[node name="ColorPicker" type="ColorPickerButton" parent="Options"] layout_mode = 2 text = "Color" @@ -38,3 +41,4 @@ script = ExtResource("2_fkxbv") [node name="ChatInput" type="LineEdit" parent="."] layout_mode = 2 +script = ExtResource("4_yd183")