From bb7f8ead08f3dc64dca3a4b8931799a58f5b2115 Mon Sep 17 00:00:00 2001 From: teatov Date: Thu, 13 Feb 2025 23:35:52 +1000 Subject: [PATCH] add chat --- project.godot | 5 +++ scenes/main.tscn | 7 +++- scenes/ui/chat.tscn | 51 ++++++++++++++++++++++++++++ scenes/ui/chat_message.tscn | 20 +++++++++++ scripts/globals/inputer.gd | 1 - scripts/globals/referencer.gd | 1 + scripts/main.gd | 2 +- scripts/ui/chat.gd | 64 +++++++++++++++++++++++++++++++++++ 8 files changed, 148 insertions(+), 3 deletions(-) create mode 100644 scenes/ui/chat.tscn create mode 100644 scenes/ui/chat_message.tscn create mode 100644 scripts/ui/chat.gd diff --git a/project.godot b/project.godot index bb62264..698e02a 100644 --- a/project.godot +++ b/project.godot @@ -160,6 +160,11 @@ look_slow={ "events": [Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":-1,"button_index":8,"pressure":0.0,"pressed":true,"script":null) ] } +chat={ +"deadzone": 0.5, +"events": [Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":-1,"window_id":0,"alt_pressed":false,"shift_pressed":false,"ctrl_pressed":false,"meta_pressed":false,"pressed":false,"keycode":0,"physical_keycode":84,"key_label":0,"unicode":116,"location":0,"echo":false,"script":null) +] +} [layer_names] diff --git a/scenes/main.tscn b/scenes/main.tscn index 46f365c..5188996 100644 --- a/scenes/main.tscn +++ b/scenes/main.tscn @@ -1,6 +1,7 @@ -[gd_scene load_steps=4 format=3 uid="uid://b7fc42grqckl0"] +[gd_scene load_steps=5 format=3 uid="uid://b7fc42grqckl0"] [ext_resource type="Script" path="res://scripts/main.gd" id="1_80y7k"] +[ext_resource type="PackedScene" uid="uid://b3t5vrqb1eym2" path="res://scenes/ui/chat.tscn" id="3_8t1rc"] [ext_resource type="Material" uid="uid://00ldcihmubqo" path="res://resources/materials/test_triplanar.tres" id="3_vk6ds"] [sub_resource type="Environment" id="Environment_wyusq"] @@ -27,6 +28,10 @@ transform = Transform3D(0.866025, -0.433013, 0.25, 0, 0.5, 0.866025, -0.5, -0.75 shadow_enabled = true shadow_blur = 2.0 +[node name="UI" type="Node" parent="."] + +[node name="Chat" parent="UI" instance=ExtResource("3_8t1rc")] + [node name="WorldEnvironment" type="WorldEnvironment" parent="."] environment = SubResource("Environment_wyusq") diff --git a/scenes/ui/chat.tscn b/scenes/ui/chat.tscn new file mode 100644 index 0000000..2400564 --- /dev/null +++ b/scenes/ui/chat.tscn @@ -0,0 +1,51 @@ +[gd_scene load_steps=2 format=3 uid="uid://b3t5vrqb1eym2"] + +[ext_resource type="Script" path="res://scripts/ui/chat.gd" id="1_ulx3h"] + +[node name="Chat" type="CanvasLayer"] +script = ExtResource("1_ulx3h") + +[node name="Root" type="MarginContainer" parent="."] +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 +mouse_filter = 2 +theme_override_constants/margin_left = 50 +theme_override_constants/margin_top = 20 +theme_override_constants/margin_right = 50 +theme_override_constants/margin_bottom = 20 + +[node name="VBox" type="VBoxContainer" parent="Root"] +layout_mode = 2 +mouse_filter = 2 +theme_override_constants/separation = 0 + +[node name="MsgScroll" type="ScrollContainer" parent="Root/VBox"] +layout_mode = 2 +size_flags_vertical = 3 +mouse_filter = 2 +horizontal_scroll_mode = 0 + +[node name="MsgMargin" type="MarginContainer" parent="Root/VBox/MsgScroll"] +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 +mouse_filter = 2 +theme_override_constants/margin_left = 10 +theme_override_constants/margin_right = 10 +theme_override_constants/margin_bottom = 30 + +[node name="MsgContainer" type="VBoxContainer" parent="Root/VBox/MsgScroll/MsgMargin"] +layout_mode = 2 +size_flags_horizontal = 3 +size_flags_vertical = 3 +mouse_filter = 2 +alignment = 2 + +[node name="MessageEdit" type="LineEdit" parent="Root/VBox"] +layout_mode = 2 +theme_override_font_sizes/font_size = 26 diff --git a/scenes/ui/chat_message.tscn b/scenes/ui/chat_message.tscn new file mode 100644 index 0000000..e6156e1 --- /dev/null +++ b/scenes/ui/chat_message.tscn @@ -0,0 +1,20 @@ +[gd_scene format=3 uid="uid://bjrbngxkvhn7f"] + +[node name="RichTextLabel" type="RichTextLabel"] +clip_contents = false +anchors_preset = 15 +anchor_right = 1.0 +anchor_bottom = 1.0 +grow_horizontal = 2 +grow_vertical = 2 +mouse_filter = 2 +theme_override_colors/font_outline_color = Color(0, 0, 0, 0.501961) +theme_override_constants/outline_size = 20 +theme_override_font_sizes/bold_italics_font_size = 26 +theme_override_font_sizes/italics_font_size = 26 +theme_override_font_sizes/mono_font_size = 26 +theme_override_font_sizes/normal_font_size = 26 +theme_override_font_sizes/bold_font_size = 26 +bbcode_enabled = true +text = "[b]name:[/b] lorem ipsum" +fit_content = true diff --git a/scripts/globals/inputer.gd b/scripts/globals/inputer.gd index 88dfff4..6394920 100644 --- a/scripts/globals/inputer.gd +++ b/scripts/globals/inputer.gd @@ -43,7 +43,6 @@ func _input(event: InputEvent) -> void: var event_mode := _get_event_mode(event) if mode == Mode.KB_MOUSE and event_mode == Mode.CONTROLLER: mode = Mode.CONTROLLER - mode_changed.emit(mode) if mode == Mode.CONTROLLER and event_mode == Mode.KB_MOUSE: diff --git a/scripts/globals/referencer.gd b/scripts/globals/referencer.gd index 87653bc..3fdb22a 100644 --- a/scripts/globals/referencer.gd +++ b/scripts/globals/referencer.gd @@ -9,3 +9,4 @@ var main: Main: if value: main_loaded.emit() main = value +var chat: Chat diff --git a/scripts/main.gd b/scripts/main.gd index 4ea22bc..fe39b47 100644 --- a/scripts/main.gd +++ b/scripts/main.gd @@ -16,7 +16,7 @@ func _exit_tree() -> void: func _unhandled_input(event: InputEvent) -> void: - if event.is_action_pressed("menu"): + if event.is_action_pressed("menu") and not Referencer.chat.open: get_tree().quit() diff --git a/scripts/ui/chat.gd b/scripts/ui/chat.gd new file mode 100644 index 0000000..f9edc8e --- /dev/null +++ b/scripts/ui/chat.gd @@ -0,0 +1,64 @@ +class_name Chat +extends CanvasLayer + +var open: bool = false + +var _chat_message_scene := preload("res://scenes/ui/chat_message.tscn") + +@onready var _messages_container: Container = $Root/VBox/MsgScroll/MsgMargin/MsgContainer +@onready var _message_edit: LineEdit = $Root/VBox/MessageEdit + + +func _ready() -> void: + _message_edit.text_submitted.connect(_on_message_edit_text_submitted) + _message_edit.visible = false + Referencer.chat = self + + +func _exit_tree() -> void: + Referencer.chat = null + + +func _input(event: InputEvent) -> void: + if event.is_action_pressed("menu"): + call_deferred("_close") + + +func _unhandled_input(event: InputEvent) -> void: + if Input.get_mouse_mode() != Input.MOUSE_MODE_CAPTURED: + return + + if event.is_action_pressed("chat") and not open: + open = true + _message_edit.visible = true + _message_edit.grab_focus() + Input.mouse_mode = Input.MOUSE_MODE_VISIBLE + + +func _close() -> void: + open = false + _message_edit.release_focus() + _message_edit.visible = false + Input.mouse_mode = Input.MOUSE_MODE_CAPTURED + + +@rpc("any_peer", "call_local", "reliable", 1) +func _send_message(message: String) -> void: + var player_name := ( + Networker.players[multiplayer.get_remote_sender_id()]["name"] as String + ) + var message_node := _make_message_node(player_name, message) + _messages_container.add_child(message_node) + print(player_name + ": " + message) + + +func _make_message_node(player_name: String, message: String) -> RichTextLabel: + var node := _chat_message_scene.instantiate() as RichTextLabel + node.text = "[b]" + player_name + ":[/b]\t" + message + + return node + + +func _on_message_edit_text_submitted(message: String) -> void: + _message_edit.text = "" + _send_message.rpc(message)