Drawable2D.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. static const unsigned MASK_VERTEX2D = MASK_POSITION | MASK_COLOR | MASK_TEXCOORD1;
  41. /// Pixel size (equal 0.01f).
  42. extern ATOMIC_API const float PIXEL_SIZE;
  43. /// Base class for 2D visible components.
  44. class ATOMIC_API Drawable2D : public Drawable
  45. {
  46. OBJECT(Drawable2D);
  47. public:
  48. /// Construct.
  49. Drawable2D(Context* context);
  50. /// Destruct.
  51. ~Drawable2D();
  52. /// Register object factory. Drawable must be registered first.
  53. static void RegisterObject(Context* context);
  54. /// Handle enabled/disabled state change.
  55. virtual void OnSetEnabled();
  56. /// Set layer.
  57. void SetLayer(int layer);
  58. /// Set order in layer.
  59. void SetOrderInLayer(int orderInLayer);
  60. /// Set texture.
  61. void SetTexture(Texture2D* texture);
  62. /// Set blend mode.
  63. void SetBlendMode(BlendMode mode);
  64. /// Set custom material.
  65. void SetCustomMaterial(Material* customMaterial);
  66. /// Return layer.
  67. int GetLayer() const { return layer_; }
  68. /// Return order in layer.
  69. int GetOrderInLayer() const { return orderInLayer_; }
  70. /// Return texture.
  71. Texture2D* GetTexture() const;
  72. /// Return blend mode.
  73. BlendMode GetBlendMode() const { return blendMode_; }
  74. /// Return custom material.
  75. Material* GetCustomMaterial() const;
  76. /// Set material (called by Renderer2D).
  77. void SetMaterial(Material* material);
  78. /// Return custom material or material (called by Renderer2D).
  79. Material* GetMaterial() const;
  80. /// Return all vertices (called by Renderer2D).
  81. const Vector<Vertex2D>& GetVertices();
  82. protected:
  83. /// Handle node being assigned.
  84. virtual void OnNodeSet(Node* node);
  85. /// Handle node transform being dirtied.
  86. virtual void OnMarkedDirty(Node* node);
  87. /// Handle layer changed.
  88. virtual void OnLayerChanged();
  89. /// Handle blend mode changed.
  90. virtual void OnBlendModeChanged();
  91. /// Update vertices.
  92. virtual void UpdateVertices() = 0;
  93. /// Layer.
  94. int layer_;
  95. /// Order in layer.
  96. int orderInLayer_;
  97. /// Texture.
  98. SharedPtr<Texture2D> texture_;
  99. /// Blend mode.
  100. BlendMode blendMode_;
  101. /// Custom material.
  102. SharedPtr<Material> customMaterial_;
  103. /// Vertices.
  104. Vector<Vertex2D> vertices_;
  105. /// Vertices dirty flag.
  106. bool verticesDirty_;
  107. /// Material.
  108. SharedPtr<Material> material_;
  109. /// Renderer2D.
  110. WeakPtr<Renderer2D> renderer_;
  111. };
  112. }