-
-
Notifications
You must be signed in to change notification settings - Fork 23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
issue_360 Fix dialog_caption size #361
base: develop
Are you sure you want to change the base?
Changes from all commits
3bc3b6a
44ecc0f
62a0acd
e3f6756
85366a1
25f6e53
bf1d6f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,14 +4,40 @@ 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.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) | ||
|
||
|
||
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 rewrite it on the next line which looks | ||
## messy. | ||
func _correct_line_breaks(msg: String) -> String: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think |
||
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()): | ||
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 | ||
Comment on lines
+25
to
+41
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like what this code does, but it shouldn't be duplicated in the 3 DialogText components. You should move this to the |
||
|
||
#endregion |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) | ||
|
||
Comment on lines
+29
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Assign the |
||
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,28 @@ 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()): | ||
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 | ||
|
||
|
||
Comment on lines
+46
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this after moving this function to the |
||
#endregion |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,4 +42,32 @@ 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()): | ||
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 | ||
|
||
Comment on lines
+50
to
+72
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this after moving this function to the |
||
#endregion |
Original file line number | Diff line number | Diff line change | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -73,14 +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) | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We removed this in #357 because it was causing issues when centering text. Please remove it again since each DialogText component should assign the color itself using BBCode ( |
||||||||||||
|
||||||||||||
# Assign the text and align mode | ||||||||||||
msg = "[color=%s]%s[/color]" % [props.color.to_html(), msg] | ||||||||||||
balloonpopper marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||
_append_text(msg, props) | ||||||||||||
if PopochiuConfig.should_talk_gibberish(): | ||||||||||||
_append_text(D.create_gibberish(msg), props) | ||||||||||||
else: | ||||||||||||
_append_text(msg, props) | ||||||||||||
Comment on lines
+84
to
+87
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be just this since the gibberish text replacement was already done in line 75.
Suggested change
|
||||||||||||
|
||||||||||||
if _secs_per_character > 0.0: | ||||||||||||
# The text will appear with an animation | ||||||||||||
|
@@ -169,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. | ||||||||||||
Comment on lines
175
to
+177
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you want to replace the description of the function, please remove the previous one. Also, I suggest a small change to make it clear what the function really does.
Suggested change
|
||||||||||||
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() | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is not necessary since the |
||||||||||||
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() | ||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the ideal approach in this case is not to modify the width (
size.x
) and X position of the RichTextLabel. Considering how this Control is configured in Godot, what we should do is ensure that the value defined in thewrap_width
property is assigned to thesize.x
property of the RichTextLabel in the_ready()
function. To control the Y position of the component, Godot's anchors can be used.