Skip to content

Commit b1b873a

Browse files
committed
Merge pull request #104731 from Delsin-Yu/stackable-text-effect
[GUI] Implement Stackable Text Outline on `Label`
2 parents bc21c32 + ac931c3 commit b1b873a

File tree

5 files changed

+496
-90
lines changed

5 files changed

+496
-90
lines changed

doc/classes/LabelSettings.xml

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,127 @@
88
</description>
99
<tutorials>
1010
</tutorials>
11+
<methods>
12+
<method name="add_stacked_outline">
13+
<return type="void" />
14+
<param index="0" name="index" type="int" default="-1" />
15+
<description>
16+
Adds a new stacked outline to the label at the given [param index]. If [param index] is [code]-1[/code], the new stacked outline will be added at the end of the list.
17+
</description>
18+
</method>
19+
<method name="add_stacked_shadow">
20+
<return type="void" />
21+
<param index="0" name="index" type="int" default="-1" />
22+
<description>
23+
Adds a new stacked shadow to the label at the given [param index]. If [param index] is [code]-1[/code], the new stacked shadow will be added at the end of the list.
24+
</description>
25+
</method>
26+
<method name="get_stacked_outline_color" qualifiers="const">
27+
<return type="Color" />
28+
<param index="0" name="index" type="int" />
29+
<description>
30+
Returns the color of the stacked outline at [param index].
31+
</description>
32+
</method>
33+
<method name="get_stacked_outline_size" qualifiers="const">
34+
<return type="int" />
35+
<param index="0" name="index" type="int" />
36+
<description>
37+
Returns the size of the stacked outline at [param index].
38+
</description>
39+
</method>
40+
<method name="get_stacked_shadow_color" qualifiers="const">
41+
<return type="Color" />
42+
<param index="0" name="index" type="int" />
43+
<description>
44+
Returns the color of the stacked shadow at [param index].
45+
</description>
46+
</method>
47+
<method name="get_stacked_shadow_offset" qualifiers="const">
48+
<return type="Vector2" />
49+
<param index="0" name="index" type="int" />
50+
<description>
51+
Returns the offset of the stacked shadow at [param index].
52+
</description>
53+
</method>
54+
<method name="get_stacked_shadow_outline_size" qualifiers="const">
55+
<return type="int" />
56+
<param index="0" name="index" type="int" />
57+
<description>
58+
Returns the outline size of the stacked shadow at [param index].
59+
</description>
60+
</method>
61+
<method name="move_stacked_outline">
62+
<return type="void" />
63+
<param index="0" name="from_index" type="int" />
64+
<param index="1" name="to_position" type="int" />
65+
<description>
66+
Moves the stacked outline at index [param from_index] to the given position [param to_position] in the array.
67+
</description>
68+
</method>
69+
<method name="move_stacked_shadow">
70+
<return type="void" />
71+
<param index="0" name="from_index" type="int" />
72+
<param index="1" name="to_position" type="int" />
73+
<description>
74+
Moves the stacked shadow at index [param from_index] to the given position [param to_position] in the array.
75+
</description>
76+
</method>
77+
<method name="remove_stacked_outline">
78+
<return type="void" />
79+
<param index="0" name="index" type="int" />
80+
<description>
81+
Removes the stacked outline at index [param index].
82+
</description>
83+
</method>
84+
<method name="remove_stacked_shadow">
85+
<return type="void" />
86+
<param index="0" name="index" type="int" />
87+
<description>
88+
Removes the stacked shadow at index [param index].
89+
</description>
90+
</method>
91+
<method name="set_stacked_outline_color">
92+
<return type="void" />
93+
<param index="0" name="index" type="int" />
94+
<param index="1" name="color" type="Color" />
95+
<description>
96+
Sets the color of the stacked outline identified by the given [param index] to [param color].
97+
</description>
98+
</method>
99+
<method name="set_stacked_outline_size">
100+
<return type="void" />
101+
<param index="0" name="index" type="int" />
102+
<param index="1" name="size" type="int" />
103+
<description>
104+
Sets the size of the stacked outline identified by the given [param index] to [param size].
105+
</description>
106+
</method>
107+
<method name="set_stacked_shadow_color">
108+
<return type="void" />
109+
<param index="0" name="index" type="int" />
110+
<param index="1" name="color" type="Color" />
111+
<description>
112+
Sets the color of the stacked shadow identified by the given [param index] to [param color].
113+
</description>
114+
</method>
115+
<method name="set_stacked_shadow_offset">
116+
<return type="void" />
117+
<param index="0" name="index" type="int" />
118+
<param index="1" name="offset" type="Vector2" />
119+
<description>
120+
Sets the offset of the stacked shadow identified by the given [param index] to [param offset].
121+
</description>
122+
</method>
123+
<method name="set_stacked_shadow_outline_size">
124+
<return type="void" />
125+
<param index="0" name="index" type="int" />
126+
<param index="1" name="size" type="int" />
127+
<description>
128+
Sets the outline size of the stacked shadow identified by the given [param index] to [param size].
129+
</description>
130+
</method>
131+
</methods>
11132
<members>
12133
<member name="font" type="Font" setter="set_font" getter="get_font">
13134
[Font] used for the text.
@@ -39,5 +160,11 @@
39160
<member name="shadow_size" type="int" setter="set_shadow_size" getter="get_shadow_size" default="1">
40161
Size of the shadow effect.
41162
</member>
163+
<member name="stacked_outline_count" type="int" setter="set_stacked_outline_count" getter="get_stacked_outline_count" default="0">
164+
The number of stacked outlines.
165+
</member>
166+
<member name="stacked_shadow_count" type="int" setter="set_stacked_shadow_count" getter="get_stacked_shadow_count" default="0">
167+
Returns the stacked shadow count.
168+
</member>
42169
</members>
43170
</class>

