StaticSprite2D.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 "Sprite2D.h"
  25. #include "StaticSprite2D.h"
  26. #include "Texture2D.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. extern const char* URHO2D_CATEGORY;
  31. StaticSprite2D::StaticSprite2D(Context* context) :
  32. Drawable2D(context),
  33. flipX_(false),
  34. flipY_(false),
  35. color_(Color::WHITE)
  36. {
  37. vertices_.Reserve(6);
  38. }
  39. StaticSprite2D::~StaticSprite2D()
  40. {
  41. }
  42. void StaticSprite2D::RegisterObject(Context* context)
  43. {
  44. context->RegisterFactory<StaticSprite2D>(URHO2D_CATEGORY);
  45. ACCESSOR_ATTRIBUTE(StaticSprite2D, VAR_BOOL, "Flip X", GetFlipX, SetFlipX, bool, false, AM_DEFAULT);
  46. ACCESSOR_ATTRIBUTE(StaticSprite2D, VAR_BOOL, "Flip Y", GetFlipY, SetFlipY, bool, false, AM_DEFAULT);
  47. REF_ACCESSOR_ATTRIBUTE(StaticSprite2D, VAR_COLOR, "Color", GetColor, SetColor, Color, Color::WHITE, AM_DEFAULT);
  48. COPY_BASE_ATTRIBUTES(StaticSprite2D, Drawable2D);
  49. }
  50. void StaticSprite2D::SetFlip(bool flipX, bool flipY)
  51. {
  52. if (flipX_ == flipX && flipY_ == flipY)
  53. return;
  54. flipX_ = flipX;
  55. flipY_ = flipY;
  56. OnMarkedDirty(node_);
  57. MarkNetworkUpdate();
  58. }
  59. void StaticSprite2D::SetFlipX(bool flipX)
  60. {
  61. SetFlip(flipX, flipY_);
  62. }
  63. void StaticSprite2D::SetFlipY(bool flipY)
  64. {
  65. SetFlip(flipX_, flipY);
  66. }
  67. void StaticSprite2D::SetColor(const Color& color)
  68. {
  69. if (color == color_)
  70. return;
  71. color_ = color;
  72. OnMarkedDirty(node_);
  73. MarkNetworkUpdate();
  74. }
  75. void StaticSprite2D::UpdateVertices()
  76. {
  77. if (!verticesDirty_)
  78. return;
  79. vertices_.Clear();
  80. if (!sprite_)
  81. return;
  82. Texture2D* texture = sprite_->GetTexture();
  83. if (!texture)
  84. return;
  85. const IntRect& rectangle_ = sprite_->GetRectangle();
  86. if (rectangle_.Width() == 0 || rectangle_.Height() == 0)
  87. return;
  88. /*
  89. V1 --------V2
  90. | / |
  91. | / |
  92. | / |
  93. | / |
  94. | / |
  95. V0 --------V3
  96. */
  97. Vertex2D vertex0;
  98. Vertex2D vertex1;
  99. Vertex2D vertex2;
  100. Vertex2D vertex3;
  101. float pixelPerUnit = 1.0f / unitPerPixel_;
  102. float width = (float)rectangle_.Width() * pixelPerUnit;
  103. float height = (float)rectangle_.Height() * pixelPerUnit;
  104. const Vector2& hotSpot = sprite_->GetHotSpot();
  105. float hotSpotX = flipX_ ? (1.0f - hotSpot.x_) : hotSpot.x_;
  106. float hotSpotY = flipY_ ? (1.0f - hotSpot.y_) : hotSpot.y_;
  107. float leftX = -width * hotSpotX;
  108. float rightX = width * (1.0f - hotSpotX);
  109. float bottomY = -height * hotSpotY;
  110. float topY = height * (1.0f - hotSpotY);
  111. vertex0.position_ = Vector3(leftX, bottomY, zValue_);
  112. vertex1.position_ = Vector3(leftX, topY, zValue_);
  113. vertex2.position_ = Vector3(rightX, topY, zValue_);
  114. vertex3.position_ = Vector3(rightX, bottomY, zValue_);
  115. float invTexW = 1.0f / (float)texture->GetWidth();
  116. float invTexH = 1.0f / (float)texture->GetHeight();
  117. float leftU = rectangle_.left_ * invTexW;
  118. float rightU = rectangle_.right_ * invTexW;
  119. float topV = rectangle_.top_ * invTexH;
  120. float bottomV = rectangle_.bottom_ * invTexH;
  121. vertex0.uv_ = Vector2(leftU, bottomV);
  122. vertex1.uv_ = Vector2(leftU, topV);
  123. vertex2.uv_ = Vector2(rightU, topV);
  124. vertex3.uv_ = Vector2(rightU, bottomV);
  125. if (flipX_)
  126. {
  127. Swap(vertex0.uv_.x_, vertex3.uv_.x_);
  128. Swap(vertex1.uv_.x_, vertex2.uv_.x_);
  129. }
  130. if (flipY_)
  131. {
  132. Swap(vertex0.uv_.y_, vertex1.uv_.y_);
  133. Swap(vertex2.uv_.y_, vertex3.uv_.y_);
  134. }
  135. vertex0.color_ = vertex1.color_ = vertex2.color_ = vertex3.color_ = color_.ToUInt();
  136. vertices_.Push(vertex0);
  137. vertices_.Push(vertex1);
  138. vertices_.Push(vertex2);
  139. vertices_.Push(vertex3);
  140. geometryDirty_ = true;
  141. verticesDirty_ = false;
  142. }
  143. }