|
| 1 | +#ifndef LXGUI_GUI_ANIMATED_TEXTURE_HPP |
| 2 | +#define LXGUI_GUI_ANIMATED_TEXTURE_HPP |
| 3 | + |
| 4 | +#include "lxgui/gui_layered_region.hpp" |
| 5 | +#include "lxgui/gui_quad.hpp" |
| 6 | +#include "lxgui/lxgui.hpp" |
| 7 | +#include "lxgui/utils.hpp" |
| 8 | + |
| 9 | +namespace lxgui::gui { |
| 10 | + |
| 11 | +class renderer; |
| 12 | + |
| 13 | +/** |
| 14 | + * \brief A layered_region that can draw animated sequences. |
| 15 | + * \details This object contains an animated texture taken from a file. |
| 16 | + */ |
| 17 | +class animated_texture : public layered_region { |
| 18 | + using base = layered_region; |
| 19 | + |
| 20 | +public: |
| 21 | + /// Constructor. |
| 22 | + explicit animated_texture( |
| 23 | + utils::control_block& block, manager& mgr, const region_core_attributes& attr); |
| 24 | + |
| 25 | + /** |
| 26 | + * \brief Prints all relevant information about this region in a string. |
| 27 | + * \param tab The offset to give to all lines |
| 28 | + * \return All relevant information about this region |
| 29 | + */ |
| 30 | + std::string serialize(const std::string& tab) const override; |
| 31 | + |
| 32 | + /// Renders this region on the current render target. |
| 33 | + void render() const override; |
| 34 | + |
| 35 | + /** |
| 36 | + * \brief Updates this region's logic. |
| 37 | + * \param delta Time spent since last update |
| 38 | + */ |
| 39 | + void update(float delta) override; |
| 40 | + |
| 41 | + /** |
| 42 | + * \brief Copies a region's parameters into this texture (inheritance). |
| 43 | + * \param obj The region to copy |
| 44 | + */ |
| 45 | + void copy_from(const region& obj) override; |
| 46 | + |
| 47 | + /** |
| 48 | + * \brief Returns this animated_texture's animation speed (frame per second). |
| 49 | + * \return This animated_texture's animation speed |
| 50 | + */ |
| 51 | + float get_speed() const; |
| 52 | + |
| 53 | + /** |
| 54 | + * \brief Returns this animated_texture's state (0: begin, 1: end). |
| 55 | + * \return This animated_texture's state |
| 56 | + */ |
| 57 | + float get_state() const; |
| 58 | + |
| 59 | + /** |
| 60 | + * \brief Check if this animated_texture is paused |
| 61 | + * \return 'true' if paused, 'false' otherwise |
| 62 | + */ |
| 63 | + float is_paused() const; |
| 64 | + |
| 65 | + /** |
| 66 | + * \brief Returns this texture's texture file. |
| 67 | + * \return This texture's texture file (empty string if none). |
| 68 | + */ |
| 69 | + const std::string& get_texture_file() const; |
| 70 | + |
| 71 | + /** |
| 72 | + * \brief Returns this texture's vertex color. |
| 73 | + * \param index The vertex index (0 to 3 included) |
| 74 | + * \return This texture's vertex color |
| 75 | + * \note This color is used to filter the texture's colors: |
| 76 | + * for each pixel, the original color is multiplied by this vertex color. |
| 77 | + */ |
| 78 | + color get_vertex_color(std::size_t index) const; |
| 79 | + |
| 80 | + /** |
| 81 | + * \brief Set this animated_texture's animation speed (frame per second). |
| 82 | + * \param speed The new animation speed |
| 83 | + */ |
| 84 | + void set_speed(float speed); |
| 85 | + |
| 86 | + /** |
| 87 | + * \brief Returns this animated_texture's state (0: begin, 1: end). |
| 88 | + * \param state The new state |
| 89 | + */ |
| 90 | + void set_state(float state); |
| 91 | + |
| 92 | + /** |
| 93 | + * \brief Check if this animated_texture is paused |
| 94 | + * \return 'true' if paused, 'false' otherwise |
| 95 | + */ |
| 96 | + void set_paused(bool is_paused); |
| 97 | + |
| 98 | + /** |
| 99 | + * \brief Sets this texture's texture file. |
| 100 | + * \param file_name The file from which to read data |
| 101 | + * \note This function takes care of checking that the file can be opened. |
| 102 | + * \note This function will replace the solid color set by set_solid_color(). If you need |
| 103 | + * to blend the texture with a color, use set_vertex_color() instead. |
| 104 | + */ |
| 105 | + void set_texture(const std::string& file_name); |
| 106 | + |
| 107 | + /** |
| 108 | + * \brief Sets this texture's vertex color. |
| 109 | + * \param c This texture's new vertex color |
| 110 | + * \param index The vertex index (-1: all vertices) |
| 111 | + * \note This color is used to filter the texture's colors: |
| 112 | + * for each pixel, the original color is multiplied |
| 113 | + * by this vertex color. |
| 114 | + */ |
| 115 | + void |
| 116 | + set_vertex_color(const color& c, std::size_t index = std::numeric_limits<std::size_t>::max()); |
| 117 | + |
| 118 | + /** |
| 119 | + * \brief Parses data from a layout_node. |
| 120 | + * \param node The layout node |
| 121 | + */ |
| 122 | + void parse_layout(const layout_node& node) override; |
| 123 | + |
| 124 | + /// Registers this region class to the provided Lua state |
| 125 | + static void register_on_lua(sol::state& lua); |
| 126 | + |
| 127 | + static constexpr const char* class_name = "AnimatedTexture"; |
| 128 | + |
| 129 | +private: |
| 130 | + void parse_attributes_(const layout_node& node) override; |
| 131 | + void parse_tex_coords_node_(const layout_node& node); |
| 132 | + void parse_gradient_node_(const layout_node& node); |
| 133 | + |
| 134 | + void update_tex_coords_(); |
| 135 | + void update_borders_() override; |
| 136 | + |
| 137 | + std::string file_; |
| 138 | + |
| 139 | + float speed_ = 1.0f; |
| 140 | + float state_ = 0.0f; |
| 141 | + bool is_paused_ = false; |
| 142 | + |
| 143 | + renderer& renderer_; |
| 144 | + quad quad_; |
| 145 | +}; |
| 146 | + |
| 147 | +} // namespace lxgui::gui |
| 148 | + |
| 149 | +#endif |
0 commit comments