From 50d50b3641122e8a74c80ff25d4f197c793cb452 Mon Sep 17 00:00:00 2001 From: Sofia Date: Fri, 24 Jul 2026 04:18:44 +0300 Subject: [PATCH] Improve chat --- godot/scenes/lobby.tscn | 2 ++ godot/scenes/ui/chat_box.tscn | 3 ++- rust/src/ui/chatbox.rs | 20 ++++++++++++++------ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/godot/scenes/lobby.tscn b/godot/scenes/lobby.tscn index 91d9970..cd4676a 100644 --- a/godot/scenes/lobby.tscn +++ b/godot/scenes/lobby.tscn @@ -123,6 +123,8 @@ layout_mode = 2 columns = 2 [node name="chat_box" parent="." unique_id=1125601400 instance=ExtResource("2_5gu6x")] +custom_minimum_size = Vector2(400, 300) +custom_maximum_size = Vector2(400, 300) layout_mode = 1 anchors_preset = 3 anchor_left = 1.0 diff --git a/godot/scenes/ui/chat_box.tscn b/godot/scenes/ui/chat_box.tscn index d74572a..7589940 100644 --- a/godot/scenes/ui/chat_box.tscn +++ b/godot/scenes/ui/chat_box.tscn @@ -3,7 +3,8 @@ [sub_resource type="StyleBoxFlat" id="StyleBoxFlat_61vwu"] bg_color = Color(0.6, 0.6, 0.6, 0) -[node name="chat_box" type="ChatBox" unique_id=1125601400 node_paths=PackedStringArray("messages", "line_edit")] +[node name="chat_box" type="ChatBox" unique_id=1125601400 node_paths=PackedStringArray("scroll", "messages", "line_edit")] +scroll = NodePath("panel/scroll_container") messages = NodePath("panel/scroll_container/v_box_container") line_edit = NodePath("line_edit") custom_minimum_size = Vector2(500, 300) diff --git a/rust/src/ui/chatbox.rs b/rust/src/ui/chatbox.rs index d79c79a..cbfc4a4 100644 --- a/rust/src/ui/chatbox.rs +++ b/rust/src/ui/chatbox.rs @@ -1,3 +1,5 @@ +use std::i32; + use godot::{ classes::{ Engine, IVBoxContainer, InputEvent, LineEdit, Panel, RichTextLabel, ScrollContainer, @@ -24,6 +26,8 @@ pub struct ChatBox { #[export] pub in_game: bool, + scroll_on_next_update: bool, + base: Base, } @@ -57,6 +61,15 @@ impl IVBoxContainer for ChatBox { } } + fn process(&mut self, _: f64) { + if self.scroll_on_next_update + && let Some(scroll) = &mut self.scroll + { + scroll.set_v_scroll(i32::MAX); + self.scroll_on_next_update = false; + } + } + fn input(&mut self, event: Gd) { if self.in_game && event.is_action_pressed("open_chat") { if let Some(line_edit) = &mut self.line_edit { @@ -88,12 +101,7 @@ impl ChatBox { if let Some(messages) = &mut self.messages { messages.add_child(&message); } - self.run_deferred(|s| { - if let Some(scroll) = &mut s.scroll { - scroll.set_h_scroll(i32::MAX); - scroll.set_v_scroll(i32::MAX); - } - }); + self.scroll_on_next_update = true; } pub fn on_submit(&mut self, message: GString) {