scene/gui/label.cpp

Lines changed: 56 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -431,13 +431,13 @@ inline void draw_glyph_shadow(const Glyph &p_gl, const RID &p_canvas, const Colo
431431
}
432432
}
433433

434-
inline void draw_glyph_shadow_outline(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_shadow_color, int p_shadow_outline_size, const Vector2 &p_ofs, const Vector2 &shadow_ofs) {
434+
inline void draw_glyph_shadow_outline(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_shadow_color, const Vector2 &p_ofs, int p_shadow_outline_size, const Vector2 &shadow_ofs) {
435435
if (p_gl.font_rid != RID()) {
436436
TS->font_draw_glyph_outline(p_gl.font_rid, p_canvas, p_gl.font_size, p_shadow_outline_size, p_ofs + Vector2(p_gl.x_off, p_gl.y_off) + shadow_ofs, p_gl.index, p_font_shadow_color);
437437
}
438438
}
439439

440-
inline void draw_glyph_outline(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_outline_color, int p_outline_size, const Vector2 &p_ofs) {
440+
inline void draw_glyph_outline(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_outline_color, const Vector2 &p_ofs, int p_outline_size) {
441441
if (p_gl.font_rid != RID()) {
442442
if (p_font_outline_color.a != 0.0 && p_outline_size > 0) {
443443
TS->font_draw_glyph_outline(p_gl.font_rid, p_canvas, p_gl.font_size, p_outline_size, p_ofs + Vector2(p_gl.x_off, p_gl.y_off), p_gl.index, p_font_outline_color);
@@ -758,6 +758,8 @@ void Label::_notification(int p_what) {
758758
Color font_outline_color = has_settings ? settings->get_outline_color() : theme_cache.font_outline_color;
759759
int outline_size = has_settings ? settings->get_outline_size() : theme_cache.font_outline_size;
760760
int shadow_outline_size = has_settings ? settings->get_shadow_size() : theme_cache.font_shadow_outline_size;
761+
Vector<LabelSettings::StackedOutlineData> stacked_outline_datas = has_settings ? settings->get_stacked_outline_data() : Vector<LabelSettings::StackedOutlineData>();
762+
Vector<LabelSettings::StackedShadowData> stacked_shadow_datas = has_settings ? settings->get_stacked_shadow_data() : Vector<LabelSettings::StackedShadowData>();
761763
bool rtl_layout = is_layout_rtl();
762764

763765
if (has_focus()) {
@@ -813,95 +815,67 @@ void Label::_notification(int p_what) {
813815

814816
ofs.y += asc;
815817

816-
// Draw shadow, outline and text. Note: Do not merge this into the single loop iteration, to prevent overlaps.
818+
// Draw text effects and main texts. Note: Do not merge this into the single loop iteration, to prevent overlaps.
817819
int processed_glyphs_step = 0;
818-
for (int step = DRAW_STEP_SHADOW_OUTLINE; step < DRAW_STEP_MAX; step++) {
819-
if (step == DRAW_STEP_SHADOW_OUTLINE && (font_shadow_color.a == 0 || shadow_outline_size <= 0)) {
820-
continue;
821-
}
822-
if (step == DRAW_STEP_SHADOW && (font_shadow_color.a == 0)) {
823-
continue;
824-
}
825-
if (step == DRAW_STEP_OUTLINE && (outline_size <= 0 || font_outline_color.a == 0)) {
826-
continue;
827-
}
828820

829-
processed_glyphs_step = processed_glyphs;
830-
Vector2 offset_step = ofs;
831-
// Draw RTL ellipsis string when necessary.
832-
if (rtl && ellipsis_pos >= 0) {
833-
for (int gl_idx = ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
834-
for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
835-
bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end + para.start > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
836-
if (!skip) {
837-
if (step == DRAW_STEP_SHADOW_OUTLINE) {
838-
draw_glyph_shadow_outline(ellipsis_glyphs[gl_idx], ci, font_shadow_color, shadow_outline_size, offset_step, shadow_ofs);
839-
} else if (step == DRAW_STEP_SHADOW) {
840-
draw_glyph_shadow(ellipsis_glyphs[gl_idx], ci, font_shadow_color, offset_step, shadow_ofs);
841-
} else if (step == DRAW_STEP_OUTLINE) {
842-
draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_outline_color, outline_size, offset_step);
843-
} else if (step == DRAW_STEP_TEXT) {
844-
draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, offset_step);
845-
}
846-
}
847-
processed_glyphs_step++;
848-
offset_step.x += ellipsis_glyphs[gl_idx].advance;
849-
}
821+
// Draw shadow outline.
822+
if (font_shadow_color.a != 0 && shadow_outline_size > 0) {
823+
draw_text(rtl, ellipsis_pos, ellipsis_gl_size, ellipsis_glyphs, trim_chars, para.start, visible_chars, trim_glyphs_ltr, processed_glyphs_step, processed_glyphs, visible_glyphs, trim_glyphs_rtl, total_glyphs, ci, ofs, gl_size, trim_pos, glyphs, font_shadow_color, draw_glyph_shadow_outline, shadow_outline_size, shadow_ofs);
824+
}
825+
826+
// Draw shadow.
827+
if (font_shadow_color.a > 0) {
828+
draw_text(rtl, ellipsis_pos, ellipsis_gl_size, ellipsis_glyphs, trim_chars, para.start, visible_chars, trim_glyphs_ltr, processed_glyphs_step, processed_glyphs, visible_glyphs, trim_glyphs_rtl, total_glyphs, ci, ofs, gl_size, trim_pos, glyphs, font_shadow_color, draw_glyph_shadow, shadow_ofs);
829+
}
830+
831+
// Draw stacked shadow.
832+
if (stacked_shadow_datas.size() != 0) {
833+
int draw_iterations = stacked_shadow_datas.size();
834+
835+
for (int draw_iteration_index = draw_iterations - 1; draw_iteration_index >= 0; --draw_iteration_index) {
836+
LabelSettings::StackedShadowData stacked_shadow_data = stacked_shadow_datas[draw_iteration_index];
837+
if (stacked_shadow_data.outline_size > 0) {
838+
draw_text(rtl, ellipsis_pos, ellipsis_gl_size, ellipsis_glyphs, trim_chars, para.start, visible_chars, trim_glyphs_ltr, processed_glyphs_step, processed_glyphs, visible_glyphs, trim_glyphs_rtl, total_glyphs, ci, ofs, gl_size, trim_pos, glyphs, stacked_shadow_data.color, draw_glyph_shadow_outline, stacked_shadow_data.outline_size, stacked_shadow_data.offset);
850839
}
840+
841+
draw_text(rtl, ellipsis_pos, ellipsis_gl_size, ellipsis_glyphs, trim_chars, para.start, visible_chars, trim_glyphs_ltr, processed_glyphs_step, processed_glyphs, visible_glyphs, trim_glyphs_rtl, total_glyphs, ci, ofs, gl_size, trim_pos, glyphs, stacked_shadow_data.color, draw_glyph_shadow, stacked_shadow_data.offset);
851842
}
852-
// Draw main text.
853-
for (int j = 0; j < gl_size; j++) {
854-
// Trim when necessary.
855-
if (trim_pos >= 0) {
856-
if (rtl) {
857-
if (j < trim_pos) {
858-
continue;
859-
}
860-
} else {
861-
if (j >= trim_pos) {
862-
break;
863-
}
864-
}
865-
}
866-
for (int k = 0; k < glyphs[j].repeat; k++) {
867-
bool skip = (trim_chars && glyphs[j].end + para.start > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
868-
if (!skip) {
869-
if (step == DRAW_STEP_SHADOW_OUTLINE) {
870-
draw_glyph_shadow_outline(glyphs[j], ci, font_shadow_color, shadow_outline_size, offset_step, shadow_ofs);
871-
} else if (step == DRAW_STEP_SHADOW) {
872-
draw_glyph_shadow(glyphs[j], ci, font_shadow_color, offset_step, shadow_ofs);
873-
} else if (step == DRAW_STEP_OUTLINE) {
874-
draw_glyph_outline(glyphs[j], ci, font_outline_color, outline_size, offset_step);
875-
} else if (step == DRAW_STEP_TEXT) {
876-
draw_glyph(glyphs[j], ci, font_color, offset_step);
877-
}
878-
}
879-
processed_glyphs_step++;
880-
offset_step.x += glyphs[j].advance;
843+
}
844+
845+
// Draw stacked outline.
846+
if (stacked_outline_datas.size() != 0) {
847+
int stacked_outline_draw_size = outline_size;
848+
849+
int draw_iterations = stacked_outline_datas.size();
850+
851+
for (int j = 0; j < draw_iterations; j++) {
852+
int stacked_outline_size = stacked_outline_datas[j].size;
853+
if (stacked_outline_size <= 0) {
854+
continue;
881855
}
856+
stacked_outline_draw_size += stacked_outline_size;
882857
}
883-
// Draw LTR ellipsis string when necessary.
884-
if (!rtl && ellipsis_pos >= 0) {
885-
for (int gl_idx = 0; gl_idx < ellipsis_gl_size; gl_idx++) {
886-
for (int j = 0; j < ellipsis_glyphs[gl_idx].repeat; j++) {
887-
bool skip = (trim_chars && ellipsis_glyphs[gl_idx].end + para.start > visible_chars) || (trim_glyphs_ltr && (processed_glyphs_step >= visible_glyphs)) || (trim_glyphs_rtl && (processed_glyphs_step < total_glyphs - visible_glyphs));
888-
if (!skip) {
889-
if (step == DRAW_STEP_SHADOW_OUTLINE) {
890-
draw_glyph_shadow_outline(ellipsis_glyphs[gl_idx], ci, font_shadow_color, shadow_outline_size, offset_step, shadow_ofs);
891-
} else if (step == DRAW_STEP_SHADOW) {
892-
draw_glyph_shadow(ellipsis_glyphs[gl_idx], ci, font_shadow_color, offset_step, shadow_ofs);
893-
} else if (step == DRAW_STEP_OUTLINE) {
894-
draw_glyph_outline(ellipsis_glyphs[gl_idx], ci, font_outline_color, outline_size, offset_step);
895-
} else if (step == DRAW_STEP_TEXT) {
896-
draw_glyph(ellipsis_glyphs[gl_idx], ci, font_color, offset_step);
897-
}
898-
}
899-
processed_glyphs_step++;
900-
offset_step.x += ellipsis_glyphs[gl_idx].advance;
901-
}
858+
859+
for (int draw_iteration_index = draw_iterations - 1; draw_iteration_index >= 0; --draw_iteration_index) {
860+
LabelSettings::StackedOutlineData stacked_outline_data = stacked_outline_datas[draw_iteration_index];
861+
if (stacked_outline_data.size <= 0) {
862+
continue;
902863
}
864+
draw_text(rtl, ellipsis_pos, ellipsis_gl_size, ellipsis_glyphs, trim_chars, para.start, visible_chars, trim_glyphs_ltr, processed_glyphs_step, processed_glyphs, visible_glyphs, trim_glyphs_rtl, total_glyphs, ci, ofs, gl_size, trim_pos, glyphs, stacked_outline_data.color, draw_glyph_outline, stacked_outline_draw_size);
865+
stacked_outline_draw_size -= stacked_outline_data.size;
903866
}
904867
}
868+
869+
// Draw outline.
870+
if (outline_size > 0 && font_outline_color.a != 0) {
871+
draw_text(rtl, ellipsis_pos, ellipsis_gl_size, ellipsis_glyphs, trim_chars, para.start, visible_chars, trim_glyphs_ltr, processed_glyphs_step, processed_glyphs, visible_glyphs, trim_glyphs_rtl, total_glyphs, ci, ofs, gl_size, trim_pos, glyphs, font_outline_color, draw_glyph_outline, outline_size);
872+
}
873+
874+
// Draw text.
875+
{
876+
draw_text(rtl, ellipsis_pos, ellipsis_gl_size, ellipsis_glyphs, trim_chars, para.start, visible_chars, trim_glyphs_ltr, processed_glyphs_step, processed_glyphs, visible_glyphs, trim_glyphs_rtl, total_glyphs, ci, ofs, gl_size, trim_pos, glyphs, font_color, draw_glyph);
877+
}
878+
905879
processed_glyphs = processed_glyphs_step;
906880
ofs.y += dsc + line_spacing;
907881
}

scene/gui/label.h

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,6 @@ class Label : public Control {
3737
GDCLASS(Label, Control);
3838

3939
private:
40-
enum LabelDrawStep {
41-
DRAW_STEP_SHADOW_OUTLINE,
42-
DRAW_STEP_SHADOW,
43-
DRAW_STEP_OUTLINE,
44-
DRAW_STEP_TEXT,
45-
DRAW_STEP_MAX,
46-
};
47-
4840
HorizontalAlignment horizontal_alignment = HORIZONTAL_ALIGNMENT_LEFT;
4941
VerticalAlignment vertical_alignment = VERTICAL_ALIGNMENT_TOP;
5042
String text;
@@ -200,4 +192,55 @@ class Label : public Control {
200192

201193
Label(const String &p_text = String());
202194
~Label();
195+
196+
template <typename... VarArgsFunc, typename... VarArgs>
197+
void draw_text(bool p_rtl, int p_ellipsis_pos, int p_ellipsis_gl_size, const Glyph *p_ellipsis_glyphs, bool p_trim_chars, int p_para_start, int p_visible_chars, bool p_trim_glyphs_ltr, int &p_processed_glyphs_step, int p_processed_glyphs, int p_visible_glyphs, bool p_trim_glyphs_rtl, int p_total_glyphs, const RID &p_ci, const Vector2 &p_ofs, int p_gl_size, int p_trim_pos, const Glyph *p_glyphs, const Color &p_color, void (*p_draw_func)(const Glyph &p_gl, const RID &p_canvas, const Color &p_font_outline_color, const Vector2 &p_ofs, VarArgsFunc... p_args), VarArgs &&...p_args) {
198+
p_processed_glyphs_step = p_processed_glyphs;
199+
Vector2 offset_step = p_ofs; /* Draw RTL ellipsis string when necessary. */
200+
if (p_rtl && p_ellipsis_pos >= 0) {
201+
for (int gl_idx = p_ellipsis_gl_size - 1; gl_idx >= 0; gl_idx--) {
202+
for (int j = 0; j < p_ellipsis_glyphs[gl_idx].repeat; j++) {
203+
bool skip = (p_trim_chars && p_ellipsis_glyphs[gl_idx].end + p_para_start > p_visible_chars) || (p_trim_glyphs_ltr && (p_processed_glyphs_step >= p_visible_glyphs)) || (p_trim_glyphs_rtl && (p_processed_glyphs_step < p_total_glyphs - p_visible_glyphs));
204+
if (!skip) {
205+
p_draw_func(p_ellipsis_glyphs[gl_idx], p_ci, p_color, offset_step, std::forward<VarArgs>(p_args)...);
206+
}
207+
p_processed_glyphs_step++;
208+
offset_step.x += p_ellipsis_glyphs[gl_idx].advance;
209+
}
210+
}
211+
} /* Draw main text. */
212+
for (int j = 0; j < p_gl_size; j++) { /* Trim when necessary. */
213+
if (p_trim_pos >= 0) {
214+
if (p_rtl) {
215+
if (j < p_trim_pos) {
216+
continue;
217+
}
218+
} else {
219+
if (j >= p_trim_pos) {
220+
break;
221+
}
222+
}
223+
}
224+
for (int k = 0; k < p_glyphs[j].repeat; k++) {
225+
bool skip = (p_trim_chars && p_glyphs[j].end + p_para_start > p_visible_chars) || (p_trim_glyphs_ltr && (p_processed_glyphs_step >= p_visible_glyphs)) || (p_trim_glyphs_rtl && (p_processed_glyphs_step < p_total_glyphs - p_visible_glyphs));
226+
if (!skip) {
227+
p_draw_func(p_glyphs[j], p_ci, p_color, offset_step, std::forward<VarArgs>(p_args)...);
228+
}
229+
p_processed_glyphs_step++;
230+
offset_step.x += p_glyphs[j].advance;
231+
}
232+
} /* Draw LTR ellipsis string when necessary. */
233+
if (!p_rtl && p_ellipsis_pos >= 0) {
234+
for (int gl_idx = 0; gl_idx < p_ellipsis_gl_size; gl_idx++) {
235+
for (int j = 0; j < p_ellipsis_glyphs[gl_idx].repeat; j++) {
236+
bool skip = (p_trim_chars && p_ellipsis_glyphs[gl_idx].end + p_para_start > p_visible_chars) || (p_trim_glyphs_ltr && (p_processed_glyphs_step >= p_visible_glyphs)) || (p_trim_glyphs_rtl && (p_processed_glyphs_step < p_total_glyphs - p_visible_glyphs));
237+
if (!skip) {
238+
p_draw_func(p_ellipsis_glyphs[gl_idx], p_ci, p_color, offset_step, std::forward<VarArgs>(p_args)...);
239+
}
240+
p_processed_glyphs_step++;
241+
offset_step.x += p_ellipsis_glyphs[gl_idx].advance;
242+
}
243+
}
244+
}
245+
}
203246
};

0 commit comments

Comments
 (0)