StaticSprite2D.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. //
  2. // Copyright (c) 2008-2017 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. useDrawRect_(false),
  44. useTextureRect_(false),
  45. hotSpot_(0.5f, 0.5f),
  46. drawRect_(Rect::ZERO),
  47. textureRect_(Rect::ZERO)
  48. {
  49. sourceBatches_.Resize(1);
  50. sourceBatches_[0].owner_ = this;
  51. }
  52. StaticSprite2D::~StaticSprite2D()
  53. {
  54. }
  55. void StaticSprite2D::RegisterObject(Context* context)
  56. {
  57. context->RegisterFactory<StaticSprite2D>(ATOMIC2D_CATEGORY);
  58. ATOMIC_ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  59. ATOMIC_COPY_BASE_ATTRIBUTES(Drawable2D);
  60. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Sprite", GetSpriteAttr, SetSpriteAttr, ResourceRef, ResourceRef(Sprite2D::GetTypeStatic()),
  61. AM_DEFAULT);
  62. ATOMIC_ENUM_ACCESSOR_ATTRIBUTE("Blend Mode", GetBlendMode, SetBlendMode, BlendMode, blendModeNames, BLEND_ALPHA, AM_DEFAULT);
  63. ATOMIC_ACCESSOR_ATTRIBUTE("Flip X", GetFlipX, SetFlipX, bool, false, AM_DEFAULT);
  64. ATOMIC_ACCESSOR_ATTRIBUTE("Flip Y", GetFlipY, SetFlipY, bool, false, AM_DEFAULT);
  65. ATOMIC_ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_DEFAULT);
  66. ATOMIC_MIXED_ACCESSOR_ATTRIBUTE("Custom material", GetCustomMaterialAttr, SetCustomMaterialAttr, ResourceRef,
  67. ResourceRef(Material::GetTypeStatic()), AM_DEFAULT);
  68. ATOMIC_ACCESSOR_ATTRIBUTE("Hot Spot", GetHotSpot, SetHotSpot, Vector2, Vector2(0.5f, 0.5f), AM_DEFAULT);
  69. ATOMIC_ACCESSOR_ATTRIBUTE("Use Hot Spot", GetUseHotSpot, SetUseHotSpot, bool, false, AM_DEFAULT);
  70. ATOMIC_ACCESSOR_ATTRIBUTE("Draw Rectangle", GetDrawRect, SetDrawRect, Rect, Rect::ZERO, AM_DEFAULT);
  71. ATOMIC_ACCESSOR_ATTRIBUTE("Use Draw Rectangle", GetUseDrawRect, SetUseDrawRect, bool, false, AM_DEFAULT);
  72. ATOMIC_ACCESSOR_ATTRIBUTE("Texture Rectangle", GetTextureRect, SetTextureRect, Rect, Rect::ZERO, AM_DEFAULT);
  73. ATOMIC_ACCESSOR_ATTRIBUTE("Use Texture Rectangle", GetUseTextureRect, SetUseTextureRect, bool, false, AM_DEFAULT);
  74. }
  75. void StaticSprite2D::SetSprite(Sprite2D* sprite)
  76. {
  77. if (sprite == sprite_)
  78. return;
  79. sprite_ = sprite;
  80. UpdateMaterial();
  81. sourceBatchesDirty_ = true;
  82. MarkNetworkUpdate();
  83. UpdateDrawRect();
  84. }
  85. void StaticSprite2D::SetDrawRect(const Rect& rect)
  86. {
  87. drawRect_ = rect;
  88. if(useDrawRect_)
  89. {
  90. sourceBatchesDirty_ = true;
  91. }
  92. }
  93. void StaticSprite2D::SetTextureRect(const Rect& rect)
  94. {
  95. textureRect_ = rect;
  96. if(useTextureRect_)
  97. {
  98. sourceBatchesDirty_ = true;
  99. }
  100. }
  101. void StaticSprite2D::SetBlendMode(BlendMode blendMode)
  102. {
  103. if (blendMode == blendMode_)
  104. return;
  105. blendMode_ = blendMode;
  106. UpdateMaterial();
  107. MarkNetworkUpdate();
  108. }
  109. void StaticSprite2D::SetFlip(bool flipX, bool flipY)
  110. {
  111. if (flipX == flipX_ && flipY == flipY_)
  112. return;
  113. flipX_ = flipX;
  114. flipY_ = flipY;
  115. sourceBatchesDirty_ = true;
  116. MarkNetworkUpdate();
  117. }
  118. void StaticSprite2D::SetFlipX(bool flipX)
  119. {
  120. SetFlip(flipX, flipY_);
  121. }
  122. void StaticSprite2D::SetFlipY(bool flipY)
  123. {
  124. SetFlip(flipX_, flipY);
  125. }
  126. void StaticSprite2D::SetColor(const Color& color)
  127. {
  128. if (color == color_)
  129. return;
  130. color_ = color;
  131. sourceBatchesDirty_ = true;
  132. MarkNetworkUpdate();
  133. }
  134. void StaticSprite2D::SetAlpha(float alpha)
  135. {
  136. if (alpha == color_.a_)
  137. return;
  138. color_.a_ = alpha;
  139. sourceBatchesDirty_ = true;
  140. MarkNetworkUpdate();
  141. }
  142. void StaticSprite2D::SetUseHotSpot(bool useHotSpot)
  143. {
  144. if (useHotSpot == useHotSpot_)
  145. return;
  146. useHotSpot_ = useHotSpot;
  147. sourceBatchesDirty_ = true;
  148. MarkNetworkUpdate();
  149. UpdateDrawRect();
  150. }
  151. void StaticSprite2D::SetUseDrawRect(bool useDrawRect)
  152. {
  153. if (useDrawRect == useDrawRect_)
  154. return;
  155. useDrawRect_ = useDrawRect;
  156. sourceBatchesDirty_ = true;
  157. MarkNetworkUpdate();
  158. UpdateDrawRect();
  159. }
  160. void StaticSprite2D::SetUseTextureRect(bool useTextureRect)
  161. {
  162. if (useTextureRect == useTextureRect_)
  163. return;
  164. useTextureRect_ = useTextureRect;
  165. sourceBatchesDirty_ = true;
  166. MarkNetworkUpdate();
  167. }
  168. void StaticSprite2D::SetHotSpot(const Vector2& hotspot)
  169. {
  170. if (hotspot == hotSpot_)
  171. return;
  172. hotSpot_ = hotspot;
  173. if (useHotSpot_)
  174. {
  175. sourceBatchesDirty_ = true;
  176. MarkNetworkUpdate();
  177. }
  178. UpdateDrawRect();
  179. }
  180. void StaticSprite2D::SetCustomMaterial(Material* customMaterial)
  181. {
  182. if (customMaterial == customMaterial_)
  183. return;
  184. customMaterial_ = customMaterial;
  185. sourceBatchesDirty_ = true;
  186. UpdateMaterial();
  187. MarkNetworkUpdate();
  188. }
  189. Sprite2D* StaticSprite2D::GetSprite() const
  190. {
  191. return sprite_;
  192. }
  193. Material* StaticSprite2D::GetCustomMaterial() const
  194. {
  195. return customMaterial_;
  196. }
  197. void StaticSprite2D::SetSpriteAttr(const ResourceRef& value)
  198. {
  199. Sprite2D* sprite = Sprite2D::LoadFromResourceRef(this, value);
  200. if (sprite)
  201. SetSprite(sprite);
  202. }
  203. ResourceRef StaticSprite2D::GetSpriteAttr() const
  204. {
  205. return Sprite2D::SaveToResourceRef(sprite_);
  206. }
  207. void StaticSprite2D::SetCustomMaterialAttr(const ResourceRef& value)
  208. {
  209. ResourceCache* cache = GetSubsystem<ResourceCache>();
  210. SetCustomMaterial(cache->GetResource<Material>(value.name_));
  211. }
  212. ResourceRef StaticSprite2D::GetCustomMaterialAttr() const
  213. {
  214. return GetResourceRef(customMaterial_, Material::GetTypeStatic());
  215. }
  216. void StaticSprite2D::OnSceneSet(Scene* scene)
  217. {
  218. Drawable2D::OnSceneSet(scene);
  219. UpdateMaterial();
  220. }
  221. void StaticSprite2D::OnWorldBoundingBoxUpdate()
  222. {
  223. boundingBox_.Clear();
  224. worldBoundingBox_.Clear();
  225. const Vector<SourceBatch2D>& sourceBatches = GetSourceBatches();
  226. for (unsigned i = 0; i < sourceBatches[0].vertices_.Size(); ++i)
  227. worldBoundingBox_.Merge(sourceBatches[0].vertices_[i].position_);
  228. boundingBox_ = worldBoundingBox_.Transformed(node_->GetWorldTransform().Inverse());
  229. }
  230. void StaticSprite2D::OnDrawOrderChanged()
  231. {
  232. sourceBatches_[0].drawOrder_ = GetDrawOrder();
  233. }
  234. void StaticSprite2D::UpdateSourceBatches()
  235. {
  236. if (!sourceBatchesDirty_)
  237. return;
  238. Vector<Vertex2D>& vertices = sourceBatches_[0].vertices_;
  239. vertices.Clear();
  240. if (!sprite_)
  241. return;
  242. if (!useTextureRect_)
  243. {
  244. if (!sprite_->GetTextureRectangle(textureRect_, flipX_, flipY_))
  245. return;
  246. }
  247. /*
  248. V1---------V2
  249. | / |
  250. | / |
  251. | / |
  252. | / |
  253. | / |
  254. V0---------V3
  255. */
  256. Vertex2D vertex0;
  257. Vertex2D vertex1;
  258. Vertex2D vertex2;
  259. Vertex2D vertex3;
  260. // Convert to world space
  261. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  262. vertex0.position_ = worldTransform * Vector3(drawRect_.min_.x_, drawRect_.min_.y_, 0.0f);
  263. vertex1.position_ = worldTransform * Vector3(drawRect_.min_.x_, drawRect_.max_.y_, 0.0f);
  264. vertex2.position_ = worldTransform * Vector3(drawRect_.max_.x_, drawRect_.max_.y_, 0.0f);
  265. vertex3.position_ = worldTransform * Vector3(drawRect_.max_.x_, drawRect_.min_.y_, 0.0f);
  266. vertex0.uv_ = textureRect_.min_;
  267. vertex1.uv_ = Vector2(textureRect_.min_.x_, textureRect_.max_.y_);
  268. vertex2.uv_ = textureRect_.max_;
  269. vertex3.uv_ = Vector2(textureRect_.max_.x_, textureRect_.min_.y_);
  270. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  271. vertices.Push(vertex0);
  272. vertices.Push(vertex1);
  273. vertices.Push(vertex2);
  274. vertices.Push(vertex3);
  275. sourceBatchesDirty_ = false;
  276. }
  277. void StaticSprite2D::UpdateMaterial()
  278. {
  279. if (customMaterial_)
  280. sourceBatches_[0].material_ = customMaterial_;
  281. else
  282. {
  283. if (sprite_ && renderer_)
  284. sourceBatches_[0].material_ = renderer_->GetMaterial(sprite_->GetTexture(), blendMode_);
  285. else
  286. sourceBatches_[0].material_ = 0;
  287. }
  288. }
  289. void StaticSprite2D::UpdateDrawRect()
  290. {
  291. if (!useDrawRect_)
  292. {
  293. if (useHotSpot_)
  294. {
  295. if (sprite_ && !sprite_->GetDrawRectangle(drawRect_, hotSpot_, flipX_, flipY_))
  296. return;
  297. }
  298. else
  299. {
  300. if (sprite_ && !sprite_->GetDrawRectangle(drawRect_, flipX_, flipY_))
  301. return;
  302. }
  303. }
  304. }
  305. }