Sprite2D.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 "../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. {
  38. }
  39. Sprite2D::~Sprite2D()
  40. {
  41. }
  42. void Sprite2D::RegisterObject(Context* context)
  43. {
  44. context->RegisterFactory<Sprite2D>();
  45. }
  46. bool Sprite2D::BeginLoad(Deserializer& source)
  47. {
  48. if (GetName().Empty())
  49. SetName(source.GetName());
  50. // Reload
  51. if (texture_)
  52. loadTexture_ = texture_;
  53. else
  54. {
  55. loadTexture_ = new Texture2D(context_);
  56. loadTexture_->SetName(GetName());
  57. }
  58. // In case we're async loading, only call BeginLoad() for the texture (load image but do not upload to GPU)
  59. if (!loadTexture_->BeginLoad(source))
  60. {
  61. // Reload failed
  62. if (loadTexture_ == texture_)
  63. texture_.Reset();
  64. loadTexture_.Reset();
  65. return false;
  66. }
  67. return true;
  68. }
  69. bool Sprite2D::EndLoad()
  70. {
  71. // Finish loading of the texture in the main thread
  72. bool success = false;
  73. if (loadTexture_ && loadTexture_->EndLoad())
  74. {
  75. success = true;
  76. SetTexture(loadTexture_);
  77. if (texture_)
  78. SetRectangle(IntRect(0, 0, texture_->GetWidth(), texture_->GetHeight()));
  79. }
  80. else
  81. {
  82. // Reload failed
  83. if (loadTexture_ == texture_)
  84. texture_.Reset();
  85. }
  86. loadTexture_.Reset();
  87. return success;
  88. }
  89. void Sprite2D::SetTexture(Texture2D* texture)
  90. {
  91. texture_ = texture;
  92. }
  93. void Sprite2D::SetRectangle(const IntRect& rectangle)
  94. {
  95. rectangle_ = rectangle;
  96. }
  97. void Sprite2D::SetHotSpot(const Vector2& hotSpot)
  98. {
  99. hotSpot_ = hotSpot;
  100. }
  101. void Sprite2D::SetOffset(const IntVector2& offset)
  102. {
  103. offset_ = offset;
  104. }
  105. void Sprite2D::SetSpriteSheet(SpriteSheet2D* spriteSheet)
  106. {
  107. spriteSheet_ = spriteSheet;
  108. }
  109. bool Sprite2D::GetDrawRectangle(Rect& rect, bool flipX, bool flipY) const
  110. {
  111. return GetDrawRectangle(rect, hotSpot_, flipX, flipY);
  112. }
  113. bool Sprite2D::GetDrawRectangle(Rect& rect, const Vector2& hotSpot, bool flipX, bool flipY) const
  114. {
  115. if (rectangle_.Width() == 0 || rectangle_.Height() == 0)
  116. return false;
  117. float width = (float)rectangle_.Width() * PIXEL_SIZE;
  118. float height = (float)rectangle_.Height() * PIXEL_SIZE;
  119. float hotSpotX = flipX ? (1.0f - hotSpot.x_) : hotSpot.x_;
  120. float hotSpotY = flipY ? (1.0f - hotSpot.y_) : hotSpot.y_;
  121. rect.min_.x_ = -width * hotSpotX;
  122. rect.max_.x_ = width * (1.0f - hotSpotX);
  123. rect.min_.y_ = -height * hotSpotY;
  124. rect.max_.y_ = height * (1.0f - hotSpotY);
  125. return true;
  126. }
  127. bool Sprite2D::GetTextureRectangle(Rect& rect, bool flipX, bool flipY) const
  128. {
  129. if (!texture_)
  130. return false;
  131. float invWidth = 1.0f / (float)texture_->GetWidth();
  132. float invHeight = 1.0f / (float)texture_->GetHeight();
  133. rect.min_.x_ = rectangle_.left_ * invWidth;
  134. rect.max_.x_ = rectangle_.right_ * invWidth;
  135. rect.min_.y_ = rectangle_.bottom_ * invHeight;
  136. rect.max_.y_ = rectangle_.top_ * invHeight;
  137. if (flipX)
  138. Swap(rect.min_.x_, rect.max_.x_);
  139. if (flipY)
  140. Swap(rect.min_.y_, rect.max_.y_);
  141. return true;
  142. }
  143. ResourceRef Sprite2D::SaveToResourceRef(Sprite2D* sprite)
  144. {
  145. SpriteSheet2D* spriteSheet = 0;
  146. if (sprite)
  147. spriteSheet = sprite->GetSpriteSheet();
  148. if (!spriteSheet)
  149. return GetResourceRef(sprite, Sprite2D::GetTypeStatic());
  150. // Combine sprite sheet name and sprite name as resource name.
  151. return ResourceRef(spriteSheet->GetType(), spriteSheet->GetName() + "@" + sprite->GetName());
  152. }
  153. Sprite2D* Sprite2D::LoadFromResourceRef(Object* object, const ResourceRef& value)
  154. {
  155. if (!object)
  156. return 0;
  157. ResourceCache* cache = object->GetSubsystem<ResourceCache>();
  158. if (value.type_ == Sprite2D::GetTypeStatic())
  159. return cache->GetResource<Sprite2D>(value.name_);
  160. if (value.type_ == SpriteSheet2D::GetTypeStatic())
  161. {
  162. // value.name_ include sprite sheet name and sprite name.
  163. Vector<String> names = value.name_.Split('@');
  164. if (names.Size() != 2)
  165. return 0;
  166. const String& spriteSheetName = names[0];
  167. const String& spriteName = names[1];
  168. SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetName);
  169. if (!spriteSheet)
  170. return 0;
  171. return spriteSheet->GetSprite(spriteName);
  172. }
  173. return 0;
  174. }
  175. }