Drawable2D.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 Atomic
  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. // ATOMIC BEGIN
  58. // Defined in header to be consistent with other constants
  59. /// Pixel size (equal 0.01f).
  60. static const float PIXEL_SIZE = 0.01f;
  61. // ATOMIC END
  62. /// Base class for 2D visible components.
  63. class ATOMIC_API Drawable2D : public Drawable
  64. {
  65. ATOMIC_OBJECT(Drawable2D, Drawable);
  66. public:
  67. /// Construct.
  68. Drawable2D(Context* context);
  69. /// Destruct.
  70. ~Drawable2D();
  71. /// Register object factory. Drawable must be registered first.
  72. static void RegisterObject(Context* context);
  73. /// Handle enabled/disabled state change.
  74. virtual void OnSetEnabled();
  75. /// Set layer.
  76. void SetLayer(int layer);
  77. /// Set order in layer.
  78. void SetOrderInLayer(int orderInLayer);
  79. /// Return layer.
  80. int GetLayer() const { return layer_; }
  81. /// Return order in layer.
  82. int GetOrderInLayer() const { return orderInLayer_; }
  83. /// Return all source batches (called by Renderer2D).
  84. const Vector<SourceBatch2D>& GetSourceBatches();
  85. protected:
  86. /// Handle scene being assigned.
  87. virtual void OnSceneSet(Scene* scene);
  88. /// Handle node transform being dirtied.
  89. virtual void OnMarkedDirty(Node* node);
  90. /// Handle draw order changed.
  91. virtual void OnDrawOrderChanged() = 0;
  92. /// Update source batches.
  93. virtual void UpdateSourceBatches() = 0;
  94. /// Return draw order by layer and order in layer.
  95. int GetDrawOrder() const { return (layer_ << 20) + (orderInLayer_ << 10); }
  96. /// Layer.
  97. int layer_;
  98. /// Order in layer.
  99. int orderInLayer_;
  100. /// Source batches.
  101. Vector<SourceBatch2D> sourceBatches_;
  102. /// Source batches dirty flag.
  103. bool sourceBatchesDirty_;
  104. /// Renderer2D.
  105. WeakPtr<Renderer2D> renderer_;
  106. };
  107. }