]> git.mxchange.org Git - flightgear.git/blob - src/Main/FGManipulator.cxx
Restore sky
[flightgear.git] / src / Main / FGManipulator.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 "FGManipulator.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 FGManipulator::FGManipulator() :
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 void FGManipulator::setByMatrix(const osg::Matrixd& matrix)
69 {
70     // Yuck
71     position = matrix.getTrans();
72     attitude = matrix.getRotate();
73 }
74
75 osg::Matrixd FGManipulator::getMatrix() const
76 {
77     return osg::Matrixd::rotate(attitude) * osg::Matrixd::translate(position);
78 }
79
80 osg::Matrixd FGManipulator::getInverseMatrix() const
81 {
82     return (osg::Matrixd::translate(-position)
83             * osg::Matrixd::rotate(attitude.inverse())) ;
84 }
85
86 // Not used, but part of the interface.
87 void FGManipulator::setNode(osg::Node* node)
88 {
89     _node = node;
90 }
91
92 const osg::Node* FGManipulator::getNode() const
93 {
94     return _node.get();
95 }
96
97 osg::Node* FGManipulator::getNode()
98 {
99     return _node.get();
100 }
101
102 namespace
103 {
104 // Translate OSG modifier mask to FG modifier mask.
105 int osgToFGModifiers(int modifiers)
106 {
107     int result = 0;
108     if (modifiers & osgGA::GUIEventAdapter::MODKEY_SHIFT)
109         result |= KEYMOD_SHIFT;
110
111     if (modifiers & osgGA::GUIEventAdapter::MODKEY_CTRL)
112         result |= KEYMOD_CTRL;
113
114     if (modifiers & osgGA::GUIEventAdapter::MODKEY_ALT)
115         result |= KEYMOD_ALT;
116
117     if (modifiers & osgGA::GUIEventAdapter::MODKEY_META)
118         result |= KEYMOD_META;
119
120     if (modifiers & osgGA::GUIEventAdapter::MODKEY_SUPER)
121         result |= KEYMOD_SUPER;
122
123     if (modifiers & osgGA::GUIEventAdapter::MODKEY_HYPER)
124         result |= KEYMOD_HYPER;
125     return result;
126 }
127 }
128
129 void FGManipulator::init(const osgGA::GUIEventAdapter& ea,
130                          osgGA::GUIActionAdapter& us)
131 {
132     currentModifiers = osgToFGModifiers(ea.getModKeyMask());
133     (void)handle(ea, us);
134 }
135
136 // Calculate event coordinates in the viewport of the GUI camera, if
137 // possible. Otherwise return false and (-1, -1).
138 namespace
139 {
140 bool
141 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
142                 int& x, int& y)
143 {
144     x = -1;
145     y = -1;
146
147     const osg::GraphicsContext* eventGC = ea.getGraphicsContext();
148     const osg::GraphicsContext::Traits* traits = eventGC->getTraits();
149     osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
150     if (!guiCamera)
151         return false;
152     osg::Viewport* vport = guiCamera->getViewport();
153     if (!vport)
154         return false;
155     
156     // Scale x, y to the dimensions of the window
157     double wx = (((ea.getX() - ea.getXmin()) / (ea.getXmax() - ea.getXmin()))
158                  * (float)traits->width);
159     double wy = (((ea.getY() - ea.getYmin()) / (ea.getYmax() - ea.getYmin()))
160                  * (float)traits->height);
161     if (vport->x() <= wx && wx <= vport->x() + vport->width()
162         && vport->y() <= wy && wy <= vport->y() + vport->height()) {
163         // Finally, into viewport coordinates. Change y to "increasing
164         // downwards".
165         x = wx - vport->x();
166         y = vport->height() - (wy - vport->y());
167         return true;
168     } else {
169         return false;
170     }
171 }
172 }
173
174 bool FGManipulator::handle(const osgGA::GUIEventAdapter& ea,
175                            osgGA::GUIActionAdapter& us)
176 {
177     int x = 0;
178     int y = 0;
179
180     switch (ea.getEventType()) {
181     case osgGA::GUIEventAdapter::FRAME:
182         if (idleHandler)
183             (*idleHandler)();
184         if (drawHandler)
185             (*drawHandler)();
186         mouseWarped = false;
187         handleStats(us);
188         return true;
189     case osgGA::GUIEventAdapter::KEYDOWN:
190     case osgGA::GUIEventAdapter::KEYUP:
191     {
192         int key, modmask;
193         handleKey(ea, key, modmask);
194         eventToViewport(ea, us, x, y);
195         if (keyHandler)
196             (*keyHandler)(key, modmask, x, y);
197         return true;
198     }
199     case osgGA::GUIEventAdapter::PUSH:
200     case osgGA::GUIEventAdapter::RELEASE:
201     {
202         bool mainWindow = eventToViewport(ea, us, x, y);
203         int button = 0;
204         switch (ea.getButton()) {
205         case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
206             button = 0;
207             break;
208         case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
209             button = 1;
210             break;
211         case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
212             button = 2;
213             break;
214         }
215         if (mouseClickHandler)
216             (*mouseClickHandler)(button,
217                                  (ea.getEventType()
218                                   == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
219         return true;
220     }
221     case osgGA::GUIEventAdapter::SCROLL:
222     {
223         bool mainWindow = eventToViewport(ea, us, x, y);
224 #ifdef X_DOUBLE_SCROLL_BUG
225         scrollButtonPressed = !scrollButtonPressed;
226         if (!scrollButtonPressed) // Drop the button release event
227             return true;
228 #endif
229         int button;
230         if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
231             button = 3;
232         else
233             button = 4;
234         if (mouseClickHandler) {
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         if (resizable && windowResizeHandler)
253             (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
254         return true;
255      case osgGA::GUIEventAdapter::CLOSE_WINDOW:
256     case osgGA::GUIEventAdapter::QUIT_APPLICATION:
257         fgOSExit(0);
258         return true;
259     default:
260         return false;
261     }
262 }
263
264 void FGManipulator::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
265                               int& modifiers)
266 {
267     using namespace osgGA;
268     key = ea.getKey();
269     // XXX Probably other translations are needed too.
270     switch (key) {
271     case GUIEventAdapter::KEY_Escape:      key = 0x1b; break;
272     case GUIEventAdapter::KEY_Return:      key = '\n'; break;
273     case GUIEventAdapter::KEY_BackSpace:   key = '\b'; break;
274     case GUIEventAdapter::KEY_Delete:      key = 0x7f; break;
275     case GUIEventAdapter::KEY_Tab:         key = '\t'; break;
276     case GUIEventAdapter::KEY_Left:        key = PU_KEY_LEFT;      break;
277     case GUIEventAdapter::KEY_Up:          key = PU_KEY_UP;        break;
278     case GUIEventAdapter::KEY_Right:       key = PU_KEY_RIGHT;     break;
279     case GUIEventAdapter::KEY_Down:        key = PU_KEY_DOWN;      break;
280     case GUIEventAdapter::KEY_Page_Up:     key = PU_KEY_PAGE_UP;   break;
281     case GUIEventAdapter::KEY_Page_Down:   key = PU_KEY_PAGE_DOWN; break;
282     case GUIEventAdapter::KEY_Home:        key = PU_KEY_HOME;      break;
283     case GUIEventAdapter::KEY_End:         key = PU_KEY_END;       break;
284     case GUIEventAdapter::KEY_Insert:      key = PU_KEY_INSERT;    break;
285     case GUIEventAdapter::KEY_F1:          key = PU_KEY_F1;        break;
286     case GUIEventAdapter::KEY_F2:          key = PU_KEY_F2;        break;
287     case GUIEventAdapter::KEY_F3:          key = PU_KEY_F3;        break;
288     case GUIEventAdapter::KEY_F4:          key = PU_KEY_F4;        break;
289     case GUIEventAdapter::KEY_F5:          key = PU_KEY_F5;        break;
290     case GUIEventAdapter::KEY_F6:          key = PU_KEY_F6;        break;
291     case GUIEventAdapter::KEY_F7:          key = PU_KEY_F7;        break;
292     case GUIEventAdapter::KEY_F8:          key = PU_KEY_F8;        break;
293     case GUIEventAdapter::KEY_F9:          key = PU_KEY_F9;        break;
294     case GUIEventAdapter::KEY_F10:         key = PU_KEY_F10;       break;
295     case GUIEventAdapter::KEY_F11:         key = PU_KEY_F11;       break;
296     case GUIEventAdapter::KEY_F12:         key = PU_KEY_F12;       break;
297     case GUIEventAdapter::KEY_KP_Delete:   key = '.';  break;
298     case GUIEventAdapter::KEY_KP_Enter:    key = '\r'; break;
299     case GUIEventAdapter::KEY_KP_Add:      key = '+';  break;
300     case GUIEventAdapter::KEY_KP_Divide:   key = '/';  break;
301     case GUIEventAdapter::KEY_KP_Multiply: key = '*';  break;
302     case GUIEventAdapter::KEY_KP_Subtract: key = '-';  break;
303     }
304     osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
305
306     std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
307
308     if (numPadIter != numlockKeyMap.end()) {
309         if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK) {
310             key = numPadIter->second;
311         }
312     }
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 FGManipulator::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 }