SlideEventHandler 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2018 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef SLIDEEVENTHANDLER
  14. #define SLIDEEVENTHANDLER 1
  15. #include <osg/Switch>
  16. #include <osg/Timer>
  17. #include <osg/ValueObject>
  18. #include <osg/ImageSequence>
  19. #include <osgGA/GUIEventHandler>
  20. #include <osgViewer/Viewer>
  21. #include <osgPresentation/CompileSlideCallback>
  22. #include <osgPresentation/PropertyManager>
  23. namespace osgPresentation
  24. {
  25. // forward declare
  26. class SlideEventHandler;
  27. /// Operations related to click to run/load/key events.
  28. enum Operation
  29. {
  30. RUN,
  31. LOAD,
  32. EVENT,
  33. JUMP,
  34. FORWARD_MOUSE_EVENT,
  35. FORWARD_TOUCH_EVENT
  36. };
  37. struct JumpData : public osg::Object
  38. {
  39. JumpData():
  40. relativeJump(true),
  41. slideNum(0),
  42. layerNum(0) {}
  43. JumpData(bool in_relativeJump, int in_slideNum, int in_layerNum):
  44. relativeJump(in_relativeJump),
  45. slideNum(in_slideNum),
  46. layerNum(in_layerNum) {}
  47. JumpData(const std::string& in_slideName, const std::string& in_layerName):
  48. relativeJump(true),
  49. slideNum(0),
  50. layerNum(0),
  51. slideName(in_slideName),
  52. layerName(in_layerName) {}
  53. JumpData(const JumpData& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
  54. osg::Object(rhs, copyop),
  55. relativeJump(rhs.relativeJump),
  56. slideNum(rhs.slideNum),
  57. layerNum(rhs.layerNum),
  58. slideName(rhs.slideName),
  59. layerName(rhs.layerName) {}
  60. JumpData& operator = (const JumpData& rhs)
  61. {
  62. if (&rhs==this) return *this;
  63. relativeJump = rhs.relativeJump;
  64. slideNum = rhs.slideNum;
  65. layerNum = rhs.layerNum;
  66. slideName = rhs.slideName;
  67. layerName = rhs.layerName;
  68. return *this;
  69. }
  70. META_Object(osgPresentation, JumpData);
  71. bool requiresJump() const
  72. {
  73. if (!slideName.empty() || !layerName.empty()) return true;
  74. return relativeJump ? (slideNum!=0 || layerNum!=0) : true;
  75. }
  76. bool jump(SlideEventHandler* seh) const;
  77. void setRelativeJump(bool flag) { relativeJump = flag; }
  78. bool getRelativeJump() const { return relativeJump; }
  79. void setSlideNum(int num) { slideNum = num; }
  80. int getSlideNum() const { return slideNum; }
  81. void setLayerNum(int num) { layerNum = num; }
  82. int getLayerNum() const { return layerNum; }
  83. void setSlideName(const std::string& name) { slideName = name; }
  84. const std::string& getSlideName() const { return slideName; }
  85. void setLayerName(const std::string& name) { layerName = name; }
  86. const std::string& getLayerName() const { return layerName; }
  87. bool relativeJump;
  88. int slideNum;
  89. int layerNum;
  90. std::string slideName;
  91. std::string layerName;
  92. };
  93. struct HomePosition : public osg::Object
  94. {
  95. HomePosition():
  96. eye(0.0, -1.0, 0.0),
  97. center(0.0, 0.0, 0.0),
  98. up(0.0, 0.0, 1.0) {}
  99. HomePosition(const osg::Vec3& in_eye, const osg::Vec3& in_center, const osg::Vec3& in_up):
  100. eye(in_eye),
  101. center(in_center),
  102. up(in_up) {}
  103. HomePosition(const HomePosition& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
  104. osg::Object(rhs, copyop),
  105. eye(rhs.eye),
  106. center(rhs.center),
  107. up(rhs.up) {}
  108. HomePosition& operator = (const HomePosition& rhs)
  109. {
  110. if (&rhs==this) return *this;
  111. eye = rhs.eye;
  112. center = rhs.center;
  113. up = rhs.up;
  114. return *this;
  115. }
  116. META_Object(osgPresentation, HomePosition);
  117. void setEye(const osg::Vec3d& e) { eye = e; }
  118. const osg::Vec3d& getEye() const { return eye; }
  119. void setCenter(const osg::Vec3d& c) { center = c; }
  120. const osg::Vec3d& getCenter() const { return center; }
  121. void setUp(const osg::Vec3d& u) { up = u; }
  122. const osg::Vec3d& getUp() const { return up; }
  123. osg::Vec3d eye;
  124. osg::Vec3d center;
  125. osg::Vec3d up;
  126. };
  127. struct KeyPosition : public osg::Object
  128. {
  129. KeyPosition(unsigned int key=0, float x=FLT_MAX, float y=FLT_MAX, bool forward_to_devices = false):
  130. _key((osgGA::GUIEventAdapter::KeySymbol)key),
  131. _x(x),
  132. _y(y),
  133. _forwardToDevices(forward_to_devices) {}
  134. KeyPosition(const KeyPosition& rhs, const osg::CopyOp& copyop=osg::CopyOp::SHALLOW_COPY):
  135. osg::Object(rhs, copyop),
  136. _key(rhs._key),
  137. _x(rhs._x),
  138. _y(rhs._y),
  139. _forwardToDevices(rhs._forwardToDevices) {}
  140. META_Object(osgPresentation, KeyPosition);
  141. KeyPosition& operator = (const KeyPosition& rhs)
  142. {
  143. if (&rhs==this) return *this;
  144. _key = rhs._key;
  145. _x = rhs._x;
  146. _y = rhs._y;
  147. _forwardToDevices = rhs._forwardToDevices;
  148. return *this;
  149. }
  150. void set(unsigned int key=0, float x=FLT_MAX, float y=FLT_MAX, bool forward_to_devices = false)
  151. {
  152. _key = (osgGA::GUIEventAdapter::KeySymbol)key;
  153. _x = x;
  154. _y = y;
  155. _forwardToDevices = forward_to_devices;
  156. }
  157. void setKey(int key) { _key = static_cast<osgGA::GUIEventAdapter::KeySymbol>(key); }
  158. int getKey() const { return _key; }
  159. void setX(float x) { _x = x; }
  160. float getX() const { return _x; }
  161. void setY(float y) { _y = y; }
  162. float getY() const { return _y; }
  163. void setForwardToDevices(bool flag) { _forwardToDevices = flag; }
  164. bool getForwardToDevices() const { return _forwardToDevices; }
  165. osgGA::GUIEventAdapter::KeySymbol _key;
  166. float _x;
  167. float _y;
  168. bool _forwardToDevices;
  169. };
  170. struct LayerCallback : public virtual osg::Referenced
  171. {
  172. virtual void operator() (osg::Node* node) const = 0;
  173. };
  174. struct OSGPRESENTATION_EXPORT LayerAttributes : public virtual osg::Referenced
  175. {
  176. LayerAttributes():_duration(0) {}
  177. LayerAttributes(double in_duration):_duration(in_duration) {}
  178. void setDuration(double duration) { _duration = duration; }
  179. double getDuration() const { return _duration; }
  180. typedef std::vector<KeyPosition> Keys;
  181. typedef std::vector<std::string> RunStrings;
  182. void setKeys(const Keys& keys) { _keys = keys; }
  183. const Keys& getKeys() const { return _keys; }
  184. void addKey(const KeyPosition& kp) { _keys.push_back(kp); }
  185. void setRunStrings(const RunStrings& runStrings) { _runStrings = runStrings; }
  186. const RunStrings& getRunStrings() const { return _runStrings; }
  187. void addRunString(const std::string& runString) { _runStrings.push_back(runString); }
  188. void setJump(const JumpData& jumpData) { _jumpData = jumpData; }
  189. const JumpData& getJumpData() const { return _jumpData; }
  190. double _duration;
  191. Keys _keys;
  192. RunStrings _runStrings;
  193. JumpData _jumpData;
  194. void addEnterCallback(LayerCallback* lc) { _enterLayerCallbacks.push_back(lc); }
  195. void addLeaveCallback(LayerCallback* lc) { _leaveLayerCallbacks.push_back(lc); }
  196. void callEnterCallbacks(osg::Node* node);
  197. void callLeaveCallbacks(osg::Node* node);
  198. typedef std::list< osg::ref_ptr<LayerCallback> > LayerCallbacks;
  199. LayerCallbacks _enterLayerCallbacks;
  200. LayerCallbacks _leaveLayerCallbacks;
  201. };
  202. struct FilePathData : public virtual osg::Referenced
  203. {
  204. FilePathData(const osgDB::FilePathList& fpl):filePathList(fpl) {}
  205. osgDB::FilePathList filePathList;
  206. };
  207. struct dereference_less
  208. {
  209. template<class T, class U>
  210. inline bool operator() (const T& lhs,const U& rhs) const
  211. {
  212. return *lhs < *rhs;
  213. }
  214. };
  215. struct ObjectOperator : public osg::Referenced
  216. {
  217. inline bool operator < (const ObjectOperator& rhs) const { return ptr() < rhs.ptr(); }
  218. virtual void* ptr() const = 0;
  219. virtual void enter(SlideEventHandler*) = 0;
  220. virtual void frame(SlideEventHandler*) {} ;
  221. virtual void maintain(SlideEventHandler*) = 0;
  222. virtual void leave(SlideEventHandler*) = 0;
  223. virtual void setPause(SlideEventHandler*, bool pause) = 0;
  224. virtual void reset(SlideEventHandler*) = 0;
  225. virtual ~ObjectOperator() {}
  226. };
  227. class OSGPRESENTATION_EXPORT ActiveOperators
  228. {
  229. public:
  230. ActiveOperators();
  231. ~ActiveOperators();
  232. void collect(osg::Node* incomingNode, osg::NodeVisitor::TraversalMode tm = osg::NodeVisitor::TRAVERSE_ACTIVE_CHILDREN);
  233. void process(SlideEventHandler* seh);
  234. void frame(SlideEventHandler*);
  235. void setPause(SlideEventHandler* seh, bool pause);
  236. bool getPause() const { return _pause; }
  237. void reset(SlideEventHandler* seh);
  238. typedef std::set< osg::ref_ptr<ObjectOperator>, dereference_less > OperatorList;
  239. protected:
  240. void processOutgoing(SlideEventHandler* seh);
  241. void processIncoming(SlideEventHandler* seh);
  242. void processMaintained(SlideEventHandler* seh);
  243. bool _pause;
  244. OperatorList _previous;
  245. OperatorList _current;
  246. OperatorList _outgoing;
  247. OperatorList _incoming;
  248. OperatorList _maintained;
  249. };
  250. class OSGPRESENTATION_EXPORT SlideEventHandler : public osgGA::GUIEventHandler
  251. {
  252. public:
  253. SlideEventHandler(osgViewer::Viewer* viewer=0);
  254. SlideEventHandler(const SlideEventHandler& seh,const osg::CopyOp& copyop);
  255. static SlideEventHandler* instance();
  256. META_Object(osgPresentation,SlideEventHandler);
  257. void set(osg::Node* model);
  258. virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&);
  259. virtual void getUsage(osg::ApplicationUsage& usage) const;
  260. osgViewer::Viewer* getViewer() { return _viewer.get(); }
  261. osg::Switch* getPresentationSwitch() { return _presentationSwitch.get(); }
  262. enum WhichPosition
  263. {
  264. FIRST_POSITION = 0,
  265. LAST_POSITION = -1
  266. };
  267. void compileSlide(unsigned int slideNum);
  268. void releaseSlide(unsigned int slideNum);
  269. unsigned int getNumSlides();
  270. int getActiveSlide() const { return _activeSlide; }
  271. int getActiveLayer() const { return _activeLayer; }
  272. osg::Switch* getSlide(int slideNum);
  273. osg::Node* getLayer(int slideNum, int layerNum);
  274. bool selectSlide(int slideNum,int layerNum=FIRST_POSITION);
  275. bool selectLayer(int layerNum);
  276. bool nextLayerOrSlide();
  277. bool previousLayerOrSlide();
  278. bool nextSlide();
  279. bool previousSlide();
  280. bool nextLayer();
  281. bool previousLayer();
  282. bool home();
  283. void setAutoSteppingActive(bool flag = true) { _autoSteppingActive = flag; }
  284. bool getAutoSteppingActive() const { return _autoSteppingActive; }
  285. void setTimeDelayBetweenSlides(double dt) { _timePerSlide = dt; }
  286. double getTimeDelayBetweenSlides() const { return _timePerSlide; }
  287. double getDuration(const osg::Node* node) const;
  288. double getCurrentTimeDelayBetweenSlides() const;
  289. void setReleaseAndCompileOnEachNewSlide(bool flag) { _releaseAndCompileOnEachNewSlide = flag; }
  290. bool getReleaseAndCompileOnEachNewSlide() const { return _releaseAndCompileOnEachNewSlide; }
  291. void setTimeDelayOnNewSlideWithMovies(float t) { _timeDelayOnNewSlideWithMovies = t; }
  292. float getTimeDelayOnNewSlideWithMovies() const { return _timeDelayOnNewSlideWithMovies; }
  293. void setLoopPresentation(bool loop) { _loopPresentation = loop; }
  294. bool getLoopPresentation() const { return _loopPresentation; }
  295. void dispatchEvent(const KeyPosition& keyPosition);
  296. void dispatchEvent(osgGA::Event* event);
  297. void forwardEventToDevices(osgGA::Event* event);
  298. void setRequestReload(bool flag);
  299. bool getRequestReload() const { return _requestReload; }
  300. double getReferenceTime() const { return _referenceTime; }
  301. virtual bool checkNeedToDoFrame();
  302. protected:
  303. ~SlideEventHandler() {}
  304. bool home(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa);
  305. void updateAlpha(bool, bool, float x, float y);
  306. void updateLight(float x, float y);
  307. void updateOperators();
  308. osg::observer_ptr<osgViewer::Viewer> _viewer;
  309. osg::observer_ptr<osg::Switch> _showSwitch;
  310. int _activePresentation;
  311. osg::observer_ptr<osg::Switch> _presentationSwitch;
  312. int _activeSlide;
  313. osg::observer_ptr<osg::Switch> _slideSwitch;
  314. int _activeLayer;
  315. bool _firstTraversal;
  316. double _referenceTime;
  317. double _previousTime;
  318. double _timePerSlide;
  319. bool _autoSteppingActive;
  320. bool _loopPresentation;
  321. bool _pause;
  322. bool _hold;
  323. bool _updateLightActive;
  324. bool _updateOpacityActive;
  325. float _previousX, _previousY;
  326. bool _cursorOn;
  327. bool _releaseAndCompileOnEachNewSlide;
  328. bool _firstSlideOrLayerChange;
  329. osg::Timer_t _tickAtFirstSlideOrLayerChange;
  330. osg::Timer_t _tickAtLastSlideOrLayerChange;
  331. float _timeDelayOnNewSlideWithMovies;
  332. double _minimumTimeBetweenKeyPresses;
  333. double _timeLastKeyPresses;
  334. ActiveOperators _activeOperators;
  335. osg::ref_ptr<CompileSlideCallback> _compileSlideCallback;
  336. bool _requestReload;
  337. };
  338. }
  339. #endif