]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/FGEventHandler.cxx
MouseInput changes to support hover.
[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     const osg::GraphicsContext::Traits* traits = eventGC->getTraits();
155     osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
156     if (!guiCamera)
157         return false;
158     osg::Viewport* vport = guiCamera->getViewport();
159     if (!vport)
160         return false;
161     
162     // Scale x, y to the dimensions of the window
163     double wx = (((ea.getX() - ea.getXmin()) / (ea.getXmax() - ea.getXmin()))
164                  * (float)traits->width);
165     double wy = (((ea.getY() - ea.getYmin()) / (ea.getYmax() - ea.getYmin()))
166                  * (float)traits->height);
167     if (vport->x() <= wx && wx <= vport->x() + vport->width()
168         && vport->y() <= wy && wy <= vport->y() + vport->height()) {
169         // Finally, into viewport coordinates. Change y to "increasing
170         // downwards".
171         x = wx - vport->x();
172         y = vport->height() - (wy - vport->y());
173         return true;
174     } else {
175         return false;
176     }
177 }
178 }
179
180 bool FGEventHandler::handle(const osgGA::GUIEventAdapter& ea,
181                             osgGA::GUIActionAdapter& us)
182 {
183     // Event handlers seem to be called even if the according event has already
184     // been handled. Already handled events shouldn't be handled multiple times
185     // so we need to exit here manually.
186     if( ea.getHandled() )
187       return false;
188
189     int x = 0;
190     int y = 0;
191
192     switch (ea.getEventType()) {
193     case osgGA::GUIEventAdapter::FRAME:
194         mouseWarped = false;
195         handleStats(us);
196         return true;
197     case osgGA::GUIEventAdapter::KEYDOWN:
198     case osgGA::GUIEventAdapter::KEYUP:
199     {
200         int key, modmask;
201         handleKey(ea, key, modmask);
202         eventToViewport(ea, us, x, y);
203         if (keyHandler)
204             (*keyHandler)(key, modmask, x, y);
205         return true;
206     }
207     case osgGA::GUIEventAdapter::PUSH:
208     case osgGA::GUIEventAdapter::RELEASE:
209     {
210         bool mainWindow = eventToViewport(ea, us, x, y);
211         int button = 0;
212         switch (ea.getButton()) {
213         case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
214             button = 0;
215             break;
216         case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
217             button = 1;
218             break;
219         case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
220             button = 2;
221             break;
222         }
223         if (mouseClickHandler)
224             (*mouseClickHandler)(button,
225                                  (ea.getEventType()
226                                   == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
227         return true;
228     }
229     case osgGA::GUIEventAdapter::SCROLL:
230     {
231         bool mainWindow = eventToViewport(ea, us, x, y);
232 #ifdef X_DOUBLE_SCROLL_BUG
233         scrollButtonPressed = !scrollButtonPressed;
234         if (!scrollButtonPressed) // Drop the button release event
235             return true;
236 #endif
237         int button;
238         if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_2D) {
239             if (ea.getScrollingDeltaY() > 0)
240                 button = 3;
241             else if (ea.getScrollingDeltaY() < 0)
242                 button = 4;
243             else
244                 button = -1;
245         } else if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
246             button = 3;
247         else
248             button = 4;
249         if (mouseClickHandler && button != -1) {
250             (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
251             (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
252         }
253         return true;
254     }
255     case osgGA::GUIEventAdapter::MOVE:
256     case osgGA::GUIEventAdapter::DRAG:
257         // If we warped the mouse, then disregard all pointer motion
258         // events for this frame. We really want to flush the event
259         // queue of mouse events, but don't have the ability to do
260         // that with osgViewer.
261         if (mouseWarped)
262             return true;
263         if (eventToViewport(ea, us, x, y) && mouseMotionHandler)
264             (*mouseMotionHandler)(x, y, &ea);
265         return true;
266     case osgGA::GUIEventAdapter::RESIZE:
267         SG_LOG(SG_VIEW, SG_DEBUG, "FGEventHandler::handle: RESIZE event " << ea.getWindowHeight() << " x " << ea.getWindowWidth() << ", resizable: " << resizable);
268         CameraGroup::getDefault()->resized();
269         if (resizable)
270           globals->get_renderer()->resize(ea.getWindowWidth(), ea.getWindowHeight());
271         statsHandler->handle(ea, us);
272       #ifdef SG_MAC
273         // work around OSG Cocoa-Viewer issue with resize event handling,
274         // where resize events are queued up, then dispatched in a batch, with
275         // no interveningd drawing calls.
276         puCleanUpJunk();
277       #endif
278         return true;
279      case osgGA::GUIEventAdapter::CLOSE_WINDOW:
280     case osgGA::GUIEventAdapter::QUIT_APPLICATION:
281         fgOSExit(0);
282         return true;
283     default:
284         return false;
285     }
286 }
287
288 void FGEventHandler::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
289                                int& modifiers)
290 {
291     using namespace osgGA;
292     key = ea.getKey();
293     // XXX Probably other translations are needed too.
294     switch (key) {
295     case GUIEventAdapter::KEY_Escape:      key = 0x1b; break;
296     case GUIEventAdapter::KEY_Return:      key = '\n'; break;
297     case GUIEventAdapter::KEY_BackSpace:   key = '\b'; break;
298     case GUIEventAdapter::KEY_Delete:      key = 0x7f; break;
299     case GUIEventAdapter::KEY_Tab:         key = '\t'; break;
300     case GUIEventAdapter::KEY_Left:        key = PU_KEY_LEFT;      break;
301     case GUIEventAdapter::KEY_Up:          key = PU_KEY_UP;        break;
302     case GUIEventAdapter::KEY_Right:       key = PU_KEY_RIGHT;     break;
303     case GUIEventAdapter::KEY_Down:        key = PU_KEY_DOWN;      break;
304     case GUIEventAdapter::KEY_Page_Up:     key = PU_KEY_PAGE_UP;   break;
305     case GUIEventAdapter::KEY_Page_Down:   key = PU_KEY_PAGE_DOWN; break;
306     case GUIEventAdapter::KEY_Home:        key = PU_KEY_HOME;      break;
307     case GUIEventAdapter::KEY_End:         key = PU_KEY_END;       break;
308     case GUIEventAdapter::KEY_Insert:      key = PU_KEY_INSERT;    break;
309     case GUIEventAdapter::KEY_F1:          key = PU_KEY_F1;        break;
310     case GUIEventAdapter::KEY_F2:          key = PU_KEY_F2;        break;
311     case GUIEventAdapter::KEY_F3:          key = PU_KEY_F3;        break;
312     case GUIEventAdapter::KEY_F4:          key = PU_KEY_F4;        break;
313     case GUIEventAdapter::KEY_F5:          key = PU_KEY_F5;        break;
314     case GUIEventAdapter::KEY_F6:          key = PU_KEY_F6;        break;
315     case GUIEventAdapter::KEY_F7:          key = PU_KEY_F7;        break;
316     case GUIEventAdapter::KEY_F8:          key = PU_KEY_F8;        break;
317     case GUIEventAdapter::KEY_F9:          key = PU_KEY_F9;        break;
318     case GUIEventAdapter::KEY_F10:         key = PU_KEY_F10;       break;
319     case GUIEventAdapter::KEY_F11:         key = PU_KEY_F11;       break;
320     case GUIEventAdapter::KEY_F12:         key = PU_KEY_F12;       break;
321     case GUIEventAdapter::KEY_KP_Enter:    key = '\r'; break;
322     case GUIEventAdapter::KEY_KP_Add:      key = '+';  break;
323     case GUIEventAdapter::KEY_KP_Divide:   key = '/';  break;
324     case GUIEventAdapter::KEY_KP_Multiply: key = '*';  break;
325     case GUIEventAdapter::KEY_KP_Subtract: key = '-';  break;
326     }
327     osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
328
329 #ifdef __APPLE__
330     // Num Lock is always true on Mac
331     std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
332     if (numPadIter != numlockKeyMap.end()) {
333         key = numPadIter->second;
334     }
335 #else
336     if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK)
337     {
338         // NumLock on: map to numeric keys
339         std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
340         if (numPadIter != numlockKeyMap.end()) {
341             key = numPadIter->second;
342         }
343     }
344     else
345     {
346         // NumLock off: map to PU arrow keys
347         std::map<int, int>::iterator numPadIter = noNumlockKeyMap.find(key);
348         if (numPadIter != noNumlockKeyMap.end()) {
349             key = numPadIter->second;
350         }
351     }
352 #endif
353
354     modifiers = osgToFGModifiers(ea.getModKeyMask());
355     currentModifiers = modifiers;
356     if (eventType == osgGA::GUIEventAdapter::KEYUP)
357         modifiers |= KEYMOD_RELEASED;
358
359     // Release the letter key, for which the key press was reported. This
360     // is to deal with Ctrl-press -> a-press -> Ctrl-release -> a-release
361     // correctly.
362     if (key >= 0 && key < 128) {
363         if (modifiers & KEYMOD_RELEASED) {
364             key = release_keys[key];
365         } else {
366             release_keys[key] = key;
367             if (key >= 1 && key <= 26) {
368                 release_keys[key + '@'] = key;
369                 release_keys[key + '`'] = key;
370             } else if (key >= 'A' && key <= 'Z') {
371                 release_keys[key - '@'] = key;
372                 release_keys[tolower(key)] = key;
373             } else if (key >= 'a' && key <= 'z') {
374                 release_keys[key - '`'] = key;
375                 release_keys[toupper(key)] = key;
376             }
377         }
378     }
379 }
380
381 void FGEventHandler::handleStats(osgGA::GUIActionAdapter& us)
382 {
383     int type = _display->getIntValue() % osgViewer::StatsHandler::LAST;
384     if (type != statsType) {
385         statsEvent->setKey(displayStatsKey);
386         do {
387             statsType = (statsType + 1) % osgViewer::StatsHandler::LAST;
388             statsHandler->handle(*statsEvent, us);
389             if (changeStatsCameraRenderOrder) {
390                 statsHandler->getCamera()->setRenderOrder(osg::Camera::POST_RENDER, 99999);
391                 changeStatsCameraRenderOrder = false;
392             }
393         } while (statsType != type);
394
395         _display->setIntValue(statsType);
396     }
397
398     if (_print->getBoolValue()) {
399         statsEvent->setKey(printStatsKey);
400         statsHandler->handle(*statsEvent, us);
401         _print->setBoolValue(false);
402     }
403 }
404
405 void eventToWindowCoords(const osgGA::GUIEventAdapter* ea,
406                          double& x, double& y)
407 {
408     using namespace osg;
409     const GraphicsContext* gc = ea->getGraphicsContext();
410     const GraphicsContext::Traits* traits = gc->getTraits() ;
411     // Scale x, y to the dimensions of the window
412     x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
413          * (double)traits->width);
414     y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
415          * (double)traits->height);
416     if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS)
417         y = (double)traits->height - y;
418 }
419
420 #if 0
421 void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea,
422                               double& x, double& y)
423 {
424     using namespace osg;
425     const GraphicsContext* gc = ea->getGraphicsContext();
426     const GraphicsContext::Traits* traits = gc->getTraits() ;
427     // Scale x, y to the dimensions of the window
428     x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
429          * (double)traits->width);
430     y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
431          * (double)traits->height);
432     if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
433         y = (double)traits->height - y;
434 }
435 #endif
436     
437 }