StaticSprite2D.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // Copyright (c) 2008-2016 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 "../Core/Context.h"
  24. #include "../Graphics/Material.h"
  25. #include "../Graphics/Texture2D.h"
  26. #include "../Resource/ResourceCache.h"
  27. #include "../Scene/Scene.h"
  28. #include "../Urho2D/Renderer2D.h"
  29. #include "../Urho2D/Sprite2D.h"
  30. #include "../Urho2D/StaticSprite2D.h"
  31. #include "../DebugNew.h"
  32. namespace Urho3D
  33. {
  34. extern const char* URHO2D_CATEGORY;
  35. extern const char* blendModeNames[];
  36. StaticSprite2D::StaticSprite2D(Context* context) :
  37. Drawable2D(context),
  38. blendMode_(BLEND_ALPHA),
  39. flipX_(false),
  40. flipY_(false),
  41. color_(Color::WHITE),
  42. useHotSpot_(false),
  43. hotSpot_(0.5f, 0.5f)
  44. {
  45. sourceBatches_.Resize(1);
  46. sourceBatches_[0].owner_ = this;
  47. }
  48. StaticSprite2D::~StaticSprite2D()
  49. {
  50. }
  51. void StaticSprite2D::RegisterObject(Context* context)
  52. {
  53. context->RegisterFactory<StaticSprite2D>(URHO2D_CATEGORY);
  54. URHO3D_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  55. URHO3D_COPY_BASE_ATTRIBUTES(Drawable2D);
  56. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Sprite", GetSpriteAttr, SetSpriteAttr, ResourceRef, ResourceRef(Sprite2D::GetTypeStatic()),
  57. AM_DEFAULT);
  58. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Blend Mode", GetBlendMode, SetBlendMode, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
  59. URHO3D_ACCESSOR_ATTRIBUTE("Flip X", GetFlipX, SetFlipX, bool, false, AM_DEFAULT);
  60. URHO3D_ACCESSOR_ATTRIBUTE("Flip Y", GetFlipY, SetFlipY, bool, false, AM_DEFAULT);
  61. URHO3D_ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_DEFAULT);
  62. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Custom material", GetCustomMaterialAttr, SetCustomMaterialAttr, ResourceRef,
  63. ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
  64. }
  65. void StaticSprite2D::SetSprite(Sprite2D* sprite)
  66. {
  67. if (sprite == sprite_)
  68. return;
  69. sprite_ = sprite;
  70. UpdateMaterial();
  71. sourceBatchesDirty_ = true;
  72. MarkNetworkUpdate();
  73. }
  74. void StaticSprite2D::SetBlendMode(BlendMode blendMode)
  75. {
  76. if (blendMode == blendMode_)
  77. return;
  78. blendMode_ = blendMode;
  79. UpdateMaterial();
  80. MarkNetworkUpdate();
  81. }
  82. void StaticSprite2D::SetFlip(bool flipX, bool flipY)
  83. {
  84. if (flipX == flipX_ && flipY == flipY_)
  85. return;
  86. flipX_ = flipX;
  87. flipY_ = flipY;
  88. sourceBatchesDirty_ = true;
  89. MarkNetworkUpdate();
  90. }
  91. void StaticSprite2D::SetFlipX(bool flipX)
  92. {
  93. SetFlip(flipX, flipY_);
  94. }
  95. void StaticSprite2D::SetFlipY(bool flipY)
  96. {
  97. SetFlip(flipX_, flipY);
  98. }
  99. void StaticSprite2D::SetColor(const Color& color)
  100. {
  101. if (color == color_)
  102. return;
  103. color_ = color;
  104. sourceBatchesDirty_ = true;
  105. MarkNetworkUpdate();
  106. }
  107. void StaticSprite2D::SetAlpha(float alpha)
  108. {
  109. if (alpha == color_.a_)
  110. return;
  111. color_.a_ = alpha;
  112. sourceBatchesDirty_ = true;
  113. MarkNetworkUpdate();
  114. }
  115. void StaticSprite2D::SetUseHotSpot(bool useHotSpot)
  116. {
  117. if (useHotSpot == useHotSpot_)
  118. return;
  119. useHotSpot_ = useHotSpot;
  120. sourceBatchesDirty_ = true;
  121. MarkNetworkUpdate();
  122. }
  123. void StaticSprite2D::SetHotSpot(const Vector2& hotspot)
  124. {
  125. if (hotspot == hotSpot_)
  126. return;
  127. hotSpot_ = hotspot;
  128. if (useHotSpot_)
  129. {
  130. sourceBatchesDirty_ = true;
  131. MarkNetworkUpdate();
  132. }
  133. }
  134. void StaticSprite2D::SetCustomMaterial(Material* customMaterial)
  135. {
  136. if (customMaterial == customMaterial_)
  137. return;
  138. customMaterial_ = customMaterial;
  139. sourceBatchesDirty_ = true;
  140. UpdateMaterial();
  141. MarkNetworkUpdate();
  142. }
  143. Sprite2D* StaticSprite2D::GetSprite() const
  144. {
  145. return sprite_;
  146. }
  147. Material* StaticSprite2D::GetCustomMaterial() const
  148. {
  149. return customMaterial_;
  150. }
  151. void StaticSprite2D::SetSpriteAttr(const ResourceRef& value)
  152. {
  153. Sprite2D* sprite = Sprite2D::LoadFromResourceRef(this, value);
  154. if (sprite)
  155. SetSprite(sprite);
  156. }
  157. ResourceRef StaticSprite2D::GetSpriteAttr() const
  158. {
  159. return Sprite2D::SaveToResourceRef(sprite_);
  160. }
  161. void StaticSprite2D::SetCustomMaterialAttr(const ResourceRef& value)
  162. {
  163. ResourceCache* cache = GetSubsystem<ResourceCache>();
  164. SetCustomMaterial(cache->GetResource<Material>(value.name_));
  165. }
  166. ResourceRef StaticSprite2D::GetCustomMaterialAttr() const
  167. {
  168. return GetResourceRef(customMaterial_, Material::GetTypeStatic());
  169. }
  170. void StaticSprite2D::OnWorldBoundingBoxUpdate()
  171. {
  172. boundingBox_.Clear();
  173. worldBoundingBox_.Clear();
  174. const Vector<SourceBatch2D>& sourceBatches = GetSourceBatches();
  175. for (unsigned i = 0; i < sourceBatches[0].vertices_.Size(); ++i)
  176. worldBoundingBox_.Merge(sourceBatches[0].vertices_[i].position_);
  177. boundingBox_ = worldBoundingBox_.Transformed(node_->GetWorldTransform().Inverse());
  178. }
  179. void StaticSprite2D::OnDrawOrderChanged()
  180. {
  181. sourceBatches_[0].drawOrder_ = GetDrawOrder();
  182. }
  183. void StaticSprite2D::UpdateSourceBatches()
  184. {
  185. if (!sourceBatchesDirty_)
  186. return;
  187. Vector<Vertex2D>& vertices = sourceBatches_[0].vertices_;
  188. vertices.Clear();
  189. if (!sprite_)
  190. return;
  191. Rect drawRect;
  192. if (useHotSpot_)
  193. {
  194. if (!sprite_->GetDrawRectangle(drawRect, hotSpot_, flipX_, flipY_))
  195. return;
  196. }
  197. else
  198. {
  199. if (!sprite_->GetDrawRectangle(drawRect, flipX_, flipY_))
  200. return;
  201. }
  202. Rect textureRect;
  203. if (!sprite_->GetTextureRectangle(textureRect, flipX_, flipY_))
  204. return;
  205. /*
  206. V1---------V2
  207. | / |
  208. | / |
  209. | / |
  210. | / |
  211. | / |
  212. V0---------V3
  213. */
  214. Vertex2D vertex0;
  215. Vertex2D vertex1;
  216. Vertex2D vertex2;
  217. Vertex2D vertex3;
  218. // Convert to world space
  219. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  220. vertex0.position_ = worldTransform * Vector3(drawRect.min_.x_, drawRect.min_.y_, 0.0f);
  221. vertex1.position_ = worldTransform * Vector3(drawRect.min_.x_, drawRect.max_.y_, 0.0f);
  222. vertex2.position_ = worldTransform * Vector3(drawRect.max_.x_, drawRect.max_.y_, 0.0f);
  223. vertex3.position_ = worldTransform * Vector3(drawRect.max_.x_, drawRect.min_.y_, 0.0f);
  224. vertex0.uv_ = textureRect.min_;
  225. vertex1.uv_ = Vector2(textureRect.min_.x_, textureRect.max_.y_);
  226. vertex2.uv_ = textureRect.max_;
  227. vertex3.uv_ = Vector2(textureRect.max_.x_, textureRect.min_.y_);
  228. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  229. vertices.Push(vertex0);
  230. vertices.Push(vertex1);
  231. vertices.Push(vertex2);
  232. vertices.Push(vertex3);
  233. sourceBatchesDirty_ = false;
  234. }
  235. void StaticSprite2D::UpdateMaterial()
  236. {
  237. if (customMaterial_)
  238. sourceBatches_[0].material_ = customMaterial_;
  239. else
  240. {
  241. if (sprite_)
  242. sourceBatches_[0].material_ = renderer_->GetMaterial(sprite_->GetTexture(), blendMode_);
  243. else
  244. sourceBatches_[0].material_ = 0;
  245. }
  246. }
  247. }