Drawable2D.cpp 7.0 KB

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