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