Drawable2D.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // Copyright (c) 2008-2015 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 Renderer2D;
  28. class Texture2D;
  29. class VertexBuffer;
  30. /// 2D vertex.
  31. struct Vertex2D
  32. {
  33. /// Position.
  34. Vector3 position_;
  35. /// Color.
  36. unsigned color_;
  37. /// UV.
  38. Vector2 uv_;
  39. };
  40. /// 2D source batch.
  41. struct SourceBatch2D
  42. {
  43. /// Construct.
  44. SourceBatch2D();
  45. /// Draw order.
  46. int drawOrder_;
  47. /// Material.
  48. SharedPtr<Material> material_;
  49. /// Vertices.
  50. Vector<Vertex2D> vertices_;
  51. };
  52. /// Pixel size (equal 0.01f).
  53. extern ATOMIC_API const float PIXEL_SIZE;
  54. /// Base class for 2D visible components.
  55. class ATOMIC_API Drawable2D : public Drawable
  56. {
  57. OBJECT(Drawable2D);
  58. public:
  59. /// Construct.
  60. Drawable2D(Context* context);
  61. /// Destruct.
  62. ~Drawable2D();
  63. /// Register object factory. Drawable must be registered first.
  64. static void RegisterObject(Context* context);
  65. /// Handle enabled/disabled state change.
  66. virtual void OnSetEnabled();
  67. /// Set layer.
  68. void SetLayer(int layer);
  69. /// Set order in layer.
  70. void SetOrderInLayer(int orderInLayer);
  71. /// Return layer.
  72. int GetLayer() const { return layer_; }
  73. /// Return order in layer.
  74. int GetOrderInLayer() const { return orderInLayer_; }
  75. /// Return all source batches (called by Renderer2D).
  76. const Vector<SourceBatch2D>& GetSourceBatches();
  77. protected:
  78. /// Handle scene being assigned.
  79. virtual void OnSceneSet(Scene* scene);
  80. /// Handle node transform being dirtied.
  81. virtual void OnMarkedDirty(Node* node);
  82. /// Handle draw order changed.
  83. virtual void OnDrawOrderChanged() = 0;
  84. /// Update source batches.
  85. virtual void UpdateSourceBatches() = 0;
  86. /// Return draw order by layer and order in layer.
  87. int GetDrawOrder() const { return (layer_ << 20) + (orderInLayer_ << 10); }
  88. /// Layer.
  89. int layer_;
  90. /// Order in layer.
  91. int orderInLayer_;
  92. /// Source batches.
  93. Vector<SourceBatch2D> sourceBatches_;
  94. /// Source batches dirty flag.
  95. bool sourceBatchesDirty_;
  96. /// Renderer2D.
  97. WeakPtr<Renderer2D> renderer_;
  98. };
  99. }