💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

176 lines
4.0 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_SCENE_TEXT_HPP
  20. #define ANTKEEPER_SCENE_TEXT_HPP
  21. #include "scene/object.hpp"
  22. #include "geom/aabb.hpp"
  23. #include "utility/fundamental-types.hpp"
  24. #include "gl/vertex-array.hpp"
  25. #include "gl/vertex-buffer.hpp"
  26. #include "renderer/material.hpp"
  27. #include "type/bitmap-font.hpp"
  28. #include "type/text-direction.hpp"
  29. namespace scene {
  30. /**
  31. * Text scene object.
  32. */
  33. class text: public object<text>
  34. {
  35. public:
  36. typedef geom::aabb<float> aabb_type;
  37. /// Constructs a text object.
  38. text();
  39. /// Destructs a text object.
  40. ~text();
  41. /// @copydoc scene::object_base::render(const render::context&, render::queue&) const
  42. virtual void render(const render::context& ctx, render::queue& queue) const;
  43. /**
  44. * Manually updates the text object if its font has been updated or altered in any way.
  45. */
  46. void refresh();
  47. /**
  48. * Sets the text material.
  49. *
  50. * @param material Text material.
  51. */
  52. void set_material(material* material);
  53. /**
  54. * Sets the text font.
  55. *
  56. * @param font Pointer to a font.
  57. */
  58. void set_font(const type::bitmap_font* font);
  59. /**
  60. * Sets the direction of the text.
  61. *
  62. * @param direction Text direction.
  63. */
  64. void set_direction(type::text_direction direction);
  65. /**
  66. * Sets the text content.
  67. *
  68. * @param content UTF-8 string of text.
  69. */
  70. void set_content(const std::string& content);
  71. /**
  72. * Sets the text color.
  73. *
  74. * Text color is passed to the text's material shader as a vertex color.
  75. *
  76. * @param color RGBA color.
  77. */
  78. void set_color(const float4& color);
  79. /// Returns the text material.
  80. material* get_material() const;
  81. /// Returns the text font.
  82. const type::bitmap_font* get_font() const;
  83. /// Returns the text direction.
  84. const type::text_direction& get_direction() const;
  85. /// Returns the text content.
  86. const std::string& get_content() const;
  87. /// Returns the text color.
  88. const float4& get_color() const;
  89. /// @copydoc scene::object::get_local_bounds() const
  90. virtual const bounding_volume_type& get_local_bounds() const;
  91. /// @copydoc scene::object::get_world_bounds() const
  92. virtual const bounding_volume_type& get_world_bounds() const;
  93. /// @copydoc scene::object::update_tweens()
  94. virtual void update_tweens();
  95. private:
  96. void update_content();
  97. void update_color();
  98. virtual void transformed();
  99. mutable render::operation render_op;
  100. aabb_type local_bounds;
  101. aabb_type world_bounds;
  102. material* material;
  103. const type::bitmap_font* font;
  104. type::text_direction direction;
  105. std::string content_u8;
  106. std::u32string content_u32;
  107. float4 color;
  108. std::size_t vertex_stride;
  109. std::size_t vertex_count;
  110. std::vector<std::byte> vertex_data;
  111. gl::vertex_array* vao;
  112. gl::vertex_buffer* vbo;
  113. };
  114. inline material* text::get_material() const
  115. {
  116. return material;
  117. }
  118. inline const type::bitmap_font* text::get_font() const
  119. {
  120. return font;
  121. }
  122. inline const type::text_direction& text::get_direction() const
  123. {
  124. return direction;
  125. }
  126. inline const std::string& text::get_content() const
  127. {
  128. return content_u8;
  129. }
  130. inline const float4& text::get_color() const
  131. {
  132. return color;
  133. }
  134. inline const typename object_base::bounding_volume_type& text::get_local_bounds() const
  135. {
  136. return local_bounds;
  137. }
  138. inline const typename object_base::bounding_volume_type& text::get_world_bounds() const
  139. {
  140. return world_bounds;
  141. }
  142. } // namespace scene
  143. #endif // ANTKEEPER_SCENE_TEXT_HPP