Skip to content

Commit

Permalink
Improve text systems and TextBubble layout system (#1579)
Browse files Browse the repository at this point in the history
# Text systems:
- adds `about_to_show` signal to text subsystem. 
- adds enabled flag to text node

# TextBubble Layout
The text bubble layout will now display a different text bubble for each "registered" character and use a fallback one (that has no position tracking) for all other characters.
Characters can be registered with the register_character() method that takes a dialogic character resource and a node2D to track to.
  • Loading branch information
Jowan-Spooner committed May 24, 2023
1 parent 55576ad commit 14ad9ba
Show file tree
Hide file tree
Showing 8 changed files with 363 additions and 261 deletions.
Original file line number Diff line number Diff line change
@@ -1,81 +1,39 @@
extends Control
extends CanvasLayer

enum SizingModes {AdjustAlways, AdjustOnStart, Fixed}
@export var sizing_mode: SizingModes = SizingModes.AdjustAlways

@export var max_width :float= 0
@export var max_lines = 10
## This layout won't do anything on it's own
var bubbles :Array[Dictionary] = []
var fallback_bubble :Control = null


func _ready():
if max_width <= 0:
max_width = get_viewport().size.x/2


func _on_DialogText_continued_revealing_text(new_character = ""):
if sizing_mode == SizingModes.AdjustAlways:
var font = $DialogText.get_theme_font("normal_font")
var line_height = font.get_height()
var longest_line_len = 0
var lines = 0
for line in $DialogText.text.substr(0, $DialogText.visible_characters).split("\n"):
longest_line_len = font.get_string_size(line).x if font.get_string_size(line).x > longest_line_len else longest_line_len
if font.get_string_size(line).x > max_width-60:
lines += ceil(font.get_string_size(line).x/(max_width-60))-1
lines += 1

# because there is a margin and a number inside the stylebox (especially to the left) this needs to be added
longest_line_len += 100
if sizing_mode == SizingModes.AdjustAlways:
custom_minimum_size.x = min(max_width, longest_line_len)
# a margin has to be added vertically as well because of the stylebox
custom_minimum_size.y = line_height*min(lines, max_lines)+20
# Enable Scroll bar when more then max lines
$DialogText.scroll_active = lines > max_lines

elif lines*line_height+40 > $DialogText.size.y:
$DialogText.scroll_active = true
Dialogic.Text.about_to_show_text.connect(_on_dialogic_text_event)

if $DialogText.scroll_active:
# $DialogText.get_v_scroll_bar().position.x = $DialogText.size.x-20
# $DialogText.get_v_scroll_bar().margin_right = -20
$DialogText.scroll_to_line($DialogText.get_line_count())


func _on_dialog_text_started_revealing_text():
print($NameLabel/DialogicNode_NameLabel.text)
if sizing_mode == SizingModes.AdjustOnStart:
var font = $DialogText.get_theme_font("normal_font")
var line_height = font.get_height()
var longest_line_len = 0
var lines = 0
for line in $DialogText.text.split("\n"):
longest_line_len = font.get_string_size(line).x if font.get_string_size(line).x > longest_line_len else longest_line_len
if font.get_string_size(line).x > max_width-60:
lines += ceil(font.get_string_size(line).x/(max_width-60))-1
lines += 1
var control := Control.new()
add_child(control)
control.position = control.get_viewport_rect().size / 2.0
fallback_bubble = preload("res://addons/dialogic/Modules/DefaultStyles/TextBubble/TextBubble.tscn").instantiate()
fallback_bubble.speaker_node = control
fallback_bubble.get_node('Tail').hide()
fallback_bubble.safe_zone = 0
fallback_bubble.base_direction = Vector2.ZERO

add_child(fallback_bubble)

# because there is a margin and a number inside the stylebox (especially to the left) this needs to be added
longest_line_len += 100
custom_minimum_size.x = min(max_width, longest_line_len)
# a margin has to be added vertically as well because of the stylebox
custom_minimum_size.y = line_height*min(lines, max_lines)
if Dialogic.Choices.is_question(Dialogic.current_event_idx):
custom_minimum_size.y += 80
$DialogText.offset_bottom = -25

# Enable Scroll bar when more then max lines
$DialogText.scroll_active = lines > max_lines

if lines*line_height+40 > $DialogText.size.y:
$DialogText.scroll_active = true
else:
$DialogText.offset_bottom = -7
func register_character(character:DialogicCharacter, node:Node2D):
var new_bubble := preload("res://addons/dialogic/Modules/DefaultStyles/TextBubble/TextBubble.tscn").instantiate()
new_bubble.speaker_node = node
add_child(new_bubble)
bubbles.append({'node':node, 'bubble':new_bubble, 'character':character})


func _on_dialog_text_finished_revealing_text():
if sizing_mode == SizingModes.AdjustAlways:
if Dialogic.Choices.is_question(Dialogic.current_event_idx):
$DialogText.offset_bottom = -25
func _on_dialogic_text_event(info:Dictionary):
var no_bubble_open := true
for b in bubbles:
if b.character == info.character:
no_bubble_open = false
b.bubble.open()
else:
$DialogText.offset_bottom = -7
b.bubble.close()
if no_bubble_open:
fallback_bubble.open()
Original file line number Diff line number Diff line change
@@ -1,191 +1,8 @@
[gd_scene load_steps=14 format=3]
[gd_scene load_steps=2 format=3 uid="uid://syki6k0e6aac"]

[ext_resource type="Script" path="res://addons/dialogic/Modules/DefaultStyles/TextBubble/DialogicTextBubbleLayout.gd" id="1_502mo"]
[ext_resource type="Texture2D" uid="uid://b3sccqj6l42w6" path="res://addons/dialogic/Example Assets/backgrounds/BubbleEnd.png" id="2_sqahx"]
[ext_resource type="Script" path="res://addons/dialogic/Modules/Text/node_dialog_text.gd" id="3_ou07r"]
[ext_resource type="Script" path="res://addons/dialogic/Modules/Text/node_type_sound.gd" id="4_6ctnn"]
[ext_resource type="Script" path="res://addons/dialogic/Modules/Choice/node_choice_button.gd" id="5_d7df1"]
[ext_resource type="Script" path="res://addons/dialogic/Modules/DefaultStyles/HideWithChild.gd" id="6_dlfb7"]
[ext_resource type="Script" path="res://addons/dialogic/Modules/Text/node_name_label.gd" id="7_xx1xf"]

[sub_resource type="StyleBoxFlat" id="11"]
bg_color = Color(0.847059, 0.627451, 0.364706, 1)
border_width_left = 2
border_width_top = 2
border_width_right = 2
border_width_bottom = 2
border_color = Color(1, 1, 1, 0.768627)
border_blend = true
corner_radius_top_left = 5
corner_radius_top_right = 5
corner_radius_bottom_right = 5
corner_radius_bottom_left = 5

[sub_resource type="StyleBoxFlat" id="12"]
content_margin_left = 5.0
content_margin_right = 5.0
bg_color = Color(0.647059, 0.647059, 0.647059, 1)
border_width_top = 10
border_width_bottom = 10
border_color = Color(0.8, 0.8, 0.8, 0)
corner_radius_top_left = 5
corner_radius_top_right = 5
corner_radius_bottom_right = 5
corner_radius_bottom_left = 5

[sub_resource type="Theme" id="13"]
VScrollBar/styles/grabber = SubResource("11")
VScrollBar/styles/grabber_highlight = SubResource("11")
VScrollBar/styles/grabber_pressed = SubResource("11")
VScrollBar/styles/scroll = SubResource("12")

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_kr20r"]
bg_color = Color(0.970602, 0.970602, 0.970602, 1)
corner_radius_top_left = 20
corner_radius_top_right = 20
corner_radius_bottom_right = 20
corner_radius_bottom_left = 20
shadow_color = Color(0, 0, 0, 0.211765)
shadow_size = 10

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_g4yjl"]
draw_center = false
border_width_bottom = 3
border_color = Color(0.227451, 0.227451, 0.227451, 1)

[sub_resource type="StyleBoxFlat" id="StyleBoxFlat_h6ls0"]
content_margin_left = 5.0
content_margin_right = 5.0
bg_color = Color(0.901961, 0.901961, 0.901961, 1)
corner_radius_top_left = 10
corner_radius_top_right = 10
corner_radius_bottom_right = 10
corner_radius_bottom_left = 10
shadow_color = Color(0, 0, 0, 0.278431)
shadow_size = 5

[node name="Control" type="Control"]
custom_minimum_size = Vector2(100, 75)
layout_mode = 3
anchors_preset = 0
offset_left = -13.0
offset_top = -112.0
offset_right = 87.0
offset_bottom = -37.0
grow_vertical = 0
theme = SubResource("13")
[node name="TextBubbleHolder" type="CanvasLayer"]
script = ExtResource("1_502mo")
sizing_mode = 1
max_width = 200.0

[node name="BubbleEnd" type="TextureRect" parent="."]
layout_mode = 1
anchors_preset = 2
anchor_top = 1.0
anchor_bottom = 1.0
offset_left = 8.0
offset_top = -4.0
offset_right = 89.0
offset_bottom = 41.0
grow_vertical = 0
texture = ExtResource("2_sqahx")
expand_mode = 1

[node name="OverlayPanel" type="Panel" parent="."]
layout_mode = 1
anchors_preset = 15
anchor_right = 1.0
anchor_bottom = 1.0
grow_horizontal = 2
grow_vertical = 2
mouse_filter = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_kr20r")

[node name="DialogText" type="RichTextLabel" parent="."]
layout_mode = 1
anchors_preset = -1
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 9.0
offset_top = 8.0
offset_right = -9.0
offset_bottom = -7.0
grow_horizontal = 2
grow_vertical = 2
theme_override_colors/default_color = Color(0, 0, 0, 1)
text = "Some Text"
scroll_active = false
script = ExtResource("3_ou07r")

[node name="DialogicNode_TypeSounds" type="AudioStreamPlayer" parent="DialogText"]
script = ExtResource("4_6ctnn")

[node name="ChoiceContainer" type="HBoxContainer" parent="."]
layout_mode = 1
anchors_preset = 12
anchor_top = 1.0
anchor_right = 1.0
anchor_bottom = 1.0
offset_left = 8.0
offset_top = -31.0
offset_right = -8.0
grow_horizontal = 2
grow_vertical = 0
alignment = 2

[node name="DialogicNode_ChoiceButton" type="Button" parent="ChoiceContainer"]
layout_mode = 2
size_flags_horizontal = 4
theme_override_colors/font_color = Color(0.188235, 0.188235, 0.188235, 1)
theme_override_colors/font_pressed_color = Color(0, 0, 0, 1)
theme_override_colors/font_hover_color = Color(0.682353, 0.482353, 0.137255, 1)
theme_override_colors/font_focus_color = Color(0.27451, 0.180392, 0.0627451, 1)
theme_override_styles/focus = SubResource("StyleBoxFlat_g4yjl")
text = "Yes!"
flat = true
script = ExtResource("5_d7df1")

[node name="DialogicNode_ChoiceButton2" type="Button" parent="ChoiceContainer"]
layout_mode = 2
size_flags_horizontal = 4
theme_override_colors/font_color = Color(0.188235, 0.188235, 0.188235, 1)
theme_override_colors/font_pressed_color = Color(0, 0, 0, 1)
theme_override_colors/font_hover_color = Color(0.682353, 0.482353, 0.137255, 1)
theme_override_colors/font_focus_color = Color(0.27451, 0.180392, 0.0627451, 1)
theme_override_styles/focus = SubResource("StyleBoxFlat_g4yjl")
text = "Yes!"
flat = true
script = ExtResource("5_d7df1")

[node name="DialogicNode_ChoiceButton3" type="Button" parent="ChoiceContainer"]
layout_mode = 2
size_flags_horizontal = 4
theme_override_colors/font_color = Color(0.188235, 0.188235, 0.188235, 1)
theme_override_colors/font_pressed_color = Color(0, 0, 0, 1)
theme_override_colors/font_hover_color = Color(0.682353, 0.482353, 0.137255, 1)
theme_override_colors/font_focus_color = Color(0.27451, 0.180392, 0.0627451, 1)
theme_override_styles/focus = SubResource("StyleBoxFlat_g4yjl")
text = "Yes!"
flat = true
script = ExtResource("5_d7df1")

[node name="NameLabel" type="PanelContainer" parent="."]
layout_mode = 1
anchors_preset = -1
offset_left = 12.0
offset_top = -15.0
offset_right = 16.0
offset_bottom = 11.0
grow_horizontal = 2
theme_override_styles/panel = SubResource("StyleBoxFlat_h6ls0")
script = ExtResource("6_dlfb7")

[node name="DialogicNode_NameLabel" type="Label" parent="NameLabel"]
layout_mode = 2
horizontal_alignment = 1
script = ExtResource("7_xx1xf")

[connection signal="continued_revealing_text" from="DialogText" to="." method="_on_DialogText_continued_revealing_text"]
[connection signal="finished_revealing_text" from="DialogText" to="." method="_on_dialog_text_finished_revealing_text"]
[connection signal="finished_revealing_text" from="DialogText" to="." method="_on_DialogText_continued_revealing_text"]
[connection signal="started_revealing_text" from="DialogText" to="." method="_on_dialog_text_started_revealing_text"]
Loading

0 comments on commit 14ad9ba

Please sign in to comment.