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