]> git.mxchange.org Git - flightgear.git/blob - src/Main/FGManipulator.cxx
Add a command to dump just the terrain portion of the scene graph to a .osg
[flightgear.git] / src / Main / FGManipulator.cxx
1 #ifdef HAVE_CONFIG_H
2 #  include <config.h>
3 #endif
4 #include <osg/Math>
5 #include <osgViewer/Viewer>
6 #include <plib/pu.h>
7 #include "FGManipulator.hxx"
8
9 #if !defined(X_DISPLAY_MISSING)
10 #define X_DOUBLE_SCROLL_BUG 1
11 #endif
12
13 // The manipulator is responsible for updating a Viewer's camera. It's
14 // event handling method is also a convenient place to run the the FG
15 // idle and draw handlers.
16
17 FGManipulator::FGManipulator() :
18     idleHandler(0), drawHandler(0), windowResizeHandler(0), keyHandler(0),
19     mouseClickHandler(0), mouseMotionHandler(0), currentModifiers(0),
20     osgModifiers(0), resizable(true), mouseWarped(false),
21     scrollButtonPressed(false)
22 {
23     using namespace osgGA;
24     
25     keyMaskMap[GUIEventAdapter::KEY_Shift_L]
26         = GUIEventAdapter::MODKEY_LEFT_SHIFT;
27     keyMaskMap[GUIEventAdapter::KEY_Shift_R]
28         = GUIEventAdapter::MODKEY_RIGHT_SHIFT;
29     keyMaskMap[GUIEventAdapter::KEY_Control_L]
30         = GUIEventAdapter::MODKEY_LEFT_CTRL;
31     keyMaskMap[GUIEventAdapter::KEY_Control_R]
32         = GUIEventAdapter::MODKEY_RIGHT_CTRL;
33     keyMaskMap[GUIEventAdapter::KEY_Alt_L] = GUIEventAdapter::MODKEY_LEFT_ALT;
34     keyMaskMap[GUIEventAdapter::KEY_Alt_R] = GUIEventAdapter::MODKEY_RIGHT_ALT;
35     // We have to implement numlock too.
36     numlockKeyMap[GUIEventAdapter::KEY_KP_Insert]  = '0';
37     numlockKeyMap[GUIEventAdapter::KEY_KP_End] = '1';
38     numlockKeyMap[GUIEventAdapter::KEY_KP_Down] = '2';
39     numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down] = '3';
40     numlockKeyMap[GUIEventAdapter::KEY_KP_Left] = '4';
41     numlockKeyMap[GUIEventAdapter::KEY_KP_Begin] = '5';
42     numlockKeyMap[GUIEventAdapter::KEY_KP_Right] = '6';
43     numlockKeyMap[GUIEventAdapter::KEY_KP_Home] = '7';
44     numlockKeyMap[GUIEventAdapter::KEY_KP_Up] = '8';
45     numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up] = '9';
46
47     for (int i = 0; i < 128; i++)
48         release_keys[i] = i;
49 }
50
51 void FGManipulator::setByMatrix(const osg::Matrixd& matrix)
52 {
53     // Yuck
54     position = matrix.getTrans();
55     attitude = matrix.getRotate();
56 }
57
58 osg::Matrixd FGManipulator::getMatrix() const
59 {
60     return osg::Matrixd::rotate(attitude) * osg::Matrixd::translate(position);
61 }
62
63 osg::Matrixd FGManipulator::getInverseMatrix() const
64 {
65     return (osg::Matrixd::translate(-position)
66             * osg::Matrixd::rotate(attitude.inverse())) ;
67 }
68
69 // Not used, but part of the interface.
70 void FGManipulator::setNode(osg::Node* node)
71 {
72     _node = node;
73 }
74     
75 const osg::Node* FGManipulator::getNode() const
76 {
77     return _node.get();
78 }
79
80 osg::Node* FGManipulator::getNode()
81 {
82     return _node.get();
83 }
84
85 // All the usual translation from window system to FG / plib
86 static int osgToFGModifiers(int modifiers)
87 {
88     int result = 0;
89     if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_SHIFT |
90                      osgGA::GUIEventAdapter::MODKEY_RIGHT_SHIFT))
91         result |= KEYMOD_SHIFT;
92     if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL |
93                      osgGA::GUIEventAdapter::MODKEY_RIGHT_CTRL))
94         result |= KEYMOD_CTRL;
95     if (modifiers & (osgGA::GUIEventAdapter::MODKEY_LEFT_ALT |
96                      osgGA::GUIEventAdapter::MODKEY_RIGHT_ALT))
97         result |= KEYMOD_ALT;
98     return result;
99 }
100
101 void FGManipulator::init(const osgGA::GUIEventAdapter& ea,
102                          osgGA::GUIActionAdapter& us)
103 {
104     currentModifiers = osgToFGModifiers(ea.getModKeyMask());
105     (void)handle(ea, us);
106 }
107
108 static 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 osgViewer::Viewer* viewer;
116   viewer = dynamic_cast<const osgViewer::Viewer*>(&us);
117   if (!viewer)
118       return false;
119
120   float lx, ly;
121   const osg::Camera* camera;
122   camera = viewer->getCameraContainingPosition(ea.getX(), ea.getY(), lx, ly);
123
124   if (!fgOSIsMainCamera(camera))
125       return false;
126
127   x = int(lx);
128   y = int(camera->getViewport()->height() - ly);
129
130   return true;
131 }
132
133 bool FGManipulator::handle(const osgGA::GUIEventAdapter& ea,
134                            osgGA::GUIActionAdapter& us)
135 {
136     int x, y;
137     switch (ea.getEventType()) {
138     case osgGA::GUIEventAdapter::FRAME:
139         if (idleHandler)
140             (*idleHandler)();
141         if (drawHandler)
142             (*drawHandler)();
143         mouseWarped = false;
144         return true;
145     case osgGA::GUIEventAdapter::KEYDOWN:
146     case osgGA::GUIEventAdapter::KEYUP:
147     {
148         int key, modmask;
149         handleKey(ea, key, modmask);
150         eventToViewport(ea, us, x, y);
151         if (keyHandler)
152             (*keyHandler)(key, modmask, x, y);
153         return true;
154     }
155     case osgGA::GUIEventAdapter::PUSH:
156     case osgGA::GUIEventAdapter::RELEASE:
157     {
158         bool mainWindow = eventToViewport(ea, us, x, y);
159         int button = 0;
160         switch (ea.getButton()) {
161         case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
162             button = 0;
163             break;
164         case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
165             button = 1;
166             break;
167         case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
168             button = 2;
169             break;
170         }
171         if (mouseClickHandler)
172             (*mouseClickHandler)(button,
173                                  (ea.getEventType()
174                                   == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
175         return true;
176     }
177     case osgGA::GUIEventAdapter::SCROLL:
178     {
179         bool mainWindow = eventToViewport(ea, us, x, y);
180 #ifdef X_DOUBLE_SCROLL_BUG
181         scrollButtonPressed = !scrollButtonPressed;
182         if (!scrollButtonPressed) // Drop the button release event
183             return true;
184 #endif
185         int button;
186         if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
187             button = 3;
188         else
189             button = 4;
190         if (mouseClickHandler) {
191             (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
192             (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
193         }
194         return true;
195     }
196     case osgGA::GUIEventAdapter::MOVE:
197     case osgGA::GUIEventAdapter::DRAG:
198         // If we warped the mouse, then disregard all pointer motion
199         // events for this frame. We really want to flush the event
200         // queue of mouse events, but don't have the ability to do
201         // that with osgViewer.
202         if (mouseWarped)
203             return true;
204         eventToViewport(ea, us, x, y);
205         if (mouseMotionHandler)
206             (*mouseMotionHandler)(x, y);
207         return true;
208     case osgGA::GUIEventAdapter::RESIZE:
209         if (resizable && windowResizeHandler)
210             (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
211         return true;
212      case osgGA::GUIEventAdapter::CLOSE_WINDOW:
213     case osgGA::GUIEventAdapter::QUIT_APPLICATION:
214         fgOSExit(0);
215         return true;
216     default:
217         return false;
218     }
219 }
220
221 void FGManipulator::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
222                               int& modifiers)
223 {
224     key = ea.getKey();
225     // XXX Probably other translations are needed too.
226     switch (key) {
227     case osgGA::GUIEventAdapter::KEY_Escape: key = 0x1b; break;
228     case osgGA::GUIEventAdapter::KEY_Return: key = '\n'; break;
229     case osgGA::GUIEventAdapter::KEY_BackSpace: key = '\b'; break;
230     case osgGA::GUIEventAdapter::KEY_Delete:   key = 0x7f; break;
231     case osgGA::GUIEventAdapter::KEY_Tab:      key = '\t'; break;
232     case osgGA::GUIEventAdapter::KEY_Left:     key = PU_KEY_LEFT;      break;
233     case osgGA::GUIEventAdapter::KEY_Up:       key = PU_KEY_UP;        break;
234     case osgGA::GUIEventAdapter::KEY_Right:    key = PU_KEY_RIGHT;     break;
235     case osgGA::GUIEventAdapter::KEY_Down:     key = PU_KEY_DOWN;      break;
236     case osgGA::GUIEventAdapter::KEY_Page_Up:   key = PU_KEY_PAGE_UP;   break;
237     case osgGA::GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break;
238     case osgGA::GUIEventAdapter::KEY_Home:     key = PU_KEY_HOME;      break;
239     case osgGA::GUIEventAdapter::KEY_End:      key = PU_KEY_END;       break;
240     case osgGA::GUIEventAdapter::KEY_Insert:   key = PU_KEY_INSERT;    break;
241     case osgGA::GUIEventAdapter::KEY_F1:       key = PU_KEY_F1;        break;
242     case osgGA::GUIEventAdapter::KEY_F2:       key = PU_KEY_F2;        break;
243     case osgGA::GUIEventAdapter::KEY_F3:       key = PU_KEY_F3;        break;
244     case osgGA::GUIEventAdapter::KEY_F4:       key = PU_KEY_F4;        break;
245     case osgGA::GUIEventAdapter::KEY_F5:       key = PU_KEY_F5;        break;
246     case osgGA::GUIEventAdapter::KEY_F6:       key = PU_KEY_F6;        break;
247     case osgGA::GUIEventAdapter::KEY_F7:       key = PU_KEY_F7;        break;
248     case osgGA::GUIEventAdapter::KEY_F8:       key = PU_KEY_F8;        break;
249     case osgGA::GUIEventAdapter::KEY_F9:       key = PU_KEY_F9;        break;
250     case osgGA::GUIEventAdapter::KEY_F10:      key = PU_KEY_F10;       break;
251     case osgGA::GUIEventAdapter::KEY_F11:      key = PU_KEY_F11;       break;
252     case osgGA::GUIEventAdapter::KEY_F12:      key = PU_KEY_F12;       break;
253     case osgGA::GUIEventAdapter::KEY_KP_Delete: key = '.'; break;
254     case osgGA::GUIEventAdapter::KEY_KP_Enter: key = '\r'; break;
255     case osgGA::GUIEventAdapter::KEY_KP_Add:   key = '+'; break;
256     case osgGA::GUIEventAdapter::KEY_KP_Divide: key = '/'; break;
257     case osgGA::GUIEventAdapter::KEY_KP_Multiply: key = '*'; break;
258     case osgGA::GUIEventAdapter::KEY_KP_Subtract: key = '-'; break;
259     }
260     osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
261     std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
262
263     if (numPadIter != numlockKeyMap.end()) {
264         if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK) {
265             key = numPadIter->second;
266         }
267     } else {
268         // Track the modifiers because OSG is currently (2.0) broken
269         KeyMaskMap::iterator iter = keyMaskMap.find(key);
270         if (iter != keyMaskMap.end()) {
271             int mask = iter->second;
272             if (eventType == osgGA::GUIEventAdapter::KEYUP)
273                 osgModifiers &= ~mask;
274             else
275                 osgModifiers |= mask;
276         }
277     }
278     modifiers = osgToFGModifiers(osgModifiers);
279     currentModifiers = modifiers;
280     if (eventType == osgGA::GUIEventAdapter::KEYUP)
281         modifiers |= KEYMOD_RELEASED;
282
283     // Release the letter key, for which the keypress was reported
284     if (key >= 0 && key < 128) {
285         if (modifiers & KEYMOD_RELEASED) {
286             key = release_keys[key];
287         } else {
288             release_keys[key] = key;
289             if (key >= 1 && key <= 26) {
290                 release_keys[key + '@'] = key;
291                 release_keys[key + '`'] = key;
292             } else if (key >= 'A' && key <= 'Z') {
293                 release_keys[key - '@'] = key;
294                 release_keys[tolower(key)] = key;
295             } else if (key >= 'a' && key <= 'z') {
296                 release_keys[key - '`'] = key;
297                 release_keys[toupper(key)] = key;
298             }
299         }
300     }
301 }
302