StaticSprite2D.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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()), AM_DEFAULT);
  56. ENUM_ACCESSOR_ATTRIBUTE("Blend Mode", GetBlendMode, SetBlendMode, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
  57. ACCESSOR_ATTRIBUTE("Flip X", GetFlipX, SetFlipX, bool, false, AM_DEFAULT);
  58. ACCESSOR_ATTRIBUTE("Flip Y", GetFlipY, SetFlipY, bool, false, AM_DEFAULT);
  59. ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_DEFAULT);
  60. MIXED_ACCESSOR_ATTRIBUTE("Custom material", GetCustomMaterialAttr, SetCustomMaterialAttr, ResourceRef, ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
  61. }
  62. void StaticSprite2D::SetSprite(Sprite2D* sprite)
  63. {
  64. if (sprite == sprite_)
  65. return;
  66. sprite_ = sprite;
  67. UpdateMaterial();
  68. sourceBatchesDirty_ = true;
  69. MarkNetworkUpdate();
  70. }
  71. void StaticSprite2D::SetBlendMode(BlendMode blendMode)
  72. {
  73. if (blendMode == blendMode_)
  74. return;
  75. blendMode_ = blendMode;
  76. UpdateMaterial();
  77. MarkNetworkUpdate();
  78. }
  79. void StaticSprite2D::SetFlip(bool flipX, bool flipY)
  80. {
  81. if (flipX == flipX_ && flipY == flipY_)
  82. return;
  83. flipX_ = flipX;
  84. flipY_ = flipY;
  85. sourceBatchesDirty_ = true;
  86. OnFlipChanged();
  87. MarkNetworkUpdate();
  88. }
  89. void StaticSprite2D::SetFlipX(bool flipX)
  90. {
  91. SetFlip(flipX, flipY_);
  92. }
  93. void StaticSprite2D::SetFlipY(bool flipY)
  94. {
  95. SetFlip(flipX_, flipY);
  96. }
  97. void StaticSprite2D::SetColor(const Color& color)
  98. {
  99. if (color == color_)
  100. return;
  101. color_ = color;
  102. sourceBatchesDirty_ = true;
  103. MarkNetworkUpdate();
  104. }
  105. void StaticSprite2D::SetAlpha(float alpha)
  106. {
  107. if (alpha == color_.a_)
  108. return;
  109. color_.a_ = alpha;
  110. sourceBatchesDirty_ = true;
  111. MarkNetworkUpdate();
  112. }
  113. void StaticSprite2D::SetUseHotSpot(bool useHotSpot)
  114. {
  115. if (useHotSpot == useHotSpot_)
  116. return;
  117. useHotSpot_ = useHotSpot;
  118. sourceBatchesDirty_ = true;
  119. MarkNetworkUpdate();
  120. }
  121. void StaticSprite2D::SetHotSpot(const Vector2& hotspot)
  122. {
  123. if (hotspot == hotSpot_)
  124. return;
  125. hotSpot_ = hotspot;
  126. if (useHotSpot_)
  127. {
  128. sourceBatchesDirty_ = true;
  129. MarkNetworkUpdate();
  130. }
  131. }
  132. void StaticSprite2D::SetCustomMaterial(Material* customMaterial)
  133. {
  134. if (customMaterial == customMaterial_)
  135. return;
  136. customMaterial_ = customMaterial;
  137. sourceBatchesDirty_ = true;
  138. UpdateMaterial();
  139. MarkNetworkUpdate();
  140. }
  141. Sprite2D* StaticSprite2D::GetSprite() const
  142. {
  143. return sprite_;
  144. }
  145. Material* StaticSprite2D::GetCustomMaterial() const
  146. {
  147. return customMaterial_;
  148. }
  149. void StaticSprite2D::SetSpriteAttr(const ResourceRef& value)
  150. {
  151. Sprite2D* sprite = Sprite2D::LoadFromResourceRef(this, value);
  152. if (sprite)
  153. SetSprite(sprite);
  154. }
  155. ResourceRef StaticSprite2D::GetSpriteAttr() const
  156. {
  157. return Sprite2D::SaveToResourceRef(sprite_);
  158. }
  159. void StaticSprite2D::SetCustomMaterialAttr(const ResourceRef& value)
  160. {
  161. ResourceCache* cache = GetSubsystem<ResourceCache>();
  162. SetCustomMaterial(cache->GetResource<Material>(value.name_));
  163. }
  164. ResourceRef StaticSprite2D::GetCustomMaterialAttr() const
  165. {
  166. return GetResourceRef(customMaterial_, Material::GetTypeStatic());
  167. }
  168. void StaticSprite2D::OnWorldBoundingBoxUpdate()
  169. {
  170. boundingBox_.Clear();
  171. if (sprite_)
  172. {
  173. const IntRect& rectangle_ = sprite_->GetRectangle();
  174. float width = (float)rectangle_.Width() * PIXEL_SIZE; // Compute width and height in pixels
  175. float height = (float)rectangle_.Height() * PIXEL_SIZE;
  176. const Vector2& hotSpot = sprite_->GetHotSpot();
  177. float hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;
  178. float hotSpotY = flipY_ ? (1.0f - hotSpot.y_) : hotSpot.y_;
  179. float leftX = -width * hotSpotX;
  180. float rightX = width * (1.0f - hotSpotX);
  181. float bottomY = -height * hotSpotY;
  182. float topY = height * (1.0f - hotSpotY);
  183. boundingBox_.Merge(Vector3(leftX, bottomY, 0.0f));
  184. boundingBox_.Merge(Vector3(rightX, topY, 0.0f));
  185. }
  186. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  187. }
  188. void StaticSprite2D::OnDrawOrderChanged()
  189. {
  190. sourceBatches_[0].drawOrder_ = GetDrawOrder();
  191. }
  192. void StaticSprite2D::UpdateSourceBatches()
  193. {
  194. if (!sourceBatchesDirty_)
  195. return;
  196. Vector<Vertex2D>& vertices = sourceBatches_[0].vertices_;
  197. vertices.Clear();
  198. if (!sprite_)
  199. return;
  200. Rect drawRect;
  201. if (useHotSpot_)
  202. {
  203. if (!sprite_->GetDrawRectangle(drawRect, hotSpot_, flipX_, flipY_))
  204. return;
  205. }
  206. else
  207. {
  208. if (!sprite_->GetDrawRectangle(drawRect, flipX_, flipY_))
  209. return;
  210. }
  211. Rect textureRect;
  212. if (!sprite_->GetTextureRectangle(textureRect, flipX_, flipY_))
  213. return;
  214. /*
  215. V1---------V2
  216. | / |
  217. | / |
  218. | / |
  219. | / |
  220. | / |
  221. V0---------V3
  222. */
  223. Vertex2D vertex0;
  224. Vertex2D vertex1;
  225. Vertex2D vertex2;
  226. Vertex2D vertex3;
  227. // Convert to world space
  228. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  229. vertex0.position_ = worldTransform * Vector3(drawRect.min_.x_, drawRect.min_.y_, 0.0f);
  230. vertex1.position_ = worldTransform * Vector3(drawRect.min_.x_, drawRect.max_.y_, 0.0f);
  231. vertex2.position_ = worldTransform * Vector3(drawRect.max_.x_, drawRect.max_.y_, 0.0f);
  232. vertex3.position_ = worldTransform * Vector3(drawRect.max_.x_, drawRect.min_.y_, 0.0f);
  233. vertex0.uv_ = textureRect.min_;
  234. vertex1.uv_ = Vector2(textureRect.min_.x_, textureRect.max_.y_);
  235. vertex2.uv_ = textureRect.max_;
  236. vertex3.uv_ = Vector2(textureRect.max_.x_, textureRect.min_.y_);
  237. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  238. vertices.Push(vertex0);
  239. vertices.Push(vertex1);
  240. vertices.Push(vertex2);
  241. vertices.Push(vertex3);
  242. sourceBatchesDirty_ = false;
  243. }
  244. void StaticSprite2D::OnFlipChanged()
  245. {
  246. }
  247. void StaticSprite2D::UpdateMaterial()
  248. {
  249. if (customMaterial_)
  250. sourceBatches_[0].material_ = customMaterial_;
  251. else
  252. {
  253. if (sprite_)
  254. sourceBatches_[0].material_ = renderer_->GetMaterial(sprite_->GetTexture(), blendMode_);
  255. else
  256. sourceBatches_[0].material_ = 0;
  257. }
  258. }
  259. }