Drawable2D.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Copyright (c) 2008-2017 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. Drawable2D(Context* context);
  66. /// Destruct.
  67. ~Drawable2D();
  68. /// Register object factory. Drawable must be registered first.
  69. static void RegisterObject(Context* context);
  70. /// Handle enabled/disabled state change.
  71. virtual void OnSetEnabled();
  72. /// Set layer.
  73. void SetLayer(int layer);
  74. /// Set order in layer.
  75. void SetOrderInLayer(int orderInLayer);
  76. /// Return layer.
  77. int GetLayer() const { return layer_; }
  78. /// Return order in layer.
  79. int GetOrderInLayer() const { return orderInLayer_; }
  80. /// Return all source batches (called by Renderer2D).
  81. const Vector<SourceBatch2D>& GetSourceBatches();
  82. protected:
  83. /// Handle scene being assigned.
  84. virtual void OnSceneSet(Scene* scene);
  85. /// Handle node transform being dirtied.
  86. virtual void OnMarkedDirty(Node* node);
  87. /// Handle draw order changed.
  88. virtual void OnDrawOrderChanged() = 0;
  89. /// Update source batches.
  90. virtual void UpdateSourceBatches() = 0;
  91. /// Return draw order by layer and order in layer.
  92. int GetDrawOrder() const { return (layer_ << 20) + (orderInLayer_ << 10); }
  93. /// Layer.
  94. int layer_;
  95. /// Order in layer.
  96. int orderInLayer_;
  97. /// Source batches.
  98. Vector<SourceBatch2D> sourceBatches_;
  99. /// Source batches dirty flag.
  100. bool sourceBatchesDirty_;
  101. /// Renderer2D.
  102. WeakPtr<Renderer2D> renderer_;
  103. };
  104. }