From 829cd9718af6fdc81ab664ab0367d22d407b0c08 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Fri, 8 Nov 2013 12:28:51 +0000 Subject: [PATCH] =?UTF-8?q?From=20Stepan=20Huber,=20"attached=20are=20my?= =?UTF-8?q?=20changes=20for=20the=20osc,=20zeroconf=20and=20resthttp-plugi?= =?UTF-8?q?n=20to=20use=20the=20new=20event-class.=20I=20refactored=20the?= =?UTF-8?q?=20osgoscdevice-example=20so=20that=20it=E2=80=99s=20working=20?= =?UTF-8?q?again.=20"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/osgoscdevice/osgoscdevice.cpp | 132 +++++++++--------- .../RestHttpDevice/RestHttpDevice.cpp | 3 +- .../ReaderWriterZeroConfDevice.cpp | 9 +- src/osgPlugins/osc/OscReceivingDevice.cpp | 4 +- src/osgPlugins/osc/OscReceivingDevice.hpp | 7 +- src/osgPlugins/osc/OscSendingDevice.cpp | 39 +++++- src/osgPlugins/osc/OscSendingDevice.hpp | 5 +- 7 files changed, 113 insertions(+), 86 deletions(-) diff --git a/examples/osgoscdevice/osgoscdevice.cpp b/examples/osgoscdevice/osgoscdevice.cpp index f982aadcc..80bea179d 100644 --- a/examples/osgoscdevice/osgoscdevice.cpp +++ b/examples/osgoscdevice/osgoscdevice.cpp @@ -154,14 +154,14 @@ void PickHandler::pick(osgViewer::View* view, const osgGA::GUIEventAdapter& ea) } -class UserEventHandler : public osgGA::GUIEventHandler { +class UserEventHandler : public osgGA::EventHandler { public: - UserEventHandler(osgText::Text* text) : osgGA::GUIEventHandler(), _text(text) {} + UserEventHandler(osgText::Text* text) : osgGA::EventHandler(), _text(text) {} ~UserEventHandler() {} - bool handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa); + virtual bool handle(osgGA::Event* event, osg::Object* object, osg::NodeVisitor* nv); private: osg::ref_ptr _text; }; @@ -195,56 +195,60 @@ private: std::ostringstream _ss; }; -bool UserEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& aa) +bool UserEventHandler::handle(osgGA::Event* event, osg::Object* object, osg::NodeVisitor* nv) { - if (ea.getEventType() == osgGA::GUIEventAdapter::USER) { - OSG_ALWAYS << "handle user-event: " << ea.getName() << std::endl; + + OSG_ALWAYS << "handle user-event: " << event->getName() << std::endl; - if (ea.getName() == "/pick-result") + if (event->getName() == "/pick-result") + { + std::string name(""); + float x(0), y(0); + event->getUserValue("name", name); + event->getUserValue("x", x); + event->getUserValue("y", y); + std::ostringstream ss; + ss << "Name: " << std::endl << name << std::endl << std::endl; + ss << "x: " << y << " y: " << y << std::endl; + + _text->setText(ss.str()); + + return true; + } + else if(event->getName() == "/osgga") + { + osg::Vec4 rect; + event->getUserValue("resize", rect); + osgGA::EventVisitor* ev = dynamic_cast(nv); + osg::View* view = ev ? dynamic_cast(ev->getActionAdapter()) : NULL; + if (view && (rect[2] > 0) && (rect[3] > 0)) { - std::string name(""); - float x(0), y(0); - ea.getUserValue("name", name); - ea.getUserValue("x", x); - ea.getUserValue("y", y); - std::ostringstream ss; - ss << "Name: " << std::endl << name << std::endl << std::endl; - ss << "x: " << y << " y: " << y << std::endl; - - _text->setText(ss.str()); + OSG_ALWAYS << "resizing view to " << rect << std::endl; + osgViewer::GraphicsWindow* win = view->getCamera()->getGraphicsContext() ? dynamic_cast(view->getCamera()->getGraphicsContext()) : NULL; + if (win) + win->setWindowRectangle(rect[2] + 10 + rect[0], rect[1], rect[2], rect[3]); } - else if(ea.getName() == "/osgga") + + return true; + } + else { + const osg::UserDataContainer* udc = event->getUserDataContainer(); + if (udc) { - osg::Vec4 rect; - ea.getUserValue("resize", rect); - osg::View* view = dynamic_cast(&aa); - if (view && (rect[2] > 0) && (rect[3] > 0)) + OSG_ALWAYS << "contents of " << udc->getName() << ": " << std::endl; + for(unsigned int i = 0; i < udc->getNumUserObjects(); ++i) { - OSG_ALWAYS << "resizing view to " << rect << std::endl; - osgViewer::GraphicsWindow* win = view->getCamera()->getGraphicsContext() ? dynamic_cast(view->getCamera()->getGraphicsContext()) : NULL; - if (win) - win->setWindowRectangle(rect[2] + 10 + rect[0], rect[1], rect[2], rect[3]); - } - } - else { - const osg::UserDataContainer* udc = ea.getUserDataContainer(); - if (udc) - { - OSG_ALWAYS << "contents of " << udc->getName() << ": " << std::endl; - for(unsigned int i = 0; i < udc->getNumUserObjects(); ++i) - { - const osg::ValueObject* vo = dynamic_cast(udc->getUserObject(i)); - OSG_ALWAYS << " " << vo->getName() << ": "; + const osg::ValueObject* vo = dynamic_cast(udc->getUserObject(i)); + OSG_ALWAYS << " " << vo->getName() << ": "; - MyValueListVisitor vlv; - vo->get(vlv); - OSG_ALWAYS << vlv.value() << std::endl; - } + MyValueListVisitor vlv; + vo->get(vlv); + OSG_ALWAYS << vlv.value() << std::endl; } } return true; } - + return false; } @@ -336,13 +340,13 @@ osg::Node* createHUD() } -class ForwardToDeviceEventHandler : public osgGA::GUIEventHandler { +class ForwardToDeviceEventHandler : public osgGA::EventHandler { public: - ForwardToDeviceEventHandler(osgGA::Device* device) : osgGA::GUIEventHandler(), _device(device) {} + ForwardToDeviceEventHandler(osgGA::Device* device) : osgGA::EventHandler(), _device(device) {} - virtual bool handle (const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa, osg::Object *, osg::NodeVisitor *) + virtual bool handle(osgGA::Event* event, osg::Object* object, osg::NodeVisitor* nv) { - _device->sendEvent(ea); + _device->sendEvent(*event); return false; } @@ -354,31 +358,29 @@ class OscServiceDiscoveredEventHandler: public ForwardToDeviceEventHandler { public: OscServiceDiscoveredEventHandler() : ForwardToDeviceEventHandler(NULL) {} - virtual bool handle (const osgGA::GUIEventAdapter &ea, osgGA::GUIActionAdapter &aa, osg::Object *o, osg::NodeVisitor *nv) + virtual bool handle(osgGA::Event* event, osg::Object* object, osg::NodeVisitor* nv) { if (_device.valid()) - return ForwardToDeviceEventHandler::handle(ea, aa, o, nv); + return ForwardToDeviceEventHandler::handle(event, object, nv); - if (ea.getEventType() == osgGA::GUIEventAdapter::USER) + if (event->getName() == "/zeroconf/service-added") { - if (ea.getName() == "/zeroconf/service-added") - { - std::string host; - unsigned int port; - ea.getUserValue("host", host); - ea.getUserValue("port", port); + std::string host; + unsigned int port; + event->getUserValue("host", host); + event->getUserValue("port", port); - OSG_ALWAYS << "new osc-service discovered: " << host << ":" << port << std::endl; + OSG_ALWAYS << "new osc-service discovered: " << host << ":" << port << std::endl; - std::ostringstream ss ; - ss << host << ":" << port << ".sender.osc"; - _device = osgDB::readFile(ss.str()); - - osgViewer::View* view = dynamic_cast(&aa); - if (view) - view->addEventHandler(new PickHandler(_device.get())); - return true; - } + std::ostringstream ss ; + ss << host << ":" << port << ".sender.osc"; + _device = osgDB::readFile(ss.str()); + + osgGA::EventVisitor* ev = dynamic_cast(nv); + osgViewer::View* view = ev ? dynamic_cast(ev->getActionAdapter()) : NULL; + if (view) + view->addEventHandler(new PickHandler(_device.get())); + return true; } return false; } diff --git a/src/osgPlugins/RestHttpDevice/RestHttpDevice.cpp b/src/osgPlugins/RestHttpDevice/RestHttpDevice.cpp index 1743ca81b..d3a380458 100644 --- a/src/osgPlugins/RestHttpDevice/RestHttpDevice.cpp +++ b/src/osgPlugins/RestHttpDevice/RestHttpDevice.cpp @@ -27,8 +27,7 @@ public: { OSG_INFO << "RestHttpDevice :: handling request " << full_request_path << " as user-event" << std::endl; - osg::ref_ptr event = new osgGA::GUIEventAdapter(); - event->setEventType(osgGA::GUIEventAdapter::USER); + osg::ref_ptr event = new osgGA::Event(); event->setName(full_request_path); event->setTime(getDevice()->getEventQueue()->getTime()); diff --git a/src/osgPlugins/ZeroConfDevice/ReaderWriterZeroConfDevice.cpp b/src/osgPlugins/ZeroConfDevice/ReaderWriterZeroConfDevice.cpp index e18ff30b8..6519cc400 100644 --- a/src/osgPlugins/ZeroConfDevice/ReaderWriterZeroConfDevice.cpp +++ b/src/osgPlugins/ZeroConfDevice/ReaderWriterZeroConfDevice.cpp @@ -43,7 +43,7 @@ public: return !(getEventQueue()->empty()); } - virtual void sendEvent(const osgGA::GUIEventAdapter& event) + virtual void sendEvent(const osgGA::Event& event) { if (event.getName() == "/zeroconf/advertise") { @@ -96,11 +96,10 @@ public: virtual bool ignoreIP6Addresses() { return true; } virtual void serviceAdded(const std::string& host, unsigned int port) { - osg::ref_ptr event = new osgGA::GUIEventAdapter(); + osg::ref_ptr event = new osgGA::Event(); OSG_NOTICE << "ZeroConfDevice :: serviceAdded: " << host << ":" << port << " event " << event << std::endl; - event->setEventType(osgGA::GUIEventAdapter::USER); event->setName("/zeroconf/service-added"); event->setUserValue("host", host); @@ -112,12 +111,10 @@ public: virtual void serviceRemoved(const std::string& host, unsigned int port) { - osg::ref_ptr event = new osgGA::GUIEventAdapter(); + osg::ref_ptr event = new osgGA::Event(); OSG_NOTICE << "ZeroConfDevice :: serviceRemoved: " << host << ":" << port << " event " << event << std::endl; - event->setEventType(osgGA::GUIEventAdapter::USER); - event->setName("/zeroconf/service-removed"); event->setUserValue("host", host); event->setUserValue("port", port); diff --git a/src/osgPlugins/osc/OscReceivingDevice.cpp b/src/osgPlugins/osc/OscReceivingDevice.cpp index 2af939c08..d6bc22539 100644 --- a/src/osgPlugins/osc/OscReceivingDevice.cpp +++ b/src/osgPlugins/osc/OscReceivingDevice.cpp @@ -96,7 +96,7 @@ public: virtual void describeTo(std::ostream& out) const { - out << getRequestPath() << ": add all transmitted arguments as ValueObjects to an USER-event"; + out << getRequestPath() << ": add all transmitted arguments as ValueObjects to an event"; if (_treatFirstArgumentAsValueName) out << ", the first argument is used as the name of the value, if it's a string"; } @@ -142,7 +142,7 @@ bool StandardRequestHandler::operator()(const std::string& request_path, const s std::string path = osgDB::getFilePath(full_request_path); std::string last_elem = osgDB::getSimpleFileName(full_request_path); - osg::ref_ptr ea = getDevice()->getOrCreateUserDataEvent(); + osg::ref_ptr ea = getDevice()->getOrCreateUserDataEvent(); osg::UserDataContainer* udc = ea->getOrCreateUserDataContainer(); diff --git a/src/osgPlugins/osc/OscReceivingDevice.hpp b/src/osgPlugins/osc/OscReceivingDevice.hpp index 33ef33c4c..5c814d0f3 100644 --- a/src/osgPlugins/osc/OscReceivingDevice.hpp +++ b/src/osgPlugins/osc/OscReceivingDevice.hpp @@ -87,12 +87,11 @@ public: return out; } - osgGA::GUIEventAdapter* getOrCreateUserDataEvent() + osgGA::Event* getOrCreateUserDataEvent() { if (!_userDataEvent.valid()) { - _userDataEvent = new osgGA::GUIEventAdapter(); - _userDataEvent->setEventType(osgGA::GUIEventAdapter::USER); + _userDataEvent = new osgGA::Event(); } return _userDataEvent.get(); } @@ -104,7 +103,7 @@ private: unsigned int _listeningPort; UdpListeningReceiveSocket* _socket; RequestHandlerMap _map; - osg::ref_ptr _userDataEvent; + osg::ref_ptr _userDataEvent; MsgIdType _lastMsgId; osg::Timer_t _lastMsgTimeStamp; diff --git a/src/osgPlugins/osc/OscSendingDevice.cpp b/src/osgPlugins/osc/OscSendingDevice.cpp index f5e191cf0..f7c35585e 100644 --- a/src/osgPlugins/osc/OscSendingDevice.cpp +++ b/src/osgPlugins/osc/OscSendingDevice.cpp @@ -47,17 +47,19 @@ OscSendingDevice::~OscSendingDevice() delete[] (_buffer); } -void OscSendingDevice::sendEvent(const osgGA::GUIEventAdapter &ea) +void OscSendingDevice::sendEvent(const osgGA::Event &ea) { static osc::int64 msg_id(0); bool msg_sent(false); unsigned int num_messages = _numMessagesPerEvent; - if((ea.getEventType() == osgGA::GUIEventAdapter::DRAG) || (ea.getEventType() == osgGA::GUIEventAdapter::MOVE)) + const osgGA::GUIEventAdapter* ui_event(ea.asGUIEventAdapter()); + + if(ui_event && ((ui_event->getEventType() == osgGA::GUIEventAdapter::DRAG) || (ui_event->getEventType() == osgGA::GUIEventAdapter::MOVE))) num_messages = 1; for(unsigned int i = 0; i < num_messages; ++i) { - msg_sent = sendEventImpl(ea, msg_id); + msg_sent = ui_event ? sendUIEventImpl(*ui_event, msg_id) : sendEventImpl(ea, msg_id); if ((_delayBetweenSendsInMilliSecs > 0) && (i < num_messages-1)) OpenThreads::Thread::microSleep(1000 * _delayBetweenSendsInMilliSecs); } @@ -66,7 +68,34 @@ void OscSendingDevice::sendEvent(const osgGA::GUIEventAdapter &ea) } -bool OscSendingDevice::sendEventImpl(const osgGA::GUIEventAdapter &ea, MsgIdType msg_id) +bool OscSendingDevice::sendEventImpl(const osgGA::Event &ea, MsgIdType msg_id) +{ + bool do_send(false); + if (ea.getUserDataContainer()) + { + std::string key = ea.getUserDataContainer()->getName(); + if (key.empty()) key = ea.getName(); + if (key.empty()) key = "user_data"; + + sendUserDataContainer(transliterateKey(key), ea.getUserDataContainer(), true, msg_id); + + do_send = true; + } + + if (do_send) + { + OSG_INFO << "OscDevice :: sending event per OSC " << std::endl; + + _transmitSocket.Send( _oscStream.Data(), _oscStream.Size() ); + _oscStream.Clear(); + } + + return do_send; +} + + + +bool OscSendingDevice::sendUIEventImpl(const osgGA::GUIEventAdapter &ea, MsgIdType msg_id) { bool do_send(false); switch(ea.getEventType()) @@ -189,7 +218,7 @@ bool OscSendingDevice::sendEventImpl(const osgGA::GUIEventAdapter &ea, MsgIdType if (do_send) { - OSG_INFO << "OscDevice :: sending event per OSC " << std::endl; + OSG_INFO << "OscDevice :: sending ui-event per OSC " << std::endl; _transmitSocket.Send( _oscStream.Data(), _oscStream.Size() ); _oscStream.Clear(); diff --git a/src/osgPlugins/osc/OscSendingDevice.hpp b/src/osgPlugins/osc/OscSendingDevice.hpp index ea28c8a7b..fa5a4ac89 100644 --- a/src/osgPlugins/osc/OscSendingDevice.hpp +++ b/src/osgPlugins/osc/OscSendingDevice.hpp @@ -25,11 +25,12 @@ public: typedef osc::int64 MsgIdType; OscSendingDevice(const std::string& address, int port, unsigned int numMessagesPerEvent = 1, unsigned int delay_between_sends_in_millisecs = 0); ~OscSendingDevice(); - virtual void sendEvent(const osgGA::GUIEventAdapter &ea); + virtual void sendEvent(const osgGA::Event &ea); virtual const char* className() const { return "OSC sending device"; } private: - bool sendEventImpl(const osgGA::GUIEventAdapter &ea,MsgIdType msg_id); + bool sendEventImpl(const osgGA::Event &ea,MsgIdType msg_id); + bool sendUIEventImpl(const osgGA::GUIEventAdapter &ea,MsgIdType msg_id); void beginBundle(MsgIdType msg_id); void beginSendInputRange(const osgGA::GUIEventAdapter& ea, MsgIdType msg_id); int getButtonNum(const osgGA::GUIEventAdapter& ea);