EventQueue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield
  2. *
  3. * This library is open source and may be redistributed and/or modified under
  4. * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
  5. * (at your option) any later version. The full license is in LICENSE file
  6. * included with this distribution, and on the openscenegraph.org website.
  7. *
  8. * This library is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * OpenSceneGraph Public License for more details.
  12. */
  13. #ifndef OSGGA_EVENTQUEUE
  14. #define OSGGA_EVENTQUEUE 1
  15. #include <osgGA/GUIEventAdapter>
  16. #include <osg/ref_ptr>
  17. #include <osg/Timer>
  18. #include <OpenThreads/Mutex>
  19. #include <list>
  20. namespace osgGA {
  21. /**
  22. * EventQueue implementation for collecting and adapting windowing events
  23. */
  24. class OSGGA_EXPORT EventQueue : public osg::Referenced
  25. {
  26. public:
  27. EventQueue(GUIEventAdapter::MouseYOrientation mouseYOrientation=GUIEventAdapter::Y_INCREASING_DOWNWARDS);
  28. typedef std::list< osg::ref_ptr<Event> > Events;
  29. bool empty() const
  30. {
  31. OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
  32. return _eventQueue.empty();
  33. }
  34. /** Set events.*/
  35. void setEvents(Events& events);
  36. /** Take the entire event queue leaving the EventQueue' event queue empty.*/
  37. bool takeEvents(Events& events);
  38. /** Take the events that were recorded before with specified time queue.*/
  39. bool takeEvents(Events& events, double cutOffTime);
  40. /** Take a copy the entire event queue leaving the EventQueue' event queue intact.*/
  41. bool copyEvents(Events& events) const;
  42. /** Add events to end of event queue.*/
  43. void appendEvents(Events& events);
  44. /** Add an event to the end of the event queue.*/
  45. void addEvent(Event* event);
  46. /** Specify if mouse coordinates should be transformed into a pre defined input range, or whether they
  47. * should be simply based on as local coordinates to the window that generated the mouse events.*/
  48. void setUseFixedMouseInputRange(bool useFixedMouseInputRange) { _useFixedMouseInputRange = useFixedMouseInputRange; }
  49. /** Get whether the mouse coordinates should be transformed into a pre defined input range.*/
  50. bool getUseFixedMouseInputRange() { return _useFixedMouseInputRange; }
  51. /** Set the graphics context associated with this event queue.*/
  52. void setGraphicsContext(osg::GraphicsContext* context) { getCurrentEventState()->setGraphicsContext(context); }
  53. osg::GraphicsContext* getGraphicsContext() { return getCurrentEventState()->getGraphicsContext(); }
  54. const osg::GraphicsContext* getGraphicsContext() const { return getCurrentEventState()->getGraphicsContext(); }
  55. /** Read the window record dimensions from the graphics context. */
  56. void syncWindowRectangleWithGraphicsContext();
  57. /** Set the mouse input range.*/
  58. void setMouseInputRange(float xMin, float yMin, float xMax, float yMax) { getCurrentEventState()->setInputRange(xMin, yMin, xMax, yMax); }
  59. /** Method for adapting window resize event, placing this event on the back of the event queue. */
  60. osgGA::GUIEventAdapter* windowResize(int x, int y, int width, int height) { return windowResize(x,y,width,height,getTime()); }
  61. /** Method for adapting window resize event, placing this event on the back of the event queue, with specified time. */
  62. osgGA::GUIEventAdapter* windowResize(int x, int y, int width, int height, double time);
  63. /** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue. */
  64. osgGA::GUIEventAdapter* mouseScroll(GUIEventAdapter::ScrollingMotion sm) { return mouseScroll(sm,getTime()); }
  65. /** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue, with specified time. */
  66. osgGA::GUIEventAdapter* mouseScroll(GUIEventAdapter::ScrollingMotion sm, double time);
  67. /** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue. */
  68. osgGA::GUIEventAdapter* mouseScroll2D(float x, float y) { return mouseScroll2D(x, y, getTime()); }
  69. /** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue. */
  70. osgGA::GUIEventAdapter* mouseScroll2D(float x, float y, double time);
  71. /** Method for adapting pen pressure events, placing this event on the back of the event queue.*/
  72. osgGA::GUIEventAdapter* penPressure(float pressure) { return penPressure(pressure, getTime()); }
  73. /** Method for adapting pen pressure events, placing this event on the back of the event queue, with specified time.*/
  74. osgGA::GUIEventAdapter* penPressure(float pressure, double time);
  75. /** Method for adapting pen orientation events, placing this event on the back of the event queue.*/
  76. osgGA::GUIEventAdapter* penOrientation(float tiltX, float tiltY, float rotation) { return penOrientation(tiltX, tiltY, rotation, getTime()); }
  77. /** Method for adapting pen orientation events, placing this event on the back of the event queue, with specified time.*/
  78. osgGA::GUIEventAdapter* penOrientation(float tiltX, float tiltY, float rotation, double time);
  79. /** Method for adapting pen proximity events, placing this event on the back of the event queue.*/
  80. osgGA::GUIEventAdapter* penProximity(GUIEventAdapter::TabletPointerType pt, bool isEntering) { return penProximity(pt, isEntering, getTime()); }
  81. /** Method for adapting pen proximity events, placing this event on the back of the event queue, with specified time.*/
  82. osgGA::GUIEventAdapter* penProximity(GUIEventAdapter::TabletPointerType pt, bool isEntering, double time);
  83. /** Method for updating in response to a mouse warp. Note, just moves the mouse position without creating a new event for it.*/
  84. void mouseWarped(float x, float y);
  85. /** Method for adapting mouse motion events, placing this event on the back of the event queue.*/
  86. osgGA::GUIEventAdapter* mouseMotion(float x, float y) { return mouseMotion(x,y, getTime()); }
  87. /** Method for adapting mouse motion events, placing this event on the back of the event queue, with specified time.*/
  88. osgGA::GUIEventAdapter* mouseMotion(float x, float y, double time);
  89. /** Method for adapting mouse button pressed events, placing this event on the back of the event queue.
  90. * Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
  91. osgGA::GUIEventAdapter* mouseButtonPress(float x, float y, unsigned int button) { return mouseButtonPress(x, y, button, getTime()); }
  92. /** Method for adapting mouse button pressed events, placing this event on the back of the event queue, with specified time.
  93. * Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
  94. osgGA::GUIEventAdapter* mouseButtonPress(float x, float y, unsigned int button, double time);
  95. /** Method for adapting mouse button pressed events, placing this event on the back of the event queue.
  96. * Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
  97. osgGA::GUIEventAdapter* mouseDoubleButtonPress(float x, float y, unsigned int button) { return mouseDoubleButtonPress(x, y, button, getTime()); }
  98. /** Method for adapting mouse button pressed events, placing this event on the back of the event queue, with specified time.
  99. * Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
  100. osgGA::GUIEventAdapter* mouseDoubleButtonPress(float x, float y, unsigned int button, double time);
  101. /** Method for adapting mouse button release events, placing this event on the back of the event queue.
  102. * Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
  103. osgGA::GUIEventAdapter* mouseButtonRelease(float x, float y, unsigned int button) { return mouseButtonRelease(x, y, button, getTime()); }
  104. /** Method for adapting mouse button release events, placing this event on the back of the event queue, with specified time.
  105. * Button numbering is 1 for left mouse button, 2 for middle, 3 for right. */
  106. osgGA::GUIEventAdapter* mouseButtonRelease(float x, float y, unsigned int button, double time);
  107. /** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings.*/
  108. osgGA::GUIEventAdapter* keyPress(int key, int unmodifiedKey = 0) { return keyPress(key, getTime(), unmodifiedKey); }
  109. /** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings, with specified time.*/
  110. osgGA::GUIEventAdapter* keyPress(int key, double time, int unmodifiedKey = 0);
  111. /** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings.*/
  112. osgGA::GUIEventAdapter* keyRelease(int key, int unmodifiedKey = 0) { return keyRelease(key, getTime(), unmodifiedKey); }
  113. /** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings, with specified time.*/
  114. osgGA::GUIEventAdapter* keyRelease(int key, double time, int unmodifiedKey = 0);
  115. GUIEventAdapter* touchBegan(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, double time);
  116. GUIEventAdapter* touchBegan(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y) {
  117. return touchBegan(id, phase, x, y, getTime());
  118. }
  119. GUIEventAdapter* touchMoved(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, double time);
  120. GUIEventAdapter* touchMoved(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y) {
  121. return touchMoved(id, phase, x, y, getTime());
  122. }
  123. GUIEventAdapter* touchEnded(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, unsigned int tap_count, double time);
  124. GUIEventAdapter* touchEnded(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, unsigned int tap_count) {
  125. return touchEnded(id, phase, x, y, tap_count, getTime());
  126. }
  127. /** Method for adapting close window events.*/
  128. osgGA::GUIEventAdapter* closeWindow() { return closeWindow(getTime()); }
  129. /** Method for adapting close window event with specified event time.*/
  130. osgGA::GUIEventAdapter* closeWindow(double time);
  131. /** Method for adapting application quit events.*/
  132. osgGA::GUIEventAdapter* quitApplication() { return quitApplication(getTime()); }
  133. /** Method for adapting application quit events with specified event time.*/
  134. osgGA::GUIEventAdapter* quitApplication(double time);
  135. /** Method for adapting frame events.*/
  136. osgGA::GUIEventAdapter* frame(double time);
  137. void setStartTick(osg::Timer_t tick) { _startTick = tick; clear(); }
  138. osg::Timer_t getStartTick() const { return _startTick; }
  139. double getTime() const { return osg::Timer::instance()->delta_s(_startTick, osg::Timer::instance()->tick()); }
  140. /** clear all events from queue. */
  141. void clear();
  142. /** convenience method for create an event ready to fill in. Clones the getCurrentEventState() to produce a up to date event state. */
  143. GUIEventAdapter* createEvent();
  144. void setCurrentEventState(GUIEventAdapter* ea) { _accumulateEventState = ea; }
  145. GUIEventAdapter* getCurrentEventState() { return _accumulateEventState.get(); }
  146. const GUIEventAdapter* getCurrentEventState() const { return _accumulateEventState.get(); }
  147. /** Method for adapting user defined events */
  148. GUIEventAdapter* userEvent(osg::Referenced* userEventData) { return userEvent(userEventData, getTime()); }
  149. /** Method for adapting user defined events with specified event time */
  150. GUIEventAdapter* userEvent(osg::Referenced* userEventData, double time);
  151. void setFirstTouchEmulatesMouse(bool b) { _firstTouchEmulatesMouse = b; }
  152. bool getFirstTouchEmulatesMouse() const { return _firstTouchEmulatesMouse; }
  153. protected:
  154. virtual ~EventQueue();
  155. /** Prevent unwanted copy operator.*/
  156. EventQueue& operator = (const EventQueue&) { return *this; }
  157. osg::ref_ptr<GUIEventAdapter> _accumulateEventState;
  158. bool _useFixedMouseInputRange;
  159. osg::Timer_t _startTick;
  160. mutable OpenThreads::Mutex _eventQueueMutex;
  161. Events _eventQueue;
  162. bool _firstTouchEmulatesMouse;
  163. };
  164. }
  165. #endif