Follow chat better

This commit is contained in:
Daniel Flanagan 2021-12-07 13:51:34 -06:00
parent cc9456da1a
commit 595cef84f1
Signed by: lytedev
GPG key ID: 5B2020A0F9921EF4
2 changed files with 46 additions and 11 deletions

View file

@ -6,6 +6,7 @@ onready var peers_list_label = $v/body/peers/label
onready var cursors = {}
onready var chat = $v/body/v/messages
onready var chat_edit = $v/body/v/h/chat
onready var auto_scroll_chk = $v/body/v/chat_head/auto_scroll
func _ready():
Global.client.signaller.connect("peer_joined", self, "_peer_joined")
@ -66,7 +67,19 @@ func _on_Button_pressed():
send_chat_message()
remotesync func add_chat(message):
chat.add_item(message)
if auto_scroll_chk.pressed: call_deferred("scroll_chat_to_bottom")
else:
call_deferred("preserve_chat_scroll", chat.cursor_get_line(), chat.get_selection_to_line(), chat.get_selection_from_line(), chat.get_selection_to_column(), chat.get_selection_from_column())
chat.text += "\n" + message
func scroll_chat_to_bottom():
chat.cursor_set_line(chat.get_line_count() + 1)
chat.scroll_vertical = 1000000000
func preserve_chat_scroll(n, tl, fl, tc, fc):
chat.cursor_set_line(n)
chat.select(fl, fc, tl, tc)
chat.center_viewport_to_cursor()
func _lobby_left(_id):
Global.lobby_browser()
@ -78,5 +91,7 @@ func _on_TextEdit_text_entered(_new_text):
send_chat_message()
func send_chat_message():
rpc("add_chat", "%s: %s" % [Global.client.signaller.display_name, chat_edit.text])
chat_edit.text = ""
var message = chat_edit.text
if message != "":
rpc("add_chat", "%s: %s" % [Global.client.signaller.display_name, message])
chat_edit.text = ""

View file

@ -81,20 +81,40 @@ margin_bottom = 506.0
size_flags_horizontal = 3
size_flags_vertical = 3
[node name="label" type="Label" parent="v/body/v"]
[node name="chat_head" type="HBoxContainer" parent="v/body/v"]
margin_right = 800.0
margin_bottom = 21.0
text = "Chat"
margin_bottom = 29.0
size_flags_horizontal = 3
[node name="messages" type="ItemList" parent="v/body/v"]
margin_top = 25.0
[node name="label" type="Label" parent="v/body/v/chat_head"]
margin_right = 680.0
margin_bottom = 29.0
size_flags_horizontal = 3
size_flags_vertical = 3
text = "Chat"
valign = 1
[node name="auto_scroll" type="CheckBox" parent="v/body/v/chat_head"]
margin_left = 684.0
margin_right = 800.0
margin_bottom = 29.0
size_flags_horizontal = 0
size_flags_vertical = 3
pressed = true
text = "Auto Scroll"
[node name="messages" type="TextEdit" parent="v/body/v"]
margin_top = 33.0
margin_right = 800.0
margin_bottom = 471.0
size_flags_horizontal = 3
size_flags_vertical = 3
__meta__ = {
"_edit_use_anchors_": false
}
text = "# Connecting..."
readonly = true
highlight_current_line = true
highlight_all_occurrences = true
virtual_keyboard_enabled = false
caret_block_mode = true
[node name="h" type="HBoxContainer" parent="v/body/v"]
margin_top = 475.0