| | 18 | |
| | 19 | class MovieEventHandler : public osgGA::GUIEventHandler |
| | 20 | { |
| | 21 | public: |
| | 22 | |
| | 23 | MovieEventHandler() {} |
| | 24 | |
| | 25 | void set(osg::Node* node); |
| | 26 | |
| | 27 | virtual void accept(osgGA::GUIEventHandlerVisitor& v) { v.visit(*this); } |
| | 28 | |
| | 29 | virtual bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&); |
| | 30 | |
| | 31 | virtual void getUsage(osg::ApplicationUsage& usage) const; |
| | 32 | |
| | 33 | typedef std::vector< osg::ref_ptr<osg::ImageStream> > ImageStreamList; |
| | 34 | |
| | 35 | protected: |
| | 36 | |
| | 37 | virtual ~MovieEventHandler() {} |
| | 38 | |
| | 39 | class FindImageStreamsVisitor : public osg::NodeVisitor |
| | 40 | { |
| | 41 | public: |
| | 42 | FindImageStreamsVisitor(ImageStreamList& imageStreamList): |
| | 43 | _imageStreamList(imageStreamList) {} |
| | 44 | |
| | 45 | virtual void apply(osg::Geode& geode) |
| | 46 | { |
| | 47 | apply(geode.getStateSet()); |
| | 48 | |
| | 49 | for(unsigned int i=0;i<geode.getNumDrawables();++i) |
| | 50 | { |
| | 51 | apply(geode.getDrawable(i)->getStateSet()); |
| | 52 | } |
| | 53 | |
| | 54 | traverse(geode); |
| | 55 | } |
| | 56 | |
| | 57 | virtual void apply(osg::Node& node) |
| | 58 | { |
| | 59 | apply(node.getStateSet()); |
| | 60 | traverse(node); |
| | 61 | } |
| | 62 | |
| | 63 | inline void apply(osg::StateSet* stateset) |
| | 64 | { |
| | 65 | if (!stateset) return; |
| | 66 | |
| | 67 | osg::StateAttribute* attr = stateset->getTextureAttribute(0,osg::StateAttribute::TEXTURE); |
| | 68 | if (attr) |
| | 69 | { |
| | 70 | osg::Texture2D* texture2D = dynamic_cast<osg::Texture2D*>(attr); |
| | 71 | if (texture2D) apply(dynamic_cast<osg::ImageStream*>(texture2D->getImage())); |
| | 72 | |
| | 73 | osg::TextureRectangle* textureRec = dynamic_cast<osg::TextureRectangle*>(attr); |
| | 74 | if (textureRec) apply(dynamic_cast<osg::ImageStream*>(textureRec->getImage())); |
| | 75 | } |
| | 76 | } |
| | 77 | |
| | 78 | inline void apply(osg::ImageStream* imagestream) |
| | 79 | { |
| | 80 | if (imagestream) _imageStreamList.push_back(imagestream); |
| | 81 | } |
| | 82 | |
| | 83 | ImageStreamList& _imageStreamList; |
| | 84 | }; |
| | 85 | |
| | 86 | |
| | 87 | ImageStreamList _imageStreamList; |
| | 88 | |
| | 89 | }; |
| | 90 | |
| | 91 | |
| | 92 | |
| | 93 | void MovieEventHandler::set(osg::Node* node) |
| | 94 | { |
| | 95 | _imageStreamList.clear(); |
| | 96 | if (node) |
| | 97 | { |
| | 98 | FindImageStreamsVisitor fisv(_imageStreamList); |
| | 99 | node->accept(fisv); |
| | 100 | } |
| | 101 | } |
| | 102 | |
| | 103 | |
| | 104 | bool MovieEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter&) |
| | 105 | { |
| | 106 | switch(ea.getEventType()) |
| | 107 | { |
| | 108 | case(osgGA::GUIEventAdapter::KEYDOWN): |
| | 109 | { |
| | 110 | if (ea.getKey()=='s') |
| | 111 | { |
| | 112 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
| | 113 | itr!=_imageStreamList.end(); |
| | 114 | ++itr) |
| | 115 | { |
| | 116 | std::cout<<"Play"<<std::endl; |
| | 117 | (*itr)->play(); |
| | 118 | } |
| | 119 | return true; |
| | 120 | } |
| | 121 | else if (ea.getKey()=='p') |
| | 122 | { |
| | 123 | for(ImageStreamList::iterator itr=_imageStreamList.begin(); |
| | 124 | itr!=_imageStreamList.end(); |
| | 125 | ++itr) |
| | 126 | { |
| | 127 | std::cout<<"Pause"<<std::endl; |
| | 128 | (*itr)->pause(); |
| | 129 | } |
| | 130 | return true; |
| | 131 | } |
| | 132 | else if (ea.getKey()=='r') |
| | 133 | { |
| | 134 | return true; |
| | 135 | } |
| | 136 | else if (ea.getKey()=='l') |
| | 137 | { |
| | 138 | return true; |
| | 139 | } |
| | 140 | return false; |
| | 141 | } |
| | 142 | |
| | 143 | default: |
| | 144 | return false; |
| | 145 | } |
| | 146 | } |
| | 147 | |
| | 148 | void MovieEventHandler::getUsage(osg::ApplicationUsage& usage) const |
| | 149 | { |
| | 150 | usage.addKeyboardMouseBinding("p","Pause movie"); |
| | 151 | usage.addKeyboardMouseBinding("s","Play movie"); |
| | 152 | usage.addKeyboardMouseBinding("r","Start movie"); |
| | 153 | usage.addKeyboardMouseBinding("l","Toggle looping of movie"); |
| | 154 | } |
| | 155 | |