]> git.mxchange.org Git - flightgear.git/blob - src/Main/FGEventHandler.cxx
16babdeada1ff98ce6949fc2ccfef12292708c39
[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         if (idleHandler)
151             (*idleHandler)();
152         if (drawHandler)
153             (*drawHandler)();
154         mouseWarped = false;
155         handleStats(us);
156         return true;
157     case osgGA::GUIEventAdapter::KEYDOWN:
158     case osgGA::GUIEventAdapter::KEYUP:
159     {
160         int key, modmask;
161         handleKey(ea, key, modmask);
162         eventToViewport(ea, us, x, y);
163         if (keyHandler)
164             (*keyHandler)(key, modmask, x, y);
165         return true;
166     }
167     case osgGA::GUIEventAdapter::PUSH:
168     case osgGA::GUIEventAdapter::RELEASE:
169     {
170         bool mainWindow = eventToViewport(ea, us, x, y);
171         int button = 0;
172         switch (ea.getButton()) {
173         case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
174             button = 0;
175             break;
176         case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
177             button = 1;
178             break;
179         case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
180             button = 2;
181             break;
182         }
183         if (mouseClickHandler)
184             (*mouseClickHandler)(button,
185                                  (ea.getEventType()
186                                   == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
187         return true;
188     }
189     case osgGA::GUIEventAdapter::SCROLL:
190     {
191         bool mainWindow = eventToViewport(ea, us, x, y);
192 #ifdef X_DOUBLE_SCROLL_BUG
193         scrollButtonPressed = !scrollButtonPressed;
194         if (!scrollButtonPressed) // Drop the button release event
195             return true;
196 #endif
197         int button;
198         if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
199             button = 3;
200         else
201             button = 4;
202         if (mouseClickHandler) {
203             (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
204             (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
205         }
206         return true;
207     }
208     case osgGA::GUIEventAdapter::MOVE:
209     case osgGA::GUIEventAdapter::DRAG:
210         // If we warped the mouse, then disregard all pointer motion
211         // events for this frame. We really want to flush the event
212         // queue of mouse events, but don't have the ability to do
213         // that with osgViewer.
214         if (mouseWarped)
215             return true;
216         if (eventToViewport(ea, us, x, y) && mouseMotionHandler)
217             (*mouseMotionHandler)(x, y);
218         return true;
219     case osgGA::GUIEventAdapter::RESIZE:
220         CameraGroup::getDefault()->resized();
221         if (resizable && windowResizeHandler)
222             (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
223         return true;
224      case osgGA::GUIEventAdapter::CLOSE_WINDOW:
225     case osgGA::GUIEventAdapter::QUIT_APPLICATION:
226         fgOSExit(0);
227         return true;
228     default:
229         return false;
230     }
231 }
232
233 void FGEventHandler::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
234                                int& modifiers)
235 {
236     using namespace osgGA;
237     key = ea.getKey();
238     // XXX Probably other translations are needed too.
239     switch (key) {
240     case GUIEventAdapter::KEY_Escape:      key = 0x1b; break;
241     case GUIEventAdapter::KEY_Return:      key = '\n'; break;
242     case GUIEventAdapter::KEY_BackSpace:   key = '\b'; break;
243     case GUIEventAdapter::KEY_Delete:      key = 0x7f; break;
244     case GUIEventAdapter::KEY_Tab:         key = '\t'; break;
245     case GUIEventAdapter::KEY_Left:        key = PU_KEY_LEFT;      break;
246     case GUIEventAdapter::KEY_Up:          key = PU_KEY_UP;        break;
247     case GUIEventAdapter::KEY_Right:       key = PU_KEY_RIGHT;     break;
248     case GUIEventAdapter::KEY_Down:        key = PU_KEY_DOWN;      break;
249     case GUIEventAdapter::KEY_Page_Up:     key = PU_KEY_PAGE_UP;   break;
250     case GUIEventAdapter::KEY_Page_Down:   key = PU_KEY_PAGE_DOWN; break;
251     case GUIEventAdapter::KEY_Home:        key = PU_KEY_HOME;      break;
252     case GUIEventAdapter::KEY_End:         key = PU_KEY_END;       break;
253     case GUIEventAdapter::KEY_Insert:      key = PU_KEY_INSERT;    break;
254     case GUIEventAdapter::KEY_F1:          key = PU_KEY_F1;        break;
255     case GUIEventAdapter::KEY_F2:          key = PU_KEY_F2;        break;
256     case GUIEventAdapter::KEY_F3:          key = PU_KEY_F3;        break;
257     case GUIEventAdapter::KEY_F4:          key = PU_KEY_F4;        break;
258     case GUIEventAdapter::KEY_F5:          key = PU_KEY_F5;        break;
259     case GUIEventAdapter::KEY_F6:          key = PU_KEY_F6;        break;
260     case GUIEventAdapter::KEY_F7:          key = PU_KEY_F7;        break;
261     case GUIEventAdapter::KEY_F8:          key = PU_KEY_F8;        break;
262     case GUIEventAdapter::KEY_F9:          key = PU_KEY_F9;        break;
263     case GUIEventAdapter::KEY_F10:         key = PU_KEY_F10;       break;
264     case GUIEventAdapter::KEY_F11:         key = PU_KEY_F11;       break;
265     case GUIEventAdapter::KEY_F12:         key = PU_KEY_F12;       break;
266     case GUIEventAdapter::KEY_KP_Delete:   key = '.';  break;
267     case GUIEventAdapter::KEY_KP_Enter:    key = '\r'; break;
268     case GUIEventAdapter::KEY_KP_Add:      key = '+';  break;
269     case GUIEventAdapter::KEY_KP_Divide:   key = '/';  break;
270     case GUIEventAdapter::KEY_KP_Multiply: key = '*';  break;
271     case GUIEventAdapter::KEY_KP_Subtract: key = '-';  break;
272     }
273     osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
274
275     std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
276
277     if (numPadIter != numlockKeyMap.end()) {
278         if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK) {
279             key = numPadIter->second;
280         }
281     }
282
283     modifiers = osgToFGModifiers(ea.getModKeyMask());
284     currentModifiers = modifiers;
285     if (eventType == osgGA::GUIEventAdapter::KEYUP)
286         modifiers |= KEYMOD_RELEASED;
287
288     // Release the letter key, for which the key press was reported. This
289     // is to deal with Ctrl-press -> a-press -> Ctrl-release -> a-release
290     // correctly.
291     if (key >= 0 && key < 128) {
292         if (modifiers & KEYMOD_RELEASED) {
293             key = release_keys[key];
294         } else {
295             release_keys[key] = key;
296             if (key >= 1 && key <= 26) {
297                 release_keys[key + '@'] = key;
298                 release_keys[key + '`'] = key;
299             } else if (key >= 'A' && key <= 'Z') {
300                 release_keys[key - '@'] = key;
301                 release_keys[tolower(key)] = key;
302             } else if (key >= 'a' && key <= 'z') {
303                 release_keys[key - '`'] = key;
304                 release_keys[toupper(key)] = key;
305             }
306         }
307     }
308 }
309
310 void FGEventHandler::handleStats(osgGA::GUIActionAdapter& us)
311 {
312     static SGPropertyNode_ptr display = fgGetNode("/sim/rendering/on-screen-statistics", true);
313     static SGPropertyNode_ptr print = fgGetNode("/sim/rendering/print-statistics", true);
314
315     int type = display->getIntValue() % osgViewer::StatsHandler::LAST;
316     if (type != statsType) {
317         statsEvent->setKey(displayStatsKey);
318         do {
319             statsType = (statsType + 1) % osgViewer::StatsHandler::LAST;
320             statsHandler->handle(*statsEvent, us);
321         } while (statsType != type);
322
323         display->setIntValue(statsType);
324     }
325
326     if (print->getBoolValue()) {
327         statsEvent->setKey(printStatsKey);
328         statsHandler->handle(*statsEvent, us);
329         print->setBoolValue(false);
330     }
331 }
332
333 void eventToWindowCoords(const osgGA::GUIEventAdapter* ea,
334                          double& x, double& y)
335 {
336     using namespace osg;
337     const GraphicsContext* gc = ea->getGraphicsContext();
338     const GraphicsContext::Traits* traits = gc->getTraits() ;
339     // Scale x, y to the dimensions of the window
340     x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
341          * (double)traits->width);
342     y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
343          * (double)traits->height);
344     if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS)
345         y = (double)traits->height - y;
346 }
347
348 void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea,
349                               double& x, double& y)
350 {
351     using namespace osg;
352     const GraphicsContext* gc = ea->getGraphicsContext();
353     const GraphicsContext::Traits* traits = gc->getTraits() ;
354     // Scale x, y to the dimensions of the window
355     x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
356          * (double)traits->width);
357     y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
358          * (double)traits->height);
359     if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
360         y = (double)traits->height - y;
361 }
362 }