Drawable2D.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. //
  2. // Copyright (c) 2008-2014 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 "Drawable.h"
  24. #include "Material.h"
  25. #include "Sprite2D.h"
  26. namespace Urho3D
  27. {
  28. class DrawableProxy2D;
  29. class MaterialCache2D;
  30. class VertexBuffer;
  31. /// Pixel size (equal 0.01f).
  32. extern URHO3D_API const float PIXEL_SIZE;
  33. /// Base class for 2D visible components.
  34. class URHO3D_API Drawable2D : public Drawable
  35. {
  36. OBJECT(Drawable2D);
  37. public:
  38. /// Construct.
  39. Drawable2D(Context* context);
  40. /// Destruct.
  41. ~Drawable2D();
  42. /// Register object factory. Drawable must be registered first.
  43. static void RegisterObject(Context* context);
  44. /// Apply attribute changes that can not be applied immediately.
  45. virtual void ApplyAttributes();
  46. /// Handle enabled/disabled state change.
  47. virtual void OnSetEnabled();
  48. /// Set layer.
  49. void SetLayer(int layer);
  50. /// Set order in layer.
  51. void SetOrderInLayer(int orderInLayer);
  52. /// Set sprite.
  53. void SetSprite(Sprite2D* sprite);
  54. /// Set blend mode.
  55. void SetBlendMode(BlendMode mode);
  56. /// Set material.
  57. void SetMaterial(Material* material);
  58. /// Return layer.
  59. int GetLayer() const { return layer_; }
  60. /// Return order in layer.
  61. int GetOrderInLayer() const { return orderInLayer_; }
  62. /// Return sprite.
  63. Sprite2D* GetSprite() const { return sprite_; }
  64. /// Return texture.
  65. Texture2D* GetTexture() const { return sprite_ ? sprite_->GetTexture() : 0; }
  66. /// Return blend mode.
  67. BlendMode GetBlendMode() const { return blendMode_; }
  68. /// Return material.
  69. Material* GetMaterial() const;
  70. /// Return used material.
  71. Material* GetUsedMaterial() const;
  72. /// Return all vertices.
  73. const Vector<Vertex2D>& GetVertices();
  74. /// Set visibility.
  75. void SetVisibility(bool visibility) { visibility_ = visibility; }
  76. /// Return visibility.
  77. bool GetVisibility() const { return visibility_; }
  78. /// Set sprite attribute.
  79. void SetSpriteAttr(ResourceRef value);
  80. /// Return sprite attribute.
  81. ResourceRef GetSpriteAttr() const;
  82. /// Set blend mode attribute.
  83. void SetBlendModeAttr(BlendMode mode);
  84. /// Set material attribute.
  85. void SetMaterialAttr(ResourceRef value);
  86. /// Return material attribute.
  87. ResourceRef GetMaterialAttr() const;
  88. protected:
  89. /// Handle node being assigned.
  90. virtual void OnNodeSet(Node* node);
  91. /// Handle node transform being dirtied.
  92. virtual void OnMarkedDirty(Node* node);
  93. /// Update vertices.
  94. virtual void UpdateVertices() = 0;
  95. /// Update the material's properties (blend mode and texture).
  96. void UpdateDefaultMaterial();
  97. /// Layer.
  98. int layer_;
  99. /// Order in layer.
  100. int orderInLayer_;
  101. /// Sprite.
  102. SharedPtr<Sprite2D> sprite_;
  103. /// Material. If null, use a default material. If non-null, use a clone of this for updating the diffuse texture.
  104. SharedPtr<Material> material_;
  105. /// Blend mode.
  106. BlendMode blendMode_;
  107. /// Vertices.
  108. Vector<Vertex2D> vertices_;
  109. /// Vertices dirty flag.
  110. bool verticesDirty_;
  111. /// Material update pending flag.
  112. bool materialUpdatePending_;
  113. /// Default material.
  114. SharedPtr<Material> defaultMaterial_;
  115. /// Material cache.
  116. WeakPtr<MaterialCache2D> materialCache_;
  117. /// Drawable proxy.
  118. WeakPtr<DrawableProxy2D> drawableProxy_;
  119. /// Test visible.
  120. bool visibility_;
  121. };
  122. inline bool CompareDrawable2Ds(Drawable2D* lhs, Drawable2D* rhs)
  123. {
  124. if (lhs->GetLayer() != rhs->GetLayer())
  125. return lhs->GetLayer() < rhs->GetLayer();
  126. if (lhs->GetOrderInLayer() != rhs->GetOrderInLayer())
  127. return lhs->GetOrderInLayer() < rhs->GetOrderInLayer();
  128. Material* lhsUsedMaterial = lhs->GetUsedMaterial();
  129. Material* rhsUsedMaterial = rhs->GetUsedMaterial();
  130. if (lhsUsedMaterial != rhsUsedMaterial)
  131. return lhsUsedMaterial->GetNameHash() < rhsUsedMaterial->GetNameHash();
  132. return lhs->GetID() < rhs->GetID();
  133. }
  134. }