EventQueue 11.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 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 <osgGA/GUIEventAdapter>

#include <osg/ref_ptr>
#include <osg/Timer>

#include <OpenThreads/Mutex>
#include <list>

namespace osgGA {

/**
 * EventQueue implementation for collecting and adapting windowing events 
 */
class OSGGA_EXPORT EventQueue : public osg::Referenced
{
    public:

        EventQueue(GUIEventAdapter::MouseYOrientation mouseYOrientation=GUIEventAdapter::Y_INCREASING_DOWNWARDS);

        typedef std::list< osg::ref_ptr<GUIEventAdapter> > Events;
                
        /** Set events.*/
        void setEvents(Events& events);

        /** Take the entire event queue leaving the EventQueue' event queue empty.*/
        bool takeEvents(Events& events);

        /** Take the events that were recorded before with specified time queue.*/
        bool takeEvents(Events& events, double cutOffTime);

        /** 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);


        /** 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; }


        /** Set the graphics context associated with this event queue.*/
        void setGraphicsContext(osg::GraphicsContext* context) { getCurrentEventState()->setGraphicsContext(context); }


        /** Set the mouse input range.*/
        void setMouseInputRange(float xMin, float yMin, float xMax, float yMax) { getCurrentEventState()->setInputRange(xMin, yMin, xMax, yMax); }


        /** Method for adapting window resize event, placing this event on the back of the event queue. */
        void windowResize(int x, int y, int width, int height) { windowResize(x,y,width,height,getTime()); }

        /** Method for adapting window resize event, placing this event on the back of the event queue, with specified time. */
        void windowResize(int x, int y, int width, int height, double time);


        /** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue. */
        void mouseScroll(GUIEventAdapter::ScrollingMotion sm) { mouseScroll(sm,getTime()); }
        
        /** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue, with specified time. */
        void mouseScroll(GUIEventAdapter::ScrollingMotion sm, double time);
        

        /** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue. */
        void mouseScroll2D(float x, float y) { mouseScroll2D(x, y, getTime()); }

        /** Method for adapting mouse scroll wheel events, placing this event on the back of the event queue. */
        void mouseScroll2D(float x, float y, double time);
        

        /** Method for adapting pen pressure events, placing this event on the back of the event queue.*/
        void penPressure(float pressure) { penPressure(pressure, getTime()); }
        
        /** Method for adapting pen pressure events, placing this event on the back of the event queue, with specified time.*/
        void penPressure(float pressure, double time);

        /** Method for adapting pen orientation events, placing this event on the back of the event queue.*/
        void penOrientation(float tiltX, float tiltY, float rotation) { penOrientation(tiltX, tiltY, rotation, getTime()); }

        /** Method for adapting pen orientation events, placing this event on the back of the event queue, with specified time.*/
        void penOrientation(float tiltX, float tiltY, float rotation, double time);
        
         /** Method for adapting pen proximity events, placing this event on the back of the event queue.*/
        void penProximity(GUIEventAdapter::TabletPointerType pt, bool isEntering) { penProximity(pt, isEntering, getTime()); }

         /** Method for adapting pen proximity events, placing this event on the back of the event queue, with specified time.*/
        void penProximity(GUIEventAdapter::TabletPointerType pt, bool isEntering, double time);


        /** Method for updating in response to a mouse warp. Note, just moves the mouse position without creating a new event for it.*/
        void mouseWarped(float x, float y);


        /** Method for adapting mouse motion events, placing this event on the back of the event queue.*/
        void mouseMotion(float x, float y) { mouseMotion(x,y, getTime()); }

        /** Method for adapting mouse motion events, placing this event on the back of the event queue, with specified time.*/
        void mouseMotion(float x, float y, double time);


        /** 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. */
        void mouseButtonPress(float x, float y, unsigned int button) { mouseButtonPress(x, y, button, getTime()); }

        /** 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. */
        void mouseButtonPress(float x, float y, unsigned int button, double time);


        /** 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. */
        void mouseDoubleButtonPress(float x, float y, unsigned int button) { mouseDoubleButtonPress(x, y, button, getTime()); }
        
        /** 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. */
        void mouseDoubleButtonPress(float x, float y, unsigned int button, double time);
        

        /** 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. */
        void mouseButtonRelease(float x, float y, unsigned int button) { mouseButtonRelease(x, y, button, getTime()); }

        /** 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. */
        void mouseButtonRelease(float x, float y, unsigned int button, double time);


        /** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings.*/
        void keyPress(int key, int unmodifiedKey = 0) { keyPress(key, getTime(), unmodifiedKey); }

        /** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings, with specified time.*/
        void keyPress(int key, double time, int unmodifiedKey = 0);


        /** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings.*/
        void keyRelease(int key, int unmodifiedKey = 0) { keyRelease(key, getTime(), unmodifiedKey); }

        /** Method for adapting keyboard press events. Note, special keys such as Ctrl/Function keys should be adapted to GUIEventAdapter::KeySymbol mappings, with specified time.*/
        void keyRelease(int key, double time, int unmodifiedKey = 0);
            
        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());
        }
        
        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());
        }
        
        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());
        }
        


        /** Method for adapting close window events.*/
        void closeWindow() { closeWindow(getTime()); }

        /** Method for adapting close window event with specified event time.*/
        void closeWindow(double time);


        /** Method for adapting application quit events.*/
        void quitApplication() { quitApplication(getTime()); }

        /** Method for adapting application quit events with specified event time.*/
        void quitApplication(double time);


        /** Method for adapting frame events.*/
        void frame(double time);
        

        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()); }


        /** convenience method for create an event ready to fill in.  Clones the getCurrentEventState() to produce a up to date event state. */
        GUIEventAdapter* createEvent();
        
        
        void setCurrentEventState(GUIEventAdapter* ea) { _accumulateEventState = ea; }
        GUIEventAdapter* getCurrentEventState() { return _accumulateEventState.get(); }
        const GUIEventAdapter* getCurrentEventState() const { return _accumulateEventState.get(); }

        /** Method for adapting user defined events */
        void userEvent(osg::Referenced* userEventData) { userEvent(userEventData, getTime()); }

        /** Method for adapting user defined events with specified event time */
        void userEvent(osg::Referenced* userEventData, double time);


    protected:

        virtual ~EventQueue();

        /** Prevent unwanted copy operator.*/
        EventQueue& operator = (const EventQueue&) { return *this; }
        
        osg::ref_ptr<GUIEventAdapter>   _accumulateEventState;

        bool                        _useFixedMouseInputRange;

        osg::Timer_t                _startTick;
        mutable OpenThreads::Mutex  _eventQueueMutex;
        Events                      _eventQueue;
    
};

}

#endif