]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/FGEventHandler.cxx
Reset: uninstall deletion-manager
[flightgear.git] / src / Viewer / FGEventHandler.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4 #include <osg/Camera>
5 #include <osg/GraphicsContext>
6 #include <osg/Math>
7 #include <osg/Viewport>
8 #include <osgViewer/Viewer>
9
10 #include <plib/pu.h>
11 #include <Main/fg_props.hxx>
12 #include "CameraGroup.hxx"
13 #include "FGEventHandler.hxx"
14 #include "WindowSystemAdapter.hxx"
15 #include "renderer.hxx"
16
17 #if !defined(X_DISPLAY_MISSING)
18 #define X_DOUBLE_SCROLL_BUG 1
19 #endif
20
21 #ifdef SG_MAC
22 // hack - during interactive resize on Mac, OSG queues and then flushes
23 // a large number of resize events, without doing any drawing.
24 extern void puCleanUpJunk ( void ) ;
25 #endif
26
27 namespace flightgear
28 {
29 const int displayStatsKey = 1;
30 const int printStatsKey = 2;
31
32
33 // The manipulator is responsible for updating a Viewer's camera. Its
34 // event handling method is also a convenient place to run the FG idle
35 // and draw handlers.
36
37 FGEventHandler::FGEventHandler() :
38     idleHandler(0),
39     keyHandler(0),
40     mouseClickHandler(0),
41     mouseMotionHandler(0),
42     statsHandler(new FGStatsHandler),
43     statsEvent(new osgGA::GUIEventAdapter),
44     statsType(osgViewer::StatsHandler::NO_STATS),
45     currentModifiers(0),
46     resizable(true),
47     mouseWarped(false),
48     scrollButtonPressed(false),
49     changeStatsCameraRenderOrder(false)
50 {
51     using namespace osgGA;
52     statsHandler->setKeyEventTogglesOnScreenStats(displayStatsKey);
53     statsHandler->setKeyEventPrintsOutStats(printStatsKey);
54     statsEvent->setEventType(GUIEventAdapter::KEYDOWN);
55
56     // OSG reports NumPad keycodes independent of the NumLock modifier.
57     // Both KP-4 and KP-Left are reported as KEY_KP_Left (0xff96), so we
58     // have to generate the locked keys ourselves.
59     numlockKeyMap[GUIEventAdapter::KEY_KP_Insert]  = '0';
60     numlockKeyMap[GUIEventAdapter::KEY_KP_End] = '1';
61     numlockKeyMap[GUIEventAdapter::KEY_KP_Down] = '2';
62     numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down] = '3';
63     numlockKeyMap[GUIEventAdapter::KEY_KP_Left] = '4';
64     numlockKeyMap[GUIEventAdapter::KEY_KP_Begin] = '5';
65     numlockKeyMap[GUIEventAdapter::KEY_KP_Right] = '6';
66     numlockKeyMap[GUIEventAdapter::KEY_KP_Home] = '7';
67     numlockKeyMap[GUIEventAdapter::KEY_KP_Up] = '8';
68     numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up] = '9';
69     numlockKeyMap[GUIEventAdapter::KEY_KP_Delete] = '.';
70
71     // The comment above is incorrect on Mac osgViewer, at least. So we
72     // need to map the 'num-locked' key codes to real values.
73     numlockKeyMap[GUIEventAdapter::KEY_KP_0]  = '0';
74     numlockKeyMap[GUIEventAdapter::KEY_KP_1] = '1';
75     numlockKeyMap[GUIEventAdapter::KEY_KP_2] = '2';
76     numlockKeyMap[GUIEventAdapter::KEY_KP_3] = '3';
77     numlockKeyMap[GUIEventAdapter::KEY_KP_4] = '4';
78     numlockKeyMap[GUIEventAdapter::KEY_KP_5] = '5';
79     numlockKeyMap[GUIEventAdapter::KEY_KP_6] = '6';
80     numlockKeyMap[GUIEventAdapter::KEY_KP_7] = '7';
81     numlockKeyMap[GUIEventAdapter::KEY_KP_8] = '8';
82     numlockKeyMap[GUIEventAdapter::KEY_KP_9] = '9';
83     numlockKeyMap[GUIEventAdapter::KEY_KP_Decimal] = '.';
84
85     
86     // mapping when NumLock is off
87     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Insert]     = PU_KEY_INSERT;
88     noNumlockKeyMap[GUIEventAdapter::KEY_KP_End]        = PU_KEY_END;
89     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Down]       = PU_KEY_DOWN;
90     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down]  = PU_KEY_PAGE_DOWN;
91     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Left]       = PU_KEY_LEFT;
92     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Begin]      = '5';
93     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Right]      = PU_KEY_RIGHT;
94     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Home]       = PU_KEY_HOME;
95     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Up]         = PU_KEY_UP;
96     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up]    = PU_KEY_PAGE_UP;
97     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Delete]     = 127;
98
99     for (int i = 0; i < 128; i++)
100         release_keys[i] = i;
101
102     _display = fgGetNode("/sim/rendering/on-screen-statistics", true);
103     _print = fgGetNode("/sim/rendering/print-statistics", true);
104 }
105
106 namespace
107 {
108 // Translate OSG modifier mask to FG modifier mask.
109 int osgToFGModifiers(int modifiers)
110 {
111     int result = 0;
112     if (modifiers & osgGA::GUIEventAdapter::MODKEY_SHIFT)
113         result |= KEYMOD_SHIFT;
114
115     if (modifiers & osgGA::GUIEventAdapter::MODKEY_CTRL)
116         result |= KEYMOD_CTRL;
117
118     if (modifiers & osgGA::GUIEventAdapter::MODKEY_ALT)
119         result |= KEYMOD_ALT;
120
121     if (modifiers & osgGA::GUIEventAdapter::MODKEY_META)
122         result |= KEYMOD_META;
123
124     if (modifiers & osgGA::GUIEventAdapter::MODKEY_SUPER)
125         result |= KEYMOD_SUPER;
126
127     if (modifiers & osgGA::GUIEventAdapter::MODKEY_HYPER)
128         result |= KEYMOD_HYPER;
129     return result;
130 }
131 }
132
133 #if 0
134 void FGEventHandler::init(const osgGA::GUIEventAdapter& ea,
135                           osgGA::GUIActionAdapter& us)
136 {
137     currentModifiers = osgToFGModifiers(ea.getModKeyMask());
138     (void)handle(ea, us);
139 }
140 #endif
141
142 // Calculate event coordinates in the viewport of the GUI camera, if
143 // possible. Otherwise return false and (-1, -1).
144 namespace
145 {
146 bool
147 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
148                 int& x, int& y)
149 {
150     x = -1;
151     y = -1;
152
153     const osg::GraphicsContext* eventGC = ea.getGraphicsContext();
154     if( !eventGC )
155       return false; // TODO how can this happen?
156     const osg::GraphicsContext::Traits* traits = eventGC->getTraits();
157     osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
158     if (!guiCamera)
159         return false;
160     osg::Viewport* vport = guiCamera->getViewport();
161     if (!vport)
162         return false;
163     
164     // Scale x, y to the dimensions of the window
165     double wx = (((ea.getX() - ea.getXmin()) / (ea.getXmax() - ea.getXmin()))
166                  * (float)traits->width);
167     double wy = (((ea.getY() - ea.getYmin()) / (ea.getYmax() - ea.getYmin()))
168                  * (float)traits->height);
169     if (vport->x() <= wx && wx <= vport->x() + vport->width()
170         && vport->y() <= wy && wy <= vport->y() + vport->height()) {
171         // Finally, into viewport coordinates. Change y to "increasing
172         // downwards".
173         x = wx - vport->x();
174         y = vport->height() - (wy - vport->y());
175         return true;
176     } else {
177         return false;
178     }
179 }
180 }
181
182 bool FGEventHandler::handle(const osgGA::GUIEventAdapter& ea,
183                             osgGA::GUIActionAdapter& us)
184 {
185     // Event handlers seem to be called even if the according event has already
186     // been handled. Already handled events shouldn't be handled multiple times
187     // so we need to exit here manually.
188     if(    ea.getHandled()
189            // Let mouse move events pass to correctly handle mouse cursor hide
190            // timeout while moving just on the canvas gui.
191            // TODO We should clean up the whole mouse input and make hide
192            //      timeout independent of the event handler which consumed the
193            //      event.
194         && ea.getEventType() != osgGA::GUIEventAdapter::MOVE
195         && ea.getEventType() != osgGA::GUIEventAdapter::DRAG )
196       return false;
197
198     int x = 0;
199     int y = 0;
200
201     switch (ea.getEventType()) {
202     case osgGA::GUIEventAdapter::FRAME:
203         mouseWarped = false;
204         handleStats(us);
205         return true;
206     case osgGA::GUIEventAdapter::KEYDOWN:
207     case osgGA::GUIEventAdapter::KEYUP:
208     {
209         int key, modmask;
210         handleKey(ea, key, modmask);
211         eventToViewport(ea, us, x, y);
212         if (keyHandler)
213             (*keyHandler)(key, modmask, x, y);
214         return true;
215     }
216     case osgGA::GUIEventAdapter::PUSH:
217     case osgGA::GUIEventAdapter::RELEASE:
218     {
219         bool mainWindow = eventToViewport(ea, us, x, y);
220         int button = 0;
221         switch (ea.getButton()) {
222         case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
223             button = 0;
224             break;
225         case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
226             button = 1;
227             break;
228         case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
229             button = 2;
230             break;
231         }
232         if (mouseClickHandler)
233             (*mouseClickHandler)(button,
234                                  (ea.getEventType()
235                                   == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
236         return true;
237     }
238     case osgGA::GUIEventAdapter::SCROLL:
239     {
240         bool mainWindow = eventToViewport(ea, us, x, y);
241 #ifdef X_DOUBLE_SCROLL_BUG
242         scrollButtonPressed = !scrollButtonPressed;
243         if (!scrollButtonPressed) // Drop the button release event
244             return true;
245 #endif
246         int button;
247         if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_2D) {
248             if (ea.getScrollingDeltaY() > 0)
249                 button = 3;
250             else if (ea.getScrollingDeltaY() < 0)
251                 button = 4;
252             else
253                 button = -1;
254         } else if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
255             button = 3;
256         else
257             button = 4;
258         if (mouseClickHandler && button != -1) {
259             (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
260             (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
261         }
262         return true;
263     }
264     case osgGA::GUIEventAdapter::MOVE:
265     case osgGA::GUIEventAdapter::DRAG:
266         // If we warped the mouse, then disregard all pointer motion
267         // events for this frame. We really want to flush the event
268         // queue of mouse events, but don't have the ability to do
269         // that with osgViewer.
270         if (mouseWarped)
271             return true;
272         if (eventToViewport(ea, us, x, y) && mouseMotionHandler)
273             (*mouseMotionHandler)(x, y, &ea);
274         return true;
275     case osgGA::GUIEventAdapter::RESIZE:
276         SG_LOG(SG_VIEW, SG_DEBUG, "FGEventHandler::handle: RESIZE event " << ea.getWindowHeight() << " x " << ea.getWindowWidth() << ", resizable: " << resizable);
277         CameraGroup::getDefault()->resized();
278         if (resizable)
279           globals->get_renderer()->resize(ea.getWindowWidth(), ea.getWindowHeight());
280         statsHandler->handle(ea, us);
281       #ifdef SG_MAC
282         // work around OSG Cocoa-Viewer issue with resize event handling,
283         // where resize events are queued up, then dispatched in a batch, with
284         // no interveningd drawing calls.
285         puCleanUpJunk();
286       #endif
287         return true;
288      case osgGA::GUIEventAdapter::CLOSE_WINDOW:
289     case osgGA::GUIEventAdapter::QUIT_APPLICATION:
290         fgOSExit(0);
291         return true;
292     default:
293         return false;
294     }
295 }
296
297 void FGEventHandler::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
298                                int& modifiers)
299 {
300     using namespace osgGA;
301     key = ea.getKey();
302     // XXX Probably other translations are needed too.
303     switch (key) {
304     case GUIEventAdapter::KEY_Escape:      key = 0x1b; break;
305     case GUIEventAdapter::KEY_Return:      key = '\n'; break;
306     case GUIEventAdapter::KEY_BackSpace:   key = '\b'; break;
307     case GUIEventAdapter::KEY_Delete:      key = 0x7f; break;
308     case GUIEventAdapter::KEY_Tab:         key = '\t'; break;
309     case GUIEventAdapter::KEY_Left:        key = PU_KEY_LEFT;      break;
310     case GUIEventAdapter::KEY_Up:          key = PU_KEY_UP;        break;
311     case GUIEventAdapter::KEY_Right:       key = PU_KEY_RIGHT;     break;
312     case GUIEventAdapter::KEY_Down:        key = PU_KEY_DOWN;      break;
313     case GUIEventAdapter::KEY_Page_Up:     key = PU_KEY_PAGE_UP;   break;
314     case GUIEventAdapter::KEY_Page_Down:   key = PU_KEY_PAGE_DOWN; break;
315     case GUIEventAdapter::KEY_Home:        key = PU_KEY_HOME;      break;
316     case GUIEventAdapter::KEY_End:         key = PU_KEY_END;       break;
317     case GUIEventAdapter::KEY_Insert:      key = PU_KEY_INSERT;    break;
318     case GUIEventAdapter::KEY_F1:          key = PU_KEY_F1;        break;
319     case GUIEventAdapter::KEY_F2:          key = PU_KEY_F2;        break;
320     case GUIEventAdapter::KEY_F3:          key = PU_KEY_F3;        break;
321     case GUIEventAdapter::KEY_F4:          key = PU_KEY_F4;        break;
322     case GUIEventAdapter::KEY_F5:          key = PU_KEY_F5;        break;
323     case GUIEventAdapter::KEY_F6:          key = PU_KEY_F6;        break;
324     case GUIEventAdapter::KEY_F7:          key = PU_KEY_F7;        break;
325     case GUIEventAdapter::KEY_F8:          key = PU_KEY_F8;        break;
326     case GUIEventAdapter::KEY_F9:          key = PU_KEY_F9;        break;
327     case GUIEventAdapter::KEY_F10:         key = PU_KEY_F10;       break;
328     case GUIEventAdapter::KEY_F11:         key = PU_KEY_F11;       break;
329     case GUIEventAdapter::KEY_F12:         key = PU_KEY_F12;       break;
330     case GUIEventAdapter::KEY_KP_Enter:    key = '\r'; break;
331     case GUIEventAdapter::KEY_KP_Add:      key = '+';  break;
332     case GUIEventAdapter::KEY_KP_Divide:   key = '/';  break;
333     case GUIEventAdapter::KEY_KP_Multiply: key = '*';  break;
334     case GUIEventAdapter::KEY_KP_Subtract: key = '-';  break;
335     }
336     osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
337
338 #ifdef __APPLE__
339     // Num Lock is always true on Mac
340     std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
341     if (numPadIter != numlockKeyMap.end()) {
342         key = numPadIter->second;
343     }
344 #else
345     if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK)
346     {
347         // NumLock on: map to numeric keys
348         std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
349         if (numPadIter != numlockKeyMap.end()) {
350             key = numPadIter->second;
351         }
352     }
353     else
354     {
355         // NumLock off: map to PU arrow keys
356         std::map<int, int>::iterator numPadIter = noNumlockKeyMap.find(key);
357         if (numPadIter != noNumlockKeyMap.end()) {
358             key = numPadIter->second;
359         }
360     }
361 #endif
362
363     modifiers = osgToFGModifiers(ea.getModKeyMask());
364     currentModifiers = modifiers;
365     if (eventType == osgGA::GUIEventAdapter::KEYUP)
366         modifiers |= KEYMOD_RELEASED;
367
368     // Release the letter key, for which the key press was reported. This
369     // is to deal with Ctrl-press -> a-press -> Ctrl-release -> a-release
370     // correctly.
371     if (key >= 0 && key < 128) {
372         if (modifiers & KEYMOD_RELEASED) {
373             key = release_keys[key];
374         } else {
375             release_keys[key] = key;
376             if (key >= 1 && key <= 26) {
377                 release_keys[key + '@'] = key;
378                 release_keys[key + '`'] = key;
379             } else if (key >= 'A' && key <= 'Z') {
380                 release_keys[key - '@'] = key;
381                 release_keys[tolower(key)] = key;
382             } else if (key >= 'a' && key <= 'z') {
383                 release_keys[key - '`'] = key;
384                 release_keys[toupper(key)] = key;
385             }
386         }
387     }
388 }
389
390 void FGEventHandler::handleStats(osgGA::GUIActionAdapter& us)
391 {
392     int type = _display->getIntValue() % osgViewer::StatsHandler::LAST;
393     if (type != statsType) {
394         statsEvent->setKey(displayStatsKey);
395         do {
396             statsType = (statsType + 1) % osgViewer::StatsHandler::LAST;
397             statsHandler->handle(*statsEvent, us);
398             if (changeStatsCameraRenderOrder) {
399                 statsHandler->getCamera()->setRenderOrder(osg::Camera::POST_RENDER, 99999);
400                 changeStatsCameraRenderOrder = false;
401             }
402         } while (statsType != type);
403
404         _display->setIntValue(statsType);
405     }
406
407     if (_print->getBoolValue()) {
408         statsEvent->setKey(printStatsKey);
409         statsHandler->handle(*statsEvent, us);
410         _print->setBoolValue(false);
411     }
412 }
413
414 void eventToWindowCoords(const osgGA::GUIEventAdapter* ea,
415                          double& x, double& y)
416 {
417     using namespace osg;
418     const GraphicsContext* gc = ea->getGraphicsContext();
419     const GraphicsContext::Traits* traits = gc->getTraits() ;
420     // Scale x, y to the dimensions of the window
421     x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
422          * (double)traits->width);
423     y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
424          * (double)traits->height);
425     if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS)
426         y = (double)traits->height - y;
427 }
428
429 #if 0
430 void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea,
431                               double& x, double& y)
432 {
433     using namespace osg;
434     const GraphicsContext* gc = ea->getGraphicsContext();
435     const GraphicsContext::Traits* traits = gc->getTraits() ;
436     // Scale x, y to the dimensions of the window
437     x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
438          * (double)traits->width);
439     y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
440          * (double)traits->height);
441     if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
442         y = (double)traits->height - y;
443 }
444 #endif
445     
446 }