💿🐜 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.

168 lines
3.8 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_bounds() const
  90. virtual const bounding_volume_type& get_bounds() const;
  91. /// @copydoc scene::object::update_tweens()
  92. virtual void update_tweens();
  93. private:
  94. void update_content();
  95. void update_color();
  96. virtual void transformed();
  97. mutable render::operation render_op;
  98. aabb_type local_bounds;
  99. aabb_type world_bounds;
  100. material* material;
  101. const type::bitmap_font* font;
  102. type::text_direction direction;
  103. std::string content_u8;
  104. std::u32string content_u32;
  105. float4 color;
  106. std::size_t vertex_stride;
  107. std::size_t vertex_count;
  108. std::vector<std::byte> vertex_data;
  109. gl::vertex_array* vao;
  110. gl::vertex_buffer* vbo;
  111. };
  112. inline material* text::get_material() const
  113. {
  114. return material;
  115. }
  116. inline const type::bitmap_font* text::get_font() const
  117. {
  118. return font;
  119. }
  120. inline const type::text_direction& text::get_direction() const
  121. {
  122. return direction;
  123. }
  124. inline const std::string& text::get_content() const
  125. {
  126. return content_u8;
  127. }
  128. inline const float4& text::get_color() const
  129. {
  130. return color;
  131. }
  132. inline const typename object_base::bounding_volume_type& text::get_bounds() const
  133. {
  134. return world_bounds;
  135. }
  136. } // namespace scene
  137. #endif // ANTKEEPER_SCENE_TEXT_HPP