UIBatch.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright (c) 2008-2023 the Urho3D project
  2. // License: MIT
  3. #pragma once
  4. #include "../Graphics/Material.h"
  5. #include "../GraphicsAPI/GraphicsDefs.h"
  6. #include "../Math/Color.h"
  7. #include "../Math/Rect.h"
  8. namespace Urho3D
  9. {
  10. class PixelShader;
  11. class Graphics;
  12. class Matrix3x4;
  13. class Texture;
  14. class UIElement;
  15. inline constexpr i32 UI_VERTEX_SIZE = 6;
  16. /// %UI rendering draw call.
  17. class URHO3D_API UIBatch
  18. {
  19. public:
  20. /// Construct with defaults.
  21. UIBatch();
  22. /// Construct.
  23. UIBatch(UIElement* element, BlendMode blendMode, const IntRect& scissor, Texture* texture, Vector<float>* vertexData);
  24. /// Set new color for the batch. Overrides gradient.
  25. void SetColor(const Color& color, bool overrideAlpha = false);
  26. /// Restore UI element's default color.
  27. void SetDefaultColor();
  28. /// Add a quad
  29. void AddQuad(float x, float y, float width, float height, int texOffsetX, int texOffsetY, int texWidth = 0, int texHeight = 0);
  30. /// Add a quad (integer version)
  31. void AddQuad(i32 x, i32 y, i32 width, i32 height, int texOffsetX, int texOffsetY, int texWidth = 0, int texHeight = 0)
  32. {
  33. AddQuad(static_cast<float>(x), static_cast<float>(y), static_cast<float>(width), static_cast<float>(height),
  34. texOffsetX, texOffsetY, texWidth, texHeight);
  35. }
  36. /// Add a quad using a transform matrix
  37. void AddQuad(const Matrix3x4& transform, int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth = 0,
  38. int texHeight = 0);
  39. /// Add a quad with tiled texture.
  40. void AddQuad(int x, int y, int width, int height, int texOffsetX, int texOffsetY, int texWidth, int texHeight, bool tiled);
  41. /// Add a quad with freeform points and UVs. Uses the current color, not gradient. Points should be specified in clockwise order.
  42. void AddQuad(const Matrix3x4& transform, const IntVector2& a, const IntVector2& b, const IntVector2& c, const IntVector2& d,
  43. const IntVector2& texA, const IntVector2& texB, const IntVector2& texC, const IntVector2& texD);
  44. /// Add a quad with freeform points, UVs and colors. Points should be specified in clockwise order.
  45. void AddQuad(const Matrix3x4& transform, const IntVector2& a, const IntVector2& b, const IntVector2& c, const IntVector2& d,
  46. const IntVector2& texA, const IntVector2& texB, const IntVector2& texC, const IntVector2& texD, const Color& colA,
  47. const Color& colB, const Color& colC, const Color& colD);
  48. /// Merge with another batch.
  49. bool Merge(const UIBatch& batch);
  50. /// Return an interpolated color for the UI element
  51. unsigned GetInterpolatedColor(float x, float y);
  52. /// Return an interpolated color for the UI element (integer version)
  53. unsigned GetInterpolatedColor(i32 x, i32 y)
  54. {
  55. return GetInterpolatedColor(static_cast<float>(x), static_cast<float>(y));
  56. }
  57. /// Add or merge a batch.
  58. static void AddOrMerge(const UIBatch& batch, Vector<UIBatch>& batches);
  59. /// Element this batch represents.
  60. UIElement* element_{};
  61. /// Blending mode.
  62. BlendMode blendMode_{BLEND_REPLACE};
  63. /// Scissor rectangle.
  64. IntRect scissor_;
  65. /// Texture.
  66. Texture* texture_{};
  67. /// Inverse texture size.
  68. Vector2 invTextureSize_{Vector2::ONE};
  69. /// Vertex data.
  70. Vector<float>* vertexData_{};
  71. /// Vertex data start index.
  72. unsigned vertexStart_{};
  73. /// Vertex data end index.
  74. unsigned vertexEnd_{};
  75. /// Current color. By default calculated from the element.
  76. color32 color_{};
  77. /// Gradient flag.
  78. bool useGradient_{};
  79. /// Custom material.
  80. Material* customMaterial_{};
  81. };
  82. }