Skip to content

Commit

Permalink
From Stepan Huber, "attached are my changes for the osc, zeroconf and…
Browse files Browse the repository at this point in the history
… resthttp-plugin to use the new event-class. I refactored the osgoscdevice-example so that it’s working again. "
  • Loading branch information
robertosfield committed Nov 8, 2013
1 parent 93c4449 commit 350d09b
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 88 deletions.
136 changes: 69 additions & 67 deletions examples/osgoscdevice/osgoscdevice.cpp
Expand Up @@ -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<osgText::Text> _text;
};
Expand Down Expand Up @@ -195,56 +195,60 @@ class MyValueListVisitor : public osg::ValueObject::GetValueVisitor {
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<osgGA::EventVisitor*>(nv);
osg::View* view = ev ? dynamic_cast<osgViewer::View*>(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<osgViewer::GraphicsWindow*>(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<osgViewer::View*>(&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<osgViewer::GraphicsWindow*>(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<const osg::ValueObject*>(udc->getUserObject(i));
OSG_ALWAYS << " " << vo->getName() << ": ";
const osg::ValueObject* vo = dynamic_cast<const osg::ValueObject*>(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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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);

OSG_ALWAYS << "new osc-service discovered: " << host << ":" << port << std::endl;

std::ostringstream ss ;
ss << host << ":" << port << ".sender.osc";
_device = osgDB::readFile<osgGA::Device>(ss.str());

osgViewer::View* view = dynamic_cast<osgViewer::View*>(&aa);
if (view)
view->addEventHandler(new PickHandler(_device.get()));
return true;
}
std::string host;
unsigned int port;
event->getUserValue("host", host);
event->getUserValue("port", port);

OSG_ALWAYS << "new osc-service discovered: " << host << ":" << port << std::endl;

std::ostringstream ss ;
ss << host << ":" << port << ".sender.osc";
_device = osgDB::readFile<osgGA::Device>(ss.str());

osgGA::EventVisitor* ev = dynamic_cast<osgGA::EventVisitor*>(nv);
osgViewer::View* view = ev ? dynamic_cast<osgViewer::View*>(ev->getActionAdapter()) : NULL;
if (view)
view->addEventHandler(new PickHandler(_device.get()));
return true;
}
return false;
}
Expand Down
3 changes: 1 addition & 2 deletions src/osgPlugins/RestHttpDevice/RestHttpDevice.cpp
Expand Up @@ -27,8 +27,7 @@ class StandardRequestHandler : public RestHttpDevice::RequestHandler {
{
OSG_INFO << "RestHttpDevice :: handling request " << full_request_path << " as user-event" << std::endl;

osg::ref_ptr<osgGA::GUIEventAdapter> event = new osgGA::GUIEventAdapter();
event->setEventType(osgGA::GUIEventAdapter::USER);
osg::ref_ptr<osgGA::Event> event = new osgGA::Event();
event->setName(full_request_path);
event->setTime(getDevice()->getEventQueue()->getTime());

Expand Down
9 changes: 3 additions & 6 deletions src/osgPlugins/ZeroConfDevice/ReaderWriterZeroConfDevice.cpp
Expand Up @@ -43,7 +43,7 @@ class ZeroConfRegisterDevice: public osgGA::Device{
return !(getEventQueue()->empty());
}

virtual void sendEvent(const osgGA::GUIEventAdapter& event)
virtual void sendEvent(const osgGA::Event& event)
{
if (event.getName() == "/zeroconf/advertise")
{
Expand Down Expand Up @@ -96,11 +96,10 @@ class MyDiscoveredServicesCallback : public DiscoveredServicesCallback {
virtual bool ignoreIP6Addresses() { return true; }
virtual void serviceAdded(const std::string& host, unsigned int port)
{
osg::ref_ptr<osgGA::GUIEventAdapter> event = new osgGA::GUIEventAdapter();
osg::ref_ptr<osgGA::Event> 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);
Expand All @@ -112,12 +111,10 @@ class MyDiscoveredServicesCallback : public DiscoveredServicesCallback {

virtual void serviceRemoved(const std::string& host, unsigned int port)
{
osg::ref_ptr<osgGA::GUIEventAdapter> event = new osgGA::GUIEventAdapter();
osg::ref_ptr<osgGA::Event> 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);
Expand Down
4 changes: 2 additions & 2 deletions src/osgPlugins/osc/OscReceivingDevice.cpp
Expand Up @@ -96,7 +96,7 @@ class StandardRequestHandler : public OscReceivingDevice::RequestHandler {

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";
}
Expand Down Expand Up @@ -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<osgGA::GUIEventAdapter> ea = getDevice()->getOrCreateUserDataEvent();
osg::ref_ptr<osgGA::Event> ea = getDevice()->getOrCreateUserDataEvent();
osg::UserDataContainer* udc = ea->getOrCreateUserDataContainer();


Expand Down
7 changes: 3 additions & 4 deletions src/osgPlugins/osc/OscReceivingDevice.hpp
Expand Up @@ -87,12 +87,11 @@ class OscReceivingDevice : public osgGA::Device, OpenThreads::Thread, osc::OscPa
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();
}
Expand All @@ -104,7 +103,7 @@ class OscReceivingDevice : public osgGA::Device, OpenThreads::Thread, osc::OscPa
unsigned int _listeningPort;
UdpListeningReceiveSocket* _socket;
RequestHandlerMap _map;
osg::ref_ptr<osgGA::GUIEventAdapter> _userDataEvent;
osg::ref_ptr<osgGA::Event> _userDataEvent;
MsgIdType _lastMsgId;
osg::Timer_t _lastMsgTimeStamp;

Expand Down
39 changes: 34 additions & 5 deletions src/osgPlugins/osc/OscSendingDevice.cpp
Expand Up @@ -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);
}
Expand All @@ -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())
Expand Down Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/osgPlugins/osc/OscSendingDevice.hpp
Expand Up @@ -25,11 +25,12 @@ class OscSendingDevice : public osgGA::Device {
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);
Expand Down

0 comments on commit 350d09b

Please sign in to comment.