Sprite.cpp 9.8 KB

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