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