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

Revision 13277, 18.3 kB (checked in by robert, 12 days ago)

Moved widgets from VolumeEditorWidget? to TransferFunctionWidget?, and widget utilities into WidgetUtils?.

  • 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(GUIEventAdapter::LEFT_MOUSE_BUTTON | _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    if(_firstTouchEmulatesMouse)
422        event->setButton(GUIEventAdapter::LEFT_MOUSE_BUTTON);
423   
424    addEvent(event);
425
426    return event;
427}
428
429
430GUIEventAdapter*  EventQueue::touchMoved(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, double time)
431{
432    if(_firstTouchEmulatesMouse)
433    {
434        _accumulateEventState->setX(x);
435        _accumulateEventState->setY(y);
436    }
437
438    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
439    event->setEventType(GUIEventAdapter::DRAG);
440    event->setTime(time);
441    event->addTouchPoint(id, phase, x, y, 0);
442    addEvent(event);
443
444    return event;
445}
446
447GUIEventAdapter*  EventQueue::touchEnded(unsigned int id, GUIEventAdapter::TouchPhase phase, float x, float y, unsigned int tap_count, double time)
448{
449    if (_firstTouchEmulatesMouse)
450    {
451        _accumulateEventState->setButtonMask(~GUIEventAdapter::LEFT_MOUSE_BUTTON & _accumulateEventState->getButtonMask());
452        _accumulateEventState->setX(x);
453        _accumulateEventState->setY(y);
454    }
455
456    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
457    event->setEventType(GUIEventAdapter::RELEASE);
458    event->setTime(time);
459    event->addTouchPoint(id, phase, x, y, tap_count);
460    if(_firstTouchEmulatesMouse)
461        event->setButton(GUIEventAdapter::LEFT_MOUSE_BUTTON);
462   
463    addEvent(event);
464
465    return event;
466}
467
468
469void EventQueue::closeWindow(double time)
470{
471    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
472    event->setEventType(GUIEventAdapter::CLOSE_WINDOW);
473    event->setTime(time);
474
475    addEvent(event);
476}
477
478void EventQueue::quitApplication(double time)
479{
480    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
481    event->setEventType(GUIEventAdapter::QUIT_APPLICATION);
482    event->setTime(time);
483
484    addEvent(event);
485}
486
487
488void EventQueue::frame(double time)
489{
490    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
491    event->setEventType(GUIEventAdapter::FRAME);
492    event->setTime(time);
493
494    addEvent(event);
495}
496
497GUIEventAdapter* EventQueue::createEvent()
498{
499    if (_accumulateEventState.valid()) return new GUIEventAdapter(*_accumulateEventState.get());
500    else return new GUIEventAdapter();
501}
502
503void EventQueue::userEvent(osg::Referenced* userEventData, double time)
504{
505    GUIEventAdapter* event = new GUIEventAdapter(*_accumulateEventState);
506    event->setEventType(GUIEventAdapter::USER);
507    event->setUserData(userEventData);
508    event->setTime(time);
509
510    addEvent(event);
511}
512
Note: See TracBrowser for help on using the browser.