Animation2D.cpp 4.5 KB

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