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