StaticSprite2D.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. ACCESSOR_ATTRIBUTE("Flip X", GetFlipX, SetFlipX, bool, false, AM_DEFAULT);
  49. ACCESSOR_ATTRIBUTE("Flip Y", GetFlipY, SetFlipY, bool, false, AM_DEFAULT);
  50. REF_ACCESSOR_ATTRIBUTE("Color", GetColor, SetColor, Color, Color::WHITE, AM_DEFAULT);
  51. COPY_BASE_ATTRIBUTES(Drawable2D);
  52. }
  53. void StaticSprite2D::SetFlip(bool flipX, bool flipY)
  54. {
  55. if (flipX == flipX_ && flipY == flipY_)
  56. return;
  57. flipX_ = flipX;
  58. flipY_ = flipY;
  59. verticesDirty_ = true;
  60. MarkNetworkUpdate();
  61. }
  62. void StaticSprite2D::SetFlipX(bool flipX)
  63. {
  64. SetFlip(flipX, flipY_);
  65. }
  66. void StaticSprite2D::SetFlipY(bool flipY)
  67. {
  68. SetFlip(flipX_, flipY);
  69. }
  70. void StaticSprite2D::SetColor(const Color& color)
  71. {
  72. if (color == color_)
  73. return;
  74. color_ = color;
  75. verticesDirty_ = true;
  76. MarkNetworkUpdate();
  77. }
  78. void StaticSprite2D::SetUseHotSpot(bool useHotSpot)
  79. {
  80. if (useHotSpot == useHotSpot_)
  81. return;
  82. useHotSpot_ = useHotSpot;
  83. verticesDirty_ = true;
  84. MarkNetworkUpdate();
  85. }
  86. void StaticSprite2D::SetHotSpot(const Vector2& hotspot)
  87. {
  88. if (hotspot == hotSpot_)
  89. return;
  90. hotSpot_ = hotspot;
  91. if (useHotSpot_)
  92. {
  93. verticesDirty_ = true;
  94. MarkNetworkUpdate();
  95. }
  96. }
  97. void StaticSprite2D::OnWorldBoundingBoxUpdate()
  98. {
  99. boundingBox_.Clear();
  100. if (sprite_)
  101. {
  102. const IntRect& rectangle_ = sprite_->GetRectangle();
  103. float width = (float)rectangle_.Width() * PIXEL_SIZE; // Compute width and height in pixels
  104. float height = (float)rectangle_.Height() * PIXEL_SIZE;
  105. const Vector2& hotSpot = sprite_->GetHotSpot();
  106. float hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;
  107. float hotSpotY = flipY_ ? (1.0f - hotSpot.y_) : hotSpot.y_;
  108. float leftX = -width * hotSpotX;
  109. float rightX = width * (1.0f - hotSpotX);
  110. float bottomY = -height * hotSpotY;
  111. float topY = height * (1.0f - hotSpotY);
  112. boundingBox_.Merge(Vector3(leftX, bottomY, 0.0f));
  113. boundingBox_.Merge(Vector3(rightX, topY, 0.0f));
  114. }
  115. worldBoundingBox_ = boundingBox_.Transformed(node_->GetWorldTransform());
  116. }
  117. void StaticSprite2D::UpdateVertices()
  118. {
  119. if (!verticesDirty_)
  120. return;
  121. vertices_.Clear();
  122. Texture2D* texture = GetTexture();
  123. if (!texture)
  124. return;
  125. const IntRect& rectangle_ = sprite_->GetRectangle();
  126. if (rectangle_.Width() == 0 || rectangle_.Height() == 0)
  127. return;
  128. /*
  129. V1---------V2
  130. | / |
  131. | / |
  132. | / |
  133. | / |
  134. | / |
  135. V0---------V3
  136. */
  137. Vertex2D vertex0;
  138. Vertex2D vertex1;
  139. Vertex2D vertex2;
  140. Vertex2D vertex3;
  141. float width = (float)rectangle_.Width() * PIXEL_SIZE; // Compute width and height in pixels
  142. float height = (float)rectangle_.Height() * PIXEL_SIZE;
  143. float hotSpotX;
  144. float hotSpotY;
  145. if (useHotSpot_)
  146. {
  147. hotSpotX = flipX_ ? (1.0f - hotSpot_.x_) : hotSpot_.x_;
  148. hotSpotY = flipY_ ? (1.0f - hotSpot_.y_) : hotSpot_.y_;
  149. }
  150. else
  151. {
  152. const Vector2& hotSpot = sprite_->GetHotSpot();
  153. hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;
  154. hotSpotY = flipY_ ? (1.0f - hotSpot.y_) : hotSpot.y_;
  155. }
  156. #ifdef URHO3D_OPENGL
  157. float leftX = -width * hotSpotX;
  158. float rightX = width * (1.0f - hotSpotX);
  159. float bottomY = -height * hotSpotY;
  160. float topY = height * (1.0f - hotSpotY);
  161. #else
  162. const float halfPixelOffset = 0.5f * PIXEL_SIZE;
  163. float leftX = -width * hotSpotX + halfPixelOffset;
  164. float rightX = width * (1.0f - hotSpotX) + halfPixelOffset;
  165. float bottomY = -height * hotSpotY + halfPixelOffset;
  166. float topY = height * (1.0f - hotSpotY) + halfPixelOffset;
  167. #endif
  168. const Matrix3x4& worldTransform = node_->GetWorldTransform();
  169. vertex0.position_ = worldTransform * Vector3(leftX, bottomY, 0.0f);
  170. vertex1.position_ = worldTransform * Vector3(leftX, topY, 0.0f);
  171. vertex2.position_ = worldTransform * Vector3(rightX, topY, 0.0f);
  172. vertex3.position_ = worldTransform * Vector3(rightX, bottomY, 0.0f);
  173. float invTexW = 1.0f / (float)texture->GetWidth();
  174. float invTexH = 1.0f / (float)texture->GetHeight();
  175. float leftU = rectangle_.left_ * invTexW;
  176. float rightU = rectangle_.right_ * invTexW;
  177. float topV = rectangle_.top_ * invTexH;
  178. float bottomV = rectangle_.bottom_ * invTexH;
  179. vertex0.uv_ = Vector2(leftU, bottomV);
  180. vertex1.uv_ = Vector2(leftU, topV);
  181. vertex2.uv_ = Vector2(rightU, topV);
  182. vertex3.uv_ = Vector2(rightU, bottomV);
  183. if (flipX_)
  184. {
  185. Swap(vertex0.uv_.x_, vertex3.uv_.x_);
  186. Swap(vertex1.uv_.x_, vertex2.uv_.x_);
  187. }
  188. if (flipY_)
  189. {
  190. Swap(vertex0.uv_.y_, vertex1.uv_.y_);
  191. Swap(vertex2.uv_.y_, vertex3.uv_.y_);
  192. }
  193. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  194. vertices_.Push(vertex0);
  195. vertices_.Push(vertex1);
  196. vertices_.Push(vertex2);
  197. vertices_.Push(vertex3);
  198. verticesDirty_ = false;
  199. }
  200. }