Sprite2D.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 "../IO/Deserializer.h"
  26. #include "../Resource/ResourceCache.h"
  27. #include "../Urho2D/Drawable2D.h"
  28. #include "../Urho2D/Sprite2D.h"
  29. #include "../Urho2D/SpriteSheet2D.h"
  30. #include "../DebugNew.h"
  31. namespace Urho3D
  32. {
  33. Sprite2D::Sprite2D(Context* context) :
  34. Resource(context),
  35. hotSpot_(0.5f, 0.5f),
  36. offset_(0, 0),
  37. edgeOffset_(M_LARGE_EPSILON)
  38. {
  39. }
  40. Sprite2D::~Sprite2D() = default;
  41. void Sprite2D::RegisterObject(Context* context)
  42. {
  43. context->RegisterFactory<Sprite2D>();
  44. }
  45. bool Sprite2D::BeginLoad(Deserializer& source)
  46. {
  47. if (GetName().Empty())
  48. SetName(source.GetName());
  49. // Reload
  50. if (texture_)
  51. loadTexture_ = texture_;
  52. else
  53. {
  54. loadTexture_ = new Texture2D(context_);
  55. loadTexture_->SetName(GetName());
  56. }
  57. // In case we're async loading, only call BeginLoad() for the texture (load image but do not upload to GPU)
  58. if (!loadTexture_->BeginLoad(source))
  59. {
  60. // Reload failed
  61. if (loadTexture_ == texture_)
  62. texture_.Reset();
  63. loadTexture_.Reset();
  64. return false;
  65. }
  66. return true;
  67. }
  68. bool Sprite2D::EndLoad()
  69. {
  70. // Finish loading of the texture in the main thread
  71. bool success = false;
  72. if (loadTexture_ && loadTexture_->EndLoad())
  73. {
  74. success = true;
  75. SetTexture(loadTexture_);
  76. if (texture_)
  77. SetRectangle(IntRect(0, 0, texture_->GetWidth(), texture_->GetHeight()));
  78. }
  79. else
  80. {
  81. // Reload failed
  82. if (loadTexture_ == texture_)
  83. texture_.Reset();
  84. }
  85. loadTexture_.Reset();
  86. return success;
  87. }
  88. void Sprite2D::SetTexture(Texture2D* texture)
  89. {
  90. texture_ = texture;
  91. // Ensure the texture doesn't have wrap addressing as that will cause bleeding bugs on the edges.
  92. // Could also choose border mode, but in that case a universally good border color (without alpha bugs)
  93. // would be hard to choose. Ideal is for the user to configure the texture parameters in its parameter
  94. // XML file.
  95. if (texture_->GetAddressMode(COORD_U) == ADDRESS_WRAP)
  96. {
  97. texture_->SetAddressMode(COORD_U, ADDRESS_CLAMP);
  98. texture_->SetAddressMode(COORD_V, ADDRESS_CLAMP);
  99. }
  100. }
  101. void Sprite2D::SetRectangle(const IntRect& rectangle)
  102. {
  103. rectangle_ = rectangle;
  104. }
  105. void Sprite2D::SetHotSpot(const Vector2& hotSpot)
  106. {
  107. hotSpot_ = hotSpot;
  108. }
  109. void Sprite2D::SetOffset(const IntVector2& offset)
  110. {
  111. offset_ = offset;
  112. }
  113. void Sprite2D::SetTextureEdgeOffset(float offset)
  114. {
  115. edgeOffset_ = offset;
  116. }
  117. void Sprite2D::SetSpriteSheet(SpriteSheet2D* spriteSheet)
  118. {
  119. spriteSheet_ = spriteSheet;
  120. }
  121. bool Sprite2D::GetDrawRectangle(Rect& rect, bool flipX, bool flipY) const
  122. {
  123. return GetDrawRectangle(rect, hotSpot_, flipX, flipY);
  124. }
  125. bool Sprite2D::GetDrawRectangle(Rect& rect, const Vector2& hotSpot, bool flipX, bool flipY) const
  126. {
  127. if (rectangle_.Width() == 0 || rectangle_.Height() == 0)
  128. return false;
  129. float width = (float)rectangle_.Width() * PIXEL_SIZE;
  130. float height = (float)rectangle_.Height() * PIXEL_SIZE;
  131. float hotSpotX = flipX ? (1.0f - hotSpot.x_) : hotSpot.x_;
  132. float hotSpotY = flipY ? (1.0f - hotSpot.y_) : hotSpot.y_;
  133. rect.min_.x_ = -width * hotSpotX;
  134. rect.max_.x_ = width * (1.0f - hotSpotX);
  135. rect.min_.y_ = -height * hotSpotY;
  136. rect.max_.y_ = height * (1.0f - hotSpotY);
  137. return true;
  138. }
  139. bool Sprite2D::GetTextureRectangle(Rect& rect, bool flipX, bool flipY) const
  140. {
  141. if (!texture_)
  142. return false;
  143. float invWidth = 1.0f / (float)texture_->GetWidth();
  144. float invHeight = 1.0f / (float)texture_->GetHeight();
  145. rect.min_.x_ = ((float)rectangle_.left_ + edgeOffset_) * invWidth;
  146. rect.max_.x_ = ((float)rectangle_.right_ - edgeOffset_) * invWidth;
  147. rect.min_.y_ = ((float)rectangle_.bottom_ - edgeOffset_) * invHeight;
  148. rect.max_.y_ = ((float)rectangle_.top_ + edgeOffset_) * invHeight;
  149. if (flipX)
  150. Swap(rect.min_.x_, rect.max_.x_);
  151. if (flipY)
  152. Swap(rect.min_.y_, rect.max_.y_);
  153. return true;
  154. }
  155. ResourceRef Sprite2D::SaveToResourceRef(Sprite2D* sprite)
  156. {
  157. SpriteSheet2D* spriteSheet = nullptr;
  158. if (sprite)
  159. spriteSheet = sprite->GetSpriteSheet();
  160. if (!spriteSheet)
  161. return GetResourceRef(sprite, Sprite2D::GetTypeStatic());
  162. // Combine sprite sheet name and sprite name as resource name.
  163. return ResourceRef(spriteSheet->GetType(), spriteSheet->GetName() + "@" + sprite->GetName());
  164. }
  165. Sprite2D* Sprite2D::LoadFromResourceRef(Object* object, const ResourceRef& value)
  166. {
  167. if (!object)
  168. return nullptr;
  169. auto* cache = object->GetSubsystem<ResourceCache>();
  170. if (value.type_ == Sprite2D::GetTypeStatic())
  171. return cache->GetResource<Sprite2D>(value.name_);
  172. if (value.type_ == SpriteSheet2D::GetTypeStatic())
  173. {
  174. // value.name_ include sprite sheet name and sprite name.
  175. Vector<String> names = value.name_.Split('@');
  176. if (names.Size() != 2)
  177. return nullptr;
  178. const String& spriteSheetName = names[0];
  179. const String& spriteName = names[1];
  180. auto* spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetName);
  181. if (!spriteSheet)
  182. return nullptr;
  183. return spriteSheet->GetSprite(spriteName);
  184. }
  185. return nullptr;
  186. }
  187. }