Sprite.cpp 9.1 KB

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