NinePatch.hpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #ifndef GUI3DS_NINEPATCH_HPP
  2. #define GUI3DS_NINEPATCH_HPP
  3. #include <cpp3ds/Graphics/Drawable.hpp>
  4. #include <cpp3ds/Graphics/Texture.hpp>
  5. #include <cpp3ds/Graphics/RenderTarget.hpp>
  6. #include <cpp3ds/Graphics/VertexArray.hpp>
  7. #include <cpp3ds/Graphics/RectangleShape.hpp>
  8. namespace gui3ds {
  9. class NinePatch : public cpp3ds::Drawable, public cpp3ds::Transformable {
  10. public:
  11. NinePatch();
  12. ~NinePatch();
  13. void setTexture(const cpp3ds::Texture *texture);
  14. const cpp3ds::Texture* getTexture() const;
  15. void setContentSize(const cpp3ds::Vector2f& size) const;
  16. void setContentSize(float width, float height) const;
  17. void setSize(const cpp3ds::Vector2f& size) const;
  18. void setSize(float width, float height) const;
  19. const cpp3ds::Vector2f& getContentSize() const;
  20. cpp3ds::Vector2f getSize() const;
  21. void setPadding(const cpp3ds::FloatRect& padding);
  22. void setPadding(float left, float top, float width, float height);
  23. const cpp3ds::FloatRect& getPadding() const;
  24. void setColor(const cpp3ds::Color& color) const;
  25. const cpp3ds::Color& getColor() const;
  26. protected:
  27. virtual void draw(cpp3ds::RenderTarget& target, cpp3ds::RenderStates states) const;
  28. void ensureUpdate() const;
  29. void updateRegions() const;
  30. void updateVertices() const;
  31. private:
  32. enum ExpandType {
  33. Stretch,
  34. Repeat,
  35. };
  36. enum RegionType {
  37. Static,
  38. DynamicWidth,
  39. DynamicHeight,
  40. DynamicBoth,
  41. };
  42. struct Region {
  43. RegionType type;
  44. cpp3ds::FloatRect rect;
  45. };
  46. const cpp3ds::Texture *m_texture;
  47. mutable cpp3ds::Color m_color;
  48. mutable cpp3ds::Vector2f m_contentSize;
  49. mutable cpp3ds::Vector2f m_size;
  50. mutable std::vector<Region> m_regions;
  51. mutable cpp3ds::VertexArray m_vertices;
  52. mutable cpp3ds::FloatRect m_padding;
  53. // Used for internal calculation
  54. mutable float m_staticWidth;
  55. mutable float m_staticHeight;
  56. mutable float m_dynamicWidth;
  57. mutable float m_dynamicHeight;
  58. mutable bool m_needsUpdate;
  59. };
  60. } // namespace gui3ds
  61. #endif // GUI3DS_NINEPATCH_HPP