Sprite.cpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. //
  2. // Copyright (c) 2008-2013 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 "ResourceCache.h"
  25. #include "Sprite.h"
  26. #include "Texture2D.h"
  27. #include "DebugNew.h"
  28. namespace Urho3D
  29. {
  30. extern const char* blendModeNames[];
  31. extern const char* horizontalAlignments[];
  32. extern const char* verticalAlignments[];
  33. OBJECTTYPESTATIC(Sprite);
  34. Sprite::Sprite(Context* context) :
  35. UIElement(context),
  36. floatPosition_(Vector2::ZERO),
  37. hotSpot_(IntVector2::ZERO),
  38. scale_(Vector2::ONE),
  39. rotation_(0.0f),
  40. imageRect_(IntRect::ZERO),
  41. blendMode_(BLEND_REPLACE)
  42. {
  43. }
  44. Sprite::~Sprite()
  45. {
  46. }
  47. void Sprite::RegisterObject(Context* context)
  48. {
  49. context->RegisterFactory<Sprite>();
  50. ACCESSOR_ATTRIBUTE(Sprite, VAR_RESOURCEREF, "Texture", GetTextureAttr, SetTextureAttr, ResourceRef, ResourceRef(Texture2D::GetTypeStatic()), AM_FILE);
  51. ENUM_ACCESSOR_ATTRIBUTE(Sprite, "Blend Mode", GetBlendMode, SetBlendMode, BlendMode, blendModeNames, 0, AM_FILE);
  52. REF_ACCESSOR_ATTRIBUTE(Sprite, VAR_STRING, "Name", GetName, SetName, String, String::EMPTY, AM_FILE);
  53. REF_ACCESSOR_ATTRIBUTE(Sprite, VAR_VECTOR2, "Position", GetPosition, SetPosition, Vector2, Vector2::ZERO, AM_FILE);
  54. REF_ACCESSOR_ATTRIBUTE(Sprite, VAR_INTVECTOR2, "Size", GetSize, SetSize, IntVector2, IntVector2::ZERO, AM_FILE);
  55. REF_ACCESSOR_ATTRIBUTE(Sprite, VAR_INTVECTOR2, "Hotspot", GetHotSpot, SetHotSpot, IntVector2, IntVector2::ZERO, AM_FILE);
  56. REF_ACCESSOR_ATTRIBUTE(Sprite, VAR_VECTOR2, "Scale", GetScale, SetScale, Vector2, Vector2::ZERO, AM_FILE);
  57. ACCESSOR_ATTRIBUTE(Sprite, VAR_FLOAT, "Rotation", GetRotation, SetRotation, float, 0.0f, AM_FILE);
  58. ENUM_ACCESSOR_ATTRIBUTE(Sprite, "Horiz Alignment", GetHorizontalAlignment, SetHorizontalAlignment, HorizontalAlignment, horizontalAlignments, HA_LEFT, AM_FILE);
  59. ENUM_ACCESSOR_ATTRIBUTE(Sprite, "Vert Alignment", GetVerticalAlignment, SetVerticalAlignment, VerticalAlignment, verticalAlignments, VA_TOP, AM_FILE);
  60. ACCESSOR_ATTRIBUTE(Sprite, VAR_INT, "Priority", GetPriority, SetPriority, int, 0, AM_FILE);
  61. ACCESSOR_ATTRIBUTE(Sprite, VAR_FLOAT, "Opacity", GetOpacity, SetOpacity, float, 1.0f, AM_FILE);
  62. REF_ACCESSOR_ATTRIBUTE(Sprite, VAR_COLOR, "Color", GetColorAttr, SetColor, Color, Color::WHITE, AM_FILE);
  63. ATTRIBUTE(Sprite, VAR_COLOR, "Top Left Color", color_[0], Color::WHITE, AM_FILE);
  64. ATTRIBUTE(Sprite, VAR_COLOR, "Top Right Color", color_[1], Color::WHITE, AM_FILE);
  65. ATTRIBUTE(Sprite, VAR_COLOR, "Bottom Left Color", color_[2], Color::WHITE, AM_FILE);
  66. ATTRIBUTE(Sprite, VAR_COLOR, "Bottom Right Color", color_[3], Color::WHITE, AM_FILE);
  67. ACCESSOR_ATTRIBUTE(Sprite, VAR_BOOL, "Is Visible", IsVisible, SetVisible, bool, true, AM_FILE);
  68. ACCESSOR_ATTRIBUTE(Sprite, VAR_BOOL, "Use Derived Opacity", GetUseDerivedOpacity, SetUseDerivedOpacity, bool, false, AM_FILE);
  69. ATTRIBUTE(Sprite, VAR_VARIANTMAP, "Variables", vars_, Variant::emptyVariantMap, AM_FILE);
  70. }
  71. bool Sprite::IsWithinScissor(const IntRect& currentScissor)
  72. {
  73. /// \todo Implement properly, for now just checks visibility flag
  74. return visible_;
  75. }
  76. const IntVector2& Sprite::GetScreenPosition() const
  77. {
  78. // This updates screen position for a sprite
  79. GetTransform();
  80. return screenPosition_;
  81. }
  82. void Sprite::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  83. {
  84. bool allOpaque = true;
  85. if (GetDerivedOpacity() < 1.0f || color_[C_TOPLEFT].a_ < 1.0f || color_[C_TOPRIGHT].a_ < 1.0f ||
  86. color_[C_BOTTOMLEFT].a_ < 1.0f || color_[C_BOTTOMRIGHT].a_ < 1.0f)
  87. allOpaque = false;
  88. const IntVector2& size = GetSize();
  89. UIBatch batch(this, blendMode_ == BLEND_REPLACE && !allOpaque ? BLEND_ALPHA : blendMode_, currentScissor, texture_, &vertexData);
  90. batch.AddQuad(GetTransform(), 0, 0, size.x_, size.y_, imageRect_.left_, imageRect_.top_, imageRect_.right_ -
  91. imageRect_.left_, imageRect_.bottom_ - imageRect_.top_);
  92. UIBatch::AddOrMerge(batch, batches);
  93. // Reset hovering for next frame
  94. hovering_ = false;
  95. }
  96. void Sprite::OnPositionSet()
  97. {
  98. // If the integer position was set (layout update?), copy to the float position
  99. floatPosition_ = Vector2((float)position_.x_, (float)position_.y_);
  100. }
  101. void Sprite::SetPosition(const Vector2& position)
  102. {
  103. if (position != floatPosition_)
  104. {
  105. floatPosition_ = position;
  106. // Copy to the integer position
  107. position_ = IntVector2((int)position.x_, (int)position.y_);
  108. MarkDirty();
  109. }
  110. }
  111. void Sprite::SetPosition(float x, float y)
  112. {
  113. SetPosition(Vector2(x, y));
  114. }
  115. void Sprite::SetHotSpot(const IntVector2& hotSpot)
  116. {
  117. if (hotSpot != hotSpot_)
  118. {
  119. hotSpot_ = hotSpot;
  120. MarkDirty();
  121. }
  122. }
  123. void Sprite::SetHotSpot(int x, int y)
  124. {
  125. SetHotSpot(IntVector2(x, y));
  126. }
  127. void Sprite::SetScale(const Vector2& scale)
  128. {
  129. if (scale != scale_)
  130. {
  131. scale_ = scale;
  132. MarkDirty();
  133. }
  134. }
  135. void Sprite::SetScale(float x, float y)
  136. {
  137. SetScale(Vector2(x, y));
  138. }
  139. void Sprite::SetScale(float scale)
  140. {
  141. SetScale(Vector2(scale, scale));
  142. }
  143. void Sprite::SetRotation(float angle)
  144. {
  145. if (angle != rotation_)
  146. {
  147. rotation_ = angle;
  148. MarkDirty();
  149. }
  150. }
  151. void Sprite::SetTexture(Texture* texture)
  152. {
  153. texture_ = texture;
  154. if (imageRect_ == IntRect::ZERO)
  155. SetFullImageRect();
  156. }
  157. void Sprite::SetImageRect(const IntRect& rect)
  158. {
  159. if (rect != IntRect::ZERO)
  160. imageRect_ = rect;
  161. }
  162. void Sprite::SetFullImageRect()
  163. {
  164. if (texture_)
  165. SetImageRect(IntRect(0, 0, texture_->GetWidth(), texture_->GetHeight()));
  166. }
  167. void Sprite::SetBlendMode(BlendMode mode)
  168. {
  169. blendMode_ = mode;
  170. }
  171. const Matrix3x4& Sprite::GetTransform() const
  172. {
  173. if (positionDirty_)
  174. {
  175. Vector2 pos = floatPosition_;
  176. Matrix3x4 parentTransform;
  177. if (parent_)
  178. {
  179. Sprite* parentSprite = dynamic_cast<Sprite*>(parent_);
  180. if (parentSprite)
  181. parentTransform = parentSprite->GetTransform();
  182. else
  183. {
  184. const IntVector2& parentScreenPos = parent_->GetScreenPosition() + parent_->GetChildOffset();
  185. parentTransform = Matrix3x4::IDENTITY;
  186. parentTransform.SetTranslation(Vector3((float)parentScreenPos.x_, (float)parentScreenPos.y_, 0.0f));
  187. }
  188. switch (GetHorizontalAlignment())
  189. {
  190. case HA_LEFT:
  191. break;
  192. case HA_CENTER:
  193. pos.x_ += (float)(parent_->GetSize().x_ / 2);
  194. break;
  195. case HA_RIGHT:
  196. pos.x_ += (float)parent_->GetSize().x_;
  197. break;
  198. }
  199. switch (GetVerticalAlignment())
  200. {
  201. case VA_TOP:
  202. break;
  203. case VA_CENTER:
  204. pos.y_ += (float)(parent_->GetSize().y_ / 2);
  205. break;
  206. case VA_BOTTOM:
  207. pos.y_ += (float)(parent_->GetSize().y_);
  208. break;
  209. }
  210. }
  211. else
  212. parentTransform = Matrix3x4::IDENTITY;
  213. Matrix3x4 hotspotAdjust(Matrix3x4::IDENTITY);
  214. hotspotAdjust.SetTranslation(Vector3((float)-hotSpot_.x_, (float)-hotSpot_.y_, 0.0f));
  215. Matrix3x4 mainTransform(Vector3(pos, 0.0f), Quaternion(rotation_, Vector3::FORWARD), Vector3(scale_, 1.0f));
  216. transform_ = parentTransform * mainTransform * hotspotAdjust;
  217. positionDirty_ = false;
  218. // Calculate an approximate screen position for GetElementAt(), or pixel-perfect child elements
  219. Vector3 topLeftCorner = transform_ * Vector3::ZERO;
  220. screenPosition_ = IntVector2((int)topLeftCorner.x_, (int)topLeftCorner.y_);
  221. }
  222. return transform_;
  223. }
  224. void Sprite::SetTextureAttr(ResourceRef value)
  225. {
  226. ResourceCache* cache = GetSubsystem<ResourceCache>();
  227. SetTexture(cache->GetResource<Texture2D>(value.id_));
  228. }
  229. ResourceRef Sprite::GetTextureAttr() const
  230. {
  231. return GetResourceRef(texture_, Texture2D::GetTypeStatic());
  232. }
  233. }