From 3bc3b6afbb8d748c4dd941d279255c2b1eb38ee4 Mon Sep 17 00:00:00 2001 From: Balloonpopper Date: Sat, 30 Nov 2024 00:16:54 +1100 Subject: [PATCH 1/7] issue_360 Fix dialog_caption size --- .../dialog_caption/dialog_caption.gd | 31 +++++++++++++++++-- .../gui/components/dialog_text/dialog_text.gd | 1 - 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd index c48fd0d5..07dbd772 100644 --- a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd +++ b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd @@ -2,16 +2,41 @@ extends PopochiuDialogText #region Private #################################################################################### -func _modify_size(msg: String, _target_position: Vector2) -> void: +func _modify_size(msg: String, target_position: Vector2) -> void: var _size := await _calculate_size(msg) - + # Define size and position (before calculating overflow) + rich_text_label.size.x = _size.x rich_text_label.size.y = _size.y + rich_text_label.position.x = (get_viewport_rect().size.x/2) - (_size.x /2) rich_text_label.position.y = get_meta(DFLT_POSITION).y - (_size.y - get_meta(DFLT_SIZE).y) +## Appends text for the dialog caption +## Ensures that where a printing a word would see it wrap to the next line that the newline +## is forced into the text. Without this the tween in dialog_text.gd would print part of the word +## until it runs out of space, then erase the part word and put it onto the next line which looks +## messy. func _append_text(msg: String, props: Dictionary) -> void: - rich_text_label.text = "[center][color=%s]%s[/color][/center]" % [props.color.to_html(), msg] + rich_text_label.text = msg + var number_of_lines_of_text = rich_text_label.get_line_count() + if number_of_lines_of_text > 1: + var current_line_number = 0 + for current_character in range(0, rich_text_label.text.length()): + + var ThisChar = rich_text_label.text[current_character] + var ThisLine = rich_text_label.get_character_line(current_character) + if rich_text_label.get_character_line(current_character) > current_line_number: + current_line_number += 1 + if rich_text_label.text[current_character-1] == " ": + rich_text_label.text[current_character-1] = "\n" + elif rich_text_label.text[current_character-1] != "\n": + rich_text_label.text = rich_text_label.text.left(current_character) + "\n" + rich_text_label.text.right(-current_character) + if current_line_number == number_of_lines_of_text - 1: + break + + msg = rich_text_label.text + rich_text_label.text = "[center][color=%s]%s[/color][/center]" % [props.color.to_html(), msg] #endregion diff --git a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_text.gd b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_text.gd index b0b0978b..b8830ba0 100644 --- a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_text.gd +++ b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_text.gd @@ -79,7 +79,6 @@ func play_text(props: Dictionary) -> void: await _modify_size(msg, props.position) # Assign the text and align mode - msg = "[color=%s]%s[/color]" % [props.color.to_html(), msg] _append_text(msg, props) if _secs_per_character > 0.0: From 44ecc0f8afc32fff676a8bb3d515e23f65358f80 Mon Sep 17 00:00:00 2001 From: balloonpopper <5151242+balloonpopper@users.noreply.github.com> Date: Sat, 30 Nov 2024 10:28:28 +1100 Subject: [PATCH 2/7] Update addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd Co-authored-by: Carenalga --- .../gui/components/dialog_text/dialog_caption/dialog_caption.gd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd index 07dbd772..ad763276 100644 --- a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd +++ b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd @@ -19,7 +19,7 @@ func _modify_size(msg: String, target_position: Vector2) -> void: ## messy. func _append_text(msg: String, props: Dictionary) -> void: rich_text_label.text = msg - var number_of_lines_of_text = rich_text_label.get_line_count() + var number_of_lines_of_text := rich_text_label.get_line_count() if number_of_lines_of_text > 1: var current_line_number = 0 for current_character in range(0, rich_text_label.text.length()): From 62a0acd86b1c1fe8740ec33387217b1c7195194b Mon Sep 17 00:00:00 2001 From: balloonpopper <5151242+balloonpopper@users.noreply.github.com> Date: Sat, 30 Nov 2024 10:28:44 +1100 Subject: [PATCH 3/7] Update addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd Co-authored-by: Carenalga --- .../gui/components/dialog_text/dialog_caption/dialog_caption.gd | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd index ad763276..f33af793 100644 --- a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd +++ b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd @@ -21,7 +21,7 @@ func _append_text(msg: String, props: Dictionary) -> void: rich_text_label.text = msg var number_of_lines_of_text := rich_text_label.get_line_count() if number_of_lines_of_text > 1: - var current_line_number = 0 + var current_line_number := 0 for current_character in range(0, rich_text_label.text.length()): var ThisChar = rich_text_label.text[current_character] From e3f675652bd94ebfa559314bb80a933463cdbf83 Mon Sep 17 00:00:00 2001 From: Balloonpopper Date: Thu, 5 Dec 2024 21:06:16 +1100 Subject: [PATCH 4/7] issue_360 Fix bug where short text ends up on 2 lines and text y location ends up wrong --- .../gui/components/dialog_text/dialog_text.gd | 34 ++++++++----------- 1 file changed, 15 insertions(+), 19 deletions(-) diff --git a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_text.gd b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_text.gd index b8830ba0..bc3eaf10 100644 --- a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_text.gd +++ b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_text.gd @@ -73,13 +73,18 @@ func play_text(props: Dictionary) -> void: if PopochiuConfig.should_talk_gibberish(): msg = D.create_gibberish(msg) - + # Call the virtual method that modifies the size of the RichTextLabel in case the dialog style # requires it. await _modify_size(msg, props.position) + rich_text_label.push_color(props.color) + # Assign the text and align mode - _append_text(msg, props) + if PopochiuConfig.should_talk_gibberish(): + _append_text(D.create_gibberish(msg), props) + else: + _append_text(msg, props) if _secs_per_character > 0.0: # The text will appear with an animation @@ -168,46 +173,37 @@ func _modify_size(_msg: String, _target_position: Vector2) -> void: ## Creates a RichTextLabel to calculate the resulting size of this node once the whole text is shown. +## Uses a RichTextLabel to provide the "get_parsed_text" function, and a label within it to work out +## the minimum size. Calculating the size from just the RichTextLabel does not work. func _calculate_size(msg: String) -> Vector2: var rt := RichTextLabel.new() rt.add_theme_font_override("normal_font", get_theme_font("normal_font")) rt.bbcode_enabled = true rt.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART rt.text = msg - rt.size = get_meta(DFLT_SIZE) rich_text_label.add_child(rt) # Create a Label to check if the text exceeds the wrap_width var lbl := Label.new() lbl.add_theme_font_override("normal_font", get_theme_font("normal_font")) - _set_default_label_size(lbl) - lbl.text = rt.get_parsed_text() + lbl.size = lbl.get_minimum_size() rich_text_label.add_child(lbl) - rt.clear() - rt.text = "" - - await get_tree().process_frame - var _size := lbl.size if _size.x > wrap_width: - # This node will have the width of the wrap_width + # This node will have the width of the wrap_width. + # The size.y value will calculate automatically. _size.x = wrap_width rt.fit_content = true rt.size.x = _size.x rt.text = msg - - await get_tree().process_frame - + + # Size is recalculated after the frame changes + await get_tree().process_frame _size = rt.size - else: - # This node will have the width of the text - _size.y = get_meta(DFLT_SIZE).y - - var characters_count := lbl.get_total_character_count() lbl.free() rt.free() From 85366a11d6528a1b6a63322f1a9a8fff2478f390 Mon Sep 17 00:00:00 2001 From: Balloonpopper Date: Thu, 5 Dec 2024 21:15:42 +1100 Subject: [PATCH 5/7] issue_360 overhead dialog now positioned correctly and line breaks inserted between lines --- .../dialog_overhead/dialog_overhead.gd | 31 +++++++++++++++++-- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_overhead/dialog_overhead.gd b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_overhead/dialog_overhead.gd index 83218acf..0a95fa59 100644 --- a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_overhead/dialog_overhead.gd +++ b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_overhead/dialog_overhead.gd @@ -4,7 +4,6 @@ extends PopochiuDialogText #region Private #################################################################################### func _modify_size(msg: String, target_position: Vector2) -> void: var _size := await _calculate_size(msg) - # Define size and position (before calculating overflow) rich_text_label.size = _size rich_text_label.position = target_position - rich_text_label.size / 2.0 @@ -27,13 +26,13 @@ func _set_default_label_size(lbl: Label) -> void: func _append_text(msg: String, props: Dictionary) -> void: + msg = _correct_line_breaks(msg) + var center: float = floor(rich_text_label.position.x + (rich_text_label.size.x / 2)) if center == props.position.x: rich_text_label.text = "[center]%s[/center]" % msg elif center < props.position.x: rich_text_label.text = "[right]%s[/right]" % msg - else: - rich_text_label.text = msg func _get_icon_from_position() -> float: @@ -44,4 +43,30 @@ func _get_icon_to_position() -> float: return rich_text_label.size.y / 2.0 + 3.0 +## Appends text for the dialog caption +## Ensures that where a printing a word would see it wrap to the next line that the newline +## is forced into the text. Without this the tween in dialog_text.gd would print part of the word +## until it runs out of space, then erase the part word and rewrite it on the next line which looks +## messy. +func _correct_line_breaks(msg: String) -> String: + rich_text_label.text = msg + var number_of_lines_of_text := rich_text_label.get_line_count() + if number_of_lines_of_text > 1: + var current_line_number := 0 + for current_character in range(0, rich_text_label.text.length()): + + var ThisChar = rich_text_label.text[current_character] + var ThisLine = rich_text_label.get_character_line(current_character) + if rich_text_label.get_character_line(current_character) > current_line_number: + current_line_number += 1 + if rich_text_label.text[current_character-1] == " ": + rich_text_label.text[current_character-1] = "\n" + elif rich_text_label.text[current_character-1] != "\n": + rich_text_label.text = rich_text_label.text.left(current_character) + "\n" + rich_text_label.text.right(-current_character) + + if current_line_number == number_of_lines_of_text - 1: + break + return rich_text_label.text + + #endregion From 25f6e53635b77c2481c3f1d3a0a18fb6e04853c0 Mon Sep 17 00:00:00 2001 From: Balloonpopper Date: Thu, 5 Dec 2024 22:01:27 +1100 Subject: [PATCH 6/7] issue_360 Added line splitting function to portrait dialog mode --- .../dialog_portrait/dialog_portrait.gd | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_portrait/dialog_portrait.gd b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_portrait/dialog_portrait.gd index ed7bd4f2..2d942e75 100644 --- a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_portrait/dialog_portrait.gd +++ b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_portrait/dialog_portrait.gd @@ -42,4 +42,34 @@ func _set_default_size() -> void: pass +func _append_text(msg: String, props: Dictionary) -> void: + msg = _correct_line_breaks(msg) + rich_text_label.text = "[color=%s]%s[/color]" % [props.color.to_html(), msg] + + +## Appends text for the dialog caption +## Ensures that where a printing a word would see it wrap to the next line that the newline +## is forced into the text. Without this the tween in dialog_text.gd would print part of the word +## until it runs out of space, then erase the part word and rewrite it on the next line which looks +## messy. +func _correct_line_breaks(msg: String) -> String: + rich_text_label.text = msg + var number_of_lines_of_text := rich_text_label.get_line_count() + if number_of_lines_of_text > 1: + var current_line_number := 0 + for current_character in range(0, rich_text_label.text.length()): + + var ThisChar = rich_text_label.text[current_character] + var ThisLine = rich_text_label.get_character_line(current_character) + if rich_text_label.get_character_line(current_character) > current_line_number: + current_line_number += 1 + if rich_text_label.text[current_character-1] == " ": + rich_text_label.text[current_character-1] = "\n" + elif rich_text_label.text[current_character-1] != "\n": + rich_text_label.text = rich_text_label.text.left(current_character) + "\n" + rich_text_label.text.right(-current_character) + + if current_line_number == number_of_lines_of_text - 1: + break + return rich_text_label.text + #endregion From bf1d6f513f6dd8920c57cd08aeb02294d12ae500 Mon Sep 17 00:00:00 2001 From: Balloonpopper Date: Thu, 5 Dec 2024 22:36:01 +1100 Subject: [PATCH 7/7] issue_360 Fix long lines --- .../dialog_caption/dialog_caption.gd | 21 ++++++++++--------- .../dialog_overhead/dialog_overhead.gd | 6 ++---- .../dialog_portrait/dialog_portrait.gd | 6 ++---- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd index f33af793..21ac60a6 100644 --- a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd +++ b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_caption/dialog_caption.gd @@ -2,7 +2,7 @@ extends PopochiuDialogText #region Private #################################################################################### -func _modify_size(msg: String, target_position: Vector2) -> void: +func _modify_size(msg: String, _target_position: Vector2) -> void: var _size := await _calculate_size(msg) # Define size and position (before calculating overflow) @@ -12,31 +12,32 @@ func _modify_size(msg: String, target_position: Vector2) -> void: rich_text_label.position.y = get_meta(DFLT_POSITION).y - (_size.y - get_meta(DFLT_SIZE).y) +func _append_text(msg: String, props: Dictionary) -> void: + msg = _correct_line_breaks(msg) + rich_text_label.text = "[center][color=%s]%s[/color][/center]" % [props.color.to_html(), msg] + + ## Appends text for the dialog caption ## Ensures that where a printing a word would see it wrap to the next line that the newline ## is forced into the text. Without this the tween in dialog_text.gd would print part of the word -## until it runs out of space, then erase the part word and put it onto the next line which looks +## until it runs out of space, then erase the part word and rewrite it on the next line which looks ## messy. -func _append_text(msg: String, props: Dictionary) -> void: +func _correct_line_breaks(msg: String) -> String: rich_text_label.text = msg var number_of_lines_of_text := rich_text_label.get_line_count() if number_of_lines_of_text > 1: var current_line_number := 0 for current_character in range(0, rich_text_label.text.length()): - - var ThisChar = rich_text_label.text[current_character] - var ThisLine = rich_text_label.get_character_line(current_character) if rich_text_label.get_character_line(current_character) > current_line_number: current_line_number += 1 if rich_text_label.text[current_character-1] == " ": rich_text_label.text[current_character-1] = "\n" elif rich_text_label.text[current_character-1] != "\n": - rich_text_label.text = rich_text_label.text.left(current_character) + "\n" + rich_text_label.text.right(-current_character) + rich_text_label.text = rich_text_label.text.left(current_character) +\ + "\n" + rich_text_label.text.right(-current_character) if current_line_number == number_of_lines_of_text - 1: break - - msg = rich_text_label.text - rich_text_label.text = "[center][color=%s]%s[/color][/center]" % [props.color.to_html(), msg] + return rich_text_label.text #endregion diff --git a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_overhead/dialog_overhead.gd b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_overhead/dialog_overhead.gd index 0a95fa59..fd0fd503 100644 --- a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_overhead/dialog_overhead.gd +++ b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_overhead/dialog_overhead.gd @@ -54,15 +54,13 @@ func _correct_line_breaks(msg: String) -> String: if number_of_lines_of_text > 1: var current_line_number := 0 for current_character in range(0, rich_text_label.text.length()): - - var ThisChar = rich_text_label.text[current_character] - var ThisLine = rich_text_label.get_character_line(current_character) if rich_text_label.get_character_line(current_character) > current_line_number: current_line_number += 1 if rich_text_label.text[current_character-1] == " ": rich_text_label.text[current_character-1] = "\n" elif rich_text_label.text[current_character-1] != "\n": - rich_text_label.text = rich_text_label.text.left(current_character) + "\n" + rich_text_label.text.right(-current_character) + rich_text_label.text = rich_text_label.text.left(current_character) + "\n" +\ + rich_text_label.text.right(-current_character) if current_line_number == number_of_lines_of_text - 1: break diff --git a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_portrait/dialog_portrait.gd b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_portrait/dialog_portrait.gd index 2d942e75..39ed27b9 100644 --- a/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_portrait/dialog_portrait.gd +++ b/addons/popochiu/engine/objects/gui/components/dialog_text/dialog_portrait/dialog_portrait.gd @@ -58,15 +58,13 @@ func _correct_line_breaks(msg: String) -> String: if number_of_lines_of_text > 1: var current_line_number := 0 for current_character in range(0, rich_text_label.text.length()): - - var ThisChar = rich_text_label.text[current_character] - var ThisLine = rich_text_label.get_character_line(current_character) if rich_text_label.get_character_line(current_character) > current_line_number: current_line_number += 1 if rich_text_label.text[current_character-1] == " ": rich_text_label.text[current_character-1] = "\n" elif rich_text_label.text[current_character-1] != "\n": - rich_text_label.text = rich_text_label.text.left(current_character) + "\n" + rich_text_label.text.right(-current_character) + rich_text_label.text = rich_text_label.text.left(current_character) + "\n" +\ + rich_text_label.text.right(-current_character) if current_line_number == number_of_lines_of_text - 1: break