Skip to content

Commit 366a832

Browse files
committed
Update dialog_node.gd
Related to #63 This commits makes the text_node use the parent's size. It also fixes (kinda) the behaviour of the text autoscroll feature. Is not fully tested, but at least tries to scroll with a tween in a fixed time with a fixed string size.
1 parent d9766f9 commit 366a832

File tree

1 file changed

+36
-13
lines changed

1 file changed

+36
-13
lines changed

addons/textalog/nodes/dialogue_base_node/dialog_node/dialog_node.gd

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func set_text(text:String) -> void:
6262
if text_fit_content_height:
6363
_enable_fit_content_height()
6464

65-
_line_count = 0
65+
_line_count = -1
6666
_last_line_count = 0
6767
_last_wordwrap_size = Vector2()
6868
text_node.get_v_scroll().value = 0
@@ -144,7 +144,7 @@ const _DEFATULT_STRING = """This is a sample text.
144144
"""
145145
const _PREVIEW_COLOR = Color("#6d0046ff")
146146

147-
var _line_count := 0
147+
var _line_count := -1
148148
var _last_wordwrap_size := Vector2()
149149
var _last_line_count := 0
150150

@@ -165,21 +165,33 @@ var _already_played:bool = false
165165

166166
var _generator = RandomNumberGenerator.new()
167167

168+
var _char_position = 0
169+
var _scrolling = false
168170

169171
func _update_displayed_text() -> void:
172+
if _scrolling:
173+
return
174+
170175
var _character = _get_current_character()
171176
_on_character_displayed(_character)
172-
emit_signal("character_displayed", _character)
173-
text_node.visible_characters += 1
174177

175178
if text_autoscroll:
176179
_scroll_to_new_line()
177180

178-
if text_node.visible_characters < text_node.get_total_character_count():
179-
_text_timer.start(text_speed)
180-
else:
181+
emit_signal("character_displayed", _character)
182+
_char_position += 1
183+
184+
if _character in "\t\n\r":
185+
_update_displayed_text()
186+
return
187+
188+
text_node.set_deferred("visible_characters", text_node.visible_characters+1)
189+
_text_timer.start(text_speed)
190+
191+
if text_node.visible_characters >= text_node.get_total_character_count():
181192
_text_timer.stop()
182193
_blip_counter = 0
194+
_char_position = 0
183195
_already_played = false
184196
emit_signal("text_displayed")
185197

@@ -212,10 +224,10 @@ func _hide_scroll() -> void:
212224
func _scroll_to_new_line() -> void:
213225
var height := text_node.get_content_height()
214226
var font := text_node.get_font("normal_font")
215-
var font_height := font.get_height()
227+
var font_height := font.get_height()+get_constant("line_separation", "RichTextLabel")
216228
var scroll := text_node.get_v_scroll()
217229

218-
var current_text = text_node.text.left(text_node.visible_characters)
230+
var current_text = text_node.text.left(_char_position)
219231
var wordwrap_size:Vector2= font.get_wordwrap_string_size(current_text, text_node.rect_size.x)
220232
var text_container_height = text_node.rect_size.y
221233

@@ -226,16 +238,24 @@ func _scroll_to_new_line() -> void:
226238

227239
if wordwrap_size.y > _last_wordwrap_size.y:
228240
_line_count += 1
229-
230241
var space_left = text_container_height-wordwrap_size.y
231242

232243
# Negative value? That means we've reached the end of the container
233244
# Scroll that thing!
234-
if space_left < font_height:
245+
if space_left < 0:
235246
_need_to_scroll = true
236247

237248
if _need_to_scroll:
238-
scroll.value += font_height*1.05 # Add a little bit more, just in case.
249+
var tween = Tween.new()
250+
tween.connect("tween_all_completed", tween, "queue_free")
251+
tween.connect("tween_all_completed", self, "set", ["_scrolling", false])
252+
tween.connect("tween_all_completed", _text_timer, "start", [text_speed])
253+
get_tree().root.add_child(tween)
254+
tween.interpolate_property(scroll, "value", null, scroll.value+font_height, 0.8)
255+
_scrolling = true
256+
tween.start()
257+
258+
# scroll.set_deferred("value", scroll.value+font_height)
239259

240260
_last_wordwrap_size = wordwrap_size
241261
_last_line_count = _line_count
@@ -260,7 +280,7 @@ func _get_current_character() -> String:
260280
_text = " "
261281

262282
var _text_length = _text.length()-1
263-
var _text_visible_characters = clamp(text_node.visible_characters, 0, _text_length)
283+
var _text_visible_characters = clamp(_char_position, 0, _text_length)
264284
var _current_character = _text[min(_text_length, _text_visible_characters)]
265285
return _current_character
266286

@@ -438,13 +458,16 @@ func _notification(what:int) -> void:
438458
func _init() -> void:
439459
_text_timer = Timer.new()
440460
_text_timer.connect("timeout", self, "_update_displayed_text")
461+
_text_timer.one_shot = true
441462
add_child(_text_timer)
442463

443464
text_node = RichTextLabel.new()
444465
text_node.bbcode_enabled = true
445466
text_node.scroll_active = false
446467
text_node.mouse_filter = Control.MOUSE_FILTER_PASS
447468
text_node.name = "TextNode"
469+
text_node.size_flags_horizontal = SIZE_EXPAND_FILL
470+
text_node.size_flags_vertical = SIZE_EXPAND_FILL
448471
var scroll := text_node.get_v_scroll()
449472
if Engine.editor_hint:
450473
connect("draw", text_node, "set", ["bbcode_text", _DEFATULT_STRING])

0 commit comments

Comments
 (0)