Animation2D.cpp 4.5 KB

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