Sprite.cpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // Copyright (c) 2008-2020 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 "../Core/Context.h"
  24. #include "../Graphics/Texture2D.h"
  25. #include "../Resource/ResourceCache.h"
  26. #include "../UI/Sprite.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. 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() = default;
  45. void Sprite::RegisterObject(Context* context)
  46. {
  47. context->RegisterFactory<Sprite>(UI_CATEGORY);
  48. URHO3D_ACCESSOR_ATTRIBUTE("Name", GetName, SetName, String, String::EMPTY, AM_FILE);
  49. URHO3D_ACCESSOR_ATTRIBUTE("Position", GetPosition, SetPosition, Vector2, Vector2::ZERO, AM_FILE);
  50. URHO3D_ACCESSOR_ATTRIBUTE("Size", GetSize, SetSize, IntVector2, IntVector2::ZERO, AM_FILE);
  51. URHO3D_ACCESSOR_ATTRIBUTE("Hotspot", GetHotSpot, SetHotSpot, IntVector2, IntVector2::ZERO, AM_FILE);
  52. URHO3D_ACCESSOR_ATTRIBUTE("Scale", GetScale, SetScale, Vector2, Vector2::ONE, AM_FILE);
  53. URHO3D_ACCESSOR_ATTRIBUTE("Rotation", GetRotation, SetRotation, float, 0.0f, AM_FILE);
  54. URHO3D_MIXED_ACCESSOR_ATTRIBUTE("Texture", GetTextureAttr, SetTextureAttr, ResourceRef, ResourceRef(Texture2D::GetTypeStatic()),
  55. AM_FILE);
  56. URHO3D_ACCESSOR_ATTRIBUTE("Image Rect", GetImageRect, SetImageRect, IntRect, IntRect::ZERO, AM_FILE);
  57. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Blend Mode", GetBlendMode, SetBlendMode, BlendMode, blendModeNames, 0, AM_FILE);
  58. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Horiz Alignment", GetHorizontalAlignment, SetHorizontalAlignment, HorizontalAlignment,
  59. horizontalAlignments, HA_LEFT, AM_FILE);
  60. URHO3D_ENUM_ACCESSOR_ATTRIBUTE("Vert Alignment", GetVerticalAlignment, SetVerticalAlignment, VerticalAlignment, verticalAlignments,
  61. VA_TOP, AM_FILE);
  62. URHO3D_ACCESSOR_ATTRIBUTE("Priority", GetPriority, SetPriority, int, 0, AM_FILE);
  63. URHO3D_ACCESSOR_ATTRIBUTE("Opacity", GetOpacity, SetOpacity, float, 1.0f, AM_FILE);
  64. URHO3D_ACCESSOR_ATTRIBUTE("Color", GetColorAttr, SetColor, Color, Color::WHITE, AM_FILE);
  65. URHO3D_ATTRIBUTE("Top Left Color", Color, colors_[0], Color::WHITE, AM_FILE);
  66. URHO3D_ATTRIBUTE("Top Right Color", Color, colors_[1], Color::WHITE, AM_FILE);
  67. URHO3D_ATTRIBUTE("Bottom Left Color", Color, colors_[2], Color::WHITE, AM_FILE);
  68. URHO3D_ATTRIBUTE("Bottom Right Color", Color, colors_[3], Color::WHITE, AM_FILE);
  69. URHO3D_ACCESSOR_ATTRIBUTE("Is Visible", IsVisible, SetVisible, bool, true, AM_FILE);
  70. URHO3D_ACCESSOR_ATTRIBUTE("Use Derived Opacity", GetUseDerivedOpacity, SetUseDerivedOpacity, bool, true, AM_FILE);
  71. URHO3D_ATTRIBUTE("Variables", VariantMap, 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. IntVector2 Sprite::ScreenToElement(const IntVector2& screenPosition)
  85. {
  86. Vector3 floatPos((float)screenPosition.x_, (float)screenPosition.y_, 0.0f);
  87. Vector3 transformedPos = GetTransform().Inverse() * floatPos;
  88. return IntVector2((int)transformedPos.x_, (int)transformedPos.y_);
  89. }
  90. IntVector2 Sprite::ElementToScreen(const IntVector2& position)
  91. {
  92. Vector3 floatPos((float)position.x_, (float)position.y_, 0.0f);
  93. Vector3 transformedPos = GetTransform() * floatPos;
  94. return IntVector2((int)transformedPos.x_, (int)transformedPos.y_);
  95. }
  96. void Sprite::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData, const IntRect& currentScissor)
  97. {
  98. bool allOpaque = true;
  99. if (GetDerivedOpacity() < 1.0f || colors_[C_TOPLEFT].a_ < 1.0f || colors_[C_TOPRIGHT].a_ < 1.0f ||
  100. colors_[C_BOTTOMLEFT].a_ < 1.0f || colors_[C_BOTTOMRIGHT].a_ < 1.0f)
  101. allOpaque = false;
  102. const IntVector2& size = GetSize();
  103. UIBatch
  104. batch(this, blendMode_ == BLEND_REPLACE && !allOpaque ? BLEND_ALPHA : blendMode_, currentScissor, texture_, &vertexData);
  105. batch.AddQuad(GetTransform(), 0, 0, size.x_, size.y_, imageRect_.left_, imageRect_.top_, imageRect_.right_ - imageRect_.left_,
  106. imageRect_.bottom_ - imageRect_.top_);
  107. UIBatch::AddOrMerge(batch, batches);
  108. // Reset hovering for next frame
  109. hovering_ = false;
  110. }
  111. void Sprite::OnPositionSet(const IntVector2& newPosition)
  112. {
  113. // If the integer position was set (layout update?), copy to the float position
  114. floatPosition_ = Vector2((float)newPosition.x_, (float)newPosition.y_);
  115. }
  116. void Sprite::SetPosition(const Vector2& position)
  117. {
  118. if (position != floatPosition_)
  119. {
  120. floatPosition_ = position;
  121. // Copy to the integer position
  122. position_ = IntVector2((int)position.x_, (int)position.y_);
  123. MarkDirty();
  124. }
  125. }
  126. void Sprite::SetPosition(float x, float y)
  127. {
  128. SetPosition(Vector2(x, y));
  129. }
  130. void Sprite::SetHotSpot(const IntVector2& hotSpot)
  131. {
  132. if (hotSpot != hotSpot_)
  133. {
  134. hotSpot_ = hotSpot;
  135. MarkDirty();
  136. }
  137. }
  138. void Sprite::SetHotSpot(int x, int y)
  139. {
  140. SetHotSpot(IntVector2(x, y));
  141. }
  142. void Sprite::SetScale(const Vector2& scale)
  143. {
  144. if (scale != scale_)
  145. {
  146. scale_ = scale;
  147. MarkDirty();
  148. }
  149. }
  150. void Sprite::SetScale(float x, float y)
  151. {
  152. SetScale(Vector2(x, y));
  153. }
  154. void Sprite::SetScale(float scale)
  155. {
  156. SetScale(Vector2(scale, scale));
  157. }
  158. void Sprite::SetRotation(float angle)
  159. {
  160. if (angle != rotation_)
  161. {
  162. rotation_ = angle;
  163. MarkDirty();
  164. }
  165. }
  166. void Sprite::SetTexture(Texture* texture)
  167. {
  168. texture_ = texture;
  169. if (imageRect_ == IntRect::ZERO)
  170. SetFullImageRect();
  171. }
  172. void Sprite::SetImageRect(const IntRect& rect)
  173. {
  174. if (rect != IntRect::ZERO)
  175. imageRect_ = rect;
  176. }
  177. void Sprite::SetFullImageRect()
  178. {
  179. if (texture_)
  180. SetImageRect(IntRect(0, 0, texture_->GetWidth(), texture_->GetHeight()));
  181. }
  182. void Sprite::SetBlendMode(BlendMode mode)
  183. {
  184. blendMode_ = mode;
  185. }
  186. const Matrix3x4& Sprite::GetTransform() const
  187. {
  188. if (positionDirty_)
  189. {
  190. Vector2 pos = floatPosition_;
  191. Matrix3x4 parentTransform;
  192. if (parent_)
  193. {
  194. auto* parentSprite = dynamic_cast<Sprite*>(parent_);
  195. if (parentSprite)
  196. parentTransform = parentSprite->GetTransform();
  197. else
  198. {
  199. const IntVector2& parentScreenPos = parent_->GetScreenPosition() + parent_->GetChildOffset();
  200. parentTransform = Matrix3x4::IDENTITY;
  201. parentTransform.SetTranslation(Vector3((float)parentScreenPos.x_, (float)parentScreenPos.y_, 0.0f));
  202. }
  203. switch (GetHorizontalAlignment())
  204. {
  205. case HA_LEFT:
  206. break;
  207. case HA_CENTER:
  208. pos.x_ += (float)parent_->GetSize().x_ / 2.f;
  209. break;
  210. case HA_RIGHT:
  211. pos.x_ += (float)parent_->GetSize().x_;
  212. break;
  213. }
  214. switch (GetVerticalAlignment())
  215. {
  216. case VA_TOP:
  217. break;
  218. case VA_CENTER:
  219. pos.y_ += (float)parent_->GetSize().y_ / 2.f;
  220. break;
  221. case VA_BOTTOM:
  222. pos.y_ += (float)(parent_->GetSize().y_);
  223. break;
  224. }
  225. }
  226. else
  227. parentTransform = Matrix3x4::IDENTITY;
  228. Matrix3x4 hotspotAdjust(Matrix3x4::IDENTITY);
  229. hotspotAdjust.SetTranslation(Vector3((float)-hotSpot_.x_, (float)-hotSpot_.y_, 0.0f));
  230. Matrix3x4 mainTransform(Vector3(pos, 0.0f), Quaternion(rotation_, Vector3::FORWARD), Vector3(scale_, 1.0f));
  231. transform_ = parentTransform * mainTransform * hotspotAdjust;
  232. positionDirty_ = false;
  233. // Calculate an approximate screen position for GetElementAt(), or pixel-perfect child elements
  234. Vector3 topLeftCorner = transform_ * Vector3::ZERO;
  235. screenPosition_ = IntVector2((int)topLeftCorner.x_, (int)topLeftCorner.y_);
  236. }
  237. return transform_;
  238. }
  239. void Sprite::SetTextureAttr(const ResourceRef& value)
  240. {
  241. auto* cache = GetSubsystem<ResourceCache>();
  242. SetTexture(cache->GetResource<Texture2D>(value.name_));
  243. }
  244. ResourceRef Sprite::GetTextureAttr() const
  245. {
  246. return GetResourceRef(texture_, Texture2D::GetTypeStatic());
  247. }
  248. }