Sprite.cpp 9.5 KB

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