StaticSprite2D.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "../Scene/Scene.h"
  25. #include "../Atomic2D/Sprite2D.h"
  26. #include "../Atomic2D/StaticSprite2D.h"
  27. #include "../Graphics/Texture2D.h"
  28. #include "../DebugNew.h"
  29. namespace Atomic
  30. {
  31. extern const char* ATOMIC2D_CATEGORY;
  32. StaticSprite2D::StaticSprite2D(Context* context) :
  33. Drawable2D(context),
  34. flipX_(false),
  35. flipY_(false),
  36. color_(Color::WHITE),
  37. useHotSpot_(false),
  38. hotSpot_(0.5f, 0.5f)
  39. {
  40. vertices_.Reserve(6);
  41. }
  42. StaticSprite2D::~StaticSprite2D()
  43. {
  44. }
  45. void StaticSprite2D::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<StaticSprite2D>(ATOMIC2D_CATEGORY);
  48. ACCESSOR_ATTRIBUTE("Is Enabled", IsEnabled, SetEnabled, bool, true, AM_DEFAULT);
  49. MIXED_ACCESSOR_ATTRIBUTE("Sprite", GetSpriteAttr, SetSpriteAttr, ResourceRef, ResourceRef(Sprite2D::GetTypeStatic()), AM_DEFAULT);
  50. ACCESSOR_ATTRIBUTE("Flip X", GetFlipX, SetFlipX, bool, false, AM_DEFAULT);
  51. ACCESSOR_ATTRIBUTE("Flip Y", GetFlipY, SetFlipY, bool, false, AM_DEFAULT);
  52. ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_DEFAULT);
  53. COPY_BASE_ATTRIBUTES(Drawable2D);
  54. }
  55. void StaticSprite2D::SetSprite(Sprite2D* sprite)
  56. {
  57. if (sprite == sprite_)
  58. return;
  59. sprite_ = sprite;
  60. if (sprite)
  61. verticesDirty_ = true;
  62. SetTexture(sprite_ ? sprite_->GetTexture() : 0);
  63. }
  64. void StaticSprite2D::SetFlip(bool flipX, bool flipY)
  65. {
  66. if (flipX == flipX_ && flipY == flipY_)
  67. return;
  68. flipX_ = flipX;
  69. flipY_ = flipY;
  70. verticesDirty_ = true;
  71. OnFlipChanged();
  72. MarkNetworkUpdate();
  73. }
  74. void StaticSprite2D::SetFlipX(bool flipX)
  75. {
  76. SetFlip(flipX, flipY_);
  77. }
  78. void StaticSprite2D::SetFlipY(bool flipY)
  79. {
  80. SetFlip(flipX_, flipY);
  81. }
  82. void StaticSprite2D::SetColor(const Color& color)
  83. {
  84. if (color == color_)
  85. return;
  86. color_ = color;
  87. verticesDirty_ = true;
  88. MarkNetworkUpdate();
  89. }
  90. void StaticSprite2D::SetAlpha(float alpha)
  91. {
  92. if (alpha == color_.a_)
  93. return;
  94. color_.a_ = alpha;
  95. verticesDirty_ = true;
  96. MarkNetworkUpdate();
  97. }
  98. void StaticSprite2D::SetUseHotSpot(bool useHotSpot)
  99. {
  100. if (useHotSpot == useHotSpot_)
  101. return;
  102. useHotSpot_ = useHotSpot;
  103. verticesDirty_ = true;
  104. MarkNetworkUpdate();
  105. }
  106. void StaticSprite2D::SetHotSpot(const Vector2& hotspot)
  107. {
  108. if (hotspot == hotSpot_)
  109. return;
  110. hotSpot_ = hotspot;
  111. if (useHotSpot_)
  112. {
  113. verticesDirty_ = true;
  114. MarkNetworkUpdate();
  115. }
  116. }
  117. Sprite2D* StaticSprite2D::GetSprite() const
  118. {
  119. return sprite_;
  120. }
  121. void StaticSprite2D::SetSpriteAttr(const ResourceRef& value)
  122. {
  123. Sprite2D* sprite = Sprite2D::LoadFromResourceRef(this, value);
  124. if (sprite)
  125. SetSprite(sprite);
  126. }
  127. ResourceRef StaticSprite2D::GetSpriteAttr() const
  128. {
  129. return Sprite2D::SaveToResourceRef(sprite_);
  130. }
  131. void StaticSprite2D::OnWorldBoundingBoxUpdate()
  132. {
  133. boundingBox_.Clear();
  134. if (sprite_)
  135. {
  136. const IntRect& rectangle_ = sprite_->GetRectangle();
  137. float width = (float)rectangle_.Width() * PIXEL_SIZE; // Compute width and height in pixels
  138. float height = (float)rectangle_.Height() * PIXEL_SIZE;
  139. const Vector2& hotSpot = sprite_->GetHotSpot();
  140. float hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;
  141. float hotSpotY = flipY_ ? (1.0f - hotSpot.y_) : hotSpot.y_;
  142. float leftX = -width * hotSpotX;
  143. float rightX = width * (1.0f - hotSpotX);
  144. float bottomY = -height * hotSpotY;
  145. float topY = height * (1.0f - hotSpotY);
  146. boundingBox_.Merge(Vector3(leftX, bottomY, 0.0f));
  147. boundingBox_.Merge(Vector3(rightX, topY, 0.0f));
  148. }
  149. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  150. }
  151. void StaticSprite2D::UpdateVertices()
  152. {
  153. if (!verticesDirty_)
  154. return;
  155. vertices_.Clear();
  156. Texture2D* texture = GetTexture();
  157. if (!texture)
  158. return;
  159. const IntRect& rectangle_ = sprite_->GetRectangle();
  160. if (rectangle_.Width() == 0 || rectangle_.Height() == 0)
  161. return;
  162. /*
  163. V1---------V2
  164. | / |
  165. | / |
  166. | / |
  167. | / |
  168. | / |
  169. V0---------V3
  170. */
  171. Vertex2D vertex0;
  172. Vertex2D vertex1;
  173. Vertex2D vertex2;
  174. Vertex2D vertex3;
  175. float width = (float)rectangle_.Width() * PIXEL_SIZE; // Compute width and height in pixels
  176. float height = (float)rectangle_.Height() * PIXEL_SIZE;
  177. float hotSpotX;
  178. float hotSpotY;
  179. if (useHotSpot_)
  180. {
  181. hotSpotX = flipX_ ? (1.0f - hotSpot_.x_) : hotSpot_.x_;
  182. hotSpotY = flipY_ ? (1.0f - hotSpot_.y_) : hotSpot_.y_;
  183. }
  184. else
  185. {
  186. const Vector2& hotSpot = sprite_->GetHotSpot();
  187. hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;
  188. hotSpotY = flipY_ ? (1.0f - hotSpot.y_) : hotSpot.y_;
  189. }
  190. #ifdef ATOMIC_OPENGL
  191. float leftX = -width * hotSpotX;
  192. float rightX = width * (1.0f - hotSpotX);
  193. float bottomY = -height * hotSpotY;
  194. float topY = height * (1.0f - hotSpotY);
  195. #else
  196. const float halfPixelOffset = 0.5f * PIXEL_SIZE;
  197. float leftX = -width * hotSpotX + halfPixelOffset;
  198. float rightX = width * (1.0f - hotSpotX) + halfPixelOffset;
  199. float bottomY = -height * hotSpotY + halfPixelOffset;
  200. float topY = height * (1.0f - hotSpotY) + halfPixelOffset;
  201. #endif
  202. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  203. vertex0.position_ = worldTransform * Vector3(leftX, bottomY, 0.0f);
  204. vertex1.position_ = worldTransform * Vector3(leftX, topY, 0.0f);
  205. vertex2.position_ = worldTransform * Vector3(rightX, topY, 0.0f);
  206. vertex3.position_ = worldTransform * Vector3(rightX, bottomY, 0.0f);
  207. float invTexW = 1.0f / (float)texture->GetWidth();
  208. float invTexH = 1.0f / (float)texture->GetHeight();
  209. float leftU = rectangle_.left_ * invTexW;
  210. float rightU = rectangle_.right_ * invTexW;
  211. float topV = rectangle_.top_ * invTexH;
  212. float bottomV = rectangle_.bottom_ * invTexH;
  213. vertex0.uv_ = Vector2(leftU, bottomV);
  214. vertex1.uv_ = Vector2(leftU, topV);
  215. vertex2.uv_ = Vector2(rightU, topV);
  216. vertex3.uv_ = Vector2(rightU, bottomV);
  217. if (flipX_)
  218. {
  219. Swap(vertex0.uv_.x_, vertex3.uv_.x_);
  220. Swap(vertex1.uv_.x_, vertex2.uv_.x_);
  221. }
  222. if (flipY_)
  223. {
  224. Swap(vertex0.uv_.y_, vertex1.uv_.y_);
  225. Swap(vertex2.uv_.y_, vertex3.uv_.y_);
  226. }
  227. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  228. vertices_.Push(vertex0);
  229. vertices_.Push(vertex1);
  230. vertices_.Push(vertex2);
  231. vertices_.Push(vertex3);
  232. verticesDirty_ = false;
  233. }
  234. void StaticSprite2D::OnFlipChanged()
  235. {
  236. }
  237. }