StaticSprite2D.cpp 7.6 KB

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