blob: 7841d57f79ab4d3f6cdad3006dedeabb47153494 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
extends PanelContainer
@export var player_color: Color = Color8(255, 255, 255)
var is_dragging := false
var drag_anchor := Vector2.ZERO
func _ready() -> void:
# clear test label
%ChatWindow.remove_child(%ChatWindow.get_child(0))
if Network.map_server:
initialize()
func initialize() -> void:
Network.map_server.received_server_message.connect(_on_server_message_packet_received)
Network.map_server.received_broadcast_formatted_message.connect(_on_broadcast_formatted_message_packet_received)
func add_message(message: String, format: ChatMessageFormat):
var label = Label.new()
label.text = message
if format.color:
label.add_theme_color_override("font_color", format.color)
if format.size:
label.add_theme_font_size_override("font_size", format.size)
%ChatWindow.add_child(label)
await get_tree().process_frame # necessary to reach bottom correctly
%ScrollContainer.scroll_vertical = %ScrollContainer.get_v_scroll_bar().max_value
func _on_line_edit_text_submitted(new_text: String) -> void:
%LineEdit.text = ""
%LineEdit.grab_focus()
var send_chat_message_packet := SendChatMessagePacket.new()
send_chat_message_packet.message = "%s : %s" % [Client.character.name, new_text]
Network.map_server.send(send_chat_message_packet)
func _on_server_message_packet_received(packet: ServerMessagePacket):
var format := ChatMessageFormat.new()
if packet.message.begins_with(Client.character.name):
format.color = player_color
add_message(packet.message, format)
func _on_broadcast_formatted_message_packet_received(packet: BroadcastFormattedMessagePacket):
var format := ChatMessageFormat.new()
format.color = packet.get_font_color()
format.size = packet.font_size
add_message(packet.message, format)
func _process(_delta: float) -> void:
if is_dragging:
global_position += get_global_mouse_position() - drag_anchor
drag_anchor = get_global_mouse_position()
func _on_handle_gui_input(event: InputEvent) -> void:
if event.is_action_pressed("primary_click"):
is_dragging = true
drag_anchor = get_global_mouse_position()
elif event.is_action_released("primary_click"):
is_dragging = false
|