From 2268cb995d77f8c4ffaa028af52253f5b69dd3ef Mon Sep 17 00:00:00 2001 From: teatov Date: Sat, 15 Feb 2025 00:07:49 +1000 Subject: [PATCH] add chat history --- scripts/ui/chat.gd | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/scripts/ui/chat.gd b/scripts/ui/chat.gd index d4d588a..860fed9 100644 --- a/scripts/ui/chat.gd +++ b/scripts/ui/chat.gd @@ -3,6 +3,9 @@ extends Menu var _chat_message_scene := preload("res://scenes/ui/chat_message.tscn") +var _message_history: PackedStringArray = [] +var _history_position: int = 0 + @onready var _msg_scroll: ScrollContainer = $MsgMargin/MsgVBox/MsgScroll @onready var _msg_container: Container = $MsgMargin/MsgVBox/MsgScroll/MsgMargin/MsgContainer @@ -25,6 +28,18 @@ func _input(event: InputEvent) -> void: if event.is_action_pressed("menu"): call_deferred("close") + if _message_edit.has_focus(): + if ( + event.is_action_pressed("ui_up") + and _history_position + 1 <= _message_history.size() + ): + _history_position += 1 + _set_message_from_history() + + if event.is_action_pressed("ui_down") and _history_position - 1 >= 0: + _history_position -= 1 + _set_message_from_history() + func _unhandled_input(event: InputEvent) -> void: if event.is_action_pressed("chat") and not is_open: @@ -62,10 +77,21 @@ func _make_message_node(player_name: String, message: String) -> RichTextLabel: return node +func _set_message_from_history() -> void: + if _history_position > 0: + var message := _message_history[-_history_position] + _message_edit.text = message + else: + _message_edit.text = "" + + func _on_message_edit_text_submitted(message: String) -> void: if _message_edit.text == "": return _message_edit.text = "" + if not (message in _message_history): + _message_history.append(message) + _history_position = 0 close() _send_message.rpc(message)