Drawable2D.cpp 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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. #include "Precompiled.h"
  23. #include "Camera.h"
  24. #include "Context.h"
  25. #include "Drawable2D.h"
  26. #include "DrawableProxy2D.h"
  27. #include "Geometry.h"
  28. #include "Log.h"
  29. #include "Material.h"
  30. #include "MaterialCache2D.h"
  31. #include "Node.h"
  32. #include "ResourceCache.h"
  33. #include "Scene.h"
  34. #include "Sprite2D.h"
  35. #include "SpriteSheet2D.h"
  36. #include "Technique.h"
  37. #include "Texture2D.h"
  38. #include "VertexBuffer.h"
  39. #include "DebugNew.h"
  40. namespace Urho3D
  41. {
  42. const float PIXEL_SIZE = 0.01f;
  43. extern const char* blendModeNames[];
  44. Drawable2D::Drawable2D(Context* context) :
  45. Drawable(context, DRAWABLE_GEOMETRY),
  46. layer_(0),
  47. orderInLayer_(0),
  48. blendMode_(BLEND_ALPHA),
  49. verticesDirty_(true),
  50. materialUpdatePending_(false),
  51. visibility_(true)
  52. {
  53. }
  54. Drawable2D::~Drawable2D()
  55. {
  56. if (drawableProxy_)
  57. drawableProxy_->RemoveDrawable(this);
  58. }
  59. void Drawable2D::RegisterObject(Context* context)
  60. {
  61. ACCESSOR_ATTRIBUTE("Layer", GetLayer, SetLayer, int, 0, AM_DEFAULT);
  62. ACCESSOR_ATTRIBUTE("Order in Layer", GetOrderInLayer, SetOrderInLayer, int, 0, AM_DEFAULT);
  63. MIXED_ACCESSOR_ATTRIBUTE("Sprite", GetSpriteAttr, SetSpriteAttr, ResourceRef, ResourceRef(Sprite2D::GetTypeStatic()), AM_DEFAULT);
  64. ENUM_ACCESSOR_ATTRIBUTE("Blend Mode", GetBlendMode, SetBlendModeAttr, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
  65. MIXED_ACCESSOR_ATTRIBUTE("Material", GetMaterialAttr, SetMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
  66. COPY_BASE_ATTRIBUTES(Drawable);
  67. }
  68. void Drawable2D::ApplyAttributes()
  69. {
  70. if (materialUpdatePending_)
  71. {
  72. materialUpdatePending_ = false;
  73. UpdateDefaultMaterial();
  74. }
  75. }
  76. void Drawable2D::OnSetEnabled()
  77. {
  78. if (!drawableProxy_)
  79. return;
  80. if (IsEnabledEffective())
  81. drawableProxy_->AddDrawable(this);
  82. else
  83. drawableProxy_->RemoveDrawable(this);
  84. }
  85. void Drawable2D::SetLayer(int layer)
  86. {
  87. if (layer == layer_)
  88. return;
  89. layer_ = layer;
  90. if (drawableProxy_)
  91. drawableProxy_->MarkOrderDirty();
  92. MarkNetworkUpdate();
  93. }
  94. void Drawable2D::SetOrderInLayer(int orderInLayer)
  95. {
  96. if (orderInLayer == orderInLayer_)
  97. return;
  98. orderInLayer_ = orderInLayer;
  99. if (drawableProxy_)
  100. drawableProxy_->MarkOrderDirty();
  101. MarkNetworkUpdate();
  102. }
  103. void Drawable2D::SetSprite(Sprite2D* sprite)
  104. {
  105. if (sprite == sprite_)
  106. return;
  107. sprite_ = sprite;
  108. verticesDirty_ = true;
  109. OnMarkedDirty(node_);
  110. UpdateDefaultMaterial();
  111. MarkNetworkUpdate();
  112. }
  113. void Drawable2D::SetBlendMode(BlendMode blendMode)
  114. {
  115. if (blendMode == blendMode_)
  116. return;
  117. blendMode_ = blendMode;
  118. UpdateDefaultMaterial();
  119. MarkNetworkUpdate();
  120. }
  121. void Drawable2D::SetMaterial(Material* material)
  122. {
  123. if (material == material_)
  124. return;
  125. material_ = material;
  126. if (drawableProxy_)
  127. drawableProxy_->MarkOrderDirty();
  128. MarkNetworkUpdate();
  129. }
  130. Material* Drawable2D::GetMaterial() const
  131. {
  132. return material_;
  133. }
  134. Material* Drawable2D::GetUsedMaterial() const
  135. {
  136. return material_ ? material_ : defaultMaterial_;
  137. }
  138. const Vector<Vertex2D>& Drawable2D::GetVertices()
  139. {
  140. if (verticesDirty_)
  141. UpdateVertices();
  142. return vertices_;
  143. }
  144. void Drawable2D::SetSpriteAttr(const ResourceRef& value)
  145. {
  146. // Delay applying material update
  147. materialUpdatePending_ = true;
  148. ResourceCache* cache = GetSubsystem<ResourceCache>();
  149. if (value.type_ == Sprite2D::GetTypeStatic())
  150. {
  151. SetSprite(cache->GetResource<Sprite2D>(value.name_));
  152. return;
  153. }
  154. if (value.type_ == SpriteSheet2D::GetTypeStatic())
  155. {
  156. // value.name_ include sprite speet name and sprite name.
  157. Vector<String> names = value.name_.Split('@');
  158. if (names.Size() != 2)
  159. return;
  160. const String& spriteSheetName = names[0];
  161. const String& spriteName = names[1];
  162. SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetName);
  163. if (!spriteSheet)
  164. return;
  165. SetSprite(spriteSheet->GetSprite(spriteName));
  166. }
  167. }
  168. ResourceRef Drawable2D::GetSpriteAttr() const
  169. {
  170. SpriteSheet2D* spriteSheet = 0;
  171. if (sprite_)
  172. spriteSheet = sprite_->GetSpriteSheet();
  173. if (!spriteSheet)
  174. return GetResourceRef(sprite_, Sprite2D::GetTypeStatic());
  175. // Combine sprite sheet name and sprite name as resource name.
  176. return ResourceRef(spriteSheet->GetType(), spriteSheet->GetName() + "@" + sprite_->GetName());
  177. }
  178. void Drawable2D::SetBlendModeAttr(BlendMode mode)
  179. {
  180. // Delay applying material update
  181. materialUpdatePending_ = true;
  182. SetBlendMode(mode);
  183. }
  184. void Drawable2D::SetMaterialAttr(const ResourceRef& value)
  185. {
  186. // Delay applying material update
  187. materialUpdatePending_ = true;
  188. ResourceCache* cache = GetSubsystem<ResourceCache>();
  189. SetMaterial(cache->GetResource<Material>(value.name_));
  190. }
  191. ResourceRef Drawable2D::GetMaterialAttr() const
  192. {
  193. return GetResourceRef(material_, Material::GetTypeStatic());
  194. }
  195. void Drawable2D::OnNodeSet(Node* node)
  196. {
  197. Drawable::OnNodeSet(node);
  198. if (node)
  199. {
  200. Scene* scene = GetScene();
  201. if (scene)
  202. {
  203. materialCache_ = scene->GetOrCreateComponent<MaterialCache2D>();
  204. drawableProxy_ = scene->GetOrCreateComponent<DrawableProxy2D>();
  205. if (IsEnabledEffective())
  206. drawableProxy_->AddDrawable(this);
  207. }
  208. }
  209. }
  210. void Drawable2D::OnMarkedDirty(Node* node)
  211. {
  212. Drawable::OnMarkedDirty(node);
  213. verticesDirty_ = true;
  214. }
  215. void Drawable2D::UpdateDefaultMaterial()
  216. {
  217. // Delay the material update
  218. if (materialUpdatePending_)
  219. return;
  220. defaultMaterial_ = materialCache_->GetMaterial(GetTexture(), blendMode_);
  221. if (drawableProxy_)
  222. drawableProxy_->MarkOrderDirty();
  223. }
  224. }