mtd/Scripts/chatbox.gd

73 lines
2.1 KiB
GDScript3
Raw Permalink Normal View History

2024-02-22 06:22:22 +11:00
class_name Chatbox extends Control
signal opened
signal closed
2024-02-22 06:22:22 +11:00
@export var input_line: LineEdit
@export var textbox: RichTextLabel
@export var text_panel: PanelContainer
@export var fade_timer: Timer
2024-02-22 06:22:22 +11:00
var text_selected: bool = false
var username: String = "default"
var color: Color = Color.TOMATO
var fading: bool = true
var time_to_fade: float = 2.0
var time_since_started_fading:float = 2.0
func _process(delta: float) -> void:
if fading:
time_since_started_fading += delta
else:
time_since_started_fading = 0.0
textbox.modulate.a = lerpf(1.0, 0.0, time_since_started_fading / time_to_fade)
text_panel.modulate.a = lerpf(1.0, 0.0, time_since_started_fading / time_to_fade)
func _input(event: InputEvent) -> void:
2023-11-09 17:56:08 +11:00
if !text_selected and event.is_action_pressed("Open Text Chat"):
get_viewport().set_input_as_handled()
opened.emit()
input_line.visible = true
input_line.grab_focus()
2023-11-09 17:56:08 +11:00
text_selected = true
fading = false
2023-11-09 17:56:08 +11:00
return
if text_selected and event is InputEventKey and event.pressed == true:
if event.keycode == KEY_ENTER:
get_viewport().set_input_as_handled()
closed.emit()
input_line.deselect()
input_line.visible = false
text_selected = false
if input_line.text.length() != 0:
if input_line.text.begins_with("/"):
Game.parse_command(input_line.text, multiplayer.get_unique_id())
else:
append_message.rpc(username, color, input_line.text)
input_line.clear()
2024-03-30 00:54:41 +11:00
fade_timer.start()
2023-11-09 17:56:08 +11:00
if event.keycode == KEY_ESCAPE:
get_viewport().set_input_as_handled()
2023-11-09 17:56:08 +11:00
closed.emit()
input_line.deselect()
input_line.visible = false
2023-11-09 17:56:08 +11:00
text_selected = false
fade_timer.start()
2024-02-22 06:22:22 +11:00
func change_username(old_name: String, new_name: String) -> void:
append_message("SERVER", Color.TOMATO, old_name + " has changed their display name to " + new_name)
2024-03-30 00:54:41 +11:00
@rpc("reliable", "call_local", "any_peer")
2024-02-22 06:22:22 +11:00
func append_message(user: String, user_color: Color, content: String) -> void:
textbox.append_text("[[color=" + user_color.to_html() + "]" + user + "[color=white]] " + content + "\n")
fading = false
fade_timer.start()
func _on_timer_timeout() -> void:
fading = true