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