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

144 lines
3.6 KiB

  1. /*
  2. * Copyright (C) 2023 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 <engine/scene/object.hpp>
  22. #include <engine/math/vector.hpp>
  23. #include <engine/gl/vertex-array.hpp>
  24. #include <engine/gl/vertex-buffer.hpp>
  25. #include <engine/render/material.hpp>
  26. #include <engine/type/bitmap-font.hpp>
  27. #include <engine/type/text-direction.hpp>
  28. namespace scene {
  29. /**
  30. * Text scene object.
  31. */
  32. class text: public object<text>
  33. {
  34. public:
  35. /// Constructs a text object.
  36. text();
  37. void render(render::context& ctx) const override;
  38. /**
  39. * Manually updates the text object if its font has been updated or altered in any way.
  40. */
  41. void refresh();
  42. /**
  43. * Sets the text material.
  44. *
  45. * @param material Text material.
  46. */
  47. void set_material(std::shared_ptr<render::material> material);
  48. /**
  49. * Sets the text font.
  50. *
  51. * @param font Pointer to a font.
  52. */
  53. void set_font(const type::bitmap_font* font);
  54. /**
  55. * Sets the direction of the text.
  56. *
  57. * @param direction Text direction.
  58. */
  59. void set_direction(type::text_direction direction);
  60. /**
  61. * Sets the text content.
  62. *
  63. * @param content UTF-8 string of text.
  64. */
  65. void set_content(const std::string& content);
  66. /**
  67. * Sets the text color.
  68. *
  69. * Text color is passed to the text's material shader as a vertex color.
  70. *
  71. * @param color RGBA color.
  72. */
  73. void set_color(const math::fvec4& color);
  74. /// Returns the text material.
  75. [[nodiscard]] inline std::shared_ptr<render::material> get_material() const noexcept
  76. {
  77. return m_render_op.material;
  78. }
  79. /// Returns the text font.
  80. [[nodiscard]] inline const type::bitmap_font* get_font() const noexcept
  81. {
  82. return m_font;
  83. }
  84. /// Returns the text direction.
  85. [[nodiscard]] inline const type::text_direction& get_direction() const noexcept
  86. {
  87. return m_direction;
  88. }
  89. /// Returns the text content.
  90. [[nodiscard]] inline const std::string& get_content() const noexcept
  91. {
  92. return m_content_u8;
  93. }
  94. /// Returns the text color.
  95. [[nodiscard]] inline const math::fvec4& get_color() const noexcept
  96. {
  97. return m_color;
  98. }
  99. [[nodiscard]] inline const aabb_type& get_bounds() const noexcept override
  100. {
  101. return m_world_bounds;
  102. }
  103. private:
  104. void update_content();
  105. void update_color();
  106. virtual void transformed();
  107. mutable render::operation m_render_op;
  108. aabb_type m_local_bounds{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}};
  109. aabb_type m_world_bounds{{0.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 0.0f}};
  110. const type::bitmap_font* m_font{nullptr};
  111. type::text_direction m_direction{type::text_direction::ltr};
  112. std::string m_content_u8;
  113. std::u32string m_content_u32;
  114. math::fvec4 m_color{1.0f, 0.0f, 1.0f, 1.0f};
  115. std::size_t m_vertex_stride{0};
  116. std::size_t m_vertex_count{0};
  117. std::vector<std::byte> m_vertex_data;
  118. std::unique_ptr<gl::vertex_array> m_vao;
  119. std::unique_ptr<gl::vertex_buffer> m_vbo;
  120. };
  121. } // namespace scene
  122. #endif // ANTKEEPER_SCENE_TEXT_HPP