StaticSprite2D.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. OnFlipChanged();
  71. MarkNetworkUpdate();
  72. }
  73. void StaticSprite2D::SetFlipX(bool flipX)
  74. {
  75. SetFlip(flipX, flipY_);
  76. }
  77. void StaticSprite2D::SetFlipY(bool flipY)
  78. {
  79. SetFlip(flipX_, flipY);
  80. }
  81. void StaticSprite2D::SetColor(const Color& color)
  82. {
  83. if (color == color_)
  84. return;
  85. color_ = color;
  86. verticesDirty_ = true;
  87. MarkNetworkUpdate();
  88. }
  89. void StaticSprite2D::SetUseHotSpot(bool useHotSpot)
  90. {
  91. if (useHotSpot == useHotSpot_)
  92. return;
  93. useHotSpot_ = useHotSpot;
  94. verticesDirty_ = true;
  95. MarkNetworkUpdate();
  96. }
  97. void StaticSprite2D::SetHotSpot(const Vector2& hotspot)
  98. {
  99. if (hotspot == hotSpot_)
  100. return;
  101. hotSpot_ = hotspot;
  102. if (useHotSpot_)
  103. {
  104. verticesDirty_ = true;
  105. MarkNetworkUpdate();
  106. }
  107. }
  108. Sprite2D* StaticSprite2D::GetSprite() const
  109. {
  110. return sprite_;
  111. }
  112. void StaticSprite2D::SetSpriteAttr(const ResourceRef& value)
  113. {
  114. Sprite2D* sprite = Sprite2D::LoadFromResourceRef(this, value);
  115. if (sprite)
  116. SetSprite(sprite);
  117. }
  118. ResourceRef StaticSprite2D::GetSpriteAttr() const
  119. {
  120. return Sprite2D::SaveToResourceRef(sprite_);
  121. }
  122. void StaticSprite2D::OnWorldBoundingBoxUpdate()
  123. {
  124. boundingBox_.Clear();
  125. if (sprite_)
  126. {
  127. const IntRect& rectangle_ = sprite_->GetRectangle();
  128. float width = (float)rectangle_.Width() * PIXEL_SIZE; // Compute width and height in pixels
  129. float height = (float)rectangle_.Height() * PIXEL_SIZE;
  130. const Vector2& hotSpot = sprite_->GetHotSpot();
  131. float hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;
  132. float hotSpotY = flipY_ ? (1.0f - hotSpot.y_) : hotSpot.y_;
  133. float leftX = -width * hotSpotX;
  134. float rightX = width * (1.0f - hotSpotX);
  135. float bottomY = -height * hotSpotY;
  136. float topY = height * (1.0f - hotSpotY);
  137. boundingBox_.Merge(Vector3(leftX, bottomY, 0.0f));
  138. boundingBox_.Merge(Vector3(rightX, topY, 0.0f));
  139. }
  140. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  141. }
  142. void StaticSprite2D::UpdateVertices()
  143. {
  144. if (!verticesDirty_)
  145. return;
  146. vertices_.Clear();
  147. Texture2D* texture = GetTexture();
  148. if (!texture)
  149. return;
  150. const IntRect& rectangle_ = sprite_->GetRectangle();
  151. if (rectangle_.Width() == 0 || rectangle_.Height() == 0)
  152. return;
  153. /*
  154. V1---------V2
  155. | / |
  156. | / |
  157. | / |
  158. | / |
  159. | / |
  160. V0---------V3
  161. */
  162. Vertex2D vertex0;
  163. Vertex2D vertex1;
  164. Vertex2D vertex2;
  165. Vertex2D vertex3;
  166. float width = (float)rectangle_.Width() * PIXEL_SIZE; // Compute width and height in pixels
  167. float height = (float)rectangle_.Height() * PIXEL_SIZE;
  168. float hotSpotX;
  169. float hotSpotY;
  170. if (useHotSpot_)
  171. {
  172. hotSpotX = flipX_ ? (1.0f - hotSpot_.x_) : hotSpot_.x_;
  173. hotSpotY = flipY_ ? (1.0f - hotSpot_.y_) : hotSpot_.y_;
  174. }
  175. else
  176. {
  177. const Vector2& hotSpot = sprite_->GetHotSpot();
  178. hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;
  179. hotSpotY = flipY_ ? (1.0f - hotSpot.y_) : hotSpot.y_;
  180. }
  181. #ifdef URHO3D_OPENGL
  182. float leftX = -width * hotSpotX;
  183. float rightX = width * (1.0f - hotSpotX);
  184. float bottomY = -height * hotSpotY;
  185. float topY = height * (1.0f - hotSpotY);
  186. #else
  187. const float halfPixelOffset = 0.5f * PIXEL_SIZE;
  188. float leftX = -width * hotSpotX + halfPixelOffset;
  189. float rightX = width * (1.0f - hotSpotX) + halfPixelOffset;
  190. float bottomY = -height * hotSpotY + halfPixelOffset;
  191. float topY = height * (1.0f - hotSpotY) + halfPixelOffset;
  192. #endif
  193. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  194. vertex0.position_ = worldTransform * Vector3(leftX, bottomY, 0.0f);
  195. vertex1.position_ = worldTransform * Vector3(leftX, topY, 0.0f);
  196. vertex2.position_ = worldTransform * Vector3(rightX, topY, 0.0f);
  197. vertex3.position_ = worldTransform * Vector3(rightX, bottomY, 0.0f);
  198. float invTexW = 1.0f / (float)texture->GetWidth();
  199. float invTexH = 1.0f / (float)texture->GetHeight();
  200. float leftU = rectangle_.left_ * invTexW;
  201. float rightU = rectangle_.right_ * invTexW;
  202. float topV = rectangle_.top_ * invTexH;
  203. float bottomV = rectangle_.bottom_ * invTexH;
  204. vertex0.uv_ = Vector2(leftU, bottomV);
  205. vertex1.uv_ = Vector2(leftU, topV);
  206. vertex2.uv_ = Vector2(rightU, topV);
  207. vertex3.uv_ = Vector2(rightU, bottomV);
  208. if (flipX_)
  209. {
  210. Swap(vertex0.uv_.x_, vertex3.uv_.x_);
  211. Swap(vertex1.uv_.x_, vertex2.uv_.x_);
  212. }
  213. if (flipY_)
  214. {
  215. Swap(vertex0.uv_.y_, vertex1.uv_.y_);
  216. Swap(vertex2.uv_.y_, vertex3.uv_.y_);
  217. }
  218. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  219. vertices_.Push(vertex0);
  220. vertices_.Push(vertex1);
  221. vertices_.Push(vertex2);
  222. vertices_.Push(vertex3);
  223. verticesDirty_ = false;
  224. }
  225. void StaticSprite2D::OnFlipChanged()
  226. {
  227. }
  228. }