From fa5ff9d1696be09d162afab387144a6621c8c886 Mon Sep 17 00:00:00 2001 From: Robert Osfield Date: Wed, 8 Mar 2006 15:40:02 +0000 Subject: [PATCH] Added EventQueue. --- include/osgGA/EventQueue | 104 ++++++++++++++++ src/osgGA/EventQueue.cpp | 249 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 353 insertions(+) create mode 100644 include/osgGA/EventQueue create mode 100644 src/osgGA/EventQueue.cpp diff --git a/include/osgGA/EventQueue b/include/osgGA/EventQueue new file mode 100644 index 000000000..69dd6d5ab --- /dev/null +++ b/include/osgGA/EventQueue @@ -0,0 +1,104 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#ifndef OSGGA_EVENTQUEUE +#define OSGGA_EVENTQUEUE 1 + +#include + +#include +#include + +#include +#include + +namespace osgGA { + +/** + * EventQueue implementation for collecting and adapting windowing events + */ +class OSGGA_EXPORT EventQueue : public osg::Referenced +{ + public: + + EventQueue(); + + typedef std::list< osg::ref_ptr > Events; + + /** Set events.*/ + void setEvents(Events& events); + + /** Take the entire event queue leaving the EventQueue' event queue empty.*/ + bool takeEvents(Events& events); + + /** Take a copy the entire event queue leaving the EventQueue' event queue intact.*/ + bool copyEvents(Events& events) const; + + /** Add events to end of event queue.*/ + void appendEvents(Events& events); + + /** Add an event to the end of the event queue.*/ + void addEvent(GUIEventAdapter* event); + + + /** method for adapting window resize event, placing this event on the back of the event queue. */ + void windowResize(float Xmin, float Ymin, float Xmax, float Ymax); + + /** method for adapting mouse scroll wheel events, placing this event on the back of the event queue. */ + void mouseScroll(GUIEventAdapter::ScrollingMotion sm); + + /** method for adapting mouse motion events whilst mouse buttons are pressed, placing this event on the back of the event queue.*/ + void mouseMotion(float x, float y); + + /** method for adapting mouse button pressed events, placing this event on the back of the event queue.*/ + void mouseButtonPress(float x, float y, unsigned int button); + + /** method for adapting mouse button release events, placing this event on the back of the event queue.*/ + void mouseButtonRelease(float x, float y, unsigned int button); + + /** method for adapting keyboard press events.*/ + void keyPress(GUIEventAdapter::KeySymbol key); + + /** method for adapting keyboard press events.*/ + void keyRelease(GUIEventAdapter::KeySymbol key); + + /** method for adapting frame events.*/ + void frame(double t); + + + void setStartTick(osg::Timer_t tick) { _startTick = tick; } + osg::Timer_t getStartTick() const { return _startTick; } + + double getTime() const { return osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()); } + + GUIEventAdapter* getCurrentEventState() { return _accumulateEventState.get(); } + const GUIEventAdapter* getCurrentEventState() const { return _accumulateEventState.get(); } + + protected: + + virtual ~EventQueue(); + + /** Prevent unwanted copy operator.*/ + EventQueue& operator = (const EventQueue&) { return *this; } + + osg::ref_ptr _accumulateEventState; + + osg::Timer_t _startTick; + mutable OpenThreads::Mutex _eventQueueMutex; + Events _eventQueue; + +}; + +} + +#endif diff --git a/src/osgGA/EventQueue.cpp b/src/osgGA/EventQueue.cpp new file mode 100644 index 000000000..1b4e3811e --- /dev/null +++ b/src/osgGA/EventQueue.cpp @@ -0,0 +1,249 @@ +/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2005 Robert Osfield + * + * This library is open source and may be redistributed and/or modified under + * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or + * (at your option) any later version. The full license is in LICENSE file + * included with this distribution, and on the openscenegraph.org website. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * OpenSceneGraph Public License for more details. +*/ + +#include +#include + +using namespace osgGA; + +EventQueue::EventQueue() +{ + _startTick = osg::Timer::instance()->tick(); + _accumulateEventState = new GUIEventAdapter(); +} + +EventQueue::~EventQueue() +{ +} + +void EventQueue::setEvents(Events& events) +{ + OpenThreads::ScopedLock lock(_eventQueueMutex); + _eventQueue = events; +} + +void EventQueue::appendEvents(Events& events) +{ + OpenThreads::ScopedLock lock(_eventQueueMutex); + _eventQueue.insert(_eventQueue.end(), events.begin(), events.end()); +} + +void EventQueue::addEvent(GUIEventAdapter* event) +{ + event->setTime(getTime()); + + OpenThreads::ScopedLock lock(_eventQueueMutex); + _eventQueue.push_back(event); +} + +bool EventQueue::takeEvents(Events& events) +{ + OpenThreads::ScopedLock lock(_eventQueueMutex); + if (!_eventQueue.empty()) + { + events.insert(events.end(),_eventQueue.begin(),_eventQueue.end()); + _eventQueue.clear(); + return true; + } + else + { + return false; + } +} + +bool EventQueue::copyEvents(Events& events) const +{ + OpenThreads::ScopedLock lock(_eventQueueMutex); + if (!_eventQueue.empty()) + { + events.insert(events.end(),_eventQueue.begin(),_eventQueue.end()); + return true; + } + else + { + return false; + } +} + + +void EventQueue::windowResize(float Xmin, float Ymin, float Xmax, float Ymax) +{ + _accumulateEventState->setWindowSize(Xmin, Ymin, Xmax, Ymax); + + GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState); + event->setEventType(GUIEventAdapter::RESIZE); + + addEvent(event); +} + +void EventQueue::mouseScroll(GUIEventAdapter::ScrollingMotion sm) +{ + GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState); + event->setEventType(GUIEventAdapter::SCROLL); + event->setScrollingMotion(sm); + + addEvent(event); +} + +void EventQueue::mouseMotion(float x, float y) +{ + _accumulateEventState->setX(x); + _accumulateEventState->setY(y); + + GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState); + event->setEventType(event->getButtonMask() ? GUIEventAdapter::DRAG : GUIEventAdapter::MOVE); + + addEvent(event); +} + +void EventQueue::mouseButtonPress(float x, float y, unsigned int button) +{ + _accumulateEventState->setX(x); + _accumulateEventState->setY(y); + + switch(button) + { + case(1): + _accumulateEventState->setButtonMask(GUIEventAdapter::LEFT_MOUSE_BUTTON | _accumulateEventState->getButtonMask()); + break; + case(2): + _accumulateEventState->setButtonMask(GUIEventAdapter::MIDDLE_MOUSE_BUTTON | _accumulateEventState->getButtonMask()); + break; + case(3): + _accumulateEventState->setButtonMask(GUIEventAdapter::RIGHT_MOUSE_BUTTON | _accumulateEventState->getButtonMask()); + break; + } + + GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState); + event->setEventType(GUIEventAdapter::PUSH); + + switch(button) + { + case(1): + event->setButton(GUIEventAdapter::LEFT_MOUSE_BUTTON); + break; + case(2): + event->setButton(GUIEventAdapter::MIDDLE_MOUSE_BUTTON); + break; + case(3): + event->setButton(GUIEventAdapter::RIGHT_MOUSE_BUTTON); + break; + } + + addEvent(event); +} + +void EventQueue::mouseButtonRelease(float x, float y, unsigned int button) +{ + _accumulateEventState->setX(x); + _accumulateEventState->setY(y); + + switch(button) + { + case(1): + _accumulateEventState->setButtonMask(~GUIEventAdapter::LEFT_MOUSE_BUTTON & _accumulateEventState->getButtonMask()); + break; + case(2): + _accumulateEventState->setButtonMask(~GUIEventAdapter::MIDDLE_MOUSE_BUTTON & _accumulateEventState->getButtonMask()); + break; + case(3): + _accumulateEventState->setButtonMask(~GUIEventAdapter::RIGHT_MOUSE_BUTTON & _accumulateEventState->getButtonMask()); + break; + } + + GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState); + event->setEventType(GUIEventAdapter::RELEASE); + switch(button) + { + case(1): + event->setButton(GUIEventAdapter::LEFT_MOUSE_BUTTON); + break; + case(2): + event->setButton(GUIEventAdapter::MIDDLE_MOUSE_BUTTON); + break; + case(3): + event->setButton(GUIEventAdapter::RIGHT_MOUSE_BUTTON); + break; + } + + addEvent(event); +} + +void EventQueue::keyPress(GUIEventAdapter::KeySymbol key) +{ + switch(key) + { + case(GUIEventAdapter::KEY_Shift_L): _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_LEFT_SHIFT | _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Shift_R): _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_RIGHT_SHIFT | _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Control_L): _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_LEFT_CTRL | _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Control_R): _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_RIGHT_CTRL | _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Meta_L): _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_LEFT_META | _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Meta_R): _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_RIGHT_META | _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Alt_L): _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_LEFT_ALT | _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Alt_R): _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_LEFT_ALT | _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Caps_Lock): + { + if ((_accumulateEventState->getModKeyMask() & GUIEventAdapter::MODKEY_CAPS_LOCK)!=0) + _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_CAPS_LOCK & _accumulateEventState->getModKeyMask()); + else + _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_CAPS_LOCK | _accumulateEventState->getModKeyMask()); + break; + } + case(GUIEventAdapter::KEY_Num_Lock): + { + if ((_accumulateEventState->getModKeyMask() & GUIEventAdapter::MODKEY_NUM_LOCK)!=0) + _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_NUM_LOCK & _accumulateEventState->getModKeyMask()); + else + _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_NUM_LOCK | _accumulateEventState->getModKeyMask()); + break; + } + default: break; + } + + GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState); + event->setEventType(GUIEventAdapter::KEYDOWN); + event->setKey(key); + + addEvent(event); +} + +void EventQueue::keyRelease(GUIEventAdapter::KeySymbol key) +{ + switch(key) + { + case(GUIEventAdapter::KEY_Shift_L): _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_LEFT_SHIFT & _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Shift_R): _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_RIGHT_SHIFT & _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Control_L): _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_LEFT_CTRL & _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Control_R): _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_RIGHT_CTRL & _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Meta_L): _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_LEFT_META & _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Meta_R): _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_RIGHT_META & _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Alt_L): _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_LEFT_ALT & _accumulateEventState->getModKeyMask()); break; + case(GUIEventAdapter::KEY_Alt_R): _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_LEFT_ALT & _accumulateEventState->getModKeyMask()); break; + default: break; + } + + GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState); + event->setEventType(GUIEventAdapter::KEYUP); + event->setKey(key); + + addEvent(event); +} + +void EventQueue::frame(double t) +{ + GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState); + event->setEventType(GUIEventAdapter::FRAME); + event->setTime(t); + + addEvent(event); +}