2024-02-22 06:22:22 +11:00
|
|
|
class_name Chatbox extends Control
|
2023-11-08 14:28:55 +11:00
|
|
|
|
|
|
|
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
|
2023-11-17 20:49:38 +11:00
|
|
|
|
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
|
2023-11-17 20:49:38 +11:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2023-11-08 14:28:55 +11:00
|
|
|
|
|
|
|
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()
|
2023-11-17 20:49:38 +11:00
|
|
|
input_line.visible = true
|
|
|
|
input_line.grab_focus()
|
2023-11-09 17:56:08 +11:00
|
|
|
text_selected = true
|
2023-11-17 20:49:38 +11:00
|
|
|
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:
|
2023-11-09 20:37:12 +11:00
|
|
|
get_viewport().set_input_as_handled()
|
2023-11-08 14:28:55 +11:00
|
|
|
closed.emit()
|
2023-11-17 20:49:38 +11:00
|
|
|
input_line.deselect()
|
|
|
|
input_line.visible = false
|
2023-11-08 14:28:55 +11:00
|
|
|
text_selected = false
|
2023-11-17 20:49:38 +11:00
|
|
|
if input_line.text.length() != 0:
|
|
|
|
if input_line.text.begins_with("/"):
|
|
|
|
Game.parse_command(input_line.text, multiplayer.get_unique_id())
|
|
|
|
fade_timer.start()
|
2023-11-08 14:28:55 +11:00
|
|
|
else:
|
2023-11-17 20:49:38 +11:00
|
|
|
append_message.rpc(username, color, input_line.text)
|
|
|
|
input_line.clear()
|
2023-11-09 17:56:08 +11:00
|
|
|
if event.keycode == KEY_ESCAPE:
|
2023-11-09 20:37:12 +11:00
|
|
|
get_viewport().set_input_as_handled()
|
2023-11-09 17:56:08 +11:00
|
|
|
closed.emit()
|
2023-11-17 20:49:38 +11:00
|
|
|
input_line.deselect()
|
|
|
|
input_line.visible = false
|
2023-11-09 17:56:08 +11:00
|
|
|
text_selected = false
|
2023-11-17 20:49:38 +11:00
|
|
|
fade_timer.start()
|
2023-11-08 14:28:55 +11:00
|
|
|
|
|
|
|
|
2024-02-22 06:22:22 +11:00
|
|
|
func change_username(old_name: String, new_name: String) -> void:
|
2023-11-17 20:49:38 +11:00
|
|
|
append_message("SERVER", Color.TOMATO, old_name + " has changed their display name to " + new_name)
|
2023-11-08 14:28:55 +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:
|
2023-11-17 20:49:38 +11:00
|
|
|
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
|