Sprite.cpp 9.1 KB

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