lxgui
gui_text.hpp
1 #ifndef LXGUI_GUI_TEXT_HPP
2 #define LXGUI_GUI_TEXT_HPP
3 
4 #include "lxgui/gui_color.hpp"
5 #include "lxgui/gui_font.hpp"
6 #include "lxgui/gui_matrix4.hpp"
7 #include "lxgui/gui_quad.hpp"
8 #include "lxgui/lxgui.hpp"
9 #include "lxgui/utils.hpp"
10 #include "lxgui/utils_maths.hpp"
11 #include "lxgui/utils_string.hpp"
12 
13 #include <array>
14 #include <limits>
15 #include <memory>
16 #include <vector>
17 
18 namespace lxgui::gui {
19 
20 class renderer;
21 class vertex_cache;
22 struct vertex;
23 
24 enum class alignment_x { left, center, right };
25 
26 enum class alignment_y { top, middle, bottom };
27 
29 class text {
30 public:
37  explicit text(
38  renderer& rdr,
39  std::shared_ptr<const font> fnt,
40  std::shared_ptr<const font> outline_fnt = nullptr);
41 
42  // Non-copiable, non-movable
43  text(const text&) = delete;
44  text(text&&) = delete;
45  text& operator=(const text&) = delete;
46  text& operator=(text&&) = delete;
47 
52  float get_line_height() const;
53 
61  void set_scaling_factor(float scaling_factor);
62 
67  float get_scaling_factor() const;
68 
77  void set_text(const utils::ustring& content);
78 
84  const utils::ustring& get_text() const;
85 
91  void set_color(const color& c, bool force_color = false);
92 
97  const color& get_color() const;
98 
103  void set_alpha(float alpha);
104 
109  float get_alpha() const;
110 
117  void set_box_dimensions(float box_width, float box_height);
118 
124  void set_box_width(float box_width);
125 
131  void set_box_height(float box_height);
132 
138  float get_width() const;
139 
145  float get_height() const;
146 
151  float get_box_width() const;
152 
157  float get_box_height() const;
158 
164  float get_text_width() const;
165 
170  std::size_t get_line_count() const;
171 
177  float get_string_width(const std::string& content) const;
178 
184  float get_string_width(const utils::ustring& content) const;
185 
191  float get_character_width(char32_t c) const;
192 
203  float get_character_kerning(char32_t c1, char32_t c2) const;
204 
210  float get_text_height() const;
211 
216  void set_alignment_x(alignment_x align_x);
217 
222  void set_alignment_y(alignment_y align_y);
223 
229 
235 
241  void set_tracking(float tracking);
242 
247  float get_tracking() const;
248 
256  void set_line_spacing(float line_spacing);
257 
262  float get_line_spacing() const;
263 
274  void set_remove_starting_spaces(bool remove_starting_spaces);
275 
280  bool get_remove_starting_spaces() const;
281 
287  void set_word_wrap_enabled(bool wrap);
288 
293  set_word_wrap_enabled(true);
294  }
295 
300  set_word_wrap_enabled(false);
301  }
302 
307  bool is_word_wrap_enabled() const;
308 
314  void set_word_ellipsis_enabled(bool add_ellipsis);
315 
321  }
322 
328  }
329 
334  bool is_word_ellipsis_enabled() const;
335 
341  void set_formatting_enabled(bool formatting);
342 
349  }
350 
356  set_formatting_enabled(false);
357  }
358 
368  void render(const matrix4f& transform = matrix4f::identity) const;
369 
375  std::size_t get_letter_count() const;
376 
386  const std::array<vertex, 4>& get_letter_quad(std::size_t index) const;
387 
393  quad create_letter_quad(char32_t c) const;
394 
400  void set_use_vertex_cache(bool use_vertex_cache);
401 
406  bool get_use_vertex_cache() const;
407 
412  const renderer& get_renderer() const {
413  return renderer_;
414  }
415 
421  return renderer_;
422  }
423 
424 private:
425  void update_() const;
426  void update_vertex_cache_() const;
427  bool use_vertex_cache_() const;
428  void notify_cache_dirty_() const;
429  void notify_vertex_cache_dirty_() const;
430 
431  float round_to_pixel_(
432  float value, utils::rounding_method method = utils::rounding_method::nearest) const;
433 
434  std::array<vertex, 4> create_letter_quad_(const gui::font& font, char32_t c) const;
435  std::array<vertex, 4> create_letter_quad_(char32_t c) const;
436  std::array<vertex, 4> create_outline_letter_quad_(char32_t c) const;
437 
438  renderer& renderer_;
439 
440  float scaling_factor_ = 1.0f;
441  float tracking_ = 0.0f;
442  float line_spacing_ = 1.0f;
443  bool remove_starting_spaces_ = false;
444  bool word_wrap_enabled_ = true;
445  bool ellipsis_enabled_ = false;
446  color color_ = color::white;
447  bool force_color_ = false;
448  float alpha_ = 1.0f;
449  bool formatting_enabled_ = false;
450  float box_width_ = std::numeric_limits<float>::infinity();
451  float box_height_ = std::numeric_limits<float>::infinity();
452  alignment_x align_x_ = alignment_x::left;
453  alignment_y align_y_ = alignment_y::middle;
454 
455  std::shared_ptr<const font> font_;
456  std::shared_ptr<const font> outline_font_;
457  utils::ustring unicode_text_;
458 
459  mutable bool update_cache_flag_ = false;
460  mutable float width_ = 0.0f;
461  mutable float height_ = 0.0f;
462  mutable std::size_t num_lines_ = 0u;
463 
464  bool use_vertex_cache_flag_ = false;
465  mutable bool update_vertex_cache_flag_ = false;
466  mutable std::vector<std::array<vertex, 4>> quad_list_;
467  mutable std::shared_ptr<vertex_cache> vertex_cache_;
468  mutable std::vector<std::array<vertex, 4>> outline_quad_list_;
469  mutable std::shared_ptr<vertex_cache> outline_vertex_cache_;
470  mutable std::vector<quad> icons_list_;
471 };
472 
473 } // namespace lxgui::gui
474 
475 #endif
Holds a single color (float RGBA, 128 bits)
Definition: gui_color.hpp:12
static const color white
Definition: gui_color.hpp:43
A texture containing characters.
Definition: gui_font.hpp:22
Abstract type for implementation specific management.
Used to draw some text on the screen.
Definition: gui_text.hpp:29
renderer & get_renderer()
Returns the renderer used to render this text.
Definition: gui_text.hpp:420
float get_alpha() const
Returns this text's transparency (alpha).
Definition: gui_text.cpp:342
bool is_word_ellipsis_enabled() const
Checks if word ellipsis is enabled.
Definition: gui_text.cpp:523
quad create_letter_quad(char32_t c) const
Creates a quad that contains the provided character.
Definition: gui_text.cpp:1045
float get_line_spacing() const
Returns this text's line spacing.
Definition: gui_text.cpp:484
text(text &&)=delete
void render(const matrix4f &transform=matrix4f::identity) const
Renders this text at the given position.
Definition: gui_text.cpp:548
alignment_y get_alignment_y() const
Returns the text vertical alignment.
Definition: gui_text.cpp:458
float get_box_width() const
Returns the width of the text box.
Definition: gui_text.cpp:384
void set_remove_starting_spaces(bool remove_starting_spaces)
Allows removal of a line's starting spaces.
Definition: gui_text.cpp:488
float get_character_kerning(char32_t c1, char32_t c2) const
Returns the kerning between two characters.
Definition: gui_text.cpp:432
void set_word_ellipsis_enabled(bool add_ellipsis)
Sets whether to show an ellipsis "..." if words don't fit in the text box.
Definition: gui_text.cpp:514
void set_use_vertex_cache(bool use_vertex_cache)
Sets whether this text object should use vertex caches or not.
Definition: gui_text.cpp:536
bool get_remove_starting_spaces() const
Checks if starting spaces removing is active.
Definition: gui_text.cpp:497
bool get_use_vertex_cache() const
Checks if this text object is using vertex cache or not.
Definition: gui_text.cpp:540
void enable_word_wrap()
Allows word wrap when the line is too long for the text box.
Definition: gui_text.hpp:292
float get_string_width(const std::string &content) const
Returns the length of a provided string.
Definition: gui_text.cpp:411
void set_tracking(float tracking)
Sets this text's tracking.
Definition: gui_text.cpp:462
float get_width() const
Returns the width of the rendered text.
Definition: gui_text.cpp:374
void set_box_height(float box_height)
Sets the height of the text box.
Definition: gui_text.cpp:365
const renderer & get_renderer() const
Returns the renderer used to render this text.
Definition: gui_text.hpp:412
void set_scaling_factor(float scaling_factor)
Set the scaling factor to use when rendering glyphs.
Definition: gui_text.cpp:294
alignment_x get_alignment_x() const
Returns the text horizontal alignment.
Definition: gui_text.cpp:454
void enable_formatting()
Enables color formatting.
Definition: gui_text.hpp:347
void enable_word_ellipsis()
Show an ellipsis "..." if words don't fit in the text box.
Definition: gui_text.hpp:319
float get_text_height() const
Returns the height of the text.
Definition: gui_text.cpp:396
const std::array< vertex, 4 > & get_letter_quad(std::size_t index) const
Returns the quad for the letter at the provided index (position, texture coords, color).
Definition: gui_text.cpp:1058
float get_text_width() const
Returns the length of the text.
Definition: gui_text.cpp:392
void set_color(const color &c, bool force_color=false)
Sets this text's default color.
Definition: gui_text.cpp:319
text & operator=(const text &)=delete
void set_alpha(float alpha)
Sets this text's transparency (alpha).
Definition: gui_text.cpp:333
void set_alignment_y(alignment_y align_y)
Sets text vertical alignment.
Definition: gui_text.cpp:445
float get_height() const
Returns the height of the rendered text.
Definition: gui_text.cpp:379
void disable_word_ellipsis()
Do not show an ellipsis "..." if words don't fit in the text box.
Definition: gui_text.hpp:326
void set_alignment_x(alignment_x align_x)
Sets text horizontal alignment.
Definition: gui_text.cpp:436
void set_box_dimensions(float box_width, float box_height)
Sets the dimensions of the text box.
Definition: gui_text.cpp:346
std::size_t get_line_count() const
Returns the number of text lines.
Definition: gui_text.cpp:406
void set_word_wrap_enabled(bool wrap)
Allows/disallows word wrap when the line is too long for the text box.
Definition: gui_text.cpp:501
void disable_word_wrap()
Disallow word wrap when the line is too long for the text box.
Definition: gui_text.hpp:299
float get_box_height() const
Returns the height of the text box.
Definition: gui_text.cpp:388
void set_formatting_enabled(bool formatting)
Enables color formatting.
Definition: gui_text.cpp:527
text & operator=(text &&)=delete
text(const text &)=delete
const color & get_color() const
Returns this text's default color.
Definition: gui_text.cpp:329
float get_tracking() const
Returns this text's tracking.
Definition: gui_text.cpp:471
float get_line_height() const
Returns the height of one line (constant).
Definition: gui_text.cpp:287
void set_text(const utils::ustring &content)
Sets the text to render (unicode character set).
Definition: gui_text.cpp:306
void set_line_spacing(float line_spacing)
Sets this text's line spacing.
Definition: gui_text.cpp:475
float get_scaling_factor() const
Returns the scaling factor used when rendering glyphs.
Definition: gui_text.cpp:302
const utils::ustring & get_text() const
Returns the text that will be rendered (unicode character set).
Definition: gui_text.cpp:315
text(renderer &rdr, std::shared_ptr< const font > fnt, std::shared_ptr< const font > outline_fnt=nullptr)
Constructor.
Definition: gui_text.cpp:283
float get_character_width(char32_t c) const
Returns the length of a single character.
Definition: gui_text.cpp:423
std::size_t get_letter_count() const
Returns the number of letters currently displayed.
Definition: gui_text.cpp:1053
void disable_formatting()
Disables color formatting.
Definition: gui_text.hpp:355
void set_box_width(float box_width)
Sets the width of the text box.
Definition: gui_text.cpp:356
bool is_word_wrap_enabled() const
Checks if word wrap is enabled.
Definition: gui_text.cpp:510
rounding_method
Rounding method for points to pixels conversions.
Definition: utils_maths.hpp:10
@ nearest
Equivalent to round()
A 4x4 matrix, used for coordinate transformations.
Definition: gui_matrix4.hpp:13
static const matrix4f identity
Definition: gui_matrix4.hpp:51
Simple structure holding four vertices and a material.
Definition: gui_quad.hpp:18