Drawable2D.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. #include "Precompiled.h"
  23. #include "../Graphics/Camera.h"
  24. #include "../Core/Context.h"
  25. #include "../Atomic2D/Drawable2D.h"
  26. #include "../Graphics/Material.h"
  27. #include "../Atomic2D/Renderer2D.h"
  28. #include "../Scene/Scene.h"
  29. #include "../Graphics/Texture2D.h"
  30. #include "../DebugNew.h"
  31. namespace Atomic
  32. {
  33. const float PIXEL_SIZE = 0.01f;
  34. extern const char* blendModeNames[];
  35. Drawable2D::Drawable2D(Context* context) :
  36. Drawable(context, DRAWABLE_GEOMETRY2D),
  37. layer_(0),
  38. orderInLayer_(0),
  39. blendMode_(BLEND_ALPHA),
  40. verticesDirty_(true)
  41. {
  42. }
  43. Drawable2D::~Drawable2D()
  44. {
  45. if (renderer_)
  46. renderer_->RemoveDrawable(this);
  47. }
  48. void Drawable2D::RegisterObject(Context* context)
  49. {
  50. ACCESSOR_ATTRIBUTE("Layer", GetLayer, SetLayer, int, 0, AM_DEFAULT);
  51. ACCESSOR_ATTRIBUTE("Order in Layer", GetOrderInLayer, SetOrderInLayer, int, 0, AM_DEFAULT);
  52. ENUM_ACCESSOR_ATTRIBUTE("Blend Mode", GetBlendMode, SetBlendMode, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
  53. COPY_BASE_ATTRIBUTES(Drawable);
  54. }
  55. void Drawable2D::OnSetEnabled()
  56. {
  57. bool enabled = IsEnabledEffective();
  58. if (enabled && renderer_)
  59. renderer_->AddDrawable(this);
  60. else if (!enabled && renderer_)
  61. renderer_->RemoveDrawable(this);
  62. }
  63. void Drawable2D::SetLayer(int layer)
  64. {
  65. if (layer == layer_)
  66. return;
  67. layer_ = layer;
  68. OnLayerChanged();
  69. if (renderer_)
  70. renderer_->MarkOrderDirty();
  71. MarkNetworkUpdate();
  72. }
  73. void Drawable2D::SetOrderInLayer(int orderInLayer)
  74. {
  75. if (orderInLayer == orderInLayer_)
  76. return;
  77. orderInLayer_ = orderInLayer;
  78. OnLayerChanged();
  79. if (renderer_)
  80. renderer_->MarkOrderDirty();
  81. MarkNetworkUpdate();
  82. }
  83. void Drawable2D::SetTexture(Texture2D* texture)
  84. {
  85. if (texture == texture_)
  86. return;
  87. texture_ = texture;
  88. verticesDirty_ = true;
  89. material_ = 0;
  90. if (renderer_)
  91. renderer_->MarkMaterialDirty(this);
  92. OnMarkedDirty(node_);
  93. MarkNetworkUpdate();
  94. }
  95. void Drawable2D::SetBlendMode(BlendMode blendMode)
  96. {
  97. if (blendMode == blendMode_)
  98. return;
  99. blendMode_ = blendMode;
  100. material_ = 0;
  101. if (renderer_)
  102. renderer_->MarkMaterialDirty(this);
  103. OnBlendModeChanged();
  104. MarkNetworkUpdate();
  105. }
  106. void Drawable2D::SetCustomMaterial(Material* customMaterial)
  107. {
  108. if (customMaterial == customMaterial_)
  109. return;
  110. customMaterial_ = customMaterial;
  111. material_ = 0;
  112. if (renderer_)
  113. renderer_->MarkMaterialDirty(this);
  114. MarkNetworkUpdate();
  115. }
  116. Texture2D* Drawable2D::GetTexture() const
  117. {
  118. return texture_;
  119. }
  120. Material* Drawable2D::GetCustomMaterial() const
  121. {
  122. return customMaterial_;
  123. }
  124. void Drawable2D::SetMaterial(Material* material)
  125. {
  126. material_ = material;
  127. }
  128. Material* Drawable2D::GetMaterial() const
  129. {
  130. return customMaterial_ ? customMaterial_ : material_;
  131. }
  132. const Vector<Vertex2D>& Drawable2D::GetVertices()
  133. {
  134. if (verticesDirty_)
  135. UpdateVertices();
  136. return vertices_;
  137. }
  138. void Drawable2D::OnNodeSet(Node* node)
  139. {
  140. // Do not call Drawable::OnNodeSet(node)
  141. if (node)
  142. {
  143. Scene* scene = GetScene();
  144. if (scene)
  145. {
  146. renderer_ = scene->GetOrCreateComponent<Renderer2D>();
  147. if (IsEnabledEffective())
  148. renderer_->AddDrawable(this);
  149. }
  150. node->AddListener(this);
  151. }
  152. else
  153. {
  154. if (renderer_)
  155. renderer_->RemoveDrawable(this);
  156. }
  157. }
  158. void Drawable2D::OnMarkedDirty(Node* node)
  159. {
  160. Drawable::OnMarkedDirty(node);
  161. verticesDirty_ = true;
  162. }
  163. void Drawable2D::OnLayerChanged()
  164. {
  165. }
  166. void Drawable2D::OnBlendModeChanged()
  167. {
  168. }
  169. }