2012-03-22 01:36:20 +08:00
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
2006-03-08 23:40:02 +08:00
*
2012-03-22 01:36:20 +08:00
* 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
2006-03-08 23:40:02 +08:00
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
2012-03-22 01:36:20 +08:00
*
2006-03-08 23:40:02 +08:00
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2012-03-22 01:36:20 +08:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2006-03-08 23:40:02 +08:00
* OpenSceneGraph Public License for more details.
*/
#ifndef OSGGA_EVENTQUEUE
#define OSGGA_EVENTQUEUE 1
#include <osgGA/GUIEventAdapter>
#include <osg/ref_ptr>
#include <osg/Timer>
#include <OpenThreads/Mutex>
#include <list>
namespace osgGA {
/**
2012-03-22 01:36:20 +08:00
* EventQueue implementation for collecting and adapting windowing events
2006-03-08 23:40:02 +08:00
*/
class OSGGA_EXPORT EventQueue : public osg::Referenced
{
public:
2006-03-13 21:19:37 +08:00
EventQueue(GUIEventAdapter::MouseYOrientation mouseYOrientation=GUIEventAdapter::Y_INCREASING_DOWNWARDS);
2006-03-08 23:40:02 +08:00
2013-10-25 22:54:15 +08:00
typedef std::list< osg::ref_ptr<Event> > Events;
2012-03-22 01:36:20 +08:00
2013-05-24 17:35:58 +08:00
bool empty() const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
return _eventQueue.empty();
}
2013-10-25 22:54:15 +08:00
2006-03-08 23:40:02 +08:00
/** Set events.*/
void setEvents(Events& events);
/** Take the entire event queue leaving the EventQueue' event queue empty.*/
bool takeEvents(Events& events);
2011-06-14 20:44:44 +08:00
/** Take the events that were recorded before with specified time queue.*/
bool takeEvents(Events& events, double cutOffTime);
2006-03-08 23:40:02 +08:00
/** 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.*/
2013-10-25 22:54:15 +08:00
void addEvent(Event* event);
2006-03-08 23:40:02 +08:00
2006-12-27 01:37:58 +08:00
2006-12-21 05:13:29 +08:00
/** Specify if mouse coordinates should be transformed into a pre defined input range, or whether they
* should be simply based on as local coordinates to the window that generated the mouse events.*/
void setUseFixedMouseInputRange(bool useFixedMouseInputRange) { _useFixedMouseInputRange = useFixedMouseInputRange; }
/** Get whether the mouse coordinates should be transformed into a pre defined input range.*/
bool getUseFixedMouseInputRange() { return _useFixedMouseInputRange; }
2006-12-27 01:37:58 +08:00
2008-06-03 01:34:47 +08:00
/** Set the graphics context associated with this event queue.*/
void setGraphicsContext(osg::GraphicsContext* context) { getCurrentEventState()->setGraphicsContext(context); }
2013-10-25 22:54:15 +08:00
2013-05-04 03:26:27 +08:00
osg::GraphicsContext* getGraphicsContext() { return getCurrentEventState()->getGraphicsContext(); }
const osg::GraphicsContext* getGraphicsContext() const { return getCurrentEventState()->getGraphicsContext(); }
/** Read the window record dimensions from the graphics context. */
2015-02-04 23:11:00 +08:00
void syncWindowRectangleWithGraphicsContext();
2008-06-03 01:34:47 +08:00
2006-12-21 05:13:29 +08:00
/** Set the mouse input range.*/
void setMouseInputRange(float xMin, float yMin, float xMax, float yMax) { getCurrentEventState()->setInputRange(xMin, yMin, xMax, yMax); }
2006-03-08 23:40:02 +08:00
2006-12-27 01:37:58 +08:00
2006-08-22 04:29:32 +08:00
/** Method for adapting window resize event, placing this event on the back of the event queue. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* windowResize(int x, int y, int width, int height) { return windowResize(x,y,width,height,getTime()); }
2006-12-27 01:37:58 +08:00
/** Method for adapting window resize event, placing this event on the back of the event queue, with specified time. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* windowResize(int x, int y, int width, int height, double time);
2006-12-27 01:37:58 +08:00
2006-03-08 23:40:02 +08:00
2006-08-22 04:29:32 +08:00
/** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseScroll(GUIEventAdapter::ScrollingMotion sm) { return mouseScroll(sm,getTime()); }
2012-03-22 01:36:20 +08:00
2006-12-27 01:37:58 +08:00
/** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue, with specified time. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseScroll(GUIEventAdapter::ScrollingMotion sm, double time);
2012-03-22 01:36:20 +08:00
2006-12-27 01:37:58 +08:00
/** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseScroll2D(float x, float y) { return mouseScroll2D(x, y, getTime()); }
2006-12-27 01:37:58 +08:00
2006-08-22 04:29:32 +08:00
/** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseScroll2D(float x, float y, double time);
2012-03-22 01:36:20 +08:00
2006-12-27 01:37:58 +08:00
/** Method for adapting pen pressure events, placing this event on the back of the event queue.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* penPressure(float pressure) { return penPressure(pressure, getTime()); }
2012-03-22 01:36:20 +08:00
2006-12-27 01:37:58 +08:00
/** Method for adapting pen pressure events, placing this event on the back of the event queue, with specified time.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* penPressure(float pressure, double time);
2006-12-27 01:37:58 +08:00
2008-04-11 21:28:09 +08:00
/** Method for adapting pen orientation events, placing this event on the back of the event queue.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* penOrientation(float tiltX, float tiltY, float rotation) { return penOrientation(tiltX, tiltY, rotation, getTime()); }
2008-04-11 21:28:09 +08:00
/** Method for adapting pen orientation events, placing this event on the back of the event queue, with specified time.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* penOrientation(float tiltX, float tiltY, float rotation, double time);
2012-03-22 01:36:20 +08:00
2006-12-27 01:37:58 +08:00
/** Method for adapting pen proximity events, placing this event on the back of the event queue.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* penProximity(GUIEventAdapter::TabletPointerType pt, bool isEntering) { return penProximity(pt, isEntering, getTime()); }
2006-12-27 01:37:58 +08:00
/** Method for adapting pen proximity events, placing this event on the back of the event queue, with specified time.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* penProximity(GUIEventAdapter::TabletPointerType pt, bool isEntering, double time);
2006-12-27 01:37:58 +08:00
2006-03-08 23:40:02 +08:00
2006-08-22 04:29:32 +08:00
/** Method for updating in response to a mouse warp. Note, just moves the mouse position without creating a new event for it.*/
2006-12-27 01:37:58 +08:00
void mouseWarped(float x, float y);
2006-03-14 21:18:21 +08:00
2006-11-16 04:24:09 +08:00
/** Method for adapting mouse motion events, placing this event on the back of the event queue.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseMotion(float x, float y) { return mouseMotion(x,y, getTime()); }
2006-12-27 01:37:58 +08:00
/** Method for adapting mouse motion events, placing this event on the back of the event queue, with specified time.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseMotion(float x, float y, double time);
2006-12-27 01:37:58 +08:00
2006-03-08 23:40:02 +08:00
2006-08-22 04:29:32 +08:00
/** Method for adapting mouse button pressed events, placing this event on the back of the event queue.
* Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseButtonPress(float x, float y, unsigned int button) { return mouseButtonPress(x, y, button, getTime()); }
2006-12-27 01:37:58 +08:00
/** Method for adapting mouse button pressed events, placing this event on the back of the event queue, with specified time.
* Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseButtonPress(float x, float y, unsigned int button, double time);
2006-12-27 01:37:58 +08:00
2006-07-27 20:32:40 +08:00
2006-08-22 04:29:32 +08:00
/** Method for adapting mouse button pressed events, placing this event on the back of the event queue.
* Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseDoubleButtonPress(float x, float y, unsigned int button) { return mouseDoubleButtonPress(x, y, button, getTime()); }
2012-03-22 01:36:20 +08:00
2006-12-27 01:37:58 +08:00
/** Method for adapting mouse button pressed events, placing this event on the back of the event queue, with specified time.
* Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseDoubleButtonPress(float x, float y, unsigned int button, double time);
2012-03-22 01:36:20 +08:00
2006-12-27 01:37:58 +08:00
2006-08-22 04:29:32 +08:00
/** Method for adapting mouse button release events, placing this event on the back of the event queue.
* Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseButtonRelease(float x, float y, unsigned int button) { return mouseButtonRelease(x, y, button, getTime()); }
2006-12-27 01:37:58 +08:00
/** Method for adapting mouse button release events, placing this event on the back of the event queue, with specified time.
* Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* mouseButtonRelease(float x, float y, unsigned int button, double time);
2006-12-27 01:37:58 +08:00
2006-03-08 23:40:02 +08:00
2006-12-21 05:13:29 +08:00
/** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* keyPress(int key, int unmodifiedKey = 0) { return keyPress(key, getTime(), unmodifiedKey); }
2006-12-27 01:37:58 +08:00
/** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings, with specified time.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* keyPress(int key, double time, int unmodifiedKey = 0);
2006-12-27 01:37:58 +08:00
2006-03-08 23:40:02 +08:00
2006-12-21 05:13:29 +08:00
/** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* keyRelease(int key, int unmodifiedKey = 0) { return keyRelease(key, getTime(), unmodifiedKey); }
2006-12-27 01:37:58 +08:00
/** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings, with specified time.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* keyRelease(int key, double time, int unmodifiedKey = 0);
2012-03-22 01:36:20 +08:00
2010-11-23 01:30:44 +08:00
GUIEventAdapter* touchBegan(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, double time);
GUIEventAdapter* touchBegan(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y) {
return touchBegan(id, phase, x, y, getTime());
}
2012-03-22 01:36:20 +08:00
2010-11-23 01:30:44 +08:00
GUIEventAdapter* touchMoved(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, double time);
GUIEventAdapter* touchMoved(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y) {
return touchMoved(id, phase, x, y, getTime());
}
2012-03-22 01:36:20 +08:00
2010-11-23 01:30:44 +08:00
GUIEventAdapter* touchEnded(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, unsigned int tap_count, double time);
GUIEventAdapter* touchEnded(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, unsigned int tap_count) {
return touchEnded(id, phase, x, y, tap_count, getTime());
}
2012-03-22 01:36:20 +08:00
2006-12-27 01:37:58 +08:00
2006-03-08 23:40:02 +08:00
2007-01-09 03:29:59 +08:00
/** Method for adapting close window events.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* closeWindow() { return closeWindow(getTime()); }
2007-01-09 03:29:59 +08:00
/** Method for adapting close window event with specified event time.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* closeWindow(double time);
2007-01-09 03:29:59 +08:00
/** Method for adapting application quit events.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* quitApplication() { return quitApplication(getTime()); }
2007-01-09 03:29:59 +08:00
/** Method for adapting application quit events with specified event time.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* quitApplication(double time);
2007-01-09 03:29:59 +08:00
2006-08-22 04:29:32 +08:00
/** Method for adapting frame events.*/
2015-05-25 19:33:04 +08:00
osgGA::GUIEventAdapter* frame(double time);
2012-03-22 01:36:20 +08:00
2006-03-08 23:40:02 +08:00
2011-11-04 20:45:32 +08:00
void setStartTick(osg::Timer_t tick) { _startTick = tick; clear(); }
2006-03-08 23:40:02 +08:00
osg::Timer_t getStartTick() const { return _startTick; }
double getTime() const { return osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()); }
2011-11-04 20:45:32 +08:00
/** clear all events from queue. */
void clear();
2006-12-27 01:37:58 +08:00
2007-12-11 01:30:18 +08:00
/** convenience method for create an event ready to fill in. Clones the getCurrentEventState() to produce a up to date event state. */
2006-03-15 23:49:21 +08:00
GUIEventAdapter* createEvent();
2012-03-22 01:36:20 +08:00
2007-07-13 19:31:56 +08:00
void setCurrentEventState(GUIEventAdapter* ea) { _accumulateEventState = ea; }
2006-03-08 23:40:02 +08:00
GUIEventAdapter* getCurrentEventState() { return _accumulateEventState.get(); }
const GUIEventAdapter* getCurrentEventState() const { return _accumulateEventState.get(); }
2007-05-19 21:39:55 +08:00
/** Method for adapting user defined events */
2015-05-25 19:33:04 +08:00
GUIEventAdapter* userEvent(osg::Referenced* userEventData) { return userEvent(userEventData, getTime()); }
2007-05-19 21:39:55 +08:00
/** Method for adapting user defined events with specified event time */
2015-05-25 19:33:04 +08:00
GUIEventAdapter* userEvent(osg::Referenced* userEventData, double time);
2007-05-19 21:39:55 +08:00
From Stephan Huber, "attached you'll find a first version of multi-touch-support for OS X (>=
10.6), which will forward all multi-touch events from a trackpad to the
corresponding osgGA-event-structures.
The support is switched off per default, but you can enable multi-touch
support via a new flag for GraphicsWindowCocoa::WindowData or directly
via the GraphicsWindowCocoa-class.
After switching multi-touch-support on, all mouse-events from the
trackpad get ignored, otherwise you'll have multiple events for the same
pointer which is very confusing (as the trackpad reports absolute
movement, and as a mouse relative movement).
I think this is not a problem, as multi-touch-input is a completely
different beast as a mouse, so you'll have to code your own
event-handlers anyway.
While coding this stuff, I asked myself if we should refactor
GUIEventAdapter/EventQueue and assign a specific event-type for
touch-input instead of using PUSH/DRAG/RELEASE. This will make it
clearer how to use the code, but will break the mouse-emulation for the
first touch-point and with that all existing manipulators. What do you
think? I am happy to code the proposed changes.
Additionally I created a small (and ugly) example osgmultitouch which
makes use of the osgGA::MultiTouchTrackballManipulator, shows all
touch-points on a HUD and demonstrates how to get the touchpoints from
an osgGA::GUIEventAdapter.
There's even a small example video here: http://vimeo.com/31611842"
2012-02-03 22:25:08 +08:00
void setFirstTouchEmulatesMouse(bool b) { _firstTouchEmulatesMouse = b; }
bool getFirstTouchEmulatesMouse() const { return _firstTouchEmulatesMouse; }
2007-05-19 21:39:55 +08:00
2006-03-08 23:40:02 +08:00
protected:
virtual ~EventQueue();
/** Prevent unwanted copy operator.*/
EventQueue& operator = (const EventQueue&) { return *this; }
2012-03-22 01:36:20 +08:00
2006-03-08 23:40:02 +08:00
osg::ref_ptr<GUIEventAdapter> _accumulateEventState;
2006-12-21 05:13:29 +08:00
bool _useFixedMouseInputRange;
2006-03-08 23:40:02 +08:00
osg::Timer_t _startTick;
mutable OpenThreads::Mutex _eventQueueMutex;
Events _eventQueue;
From Stephan Huber, "attached you'll find a first version of multi-touch-support for OS X (>=
10.6), which will forward all multi-touch events from a trackpad to the
corresponding osgGA-event-structures.
The support is switched off per default, but you can enable multi-touch
support via a new flag for GraphicsWindowCocoa::WindowData or directly
via the GraphicsWindowCocoa-class.
After switching multi-touch-support on, all mouse-events from the
trackpad get ignored, otherwise you'll have multiple events for the same
pointer which is very confusing (as the trackpad reports absolute
movement, and as a mouse relative movement).
I think this is not a problem, as multi-touch-input is a completely
different beast as a mouse, so you'll have to code your own
event-handlers anyway.
While coding this stuff, I asked myself if we should refactor
GUIEventAdapter/EventQueue and assign a specific event-type for
touch-input instead of using PUSH/DRAG/RELEASE. This will make it
clearer how to use the code, but will break the mouse-emulation for the
first touch-point and with that all existing manipulators. What do you
think? I am happy to code the proposed changes.
Additionally I created a small (and ugly) example osgmultitouch which
makes use of the osgGA::MultiTouchTrackballManipulator, shows all
touch-points on a HUD and demonstrates how to get the touchpoints from
an osgGA::GUIEventAdapter.
There's even a small example video here: http://vimeo.com/31611842"
2012-02-03 22:25:08 +08:00
bool _firstTouchEmulatesMouse;
2012-03-22 01:36:20 +08:00
2006-03-08 23:40:02 +08:00
};
}
#endif