Animation2D.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 "Animation2D.h"
  24. #include "Context.h"
  25. #include "Deserializer.h"
  26. #include "FileSystem.h"
  27. #include "Log.h"
  28. #include "ResourceCache.h"
  29. #include "Serializer.h"
  30. #include "Sprite2D.h"
  31. #include "SpriteSheet2D.h"
  32. #include "XMLFile.h"
  33. #include "DebugNew.h"
  34. namespace Urho3D
  35. {
  36. Animation2D::Animation2D(Context* context) :
  37. Resource(context)
  38. {
  39. }
  40. Animation2D::~Animation2D()
  41. {
  42. }
  43. void Animation2D::RegisterObject(Context* context)
  44. {
  45. context->RegisterFactory<Animation2D>();
  46. }
  47. bool Animation2D::Load(Deserializer& source)
  48. {
  49. frameEndTimes_.Clear();
  50. frameSprites_.Clear();
  51. SharedPtr<XMLFile> xmlFile(new XMLFile(context_));
  52. if(!xmlFile->Load(source))
  53. {
  54. LOGERROR("Could not load animation");
  55. return false;
  56. }
  57. SetMemoryUse(source.GetSize());
  58. XMLElement rootElem = xmlFile->GetRoot("animation");
  59. if (!rootElem)
  60. {
  61. LOGERROR("Invalid animation");
  62. return false;
  63. }
  64. ResourceCache* cache = GetSubsystem<ResourceCache>();
  65. XMLElement keyFrameElem = rootElem.GetChild("frame");
  66. if (!keyFrameElem)
  67. {
  68. LOGERROR("Could not found key frame");
  69. return false;
  70. }
  71. float endTime = 0.0f;
  72. while (keyFrameElem)
  73. {
  74. endTime += keyFrameElem.GetFloat("duration");
  75. frameEndTimes_.Push(endTime);
  76. SharedPtr<Sprite2D> sprite;
  77. Vector<String> names = keyFrameElem.GetAttribute("sprite").Split('@');
  78. if (names.Size() == 1)
  79. sprite = cache->GetResource<Sprite2D>(names[0]);
  80. else if (names.Size() == 2)
  81. {
  82. SpriteSheet2D* spriteSheet = cache->GetResource<SpriteSheet2D>(names[0], false);
  83. // If sprite sheet not found, try get in current directory
  84. if (!spriteSheet)
  85. spriteSheet = cache->GetResource<SpriteSheet2D>(GetParentPath(GetName()) + names[0]);
  86. if (!spriteSheet)
  87. {
  88. LOGERROR("Could not get sprite speet");
  89. return false;
  90. }
  91. sprite = spriteSheet->GetSprite(names[1]);
  92. }
  93. if (!sprite)
  94. {
  95. LOGERROR("Could not get sprite");
  96. return false;
  97. }
  98. frameSprites_.Push(sprite);
  99. keyFrameElem = keyFrameElem.GetNext("frame");
  100. }
  101. return true;
  102. }
  103. bool Animation2D::Save(Serializer& dest) const
  104. {
  105. XMLFile xmlFile(context_);
  106. XMLElement rootElem = xmlFile.CreateRoot("animation");
  107. float endTime = 0.0f;
  108. for (unsigned i = 0; i < frameSprites_.Size(); ++i)
  109. {
  110. XMLElement frameElem = rootElem.CreateChild("frame");
  111. frameElem.SetFloat("duration", frameEndTimes_[i] - endTime);
  112. endTime = frameEndTimes_[i];
  113. Sprite2D* sprite = frameSprites_[i];
  114. SpriteSheet2D* spriteSheet = sprite->GetSpriteSheet();
  115. if (!spriteSheet)
  116. frameElem.SetString("sprite", sprite->GetName());
  117. else
  118. frameElem.SetString("sprite", spriteSheet->GetName() + "@" + sprite->GetName());
  119. }
  120. return xmlFile.Save(dest);
  121. }
  122. float Animation2D::GetTotalTime() const
  123. {
  124. return frameEndTimes_.Empty() ? 0.0f : frameEndTimes_.Back();
  125. }
  126. unsigned Animation2D::GetNumFrames() const
  127. {
  128. return frameSprites_.Size();
  129. }
  130. Sprite2D* Animation2D::GetFrameSprite(unsigned index) const
  131. {
  132. if (index < frameSprites_.Size())
  133. return frameSprites_[index];
  134. return 0;
  135. }
  136. Sprite2D* Animation2D::GetFrameSpriteByTime(float time) const
  137. {
  138. if (time < 0.0f)
  139. return 0;
  140. for (unsigned i = 0; i < frameEndTimes_.Size(); ++i)
  141. {
  142. if (time <= frameEndTimes_[i])
  143. return frameSprites_[i];
  144. }
  145. return 0;
  146. }
  147. }