Sprite.cpp 8.7 KB

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