Unravel Engine C++ Reference
Loading...
Searching...
No Matches
text_component.cpp
Go to the documentation of this file.
1#include "text_component.hpp"
3
9
10namespace unravel
11{
12
14{
17
18 // predicates for conditional GUI
19 auto auto_size_pred_entt = entt::property_predicate(
20 [](const entt::meta_any& i)
21 {
22 return i.try_cast<text_component>()->get_auto_size();
23 });
24
25 auto font_size_read_only_entt = entt::property_predicate(
26 [](const entt::meta_any& i)
27 {
28 return i.try_cast<text_component>()->get_auto_size();
29 });
30 // Register text_style with entt
31 entt::meta_factory<text_style>{}
32 .type("text_style"_hs)
34 entt::attribute{"name", "text_style"},
35 entt::attribute{"pretty_name", "TextStyle"},
36 })
37 .data<&text_style::set_opacity, &text_style::get_opacity>("opacity"_hs)
39 entt::attribute{"name", "opacity"},
40 entt::attribute{"pretty_name", "Opacity"},
41 entt::attribute{"tooltip", "."},
42 entt::attribute{"min", 0.0f},
43 entt::attribute{"max", 1.0f},
44 })
45 .data<&text_style::set_text_color, &text_style::get_text_color>("text_color"_hs)
47 entt::attribute{"name", "text_color"},
48 entt::attribute{"pretty_name", "Color"},
49 entt::attribute{"tooltip", "."},
50 })
51 .data<&text_style::set_outline_color, &text_style::get_outline_color>("outline_color"_hs)
53 entt::attribute{"name", "outline_color"},
54 entt::attribute{"pretty_name", "Outline Color"},
55 entt::attribute{"tooltip", "."},
56 entt::attribute{"group", "Outline"},
57 })
58 .data<&text_style::outline_width>("outline_width"_hs)
60 entt::attribute{"name", "outline_width"},
61 entt::attribute{"pretty_name", "Outline Width"},
62 entt::attribute{"tooltip", "."},
63 entt::attribute{"min", 0.0f},
64 entt::attribute{"step", 0.01f},
65 entt::attribute{"group", "Outline"},
66 })
67 .data<&text_style::set_shadow_color, &text_style::get_shadow_color>("shadow_color"_hs)
69 entt::attribute{"name", "shadow_color"},
70 entt::attribute{"pretty_name", "Shadow Color"},
71 entt::attribute{"tooltip", "."},
72 entt::attribute{"group", "Shadow"},
73 })
74 .data<&text_style::shadow_softener>("shadow_softener"_hs)
76 entt::attribute{"name", "shadow_softener"},
77 entt::attribute{"pretty_name", "Shadow Softenss"},
78 entt::attribute{"tooltip", "."},
79 entt::attribute{"min", 0.0f},
80 entt::attribute{"max", 10.0f},
81 entt::attribute{"group", "Shadow"},
82 })
83 .data<&text_style::shadow_offsets>("shadow_offsets"_hs)
85 entt::attribute{"name", "shadow_offsets"},
86 entt::attribute{"pretty_name", "Shadow Offsets"},
87 entt::attribute{"tooltip", "."},
88 entt::attribute{"group", "Shadow"},
89 })
90 .data<&text_style::set_style_flags, &text_style::get_style_flags>("style_flags"_hs)
92 entt::attribute{"name", "style_flags"},
93 entt::attribute{"pretty_name", "Flags"},
94 entt::attribute{"tooltip", "."},
95 entt::attribute{"group", "Style"},
96 })
97 .data<&text_style::set_background_color, &text_style::get_background_color>("background_color"_hs)
99 entt::attribute{"name", "background_color"},
100 entt::attribute{"pretty_name", "Background Color"},
101 entt::attribute{"tooltip", "."},
102 entt::attribute{"group", "Style"},
103 })
104 .data<&text_style::set_foreground_color, &text_style::get_foreground_color>("foreground_color"_hs)
106 entt::attribute{"name", "foreground_color"},
107 entt::attribute{"pretty_name", "Foreground Color"},
108 entt::attribute{"tooltip", "."},
109 entt::attribute{"group", "Style"},
110 })
111 .data<&text_style::set_overline_color, &text_style::get_overline_color>("overline_color"_hs)
113 entt::attribute{"name", "overline_color"},
114 entt::attribute{"pretty_name", "Overline Color"},
115 entt::attribute{"tooltip", "."},
116 entt::attribute{"group", "Style"},
117 })
118 .data<&text_style::set_underline_color, &text_style::get_underline_color>("underline_color"_hs)
120 entt::attribute{"name", "underline_color"},
121 entt::attribute{"pretty_name", "Underline Color"},
122 entt::attribute{"tooltip", "."},
123 entt::attribute{"group", "Style"},
124 })
125 .data<&text_style::set_strike_color, &text_style::get_strike_color>("strike_color"_hs)
127 entt::attribute{"name", "strike_color"},
128 entt::attribute{"pretty_name", "Strike Color"},
129 entt::attribute{"tooltip", "."},
130 entt::attribute{"group", "Style"},
131 });
132
133 // Register buffer_type enum with entt
134 entt::meta_factory<BT>{}
135 .type("buffer_type"_hs)
137 entt::attribute{"name", "buffer_type"},
138 entt::attribute{"pretty_name", "Buffer Type"},
139 })
140 .data<BT::static_buffer>("static_buffer"_hs)
142 entt::attribute{"name", "static_buffer"},
143 entt::attribute{"pretty_name", "Static"},
144 })
145 .data<BT::dynamic_buffer>("dynamic_buffer"_hs)
147 entt::attribute{"name", "dynamic_buffer"},
148 entt::attribute{"pretty_name", "Dynamic"},
149 })
150 .data<BT::transient_buffer>("transient_buffer"_hs)
152 entt::attribute{"name", "transient_buffer"},
153 entt::attribute{"pretty_name", "Transient"},
154 });
155
156 // Register overflow_type enum with entt
157 entt::meta_factory<OT>{}
158 .type("overflow_type"_hs)
160 entt::attribute{"name", "overflow_type"},
161 entt::attribute{"pretty_name", "Overflow Type"},
162 })
163 .data<OT::none>("none"_hs)
165 entt::attribute{"name", "none"},
166 entt::attribute{"pretty_name", "None"},
167 })
168 .data<OT::word>("word"_hs)
170 entt::attribute{"name", "word"},
171 entt::attribute{"pretty_name", "Word"},
172 })
173 .data<OT::grapheme>("grapheme"_hs)
175 entt::attribute{"name", "grapheme"},
176 entt::attribute{"pretty_name", "Grapheme"},
177 });
178
179 // Register text_component with entt
180 entt::meta_factory<text_component>{}
181 .type("text_component"_hs)
183 entt::attribute{"name", "text_component"},
184 entt::attribute{"category", "UI"},
185 entt::attribute{"pretty_name", "Text"},
186 })
187 .func<&component_meta<text_component>::exists>("component_exists"_hs)
188 .func<&component_meta<text_component>::add>("component_add"_hs)
189 .func<&component_meta<text_component>::remove>("component_remove"_hs)
190 .func<&component_meta<text_component>::save>("component_save"_hs)
191 .func<&component_meta<text_component>::load>("component_load"_hs)
193 .custom<entt::attributes>(entt::attributes{
194 entt::attribute{"name", "text"},
195 entt::attribute{"pretty_name", "Text"},
196 entt::attribute{"tooltip", "The UTF-8 string to display."},
197 entt::attribute{"multiline", true},
198 entt::attribute{"wrap", true},
199 entt::attribute{"example", R"(<color=blue>Blue text with <background-color=yellow>yellow background</background-color> and <style=underline>underlined</style> <alpha=0.4>transparent words</alpha>.</color>
200<outline-width=1><outline-color=red>This text has a red outline</outline-color> and <shadow-offset=2,2><shadow-color=gray>gray shadow</shadow-color></shadow-offset>.</outline-width>
201
202<color=green>Green text with <style=overline>overlined</style> and <style=strikethrough>strikethrough</style> styles.</color>
203<shadow-offset=3,3><shadow-color=black><shadow-softener=2>This text has a softened shadow</shadow-softener> and <foreground-color=#FFD70055><color=black>black text with gold transparent foreground</color></foreground-color>.</shadow-color></shadow-offset>
204
205<color=purple>Purple text with <style=underline|overline>both underline and overline</style> effects.</color>)"},
206 })
207 .data<&text_component::set_is_rich_text, &text_component::get_is_rich_text>("is_rich"_hs)
209 entt::attribute{"name", "is_rich"},
210 entt::attribute{"pretty_name", "Rich Text"},
211 entt::attribute{"tooltip", "Enable parsing of <color> / <style> tags."},
212 })
213 .data<&text_component::set_font, &text_component::get_font>("font"_hs)
215 entt::attribute{"name", "font"},
216 entt::attribute{"pretty_name", "Font"},
217 entt::attribute{"tooltip", "The font asset to use."},
218 })
219 .data<&text_component::set_font_size, &text_component::get_font_size>("font_size"_hs)
221 entt::attribute{"name", "font_size"},
222 entt::attribute{"pretty_name", "Font Size"},
223 entt::attribute{"tooltip", "Desired base font size."},
224 entt::attribute{"readonly_predicate", font_size_read_only_entt},
225 })
226 .data<nullptr, &text_component::get_render_font_size>("render_font_size"_hs)
228 entt::attribute{"name", "render_font_size"},
229 entt::attribute{"pretty_name", "Render Font Size"},
230 entt::attribute{"tooltip", "Actual size used after auto-scaling."},
231 })
232 .data<nullptr, &text_component::get_render_buffers_count>("render_buffers_count"_hs)
234 entt::attribute{"name", "render_buffers_count"},
235 entt::attribute{"pretty_name", "Render Buffers"},
236 entt::attribute{"tooltip", "How many render buffers are used for this text."},
237 })
238 .data<&text_component::set_auto_size, &text_component::get_auto_size>("auto_size"_hs)
240 entt::attribute{"name", "auto_size"},
241 entt::attribute{"pretty_name", "Auto Size"},
242 entt::attribute{"tooltip", "Automatically shrink or grow font to fit area."},
243 })
244 .data<&text_component::set_auto_size_range, &text_component::get_auto_size_range>("auto_size_range"_hs)
246 entt::attribute{"name", "auto_size_range"},
247 entt::attribute{"pretty_name", "Auto Size Range"},
248 entt::attribute{"tooltip", "Min/Max font sizes when Auto Size is enabled."},
249 entt::attribute{"predicate", auto_size_pred_entt},
250 })
251 .data<&text_component::set_alignment, &text_component::get_alignment>("alignment"_hs)
253 entt::attribute{"name", "alignment"},
254 entt::attribute{"pretty_name", "Alignment"},
255 entt::attribute{"tooltip", "Horizontal and vertical alignment flags."},
256 })
257 .data<&text_component::set_apply_kerning, &text_component::get_apply_kerning>("apply_kerning"_hs)
259 entt::attribute{"name", "apply_kerning"},
260 entt::attribute{"pretty_name", "Apply Kerning"},
261 entt::attribute{"tooltip", "Enable kerning."},
262 })
263 .data<&text_component::set_overflow_type, &text_component::get_overflow_type>("overflow"_hs)
265 entt::attribute{"name", "overflow"},
266 entt::attribute{"pretty_name", "Overflow"},
267 entt::attribute{"tooltip", "How text should wrap or overflow the area."},
268 })
269 .data<&text_component::set_buffer_type, &text_component::get_buffer_type>("buffer_type"_hs)
271 entt::attribute{"name", "buffer_type"},
272 entt::attribute{"pretty_name", "Buffer Type"},
273 entt::attribute{"tooltip", "Static, Dynamic, or Transient text buffer storage."},
274 })
275 .data<&text_component::set_area, &text_component::get_area>("area"_hs)
277 entt::attribute{"name", "area"},
278 entt::attribute{"pretty_name", "Area"},
279 entt::attribute{"tooltip", "Bounds (width × height)."},
280 })
281 .data<&text_component::set_style, &text_component::get_style>("style"_hs)
283 entt::attribute{"name", "style"},
284 entt::attribute{"pretty_name", "Style"},
285 entt::attribute{"tooltip", "Main style for the text"},
286 });
287}
288
290{
291 try_serialize(ar, ser20::make_nvp("opacity", obj.opacity));
292 try_serialize(ar, ser20::make_nvp("text_color", obj.text_color));
293 try_serialize(ar, ser20::make_nvp("background_color", obj.background_color));
294 try_serialize(ar, ser20::make_nvp("foreground_color", obj.foreground_color));
295 try_serialize(ar, ser20::make_nvp("overline_color", obj.overline_color));
296 try_serialize(ar, ser20::make_nvp("underline_color", obj.underline_color));
297 try_serialize(ar, ser20::make_nvp("strike_color", obj.strike_color));
298 try_serialize(ar, ser20::make_nvp("outline_color", obj.outline_color));
299 try_serialize(ar, ser20::make_nvp("shadow_color", obj.shadow_color));
300 try_serialize(ar, ser20::make_nvp("shadow_softener", obj.shadow_softener));
301 try_serialize(ar, ser20::make_nvp("shadow_offsets", obj.shadow_offsets));
302 try_serialize(ar, ser20::make_nvp("style_flags", obj.style_flags));
303}
304
306{
307 try_save(ar, ser20::make_nvp("text", obj.get_text()));
308 try_save(ar, ser20::make_nvp("is_rich", obj.get_is_rich_text()));
309 try_save(ar, ser20::make_nvp("font", obj.get_font()));
310 try_save(ar, ser20::make_nvp("font_size", obj.get_font_size()));
311 try_save(ar, ser20::make_nvp("auto_size", obj.get_auto_size()));
312 try_save(ar, ser20::make_nvp("auto_size_range", obj.get_auto_size_range()));
313 try_save(ar, ser20::make_nvp("alignment", obj.get_alignment().flags));
314 try_save(ar, ser20::make_nvp("overflow", obj.get_overflow_type()));
315 try_save(ar, ser20::make_nvp("area", obj.get_area()));
316 try_save(ar, ser20::make_nvp("buffer_type", obj.get_buffer_type()));
317 try_save(ar, ser20::make_nvp("style", obj.get_style()));
318}
321
323{
324 std::string text;
325 if(try_load(ar, ser20::make_nvp("text", text)))
326 {
327 obj.set_text(text);
328 }
329
330 bool is_rich = false;
331 if(try_load(ar, ser20::make_nvp("is_rich", is_rich)))
332 {
333 obj.set_is_rich_text(is_rich);
334 }
335
336 asset_handle<font> font_h;
337 if(try_load(ar, ser20::make_nvp("font", font_h)))
338 {
339 obj.set_font(font_h);
340 }
341
342 int font_sz = 0;
343 if(try_load(ar, ser20::make_nvp("font_size", font_sz)))
344 {
345 obj.set_font_size(font_sz);
346 }
347
348 bool auto_sz = false;
349 if(try_load(ar, ser20::make_nvp("auto_size", auto_sz)))
350 {
351 obj.set_auto_size(auto_sz);
352 }
353
354 urange32_t auto_range;
355 if(try_load(ar, ser20::make_nvp("auto_size_range", auto_range)))
356 {
357 obj.set_auto_size_range(auto_range);
358 }
359
360 uint32_t align = 0;
361 if(try_load(ar, ser20::make_nvp("alignment", align)))
362 {
363 obj.set_alignment({align});
364 }
365
367 if(try_load(ar, ser20::make_nvp("overflow", ov)))
368 {
369 obj.set_overflow_type(ov);
370 }
371
372 fsize_t area{};
373 if(try_load(ar, ser20::make_nvp("area", area)))
374 {
375 obj.set_area(area);
376 }
377
379 if(try_load(ar, ser20::make_nvp("buffer_type", bt)))
380 {
381 obj.set_buffer_type(bt);
382 }
383
384 text_style style;
385 if(try_load(ar, ser20::make_nvp("style", style)))
386 {
387 obj.set_style(style);
388 }
389}
390
393} // namespace unravel
auto get_text() const -> const std::string &
Gets the current text content.
void set_text(const std::string &text)
Sets the text content to be rendered.
attributes::value_type attribute
Definition reflection.h:19
std::map< std::string, meta_any > attributes
Definition reflection.h:18
auto property_predicate(property_predicate_t predicate) -> property_predicate_t
BinaryInputArchive iarchive_binary_t
simd::JSONOutputArchive oarchive_associative_t
BinaryOutputArchive oarchive_binary_t
simd::JSONInputArchive iarchive_associative_t
#define REFLECT(cls)
Definition reflection.h:133
#define SAVE_INSTANTIATE(cls, Archive)
#define LOAD(cls)
auto try_serialize(Archive &ar, ser20::NameValuePair< T > &&t, const hpp::source_location &loc=hpp::source_location::current()) -> bool
auto try_save(Archive &ar, ser20::NameValuePair< T > &&t, const hpp::source_location &loc=hpp::source_location::current()) -> bool
#define LOAD_INSTANTIATE(cls, Archive)
#define SERIALIZE_INLINE(cls)
#define SAVE(cls)
auto try_load(Archive &ar, ser20::NameValuePair< T > &&t, const hpp::source_location &loc=hpp::source_location::current()) -> bool
Represents a handle to an asset, providing access and management functions.