diff --git a/applications/present3D/deprecated/present3D.cpp b/applications/present3D/deprecated/present3D.cpp index aefecb6b4..12fb2c3f1 100644 --- a/applications/present3D/deprecated/present3D.cpp +++ b/applications/present3D/deprecated/present3D.cpp @@ -133,17 +133,44 @@ void setViewer(osgViewer::Viewer& viewer, float width, float height, float dista class ForwardToDeviceEventHandler : public osgGA::GUIEventHandler { public: - ForwardToDeviceEventHandler(osgGA::Device* device) : osgGA::GUIEventHandler(), _device(device) {} + ForwardToDeviceEventHandler(osgGA::Device* device, bool format_mouse_events) : osgGA::GUIEventHandler(), _device(device), _forwardMouseEvents(format_mouse_events) {} virtual bool handle (const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa, osg::Object *, osg::NodeVisitor *) { - OSG_INFO<<"ForwardToDeviceEventHandler::setEvent("<sendEvent(ea); + switch (ea.getEventType()) + { + case osgGA::GUIEventAdapter::PUSH: + case osgGA::GUIEventAdapter::RELEASE: + case osgGA::GUIEventAdapter::MOVE: + case osgGA::GUIEventAdapter::DRAG: + case osgGA::GUIEventAdapter::SCROLL: + if (_forwardMouseEvents) + _device->sendEvent(ea); + break; + + default: + _device->sendEvent(ea); + break; + } return false; } + + + bool handle(osgGA::Event* event, osg::Object* object, osg::NodeVisitor* nv) + { + if (event->asGUIEventAdapter()) + return osgGA::GUIEventHandler::handle(event, object, nv); + else + { + _device->sendEvent(*event); + return false; + } + } + private: osg::ref_ptr _device; + bool _forwardMouseEvents; }; @@ -183,7 +210,7 @@ void processLoadedModel(osg::ref_ptr& loadedModel, int optimizer_opti } } -void addDeviceTo(osgViewer::Viewer& viewer, const std::string& device_name) +void addDeviceTo(osgViewer::Viewer& viewer, const std::string& device_name, bool forward_mouse_events) { osg::ref_ptr dev = osgDB::readFile(device_name); if (dev.valid()) @@ -191,8 +218,8 @@ void addDeviceTo(osgViewer::Viewer& viewer, const std::string& device_name) OSG_INFO << "Adding Device : " << device_name << std::endl; viewer.addDevice(dev.get()); - if (dev->getCapabilities() & osgGA::Device::SEND_EVENTS) - viewer.getEventHandlers().push_front(new ForwardToDeviceEventHandler(dev.get())); + if ((dev->getCapabilities() & osgGA::Device::SEND_EVENTS)) + viewer.getEventHandlers().push_front(new ForwardToDeviceEventHandler(dev.get(), forward_mouse_events)); } else { @@ -226,6 +253,8 @@ int main( int argc, char **argv ) arguments.getApplicationUsage()->addCommandLineOption("--html ","Print out slides to a series of html & image files."); arguments.getApplicationUsage()->addCommandLineOption("--loop","Switch on looping of presentation."); arguments.getApplicationUsage()->addCommandLineOption("--devices","Print the Video input capability via QuickTime and exit."); + arguments.getApplicationUsage()->addCommandLineOption("--forwardMouseEvents","forward also mouse/touch-events to the devices"); + arguments.getApplicationUsage()->addCommandLineOption("--suppressEnvTags", "suppresses all found ENV-tags in the presentation"); // add alias from xml to p3d to provide backwards compatibility for old p3d files. osgDB::Registry::instance()->addFileExtensionAlias("xml","p3d"); @@ -238,10 +267,13 @@ int main( int argc, char **argv ) return 1; } + bool suppress_env_tags = false; + if (arguments.read("--suppressEnvTags")) + suppress_env_tags = true; // read any env vars from presentations before we create viewer to make sure the viewer // utilises these env vars - if (p3d::readEnvVars(arguments)) + if (!suppress_env_tags && p3d::readEnvVars(arguments)) { osg::DisplaySettings::instance()->readEnvironmentalVariables(); } @@ -328,6 +360,10 @@ int main( int argc, char **argv ) viewer.readConfiguration(configurationFile); doSetViewer = false; } + + bool forwardMouseEvents = false; + if (arguments.read("--forwardMouseEvents")) + forwardMouseEvents = true; const char* p3dDevice = getenv("P3D_DEVICE"); if (p3dDevice) @@ -336,7 +372,7 @@ int main( int argc, char **argv ) osgDB::split(p3dDevice, devices); for(osgDB::StringList::iterator i = devices.begin(); i != devices.end(); ++i) { - addDeviceTo(viewer, *i); + addDeviceTo(viewer, *i, forwardMouseEvents); } } @@ -344,7 +380,7 @@ int main( int argc, char **argv ) std::string device; while (arguments.read("--device", device)) { - addDeviceTo(viewer, device); + addDeviceTo(viewer, device, forwardMouseEvents); } @@ -658,6 +694,9 @@ int main( int argc, char **argv ) osg::ref_ptr cacheAllOption = new osgDB::ReaderWriter::Options; + if(suppress_env_tags) + cacheAllOption->setPluginStringData("suppressEnvTags", "true"); + cacheAllOption->setObjectCacheHint(osgDB::ReaderWriter::Options::CACHE_ALL); osgDB::Registry::instance()->setOptions(cacheAllOption.get()); diff --git a/include/osgPresentation/deprecated/SlideEventHandler b/include/osgPresentation/deprecated/SlideEventHandler index 41499bf12..5b6e3afb7 100644 --- a/include/osgPresentation/deprecated/SlideEventHandler +++ b/include/osgPresentation/deprecated/SlideEventHandler @@ -110,22 +110,25 @@ struct HomePosition : public virtual osg::Referenced struct KeyPosition { - KeyPosition(unsigned int key=0, float x=FLT_MAX, float y=FLT_MAX): + KeyPosition(unsigned int key=0, float x=FLT_MAX, float y=FLT_MAX, bool forward_to_devices = false): _key((osgGA::GUIEventAdapter::KeySymbol)key), _x(x), - _y(y) {} + _y(y), + _forwardToDevices(forward_to_devices) {} - void set(unsigned int key=0, float x=FLT_MAX, float y=FLT_MAX) + void set(unsigned int key=0, float x=FLT_MAX, float y=FLT_MAX, bool forward_to_devices = false) { _key = (osgGA::GUIEventAdapter::KeySymbol)key; _x = x; _y = y; + _forwardToDevices = forward_to_devices; } osgGA::GUIEventAdapter::KeySymbol _key; float _x; float _y; + bool _forwardToDevices; }; struct LayerCallback : public virtual osg::Referenced @@ -313,6 +316,7 @@ public: bool getLoopPresentation() const { return _loopPresentation; } void dispatchEvent(const KeyPosition& keyPosition); + void forwardEventToDevices(osgGA::Event* event); void setRequestReload(bool flag); bool getRequestReload() const { return _requestReload; } diff --git a/src/osgPlugins/p3d/ReaderWriterP3D.cpp b/src/osgPlugins/p3d/ReaderWriterP3D.cpp index 917a53644..8c03c43d6 100644 --- a/src/osgPlugins/p3d/ReaderWriterP3D.cpp +++ b/src/osgPlugins/p3d/ReaderWriterP3D.cpp @@ -41,6 +41,8 @@ class ReaderWriterP3DXML : public osgDB::ReaderWriter public: ReaderWriterP3DXML() { + supportsOption("suppressEnvTags", "if set to (true|1) all env-tags in the p3d-file will be suppressed"); + _colorMap["WHITE"] .set(1.0f,1.0f,1.0f,1.0f); _colorMap["BLACK"] .set(0.0f,0.0f,0.0f,1.0f); _colorMap["PURPLE"] .set(1.0f,0.0f,1.0f,1.0f); @@ -112,6 +114,7 @@ public: return osgDB::equalCaseInsensitive(extension,"p3d") || osgDB::equalCaseInsensitive(extension,"xml") ; } + virtual ReadResult readNode(const std::string& fileName, const osgDB::ReaderWriter::Options* options) const; @@ -175,7 +178,8 @@ public: inline bool read(const char* str, osg::Vec2& value) const; inline bool read(const char* str, osg::Vec3& value) const; inline bool read(const char* str, osg::Vec4& value) const; - + + inline bool read(const std::string& str, bool& value) const; inline bool read(const std::string& str, int& value) const; inline bool read(const std::string& str, float& value) const; inline bool read(const std::string& str, double& value) const; @@ -186,6 +190,7 @@ public: bool getProperty(osgDB::XmlNode*cur, const char* token) const; bool getKeyProperty(osgDB::XmlNode*cur, const char* token, int& value) const; + bool getProperty(osgDB::XmlNode*cur, const char* token, bool& value) const; bool getProperty(osgDB::XmlNode*cur, const char* token, int& value) const; bool getProperty(osgDB::XmlNode*cur, const char* token, float& value) const; bool getProperty(osgDB::XmlNode*cur, const char* token, double& value) const; @@ -324,6 +329,17 @@ bool ReaderWriterP3DXML::read(const char* str, osg::Vec4& value) const return !iss.fail(); } +bool ReaderWriterP3DXML::read(const std::string& str, bool& value) const +{ + if ((str == "1") || (str == "0")) { + value = (str == "1"); + return true; + } + std::string s(osgDB::convertToLowerCase(str)); + value = (s == "true"); + return true; +} + bool ReaderWriterP3DXML::read(const std::string& str, int& value) const { std::istringstream iss(str); @@ -383,6 +399,13 @@ bool ReaderWriterP3DXML::getProperty(osgDB::XmlNode* cur, const char* token) con return cur->properties.count(token)!=0; } +bool ReaderWriterP3DXML::getProperty(osgDB::XmlNode*cur, const char* token, bool& value) const +{ + osgDB::XmlNode::Properties::iterator itr = cur->properties.find(token); + if (itr==cur->properties.end()) return false; + return read(itr->second,value); +} + bool ReaderWriterP3DXML::getProperty(osgDB::XmlNode*cur, const char* token, int& value) const { osgDB::XmlNode::Properties::iterator itr = cur->properties.find(token); @@ -1426,7 +1449,7 @@ bool ReaderWriterP3DXML::getKeyPosition(osgDB::XmlNode*cur, osgPresentation::Key cur->name == "esc" || cur->name == "exit") { - keyPosition.set(osgGA::GUIEventAdapter::KEY_Escape, 0.0f, 0.0f); + keyPosition.set(osgGA::GUIEventAdapter::KEY_Escape, 0.0f, 0.0f, false); return true; } return false; @@ -1455,7 +1478,9 @@ bool ReaderWriterP3DXML::getKeyPositionInner(osgDB::XmlNode*cur, osgPresentation // v in range 0.0 to 1, from bottom to top y = v*2.0f-1.0f; } - + + bool forward_to_devices = false; + getProperty(cur, "forward_to_devices", forward_to_devices); std::string key = cur->getTrimmedContents(); unsigned int keyValue = 0; @@ -1494,7 +1519,7 @@ bool ReaderWriterP3DXML::getKeyPositionInner(osgDB::XmlNode*cur, osgPresentation return false; } - keyPosition.set(keyValue,x,y); + keyPosition.set(keyValue,x,y, forward_to_devices); return true; } @@ -2855,13 +2880,15 @@ osg::Node* ReaderWriterP3DXML::parseXmlGraph(osgDB::XmlNode* root, bool readOnly osgDB::FilePathList previousPaths = osgDB::getDataFilePathList(); + bool env_tag_suppressed = false || (options && ((options->getPluginStringData("suppressEnvTags") == "1") || (options->getPluginStringData("suppressEnvTags") == "true"))); + for(osgDB::XmlNode::Children::iterator itr = root->children.begin(); itr != root->children.end(); ++itr) { osgDB::XmlNode* cur = itr->get(); - if (cur->name=="env") + if (cur->name=="env" && !env_tag_suppressed) { char* str = strdup(cur->contents.c_str()); OSG_INFO<<"putenv("< " << cloned_ea->getX() << "/" <getY() << std::endl; - - // dispatch cloned event to devices - osgViewer::View::Devices& devices = viewer->getDevices(); - for(osgViewer::View::Devices::iterator i = devices.begin(); i != devices.end(); ++i) - { - if((*i)->getCapabilities() & osgGA::Device::SEND_EVENTS) - { - (*i)->sendEvent(*cloned_ea); - } - } + SlideEventHandler::instance()->forwardEventToDevices(cloned_ea); } else { diff --git a/src/osgPresentation/deprecated/SlideEventHandler.cpp b/src/osgPresentation/deprecated/SlideEventHandler.cpp index c5dd145b6..6889654a7 100644 --- a/src/osgPresentation/deprecated/SlideEventHandler.cpp +++ b/src/osgPresentation/deprecated/SlideEventHandler.cpp @@ -1550,8 +1550,39 @@ void SlideEventHandler::releaseSlide(unsigned int slideNum) _presentationSwitch->getChild(slideNum)->accept(globjVisitor); } +void SlideEventHandler::forwardEventToDevices(osgGA::Event* event) +{ + // dispatch cloned event to devices + osgViewer::View::Devices& devices = _viewer->getDevices(); + for(osgViewer::View::Devices::iterator i = devices.begin(); i != devices.end(); ++i) + { + if((*i)->getCapabilities() & osgGA::Device::SEND_EVENTS) + { + (*i)->sendEvent(*event); + } + } +} + void SlideEventHandler::dispatchEvent(const KeyPosition& keyPosition) { + if (keyPosition._forwardToDevices) + { + osg::ref_ptr event = new osgGA::GUIEventAdapter(); + event->setKey(keyPosition._key); + event->setTime(_viewer->getEventQueue()->getTime()); + + // forward key-down + event->setEventType(osgGA::GUIEventAdapter::KEYDOWN); + forwardEventToDevices(event); + + // forward key-up + event->setEventType(osgGA::GUIEventAdapter::KEYUP); + forwardEventToDevices(event); + + // ignore local event-queue + return; + } + osgGA::EventQueue* eq = _viewer->getEventQueue(); // reset the time of the last key press to ensure that the event is disgarded as a key repeat.