root/OpenSceneGraph/trunk/src/osgGA/EventQueue.cpp @ 13041

Revision 13041, 18.1 kB (checked in by robert, 3 years ago)

Ran script to remove trailing spaces and tabs

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
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
14#include <osgGA/EventQueue>
15#include <osg/Notify>
16
17using namespace osgGA;
18
19EventQueue::EventQueue(GUIEventAdapter::MouseYOrientation mouseYOrientation)
20{
21    _useFixedMouseInputRange = false;
22
23    _startTick = osg::Timer::instance()->getStartTick();
24
25    _accumulateEventState = new GUIEventAdapter();
26    _accumulateEventState->setMouseYOrientation(mouseYOrientation);
27
28    _firstTouchEmulatesMouse = true;
29}
30
31EventQueue::~EventQueue()
32{
33}
34
35void EventQueue::clear()
36{
37    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
38    _eventQueue.clear();
39}
40
41
42void EventQueue::setEvents(Events& events)
43{
44    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
45    _eventQueue = events;
46}
47
48void EventQueue::appendEvents(Events& events)
49{
50    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
51    _eventQueue.insert(_eventQueue.end(), events.begin(), events.end());
52}
53
54void EventQueue::addEvent(GUIEventAdapter* event)
55{
56    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
57    _eventQueue.push_back(event);
58}
59
60bool EventQueue::takeEvents(Events& events)
61{
62    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
63    if (!_eventQueue.empty())
64    {
65        events.splice(events.end(), _eventQueue);
66        return true;
67    }
68    else
69    {
70        return false;
71    }
72}
73
74bool EventQueue::takeEvents(Events& events, double cutOffTime)
75{
76    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
77    if (!_eventQueue.empty())
78    {
79        // find last event if queue that came in before the cuttof.
80        Events::reverse_iterator ritr = _eventQueue.rbegin();
81        for(; ritr != _eventQueue.rend() && ((*ritr)->getTime() > cutOffTime); ++ritr) {}
82
83        if (ritr==_eventQueue.rend()) return false;
84
85        for(Events::iterator itr = _eventQueue.begin();
86            itr != ritr.base();
87            ++itr)
88        {
89            events.push_back(*itr);
90        }
91
92        // make sure that the events are in ascending time order, and any out of out events
93        // have their time reset to the next valid time after them in the events list.
94        double previousTime = cutOffTime;
95        for(Events::reverse_iterator itr = events.rbegin();
96            itr != events.rend();
97            ++itr)
98        {
99            if ((*itr)->getTime() > previousTime)
100            {
101                OSG_INFO<<"Reset event time from "<<(*itr)->getTime()<<" to "<<previousTime<<std::endl;
102                (*itr)->setTime(previousTime);
103            }
104            else
105            {
106                previousTime = (*itr)->getTime();
107            }
108        }
109
110        // remove the events we are taking from the original event queue.
111        _eventQueue.erase(_eventQueue.begin(), ritr.base());
112
113        return true;
114    }
115    else
116    {
117        return false;
118    }
119}
120
121bool EventQueue::copyEvents(Events& events) const
122{
123    OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_eventQueueMutex);
124    if (!_eventQueue.empty())
125    {
126        events.insert(events.end(),_eventQueue.begin(),_eventQueue.end());
127        return true;
128    }
129    else
130    {
131        return false;
132    }
133}
134
135
136void EventQueue::windowResize(int x, int y, int width, int height, double time)
137{
138    _accumulateEventState->setWindowRectangle(x, y, width, height, !_useFixedMouseInputRange);
139
140    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
141    event->setEventType(GUIEventAdapter::RESIZE);
142    event->setTime(time);
143
144    addEvent(event);
145}
146
147void EventQueue::penPressure(float pressure, double time)
148{
149    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
150    event->setEventType(GUIEventAdapter::PEN_PRESSURE);
151    event->setPenPressure(pressure);
152    event->setTime(time);
153
154    addEvent(event);
155}
156
157void EventQueue::penOrientation(float tiltX, float tiltY, float rotation, double time)
158{
159    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
160    event->setEventType(GUIEventAdapter::PEN_ORIENTATION);
161    event->setPenTiltX(tiltX);
162    event->setPenTiltY(tiltY);
163    event->setPenRotation(rotation);
164    event->setTime(time);
165
166    addEvent(event);
167}
168
169void EventQueue::penProximity(GUIEventAdapter::TabletPointerType pt, bool isEntering, double time)
170{
171    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
172    event->setEventType( (isEntering) ? GUIEventAdapter::PEN_PROXIMITY_ENTER : GUIEventAdapter::PEN_PROXIMITY_LEAVE);
173    event->setTabletPointerType(pt);
174    event->setTime(time);
175
176    addEvent(event);
177}
178
179void EventQueue::mouseScroll(GUIEventAdapter::ScrollingMotion sm, double time)
180{
181    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
182    event->setEventType(GUIEventAdapter::SCROLL);
183    event->setScrollingMotion(sm);
184    event->setTime(time);
185
186    addEvent(event);
187}
188
189void EventQueue::mouseScroll2D(float x, float y, double time)
190{
191    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
192    event->setEventType(GUIEventAdapter::SCROLL);
193    event->setScrollingMotionDelta(x,y);
194    event->setTime(time);
195
196    addEvent(event);
197}
198
199
200void EventQueue::mouseWarped(float x, float y)
201{
202    _accumulateEventState->setX(x);
203    _accumulateEventState->setY(y);
204}
205
206
207void EventQueue::mouseMotion(float x, float y, double time)
208{
209    _accumulateEventState->setX(x);
210    _accumulateEventState->setY(y);
211
212    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
213    event->setEventType(event->getButtonMask() ? GUIEventAdapter::DRAG : GUIEventAdapter::MOVE);
214    event->setTime(time);
215
216    addEvent(event);
217}
218
219void EventQueue::mouseButtonPress(float x, float y, unsigned int button, double time)
220{
221    _accumulateEventState->setX(x);
222    _accumulateEventState->setY(y);
223
224    switch(button)
225    {
226        case(1):
227            _accumulateEventState->setButtonMask(GUIEventAdapter::LEFT_MOUSE_BUTTON | _accumulateEventState->getButtonMask());
228            break;
229        case(2):
230            _accumulateEventState->setButtonMask(GUIEventAdapter::MIDDLE_MOUSE_BUTTON | _accumulateEventState->getButtonMask());
231            break;
232        case(3):
233            _accumulateEventState->setButtonMask(GUIEventAdapter::RIGHT_MOUSE_BUTTON | _accumulateEventState->getButtonMask());
234            break;
235    }
236
237    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
238    event->setEventType(GUIEventAdapter::PUSH);
239    event->setTime(time);
240
241    switch(button)
242    {
243        case(1):
244            event->setButton(GUIEventAdapter::LEFT_MOUSE_BUTTON);
245            break;
246        case(2):
247            event->setButton(GUIEventAdapter::MIDDLE_MOUSE_BUTTON);
248            break;
249        case(3):
250            event->setButton(GUIEventAdapter::RIGHT_MOUSE_BUTTON);
251            break;
252    }
253
254    addEvent(event);
255}
256
257void EventQueue::mouseDoubleButtonPress(float x, float y, unsigned int button, double time)
258{
259    _accumulateEventState->setX(x);
260    _accumulateEventState->setY(y);
261
262    switch(button)
263    {
264        case(1):
265            _accumulateEventState->setButtonMask(GUIEventAdapter::LEFT_MOUSE_BUTTON | _accumulateEventState->getButtonMask());
266            break;
267        case(2):
268            _accumulateEventState->setButtonMask(GUIEventAdapter::MIDDLE_MOUSE_BUTTON | _accumulateEventState->getButtonMask());
269            break;
270        case(3):
271            _accumulateEventState->setButtonMask(GUIEventAdapter::RIGHT_MOUSE_BUTTON | _accumulateEventState->getButtonMask());
272            break;
273    }
274
275    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
276    event->setEventType(GUIEventAdapter::DOUBLECLICK);
277    event->setTime(time);
278
279    switch(button)
280    {
281        case(1):
282            event->setButton(GUIEventAdapter::LEFT_MOUSE_BUTTON);
283            break;
284        case(2):
285            event->setButton(GUIEventAdapter::MIDDLE_MOUSE_BUTTON);
286            break;
287        case(3):
288            event->setButton(GUIEventAdapter::RIGHT_MOUSE_BUTTON);
289            break;
290    }
291
292    addEvent(event);
293}
294
295void EventQueue::mouseButtonRelease(float x, float y, unsigned int button, double time)
296{
297    _accumulateEventState->setX(x);
298    _accumulateEventState->setY(y);
299
300    switch(button)
301    {
302        case(1):
303            _accumulateEventState->setButtonMask(~GUIEventAdapter::LEFT_MOUSE_BUTTON & _accumulateEventState->getButtonMask());
304            break;
305        case(2):
306            _accumulateEventState->setButtonMask(~GUIEventAdapter::MIDDLE_MOUSE_BUTTON & _accumulateEventState->getButtonMask());
307            break;
308        case(3):
309            _accumulateEventState->setButtonMask(~GUIEventAdapter::RIGHT_MOUSE_BUTTON & _accumulateEventState->getButtonMask());
310            break;
311    }
312
313    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
314    event->setEventType(GUIEventAdapter::RELEASE);
315    event->setTime(time);
316
317    switch(button)
318    {
319        case(1):
320            event->setButton(GUIEventAdapter::LEFT_MOUSE_BUTTON);
321            break;
322        case(2):
323            event->setButton(GUIEventAdapter::MIDDLE_MOUSE_BUTTON);
324            break;
325        case(3):
326            event->setButton(GUIEventAdapter::RIGHT_MOUSE_BUTTON);
327            break;
328    }
329
330    addEvent(event);
331}
332
333void EventQueue::keyPress(int key, double time, int unmodifiedKey)
334{
335    switch(key)
336    {
337        case(GUIEventAdapter::KEY_Shift_L):      _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_LEFT_SHIFT | _accumulateEventState->getModKeyMask()); break;
338        case(GUIEventAdapter::KEY_Shift_R):      _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_RIGHT_SHIFT | _accumulateEventState->getModKeyMask()); break;
339        case(GUIEventAdapter::KEY_Control_L):    _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_LEFT_CTRL | _accumulateEventState->getModKeyMask()); break;
340        case(GUIEventAdapter::KEY_Control_R):    _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_RIGHT_CTRL | _accumulateEventState->getModKeyMask()); break;
341        case(GUIEventAdapter::KEY_Meta_L):       _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_LEFT_META | _accumulateEventState->getModKeyMask()); break;
342        case(GUIEventAdapter::KEY_Meta_R):       _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_RIGHT_META | _accumulateEventState->getModKeyMask()); break;
343        case(GUIEventAdapter::KEY_Alt_L):        _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_LEFT_ALT | _accumulateEventState->getModKeyMask()); break;
344        case(GUIEventAdapter::KEY_Alt_R):        _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_RIGHT_ALT | _accumulateEventState->getModKeyMask()); break;
345        case(GUIEventAdapter::KEY_Super_L):      _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_LEFT_SUPER | _accumulateEventState->getModKeyMask()); break;
346        case(GUIEventAdapter::KEY_Super_R):      _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_RIGHT_SUPER | _accumulateEventState->getModKeyMask()); break;
347        case(GUIEventAdapter::KEY_Hyper_L):      _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_LEFT_HYPER | _accumulateEventState->getModKeyMask()); break;
348        case(GUIEventAdapter::KEY_Hyper_R):      _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_RIGHT_HYPER | _accumulateEventState->getModKeyMask()); break;
349        case(GUIEventAdapter::KEY_Caps_Lock):
350        {
351            if ((_accumulateEventState->getModKeyMask() & GUIEventAdapter::MODKEY_CAPS_LOCK)!=0)
352                _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_CAPS_LOCK & _accumulateEventState->getModKeyMask());
353            else
354                _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_CAPS_LOCK | _accumulateEventState->getModKeyMask());
355            break;
356        }
357        case(GUIEventAdapter::KEY_Num_Lock):
358        {
359            if ((_accumulateEventState->getModKeyMask() & GUIEventAdapter::MODKEY_NUM_LOCK)!=0)
360                 _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_NUM_LOCK & _accumulateEventState->getModKeyMask());
361            else
362                 _accumulateEventState->setModKeyMask(GUIEventAdapter::MODKEY_NUM_LOCK | _accumulateEventState->getModKeyMask());
363            break;
364        }
365        default: break;
366    }
367
368    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
369    event->setEventType(GUIEventAdapter::KEYDOWN);
370    event->setKey(key);
371    event->setUnmodifiedKey(unmodifiedKey);
372    event->setTime(time);
373
374    addEvent(event);
375}
376
377void EventQueue::keyRelease(int key, double time, int unmodifiedKey)
378{
379    switch(key)
380    {
381        case(GUIEventAdapter::KEY_Shift_L):      _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_LEFT_SHIFT & _accumulateEventState->getModKeyMask()); break;
382        case(GUIEventAdapter::KEY_Shift_R):      _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_RIGHT_SHIFT & _accumulateEventState->getModKeyMask()); break;
383        case(GUIEventAdapter::KEY_Control_L):    _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_LEFT_CTRL & _accumulateEventState->getModKeyMask()); break;
384        case(GUIEventAdapter::KEY_Control_R):    _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_RIGHT_CTRL & _accumulateEventState->getModKeyMask()); break;
385        case(GUIEventAdapter::KEY_Meta_L):       _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_LEFT_META & _accumulateEventState->getModKeyMask()); break;
386        case(GUIEventAdapter::KEY_Meta_R):       _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_RIGHT_META & _accumulateEventState->getModKeyMask()); break;
387        case(GUIEventAdapter::KEY_Alt_L):        _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_LEFT_ALT & _accumulateEventState->getModKeyMask()); break;
388        case(GUIEventAdapter::KEY_Alt_R):        _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_RIGHT_ALT & _accumulateEventState->getModKeyMask()); break;
389        case(GUIEventAdapter::KEY_Super_L):      _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_LEFT_SUPER & _accumulateEventState->getModKeyMask()); break;
390        case(GUIEventAdapter::KEY_Super_R):      _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_RIGHT_SUPER & _accumulateEventState->getModKeyMask()); break;
391        case(GUIEventAdapter::KEY_Hyper_L):      _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_LEFT_HYPER & _accumulateEventState->getModKeyMask()); break;
392        case(GUIEventAdapter::KEY_Hyper_R):      _accumulateEventState->setModKeyMask(~GUIEventAdapter::MODKEY_RIGHT_HYPER & _accumulateEventState->getModKeyMask()); break;
393        default: break;
394    }
395
396    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
397    event->setEventType(GUIEventAdapter::KEYUP);
398    event->setKey(key);
399    event->setUnmodifiedKey(unmodifiedKey);
400    event->setTime(time);
401
402    addEvent(event);
403}
404
405
406GUIEventAdapter*  EventQueue::touchBegan(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, double time)
407{
408    if(_firstTouchEmulatesMouse)
409    {
410        // emulate left mouse button press
411
412        _accumulateEventState->setButtonMask((1) | _accumulateEventState->getButtonMask());
413        _accumulateEventState->setX(x);
414        _accumulateEventState->setY(y);
415    }
416
417    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
418    event->setEventType(GUIEventAdapter::PUSH);
419    event->setTime(time);
420    event->addTouchPoint(id, phase, x, y, 0);
421
422    addEvent(event);
423
424    return event;
425}
426
427
428GUIEventAdapter*  EventQueue::touchMoved(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, double time)
429{
430    if(_firstTouchEmulatesMouse)
431    {
432        _accumulateEventState->setX(x);
433        _accumulateEventState->setY(y);
434    }
435
436    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
437    event->setEventType(GUIEventAdapter::DRAG);
438    event->setTime(time);
439    event->addTouchPoint(id, phase, x, y, 0);
440    addEvent(event);
441
442    return event;
443}
444
445GUIEventAdapter*  EventQueue::touchEnded(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, unsigned int tap_count, double time)
446{
447    if (_firstTouchEmulatesMouse)
448    {
449        _accumulateEventState->setButtonMask(~(1) & _accumulateEventState->getButtonMask());
450        _accumulateEventState->setX(x);
451        _accumulateEventState->setY(y);
452    }
453
454    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
455    event->setEventType(GUIEventAdapter::RELEASE);
456    event->setTime(time);
457    event->addTouchPoint(id, phase, x, y, tap_count);
458    addEvent(event);
459
460    return event;
461}
462
463
464void EventQueue::closeWindow(double time)
465{
466    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
467    event->setEventType(GUIEventAdapter::CLOSE_WINDOW);
468    event->setTime(time);
469
470    addEvent(event);
471}
472
473void EventQueue::quitApplication(double time)
474{
475    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
476    event->setEventType(GUIEventAdapter::QUIT_APPLICATION);
477    event->setTime(time);
478
479    addEvent(event);
480}
481
482
483void EventQueue::frame(double time)
484{
485    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
486    event->setEventType(GUIEventAdapter::FRAME);
487    event->setTime(time);
488
489    addEvent(event);
490}
491
492GUIEventAdapter* EventQueue::createEvent()
493{
494    if (_accumulateEventState.valid()) return new GUIEventAdapter(*_accumulateEventState.get());
495    else return new GUIEventAdapter();
496}
497
498void EventQueue::userEvent(osg::Referenced* userEventData, double time)
499{
500    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
501    event->setEventType(GUIEventAdapter::USER);
502    event->setUserData(userEventData);
503    event->setTime(time);
504
505    addEvent(event);
506}
507
Note: See TracBrowser for help on using the browser.