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