]> git.mxchange.org Git - flightgear.git/blob - src/Viewer/FGEventHandler.cxx
Rely entirely on the configurable renderer and add more configuration options in...
[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     // mapping when NumLock is off
72     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Insert]     = PU_KEY_INSERT;
73     noNumlockKeyMap[GUIEventAdapter::KEY_KP_End]        = PU_KEY_END;
74     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Down]       = PU_KEY_DOWN;
75     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down]  = PU_KEY_PAGE_DOWN;
76     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Left]       = PU_KEY_LEFT;
77     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Begin]      = '5';
78     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Right]      = PU_KEY_RIGHT;
79     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Home]       = PU_KEY_HOME;
80     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Up]         = PU_KEY_UP;
81     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up]    = PU_KEY_PAGE_UP;
82     noNumlockKeyMap[GUIEventAdapter::KEY_KP_Delete]     = 127;
83
84     for (int i = 0; i < 128; i++)
85         release_keys[i] = i;
86 }
87
88 namespace
89 {
90 // Translate OSG modifier mask to FG modifier mask.
91 int osgToFGModifiers(int modifiers)
92 {
93     int result = 0;
94     if (modifiers & osgGA::GUIEventAdapter::MODKEY_SHIFT)
95         result |= KEYMOD_SHIFT;
96
97     if (modifiers & osgGA::GUIEventAdapter::MODKEY_CTRL)
98         result |= KEYMOD_CTRL;
99
100     if (modifiers & osgGA::GUIEventAdapter::MODKEY_ALT)
101         result |= KEYMOD_ALT;
102
103     if (modifiers & osgGA::GUIEventAdapter::MODKEY_META)
104         result |= KEYMOD_META;
105
106     if (modifiers & osgGA::GUIEventAdapter::MODKEY_SUPER)
107         result |= KEYMOD_SUPER;
108
109     if (modifiers & osgGA::GUIEventAdapter::MODKEY_HYPER)
110         result |= KEYMOD_HYPER;
111     return result;
112 }
113 }
114
115 #if 0
116 void FGEventHandler::init(const osgGA::GUIEventAdapter& ea,
117                           osgGA::GUIActionAdapter& us)
118 {
119     currentModifiers = osgToFGModifiers(ea.getModKeyMask());
120     (void)handle(ea, us);
121 }
122 #endif
123
124 // Calculate event coordinates in the viewport of the GUI camera, if
125 // possible. Otherwise return false and (-1, -1).
126 namespace
127 {
128 bool
129 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
130                 int& x, int& y)
131 {
132     x = -1;
133     y = -1;
134
135     const osg::GraphicsContext* eventGC = ea.getGraphicsContext();
136     const osg::GraphicsContext::Traits* traits = eventGC->getTraits();
137     osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
138     if (!guiCamera)
139         return false;
140     osg::Viewport* vport = guiCamera->getViewport();
141     if (!vport)
142         return false;
143     
144     // Scale x, y to the dimensions of the window
145     double wx = (((ea.getX() - ea.getXmin()) / (ea.getXmax() - ea.getXmin()))
146                  * (float)traits->width);
147     double wy = (((ea.getY() - ea.getYmin()) / (ea.getYmax() - ea.getYmin()))
148                  * (float)traits->height);
149     if (vport->x() <= wx && wx <= vport->x() + vport->width()
150         && vport->y() <= wy && wy <= vport->y() + vport->height()) {
151         // Finally, into viewport coordinates. Change y to "increasing
152         // downwards".
153         x = wx - vport->x();
154         y = vport->height() - (wy - vport->y());
155         return true;
156     } else {
157         return false;
158     }
159 }
160 }
161
162 bool FGEventHandler::handle(const osgGA::GUIEventAdapter& ea,
163                             osgGA::GUIActionAdapter& us)
164 {
165     int x = 0;
166     int y = 0;
167
168     switch (ea.getEventType()) {
169     case osgGA::GUIEventAdapter::FRAME:
170         mouseWarped = false;
171         handleStats(us);
172         return true;
173     case osgGA::GUIEventAdapter::KEYDOWN:
174     case osgGA::GUIEventAdapter::KEYUP:
175     {
176         int key, modmask;
177         handleKey(ea, key, modmask);
178         eventToViewport(ea, us, x, y);
179         if (keyHandler)
180             (*keyHandler)(key, modmask, x, y);
181         return true;
182     }
183     case osgGA::GUIEventAdapter::PUSH:
184     case osgGA::GUIEventAdapter::RELEASE:
185     {
186         bool mainWindow = eventToViewport(ea, us, x, y);
187         int button = 0;
188         switch (ea.getButton()) {
189         case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
190             button = 0;
191             break;
192         case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
193             button = 1;
194             break;
195         case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
196             button = 2;
197             break;
198         }
199         if (mouseClickHandler)
200             (*mouseClickHandler)(button,
201                                  (ea.getEventType()
202                                   == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
203         return true;
204     }
205     case osgGA::GUIEventAdapter::SCROLL:
206     {
207         bool mainWindow = eventToViewport(ea, us, x, y);
208 #ifdef X_DOUBLE_SCROLL_BUG
209         scrollButtonPressed = !scrollButtonPressed;
210         if (!scrollButtonPressed) // Drop the button release event
211             return true;
212 #endif
213         int button;
214         if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_2D) {
215             if (ea.getScrollingDeltaY() > 0)
216                 button = 3;
217             else if (ea.getScrollingDeltaY() < 0)
218                 button = 4;
219             else
220                 button = -1;
221         } else if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
222             button = 3;
223         else
224             button = 4;
225         if (mouseClickHandler && button != -1) {
226             (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
227             (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
228         }
229         return true;
230     }
231     case osgGA::GUIEventAdapter::MOVE:
232     case osgGA::GUIEventAdapter::DRAG:
233         // If we warped the mouse, then disregard all pointer motion
234         // events for this frame. We really want to flush the event
235         // queue of mouse events, but don't have the ability to do
236         // that with osgViewer.
237         if (mouseWarped)
238             return true;
239         if (eventToViewport(ea, us, x, y) && mouseMotionHandler)
240             (*mouseMotionHandler)(x, y);
241         return true;
242     case osgGA::GUIEventAdapter::RESIZE:
243         SG_LOG(SG_VIEW, SG_DEBUG, "FGEventHandler::handle: RESIZE event " << ea.getWindowHeight() << " x " << ea.getWindowWidth() << ", resizable: " << resizable);
244         CameraGroup::getDefault()->resized();
245         if (resizable)
246           globals->get_renderer()->resize(ea.getWindowWidth(), ea.getWindowHeight());
247         statsHandler->handle(ea, us);
248       #ifdef SG_MAC
249         // work around OSG Cocoa-Viewer issue with resize event handling,
250         // where resize events are queued up, then dispatched in a batch, with
251         // no interveningd drawing calls.
252         puCleanUpJunk();
253       #endif
254         return true;
255      case osgGA::GUIEventAdapter::CLOSE_WINDOW:
256     case osgGA::GUIEventAdapter::QUIT_APPLICATION:
257         fgOSExit(0);
258         return true;
259     default:
260         return false;
261     }
262 }
263
264 void FGEventHandler::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
265                                int& modifiers)
266 {
267     using namespace osgGA;
268     key = ea.getKey();
269     // XXX Probably other translations are needed too.
270     switch (key) {
271     case GUIEventAdapter::KEY_Escape:      key = 0x1b; break;
272     case GUIEventAdapter::KEY_Return:      key = '\n'; break;
273     case GUIEventAdapter::KEY_BackSpace:   key = '\b'; break;
274     case GUIEventAdapter::KEY_Delete:      key = 0x7f; break;
275     case GUIEventAdapter::KEY_Tab:         key = '\t'; break;
276     case GUIEventAdapter::KEY_Left:        key = PU_KEY_LEFT;      break;
277     case GUIEventAdapter::KEY_Up:          key = PU_KEY_UP;        break;
278     case GUIEventAdapter::KEY_Right:       key = PU_KEY_RIGHT;     break;
279     case GUIEventAdapter::KEY_Down:        key = PU_KEY_DOWN;      break;
280     case GUIEventAdapter::KEY_Page_Up:     key = PU_KEY_PAGE_UP;   break;
281     case GUIEventAdapter::KEY_Page_Down:   key = PU_KEY_PAGE_DOWN; break;
282     case GUIEventAdapter::KEY_Home:        key = PU_KEY_HOME;      break;
283     case GUIEventAdapter::KEY_End:         key = PU_KEY_END;       break;
284     case GUIEventAdapter::KEY_Insert:      key = PU_KEY_INSERT;    break;
285     case GUIEventAdapter::KEY_F1:          key = PU_KEY_F1;        break;
286     case GUIEventAdapter::KEY_F2:          key = PU_KEY_F2;        break;
287     case GUIEventAdapter::KEY_F3:          key = PU_KEY_F3;        break;
288     case GUIEventAdapter::KEY_F4:          key = PU_KEY_F4;        break;
289     case GUIEventAdapter::KEY_F5:          key = PU_KEY_F5;        break;
290     case GUIEventAdapter::KEY_F6:          key = PU_KEY_F6;        break;
291     case GUIEventAdapter::KEY_F7:          key = PU_KEY_F7;        break;
292     case GUIEventAdapter::KEY_F8:          key = PU_KEY_F8;        break;
293     case GUIEventAdapter::KEY_F9:          key = PU_KEY_F9;        break;
294     case GUIEventAdapter::KEY_F10:         key = PU_KEY_F10;       break;
295     case GUIEventAdapter::KEY_F11:         key = PU_KEY_F11;       break;
296     case GUIEventAdapter::KEY_F12:         key = PU_KEY_F12;       break;
297     case GUIEventAdapter::KEY_KP_Enter:    key = '\r'; break;
298     case GUIEventAdapter::KEY_KP_Add:      key = '+';  break;
299     case GUIEventAdapter::KEY_KP_Divide:   key = '/';  break;
300     case GUIEventAdapter::KEY_KP_Multiply: key = '*';  break;
301     case GUIEventAdapter::KEY_KP_Subtract: key = '-';  break;
302     }
303     osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
304
305 #ifdef __APPLE__
306     // Num Lock is always true on Mac
307     std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
308     if (numPadIter != numlockKeyMap.end()) {
309         key = numPadIter->second;
310     }
311 #else
312     if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK)
313     {
314         // NumLock on: map to numeric keys
315         std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
316         if (numPadIter != numlockKeyMap.end()) {
317             key = numPadIter->second;
318         }
319     }
320     else
321     {
322         // NumLock off: map to PU arrow keys
323         std::map<int, int>::iterator numPadIter = noNumlockKeyMap.find(key);
324         if (numPadIter != noNumlockKeyMap.end()) {
325             key = numPadIter->second;
326         }
327     }
328 #endif
329
330     modifiers = osgToFGModifiers(ea.getModKeyMask());
331     currentModifiers = modifiers;
332     if (eventType == osgGA::GUIEventAdapter::KEYUP)
333         modifiers |= KEYMOD_RELEASED;
334
335     // Release the letter key, for which the key press was reported. This
336     // is to deal with Ctrl-press -> a-press -> Ctrl-release -> a-release
337     // correctly.
338     if (key >= 0 && key < 128) {
339         if (modifiers & KEYMOD_RELEASED) {
340             key = release_keys[key];
341         } else {
342             release_keys[key] = key;
343             if (key >= 1 && key <= 26) {
344                 release_keys[key + '@'] = key;
345                 release_keys[key + '`'] = key;
346             } else if (key >= 'A' && key <= 'Z') {
347                 release_keys[key - '@'] = key;
348                 release_keys[tolower(key)] = key;
349             } else if (key >= 'a' && key <= 'z') {
350                 release_keys[key - '`'] = key;
351                 release_keys[toupper(key)] = key;
352             }
353         }
354     }
355 }
356
357 void FGEventHandler::handleStats(osgGA::GUIActionAdapter& us)
358 {
359     static SGPropertyNode_ptr display = fgGetNode("/sim/rendering/on-screen-statistics", true);
360     static SGPropertyNode_ptr print = fgGetNode("/sim/rendering/print-statistics", true);
361
362     int type = display->getIntValue() % osgViewer::StatsHandler::LAST;
363     if (type != statsType) {
364         statsEvent->setKey(displayStatsKey);
365         do {
366             statsType = (statsType + 1) % osgViewer::StatsHandler::LAST;
367             statsHandler->handle(*statsEvent, us);
368             if (changeStatsCameraRenderOrder) {
369                 statsHandler->getCamera()->setRenderOrder(osg::Camera::POST_RENDER, 99999);
370                 changeStatsCameraRenderOrder = false;
371             }
372         } while (statsType != type);
373
374         display->setIntValue(statsType);
375     }
376
377     if (print->getBoolValue()) {
378         statsEvent->setKey(printStatsKey);
379         statsHandler->handle(*statsEvent, us);
380         print->setBoolValue(false);
381     }
382 }
383
384 void eventToWindowCoords(const osgGA::GUIEventAdapter* ea,
385                          double& x, double& y)
386 {
387     using namespace osg;
388     const GraphicsContext* gc = ea->getGraphicsContext();
389     const GraphicsContext::Traits* traits = gc->getTraits() ;
390     // Scale x, y to the dimensions of the window
391     x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
392          * (double)traits->width);
393     y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
394          * (double)traits->height);
395     if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS)
396         y = (double)traits->height - y;
397 }
398
399 void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea,
400                               double& x, double& y)
401 {
402     using namespace osg;
403     const GraphicsContext* gc = ea->getGraphicsContext();
404     const GraphicsContext::Traits* traits = gc->getTraits() ;
405     // Scale x, y to the dimensions of the window
406     x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
407          * (double)traits->width);
408     y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
409          * (double)traits->height);
410     if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
411         y = (double)traits->height - y;
412 }
413 }