add chat history

This commit is contained in:
Teatov 2025-02-15 00:07:49 +10:00
parent 01f93fbbad
commit 2268cb995d

View File

@ -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)