Drawable2D.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // Copyright (c) 2008-2020 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #pragma once
  23. #include "../Graphics/Drawable.h"
  24. #include "../Graphics/GraphicsDefs.h"
  25. namespace Urho3D
  26. {
  27. class Drawable2D;
  28. class Renderer2D;
  29. class Texture2D;
  30. class VertexBuffer;
  31. /// 2D vertex.
  32. struct Vertex2D
  33. {
  34. /// Position.
  35. Vector3 position_;
  36. /// Color.
  37. unsigned color_;
  38. /// UV.
  39. Vector2 uv_;
  40. };
  41. /// 2D source batch.
  42. struct SourceBatch2D
  43. {
  44. /// Construct.
  45. SourceBatch2D();
  46. /// Owner.
  47. WeakPtr<Drawable2D> owner_;
  48. /// Distance to camera.
  49. mutable float distance_;
  50. /// Draw order.
  51. int drawOrder_;
  52. /// Material.
  53. SharedPtr<Material> material_;
  54. /// Vertices.
  55. Vector<Vertex2D> vertices_;
  56. };
  57. /// Pixel size (equal 0.01f).
  58. extern URHO3D_API const float PIXEL_SIZE;
  59. /// Base class for 2D visible components.
  60. class URHO3D_API Drawable2D : public Drawable
  61. {
  62. URHO3D_OBJECT(Drawable2D, Drawable);
  63. public:
  64. /// Construct.
  65. explicit Drawable2D(Context* context);
  66. /// Destruct.
  67. ~Drawable2D() override;
  68. /// Register object factory. Drawable must be registered first.
  69. static void RegisterObject(Context* context);
  70. /// Handle enabled/disabled state change.
  71. void OnSetEnabled() override;
  72. /// Set layer.
  73. /// @property
  74. void SetLayer(int layer);
  75. /// Set order in layer.
  76. /// @property
  77. void SetOrderInLayer(int orderInLayer);
  78. /// Return layer.
  79. /// @property
  80. int GetLayer() const { return layer_; }
  81. /// Return order in layer.
  82. /// @property
  83. int GetOrderInLayer() const { return orderInLayer_; }
  84. /// Return all source batches (called by Renderer2D).
  85. const Vector<SourceBatch2D>& GetSourceBatches();
  86. protected:
  87. /// Handle scene being assigned.
  88. void OnSceneSet(Scene* scene) override;
  89. /// Handle node transform being dirtied.
  90. void OnMarkedDirty(Node* node) override;
  91. /// Handle draw order changed.
  92. virtual void OnDrawOrderChanged() = 0;
  93. /// Update source batches.
  94. virtual void UpdateSourceBatches() = 0;
  95. /// Return draw order by layer and order in layer.
  96. int GetDrawOrder() const { return layer_ << 16u | orderInLayer_; }
  97. /// Layer.
  98. int layer_;
  99. /// Order in layer.
  100. int orderInLayer_;
  101. /// Source batches.
  102. Vector<SourceBatch2D> sourceBatches_;
  103. /// Source batches dirty flag.
  104. bool sourceBatchesDirty_;
  105. /// Renderer2D.
  106. WeakPtr<Renderer2D> renderer_;
  107. };
  108. }