Sprite2D.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // Copyright (c) 2008-2014 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 "Context.h"
  24. #include "Deserializer.h"
  25. #include "ResourceCache.h"
  26. #include "Sprite2D.h"
  27. #include "SpriteSheet2D.h"
  28. #include "Texture2D.h"
  29. #include "DebugNew.h"
  30. namespace Urho3D
  31. {
  32. Sprite2D::Sprite2D(Context* context) :
  33. Resource(context),
  34. hotSpot_(0.5f, 0.5f),
  35. offset_(0, 0)
  36. {
  37. }
  38. Sprite2D::~Sprite2D()
  39. {
  40. }
  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. loadTexture_ = new Texture2D(context_);
  50. loadTexture_->SetName(GetName());
  51. // In case we're async loading, only call BeginLoad() for the texture (load image but do not upload to GPU)
  52. if (!loadTexture_->BeginLoad(source))
  53. {
  54. loadTexture_.Reset();
  55. return false;
  56. }
  57. return true;
  58. }
  59. bool Sprite2D::EndLoad()
  60. {
  61. // Finish loading of the texture in the main thread
  62. bool success = false;
  63. if (loadTexture_ && loadTexture_->EndLoad())
  64. {
  65. success = true;
  66. SetTexture(loadTexture_);
  67. if (texture_)
  68. SetRectangle(IntRect(0, 0, texture_->GetWidth(), texture_->GetHeight()));
  69. }
  70. loadTexture_.Reset();
  71. return success;
  72. }
  73. void Sprite2D::SetTexture(Texture2D* texture)
  74. {
  75. texture_ = texture;
  76. }
  77. void Sprite2D::SetRectangle(const IntRect& rectangle)
  78. {
  79. rectangle_ = rectangle;
  80. }
  81. void Sprite2D::SetHotSpot(const Vector2& hotSpot)
  82. {
  83. hotSpot_ = hotSpot;
  84. }
  85. void Sprite2D::SetOffset(const IntVector2& offset)
  86. {
  87. offset_ = offset;
  88. }
  89. void Sprite2D::SetSpriteSheet(SpriteSheet2D* spriteSheet)
  90. {
  91. spriteSheet_ = spriteSheet;
  92. }
  93. ResourceRef Sprite2D::SaveToResourceRef(Sprite2D* sprite)
  94. {
  95. SpriteSheet2D* spriteSheet = 0;
  96. if (sprite)
  97. spriteSheet = sprite->GetSpriteSheet();
  98. if (!spriteSheet)
  99. return GetResourceRef(sprite, Sprite2D::GetTypeStatic());
  100. // Combine sprite sheet name and sprite name as resource name.
  101. return ResourceRef(spriteSheet->GetType(), spriteSheet->GetName() + "@" + sprite->GetName());
  102. }
  103. Sprite2D* Sprite2D::LoadFromResourceRef(Object* object, const ResourceRef& value)
  104. {
  105. if (!object)
  106. return 0;
  107. ResourceCache* cache = object->GetSubsystem<ResourceCache>();
  108. if (value.type_ == Sprite2D::GetTypeStatic())
  109. return cache->GetResource<Sprite2D>(value.name_);
  110. if (value.type_ == SpriteSheet2D::GetTypeStatic())
  111. {
  112. // value.name_ include sprite sheet name and sprite name.
  113. Vector<String> names = value.name_.Split('@');
  114. if (names.Size() != 2)
  115. return 0;
  116. const String& spriteSheetName = names[0];
  117. const String& spriteName = names[1];
  118. SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(spriteSheetName);
  119. if (!spriteSheet)
  120. return 0;
  121. return spriteSheet->GetSprite(spriteName);
  122. }
  123. return 0;
  124. }
  125. }