AnimationUpdateCallback 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* -*-c++-*-
  2. * Copyright (C) 2009 Cedric Pinson <cedric.pinson@plopbyte.net>
  3. *
  4. * This library is open source and may be redistributed and/or modified under
  5. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  6. * (at your option) any later version. The full license is in LICENSE file
  7. * included with this distribution, and on the openscenegraph.org website.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * OpenSceneGraph Public License for more details.
  13. */
  14. #ifndef OSGANIMATION_ANIMATION_UPDATE_CALLBACK
  15. #define OSGANIMATION_ANIMATION_UPDATE_CALLBACK 1
  16. #include <osg/Object>
  17. #include <osgAnimation/Channel>
  18. #include <osgAnimation/Animation>
  19. #include <string>
  20. namespace osgAnimation
  21. {
  22. class AnimationUpdateCallbackBase : public virtual osg::Object
  23. {
  24. public:
  25. virtual bool link(Channel* channel) = 0;
  26. virtual int link(Animation* animation) = 0;
  27. };
  28. template <class T>
  29. class AnimationUpdateCallback : public AnimationUpdateCallbackBase, public T
  30. {
  31. public:
  32. AnimationUpdateCallback() {}
  33. AnimationUpdateCallback(const std::string& name) { T::setName(name);}
  34. AnimationUpdateCallback(const AnimationUpdateCallback& apc,const osg::CopyOp& copyop): T(apc, copyop) {}
  35. META_Object(osgAnimation, AnimationUpdateCallback<T>);
  36. virtual osg::Callback* asCallback() { return T::asCallback(); }
  37. virtual const osg::Callback* asCallback() const { return T::asCallback(); }
  38. virtual osg::CallbackObject* asCallbackObject() { return T::asCallbackObject(); }
  39. virtual const osg::CallbackObject* asCallbackObject() const { return T::asCallbackObject(); }
  40. const std::string& getName() const { return T::getName(); }
  41. bool link(Channel* /*channel*/) { return 0; }
  42. int link(Animation* animation)
  43. {
  44. if (T::getName().empty())
  45. {
  46. osg::notify(osg::WARN) << "An update callback has no name, it means it could link only with \"\" named Target, often an error, discard" << std::endl;
  47. return 0;
  48. }
  49. int nbLinks = 0;
  50. for (ChannelList::iterator it = animation->getChannels().begin();
  51. it != animation->getChannels().end();
  52. ++it)
  53. {
  54. std::string targetName = (*it)->getTargetName();
  55. if (targetName == T::getName())
  56. {
  57. AnimationUpdateCallbackBase* a = this;
  58. a->link((*it).get());
  59. nbLinks++;
  60. }
  61. }
  62. return nbLinks;
  63. }
  64. };
  65. }
  66. #endif