SceneObject.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 "AttributeAnimation.h"
  24. #include "Log.h"
  25. #include "Scene.h"
  26. #include "SceneEvents.h"
  27. #include "SceneObject.h"
  28. #include "DebugNew.h"
  29. namespace Urho3D
  30. {
  31. SceneObject::SceneObject(Context* context) :
  32. Serializable(context),
  33. animationTime_(0.0f)
  34. {
  35. }
  36. SceneObject::~SceneObject()
  37. {
  38. }
  39. void SceneObject::SetAttributeAnimation(const String& name, AttributeAnimation* animation)
  40. {
  41. if (!animation)
  42. {
  43. for (HashMap<const AttributeInfo*, SharedPtr<AttributeAnimation> >::Iterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
  44. {
  45. if (i->first_->name_.Contains(name, true))
  46. {
  47. attributeAnimations_.Erase(i);
  48. break;
  49. }
  50. }
  51. if (attributeAnimations_.Empty())
  52. UnsubscribeFromEvent(GetScene(), E_SCENEPOSTUPDATE);
  53. }
  54. else
  55. {
  56. const Vector<AttributeInfo>* attributes = GetAttributes();
  57. if (!attributes)
  58. {
  59. LOGERROR(GetTypeName() + " has no attributes");
  60. return;
  61. }
  62. for (Vector<AttributeInfo>::ConstIterator i = attributes->Begin(); i != attributes->End(); ++i)
  63. {
  64. const AttributeInfo* attributeInfo = &(*i);
  65. if (!attributeInfo->name_.Compare(name, true))
  66. {
  67. // Check that the new value's type matches the attribute type
  68. if (animation->GetValueType() == attributeInfo->type_)
  69. {
  70. if (attributeAnimations_.Empty())
  71. {
  72. // Reset anmation time
  73. animationTime_ = 0.0f;
  74. SubscribeToEvent(GetScene(), E_SCENEPOSTUPDATE, HANDLER(SceneObject, HandleScenePostUpdate));
  75. }
  76. attributeAnimations_[attributeInfo] = SharedPtr<AttributeAnimation>(animation);
  77. return;
  78. }
  79. }
  80. }
  81. LOGERROR("Could not find attribute " + name + " in " + GetTypeName());
  82. }
  83. }
  84. AttributeAnimation* SceneObject::GetAttributeAnimation(const String& name) const
  85. {
  86. for (HashMap<const AttributeInfo*, SharedPtr<AttributeAnimation> >::ConstIterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
  87. {
  88. if (i->first_->name_.Contains(name, true))
  89. return i->second_;
  90. }
  91. return 0;
  92. }
  93. void SceneObject::UpdateAttributeAnimations(float timeStep)
  94. {
  95. animationTime_ += timeStep;
  96. for (HashMap<const AttributeInfo*, SharedPtr<AttributeAnimation> >::Iterator i = attributeAnimations_.Begin(); i != attributeAnimations_.End(); ++i)
  97. {
  98. Variant animationValue;
  99. i->second_->GetValue(animationTime_, animationValue);
  100. OnSetAttribute(*i->first_, animationValue);
  101. }
  102. }
  103. void SceneObject::HandleScenePostUpdate(StringHash eventType, VariantMap& eventData)
  104. {
  105. using namespace ScenePostUpdate;
  106. UpdateAttributeAnimations(eventData[P_TIMESTEP].GetFloat());
  107. }
  108. }