Drawable2D.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "Sprite2D.h"
  25. namespace Urho3D
  26. {
  27. class VertexBuffer;
  28. /// Base class for 2D visible components.
  29. class URHO3D_API Drawable2D : public Drawable
  30. {
  31. OBJECT(Drawable2D);
  32. public:
  33. /// Construct.
  34. Drawable2D(Context* context);
  35. /// Destruct.
  36. ~Drawable2D();
  37. /// Register object factory. Drawable must be registered first.
  38. static void RegisterObject(Context* context);
  39. /// Apply attribute changes that can not be applied immediately.
  40. virtual void ApplyAttributes();
  41. /// Calculate distance and prepare batches for rendering. May be called from worker thread(s), possibly re-entrantly.
  42. virtual void UpdateBatches(const FrameInfo& frame);
  43. /// Prepare geometry for rendering. Called from a worker thread if possible (no GPU update.)
  44. virtual void UpdateGeometry(const FrameInfo& frame);
  45. /// Return whether a geometry update is necessary, and if it can happen in a worker thread.
  46. virtual UpdateGeometryType GetUpdateGeometryType();
  47. /// Set pixels per coordinate unit.
  48. void SetPixelsPerUnit(float pixelsPerUnit);
  49. /// Set sprite.
  50. void SetSprite(Sprite2D* sprite);
  51. /// Set material.
  52. void SetMaterial(Material* material);
  53. /// Set blend mode.
  54. void SetBlendMode(BlendMode mode);
  55. /// Set Z value.
  56. void SetZValue(float zValue);
  57. /// Return pixels per coordinate unit.
  58. float GetPixelsPerUnit() const { return pixelsPerUnit_; }
  59. /// Return sprite.
  60. Sprite2D* GetSprite() const { return sprite_; }
  61. /// Return material.
  62. Material* GetMaterial() const;
  63. /// Return blend mode.
  64. BlendMode GetBlendMode() const { return blendMode_; }
  65. /// Return Z value.
  66. float GetZValue() const { return zValue_; }
  67. /// Return all vertices.
  68. const Vector<Vertex2D>& GetVertices() const { return vertices_; }
  69. /// Set sprite attribute.
  70. void SetSpriteAttr(ResourceRef value);
  71. /// Return sprite attribute.
  72. ResourceRef GetSpriteAttr() const;
  73. /// Set material attribute.
  74. void SetMaterialAttr(ResourceRef value);
  75. /// Return material attribute.
  76. ResourceRef GetMaterialAttr() const;
  77. /// Set blend mode attribute.
  78. void SetBlendModeAttr(BlendMode mode);
  79. protected:
  80. /// Recalculate the world-space bounding box.
  81. virtual void OnWorldBoundingBoxUpdate();
  82. /// Update vertices.
  83. virtual void UpdateVertices() = 0;
  84. /// Create a default material when a material is not specified.
  85. void CreateDefaultMaterial();
  86. /// Update the material's properties (blend mode and texture).
  87. void UpdateMaterial();
  88. /// Pixels per coordinate unit.
  89. float pixelsPerUnit_;
  90. /// Z value.
  91. float zValue_;
  92. /// Sprite.
  93. SharedPtr<Sprite2D> sprite_;
  94. /// Material. If null, use a default material. If non-null, use a clone of this for updating the diffuse texture.
  95. SharedPtr<Material> material_;
  96. /// Blend mode.
  97. BlendMode blendMode_;
  98. /// Vertices.
  99. Vector<Vertex2D> vertices_;
  100. /// Geometry.
  101. SharedPtr<Geometry> geometry_;
  102. /// Vertex buffer.
  103. SharedPtr<VertexBuffer> vertexBuffer_;
  104. /// Vertices dirty flag.
  105. bool verticesDirty_;
  106. /// Geometry dirty flag.
  107. bool geometryDirty_;
  108. /// Material update pending flag.
  109. bool materialUpdatePending_;
  110. };
  111. }