Skip to content

Commit f647625

Browse files
Add support for custom RichTextEffects & Example fade effects (#2607)
* Add support for custom rich text effects These can now be added in the dialogic text settings. * Add a few example fade RichTextEffects
1 parent a5150f6 commit f647625

File tree

11 files changed

+301
-39
lines changed

11 files changed

+301
-39
lines changed
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
@tool
2+
class_name DialogicRichTextTransitionEffect
3+
extends RichTextEffect
4+
5+
var visible_characters := -1
6+
7+
@export var bbcode := "animate_in"
8+
var cache := []
9+
10+
@export_range(0.0, 5.0, 0.01) var time := 0.2
11+
@export_group("Color", "color")
12+
@export var color_modulate: Gradient = null
13+
@export var color_replace: Gradient = null
14+
@export_group("Scale", "scale")
15+
@export var scale_enabled := false
16+
@export var scale_curve := Curve.new()
17+
@export var scale_pivot := Vector2()
18+
@export_group("Position", "position")
19+
@export var position_enabled := false
20+
@export var position_x_curve := Curve.new()
21+
@export var position_y_curve := Curve.new()
22+
23+
@export_group("Test", "test")
24+
@export_range(-0.1, 1.0, 0.1) var test_value := -0.1
25+
26+
var was_skipped := false
27+
var was_reset := false
28+
29+
30+
func reset() -> void:
31+
was_reset = true
32+
was_skipped = false
33+
cache.clear()
34+
35+
36+
func skip() -> void:
37+
was_skipped = true
38+
39+
40+
func _process_custom_fx(char_fx: CharFXTransform) -> bool:
41+
var char_age :float = 0.0
42+
if test_value >= 0:
43+
char_age = test_value
44+
45+
else:
46+
if visible_characters == 0:
47+
cache.clear()
48+
return false
49+
if was_reset:
50+
if visible_characters != -1:
51+
was_reset = false
52+
else:
53+
return false
54+
55+
if len(cache) < visible_characters or visible_characters == -1 or was_skipped:
56+
if char_fx.range.x >= len(cache):
57+
cache.append(char_fx.elapsed_time)
58+
59+
if was_skipped:
60+
for i in range(len(cache)):
61+
cache[i] = char_fx.elapsed_time-time
62+
63+
if len(cache) > char_fx.range.x:
64+
char_age = char_fx.elapsed_time - cache[char_fx.range.x]
65+
66+
var text_server := TextServerManager.get_primary_interface()
67+
var trans: float = clamp(char_age, 0.0, time)/time
68+
69+
if color_replace:
70+
var c := color_replace.sample(trans)
71+
c.a = 1
72+
char_fx.color = char_fx.color.lerp(c, color_replace.sample(trans).a)
73+
if color_modulate:
74+
char_fx.color *= color_modulate.sample(trans)
75+
var glyph_size := text_server.font_get_glyph_size(char_fx.font, Vector2i(16,1), char_fx.glyph_index)
76+
if scale_enabled:
77+
char_fx.transform = char_fx.transform.translated_local(scale_pivot*glyph_size*Vector2(1, -1)*(1-scale_curve.sample(trans)))
78+
char_fx.transform = char_fx.transform.scaled_local(Vector2.ONE*scale_curve.sample(trans))
79+
80+
if position_enabled:
81+
char_fx.transform = char_fx.transform.translated_local(Vector2(position_x_curve.sample(trans), position_y_curve.sample(trans))*glyph_size)
82+
83+
return true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
uid://wf7hpguw17ex
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[gd_resource type="RichTextEffect" script_class="DialogicRichTextTransitionEffect" load_steps=3 format=3 uid="uid://qegqrr4g2riu"]
2+
3+
[ext_resource type="Script" uid="uid://wf7hpguw17ex" path="res://addons/dialogic/Example Assets/bbcode_transitions/bbcode_transition_effect.gd" id="1_5w3vn"]
4+
5+
[sub_resource type="Gradient" id="Gradient_5w3vn"]
6+
colors = PackedColorArray(1, 1, 1, 0, 1, 1, 1, 1)
7+
8+
[resource]
9+
script = ExtResource("1_5w3vn")
10+
bbcode = "fade_in"
11+
time = 0.2
12+
color_modulate = SubResource("Gradient_5w3vn")
13+
scale_enabled = false
14+
scale_pivot = Vector2(0, 0)
15+
position_enabled = false
16+
test_value = -0.1
17+
metadata/_custom_type_script = "uid://wf7hpguw17ex"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[gd_resource type="RichTextEffect" script_class="DialogicRichTextTransitionEffect" load_steps=4 format=3 uid="uid://hx8qyt5ry3h0"]
2+
3+
[ext_resource type="Script" uid="uid://wf7hpguw17ex" path="res://addons/dialogic/Example Assets/bbcode_transitions/bbcode_transition_effect.gd" id="1_rsak6"]
4+
5+
[sub_resource type="Gradient" id="Gradient_rsak6"]
6+
offsets = PackedFloat32Array(0, 0.6351706)
7+
colors = PackedColorArray(1, 1, 1, 0, 1, 1, 1, 1)
8+
9+
[sub_resource type="Curve" id="Curve_oitc5"]
10+
_limits = [0.0, 2.0, 0.0, 1.0]
11+
_data = [Vector2(0, 2), 0.0, 0.0, 0, 0, Vector2(0.40294844, 1.0066038), -0.13865282, 0.0, 0, 0]
12+
point_count = 2
13+
14+
[resource]
15+
script = ExtResource("1_rsak6")
16+
bbcode = "fade_scale_in"
17+
time = 0.30000000000000004
18+
color_modulate = SubResource("Gradient_rsak6")
19+
scale_enabled = true
20+
scale_curve = SubResource("Curve_oitc5")
21+
scale_pivot = Vector2(0.5, 0.5)
22+
position_enabled = false
23+
test_value = -0.1
24+
metadata/_custom_type_script = "uid://wf7hpguw17ex"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[gd_resource type="RichTextEffect" script_class="DialogicRichTextTransitionEffect" load_steps=6 format=3 uid="uid://c8b884puc720d"]
2+
3+
[ext_resource type="Script" uid="uid://wf7hpguw17ex" path="res://addons/dialogic/Example Assets/bbcode_transitions/bbcode_transition_effect.gd" id="1_n3lqs"]
4+
5+
[sub_resource type="Gradient" id="Gradient_n3lqs"]
6+
offsets = PackedFloat32Array(0, 0.45292622)
7+
colors = PackedColorArray(1, 1, 1, 0, 1, 1, 1, 1)
8+
9+
[sub_resource type="Gradient" id="Gradient_lhhwu"]
10+
interpolation_mode = 1
11+
offsets = PackedFloat32Array(0, 0.6666667, 0.86513996)
12+
colors = PackedColorArray(0.5208, 0.76631993, 0.93, 0, 0.5242275, 0.76595265, 0.93170327, 1, 0.5208, 0.76631993, 0.93, 0)
13+
14+
[sub_resource type="Curve" id="Curve_lhhwu"]
15+
_data = [Vector2(0.002457004, 1), 0.0, -1.993977, 0, 0, Vector2(1, 0), 0.0, 0.0, 0, 0]
16+
point_count = 2
17+
18+
[sub_resource type="Curve" id="Curve_4i73d"]
19+
_limits = [0.0, 1.5, 0.0, 1.0]
20+
_data = [Vector2(0, 0.6627359), 0.0, 1.7969435, 0, 0, Vector2(0.4987715, 1.0308962), -1.6188686, -1.6188686, 0, 0, Vector2(0.66093373, 1), -0.34929827, 0.0, 0, 0]
21+
point_count = 3
22+
23+
[resource]
24+
script = ExtResource("1_n3lqs")
25+
bbcode = "fancy_in"
26+
time = 0.4
27+
color_modulate = SubResource("Gradient_n3lqs")
28+
color_replace = SubResource("Gradient_lhhwu")
29+
scale_enabled = true
30+
scale_curve = SubResource("Curve_4i73d")
31+
scale_pivot = Vector2(0.5, 0)
32+
position_enabled = true
33+
position_x_curve = SubResource("Curve_lhhwu")
34+
test_value = -0.1
35+
metadata/_custom_type_script = "uid://wf7hpguw17ex"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[gd_resource type="RichTextEffect" script_class="DialogicRichTextTransitionEffect" load_steps=6 format=3 uid="uid://dnxkgwncm1pt5"]
2+
3+
[ext_resource type="Script" uid="uid://wf7hpguw17ex" path="res://addons/dialogic/Example Assets/bbcode_transitions/bbcode_transition_effect.gd" id="1_ur6c5"]
4+
5+
[sub_resource type="Gradient" id="Gradient_ur6c5"]
6+
offsets = PackedFloat32Array(0, 0.5089058)
7+
colors = PackedColorArray(1, 1, 1, 0, 1, 1, 1, 1)
8+
9+
[sub_resource type="Curve" id="Curve_5qe3f"]
10+
_limits = [-0.5, 0.51556605, 0.0, 1.0]
11+
_data = [Vector2(0, 0.0235914), 0.0, 0.0, 0, 0, Vector2(0.019656021, -0.2441923), 0.0, 0.0, 0, 0, Vector2(0.046683047, 0.17305207), 0.0, 0.0, 0, 0, Vector2(0.1081081, -0.23173726), 0.0, 0.0, 0, 0, Vector2(0.16216215, 0.12323183), 0.0, 0.0, 0, 0, Vector2(0.2997543, -0.16946197), 0.0, 0.0, 0, 0, Vector2(0.38329238, 0.042274), 0.0, 0.0, 0, 0, Vector2(0.46928746, -0.107186675), 0.0, 0.0, 0, 0, Vector2(0.5135135, 0.054729044), 0.0, 0.0, 0, 0, Vector2(0.66339064, -0.07604903), 0.0, 0.0, 0, 0, Vector2(0.86240786, 0.054729044), 0.0, 0.0, 0, 0, Vector2(1, 0), 0.0, 0.0, 0, 0]
12+
point_count = 12
13+
14+
[sub_resource type="Curve" id="Curve_ur6c5"]
15+
_limits = [-0.5, 0.51556605, 0.0, 1.0]
16+
_data = [Vector2(0, 0.042274), 0.0, 0.0, 0, 0, Vector2(0.051597048, -0.13209677), 0.0, 0.0, 0, 0, Vector2(0.09336609, 0.21041724), 0.0, 0.0, 0, 0, Vector2(0.14742014, -0.25664735), 0.0, 0.0, 0, 0, Vector2(0.22850122, 0.098321736), 0.0, 0.0, 0, 0, Vector2(0.31203932, -0.107186675), 0.0, 0.0, 0, 0, Vector2(0.44717443, 0.054729044), 0.0, 0.0, 0, 0, Vector2(0.5995086, -0.08227658), 0.0, 0.0, 0, 0, Vector2(0.8132678, 0.042274), 0.0, 0.0, 0, 0, Vector2(1, 0), 0.0, 0.0, 0, 0]
17+
point_count = 10
18+
19+
[sub_resource type="Curve" id="Curve_qelc7"]
20+
_data = [Vector2(0, 0.57688683), 0.0, 0.0, 0, 0, Vector2(0.14250615, 0.77311325), 0.0, 0.0, 0, 0, Vector2(0.2850123, 0.6443397), 0.0, 0.0, 0, 0, Vector2(0.41769046, 0.7976416), 0.0, 0.0, 0, 0, Vector2(0.5503686, 0.74245286), 0.0, 0.0, 0, 0, Vector2(0.6781328, 0.8712265), 0.0, 0.0, 0, 0, Vector2(0.79606885, 0.8528303), 0.0, 0.0, 0, 0, Vector2(1, 1), 0.0, 0.0, 0, 0]
21+
point_count = 8
22+
23+
[resource]
24+
script = ExtResource("1_ur6c5")
25+
bbcode = "shaky_in"
26+
time = 0.4
27+
color_modulate = SubResource("Gradient_ur6c5")
28+
scale_enabled = true
29+
scale_curve = SubResource("Curve_qelc7")
30+
scale_pivot = Vector2(0.5, 0.5)
31+
position_enabled = true
32+
position_x_curve = SubResource("Curve_5qe3f")
33+
position_y_curve = SubResource("Curve_ur6c5")
34+
test_value = -0.1
35+
metadata/_custom_type_script = "uid://wf7hpguw17ex"

addons/dialogic/Modules/Text/event_text.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ func _execute() -> void:
133133
var segment: String = dialogic.Text.parse_text(split_text[section_idx][0])
134134
var is_append: bool = split_text[section_idx][1]
135135

136-
final_text = segment
136+
final_text = ProjectSettings.get_setting("dialogic/text/dialog_text_prefix", "")+segment
137137
dialogic.Text.about_to_show_text.emit({'text':final_text, 'character':character, 'portrait':portrait, 'append': is_append})
138138

139139
await dialogic.Text.update_textbox(final_text, false)

addons/dialogic/Modules/Text/node_dialog_text.gd

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ func _ready() -> void:
5252
textbox_root.hide()
5353
text = ""
5454

55+
var custom_bbcode_effects: Array = ProjectSettings.get_setting("dialogic/text/custom_bbcode_effects", "").split(",")
56+
for i in custom_bbcode_effects:
57+
var x : Resource = load(i.strip_edges())
58+
if x is RichTextEffect:
59+
custom_effects.append(x)
60+
5561

5662
# this is called by the DialogicGameHandler to set text
5763

@@ -60,6 +66,8 @@ func reveal_text(_text: String, keep_previous:=false) -> void:
6066
return
6167
show()
6268

69+
custom_fx_reset()
70+
6371
if !keep_previous:
6472
text = _text
6573
base_visible_characters = 0
@@ -73,6 +81,7 @@ func reveal_text(_text: String, keep_previous:=false) -> void:
7381
else:
7482
base_visible_characters = len(text)
7583
visible_characters = len(get_parsed_text())
84+
custom_fx_update()
7685
text = text + _text
7786

7887
# If Auto-Skip is enabled and we append the text (keep_previous),
@@ -113,16 +122,21 @@ func continue_reveal() -> void:
113122

114123
if visible_characters > -1 and visible_characters <= len(get_parsed_text()):
115124
continued_revealing_text.emit(get_parsed_text()[visible_characters-1])
125+
126+
custom_fx_update()
116127
else:
117-
finish_text()
128+
finish_text(true)
118129
# if the text finished organically, add a small input block
119130
# this prevents accidental skipping when you expected the text to be longer
120131
DialogicUtil.autoload().Inputs.block_input(ProjectSettings.get_setting('dialogic/text/advance_delay', 0.1))
121132

122133

123134
## Reveals the entire text instantly.
124-
func finish_text() -> void:
135+
func finish_text(is_organic := false) -> void:
125136
visible_ratio = 1
137+
custom_fx_update()
138+
if not is_organic:
139+
custom_fx_skip()
126140
DialogicUtil.autoload().Text.execute_effects(-1, self, true)
127141
revealing = false
128142
DialogicUtil.autoload().current_state = DialogicGameHandler.States.IDLE
@@ -156,3 +170,21 @@ func _on_meta_clicked(_meta:Variant) -> void:
156170
## Handle mouse input
157171
func on_gui_input(event:InputEvent) -> void:
158172
DialogicUtil.autoload().Inputs.handle_node_gui_input(event)
173+
174+
175+
func custom_fx_update() -> void:
176+
for effect in custom_effects:
177+
if "visible_characters" in effect:
178+
effect.visible_characters = visible_characters
179+
180+
181+
func custom_fx_reset() -> void:
182+
for effect in custom_effects:
183+
if effect.has_method("reset"):
184+
effect.reset()
185+
186+
187+
func custom_fx_skip() -> void:
188+
for effect in custom_effects:
189+
if effect.has_method("skip"):
190+
effect.skip()

addons/dialogic/Modules/Text/settings_text.gd

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const _SETTING_TEXT_REVEAL_SKIPPABLE_DELAY := 'dialogic/text/text_reveal_skip_d
1212
const _SETTING_TEXT_ADVANCE_DELAY := 'dialogic/text/advance_delay'
1313

1414
const _SETTING_AUTOCOLOR_NAMES := 'dialogic/text/autocolor_names'
15+
const _SETTING_TEXT_PREFIX := 'dialogic/text/dialog_text_prefix'
16+
const _SETTING_CUSTOM_BBCODE_EFFECTS := 'dialogic/text/custom_bbcode_effects'
1517
const _SETTING_SPLIT_AT_NEW_LINES := 'dialogic/text/split_at_new_lines'
1618
const _SETTING_SPLIT_AT_NEW_LINES_AS := 'dialogic/text/split_at_new_lines_as'
1719

@@ -44,6 +46,8 @@ func _ready() -> void:
4446
%AdvanceDelay.value_changed.connect(_on_float_set.bind(_SETTING_TEXT_ADVANCE_DELAY))
4547

4648
%AutocolorNames.toggled.connect(_on_bool_set.bind(_SETTING_AUTOCOLOR_NAMES))
49+
%TextPrefix.text_changed.connect(_on_string_set.bind(_SETTING_TEXT_PREFIX))
50+
%CustomBBCodeEffects.text_changed.connect(_on_string_set.bind(_SETTING_CUSTOM_BBCODE_EFFECTS))
4751

4852
%NewEvents.toggled.connect(_on_bool_set.bind(_SETTING_SPLIT_AT_NEW_LINES))
4953

@@ -69,6 +73,8 @@ func _refresh() -> void:
6973
%AdvanceDelay.value = ProjectSettings.get_setting(_SETTING_TEXT_ADVANCE_DELAY, 0.1)
7074

7175
%AutocolorNames.button_pressed = ProjectSettings.get_setting(_SETTING_AUTOCOLOR_NAMES, false)
76+
%TextPrefix.text = ProjectSettings.get_setting(_SETTING_TEXT_PREFIX, "")
77+
%CustomBBCodeEffects.text = ProjectSettings.get_setting(_SETTING_CUSTOM_BBCODE_EFFECTS, "")
7278

7379
%NewEvents.button_pressed = ProjectSettings.get_setting(_SETTING_SPLIT_AT_NEW_LINES, false)
7480
%NewEventOption.select(ProjectSettings.get_setting(_SETTING_SPLIT_AT_NEW_LINES_AS, 0))
@@ -115,14 +121,18 @@ func _on_float_set(value:float, setting:String) -> void:
115121
ProjectSettings.save()
116122

117123

124+
func _on_string_set(value:String, setting:String) -> void:
125+
ProjectSettings.set_setting(setting, value)
126+
ProjectSettings.save()
127+
118128
#region BEHAVIOUR
119129
################################################################################
120130

121-
func _on_InputAction_value_changed(property_name:String, value:String) -> void:
131+
func _on_InputAction_value_changed(_property_name:String, value:String) -> void:
122132
ProjectSettings.set_setting(_SETTING_INPUT_ACTION, value)
123133
ProjectSettings.save()
124134

125-
func suggest_actions(search:String) -> Dictionary:
135+
func suggest_actions(_search:String) -> Dictionary:
126136
var suggs := {}
127137
for prop in ProjectSettings.get_property_list():
128138
if prop.name.begins_with('input/') and not prop.name.begins_with('input/ui_') :

0 commit comments

Comments
 (0)