StaticSprite2D.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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 "../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 "../Atomic2D/Renderer2D.h"
  29. #include "../Atomic2D/Sprite2D.h"
  30. #include "../Atomic2D/StaticSprite2D.h"
  31. #include "../DebugNew.h"
  32. namespace Atomic
  33. {
  34. extern const char* ATOMIC2D_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. }
  47. StaticSprite2D::~StaticSprite2D()
  48. {
  49. }
  50. void StaticSprite2D::RegisterObject(Context* context)
  51. {
  52. context->RegisterFactory<StaticSprite2D>(ATOMIC2D_CATEGORY);
  53. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  54. COPY_BASE_ATTRIBUTES(Drawable2D);
  55. MIXED_ACCESSOR_ATTRIBUTE("Sprite", GetSpriteAttr, SetSpriteAttr, ResourceRef, ResourceRef(Sprite2D::GetTypeStatic()),
  56. AM_DEFAULT);
  57. ENUM_ACCESSOR_ATTRIBUTE("Blend Mode", GetBlendMode, SetBlendMode, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
  58. ACCESSOR_ATTRIBUTE("Flip X", GetFlipX, SetFlipX, bool, false, AM_DEFAULT);
  59. ACCESSOR_ATTRIBUTE("Flip Y", GetFlipY, SetFlipY, bool, false, AM_DEFAULT);
  60. ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_DEFAULT);
  61. MIXED_ACCESSOR_ATTRIBUTE("Custom material", GetCustomMaterialAttr, SetCustomMaterialAttr, ResourceRef,
  62. ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
  63. }
  64. void StaticSprite2D::SetSprite(Sprite2D* sprite)
  65. {
  66. if (sprite == sprite_)
  67. return;
  68. sprite_ = sprite;
  69. UpdateMaterial();
  70. sourceBatchesDirty_ = true;
  71. MarkNetworkUpdate();
  72. }
  73. void StaticSprite2D::SetBlendMode(BlendMode blendMode)
  74. {
  75. if (blendMode == blendMode_)
  76. return;
  77. blendMode_ = blendMode;
  78. UpdateMaterial();
  79. MarkNetworkUpdate();
  80. }
  81. void StaticSprite2D::SetFlip(bool flipX, bool flipY)
  82. {
  83. if (flipX == flipX_ && flipY == flipY_)
  84. return;
  85. flipX_ = flipX;
  86. flipY_ = flipY;
  87. sourceBatchesDirty_ = true;
  88. OnFlipChanged();
  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. if (sprite_)
  174. {
  175. const IntRect& rectangle_ = sprite_->GetRectangle();
  176. float width = (float)rectangle_.Width() * PIXEL_SIZE; // Compute width and height in pixels
  177. float height = (float)rectangle_.Height() * PIXEL_SIZE;
  178. const Vector2& hotSpot = sprite_->GetHotSpot();
  179. float hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;
  180. float hotSpotY = flipY_ ? (1.0f - hotSpot.y_) : hotSpot.y_;
  181. float leftX = -width * hotSpotX;
  182. float rightX = width * (1.0f - hotSpotX);
  183. float bottomY = -height * hotSpotY;
  184. float topY = height * (1.0f - hotSpotY);
  185. boundingBox_.Merge(Vector3(leftX, bottomY, 0.0f));
  186. boundingBox_.Merge(Vector3(rightX, topY, 0.0f));
  187. }
  188. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  189. }
  190. void StaticSprite2D::OnDrawOrderChanged()
  191. {
  192. sourceBatches_[0].drawOrder_ = GetDrawOrder();
  193. }
  194. void StaticSprite2D::UpdateSourceBatches()
  195. {
  196. if (!sourceBatchesDirty_)
  197. return;
  198. Vector<Vertex2D>& vertices = sourceBatches_[0].vertices_;
  199. vertices.Clear();
  200. if (!sprite_)
  201. return;
  202. Rect drawRect;
  203. if (useHotSpot_)
  204. {
  205. if (!sprite_->GetDrawRectangle(drawRect, hotSpot_, flipX_, flipY_))
  206. return;
  207. }
  208. else
  209. {
  210. if (!sprite_->GetDrawRectangle(drawRect, flipX_, flipY_))
  211. return;
  212. }
  213. Rect textureRect;
  214. if (!sprite_->GetTextureRectangle(textureRect, flipX_, flipY_))
  215. return;
  216. /*
  217. V1---------V2
  218. | / |
  219. | / |
  220. | / |
  221. | / |
  222. | / |
  223. V0---------V3
  224. */
  225. Vertex2D vertex0;
  226. Vertex2D vertex1;
  227. Vertex2D vertex2;
  228. Vertex2D vertex3;
  229. // Convert to world space
  230. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  231. vertex0.position_ = worldTransform * Vector3(drawRect.min_.x_, drawRect.min_.y_, 0.0f);
  232. vertex1.position_ = worldTransform * Vector3(drawRect.min_.x_, drawRect.max_.y_, 0.0f);
  233. vertex2.position_ = worldTransform * Vector3(drawRect.max_.x_, drawRect.max_.y_, 0.0f);
  234. vertex3.position_ = worldTransform * Vector3(drawRect.max_.x_, drawRect.min_.y_, 0.0f);
  235. vertex0.uv_ = textureRect.min_;
  236. vertex1.uv_ = Vector2(textureRect.min_.x_, textureRect.max_.y_);
  237. vertex2.uv_ = textureRect.max_;
  238. vertex3.uv_ = Vector2(textureRect.max_.x_, textureRect.min_.y_);
  239. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  240. vertices.Push(vertex0);
  241. vertices.Push(vertex1);
  242. vertices.Push(vertex2);
  243. vertices.Push(vertex3);
  244. sourceBatchesDirty_ = false;
  245. }
  246. void StaticSprite2D::OnFlipChanged()
  247. {
  248. }
  249. void StaticSprite2D::UpdateMaterial()
  250. {
  251. if (customMaterial_)
  252. sourceBatches_[0].material_ = customMaterial_;
  253. else
  254. {
  255. if (sprite_)
  256. sourceBatches_[0].material_ = renderer_->GetMaterial(sprite_->GetTexture(), blendMode_);
  257. else
  258. sourceBatches_[0].material_ = 0;
  259. }
  260. }
  261. }