5 #include <osg/GraphicsContext>
7 #include <osg/Viewport>
8 #include <osgViewer/Viewer>
11 #include <Main/fg_props.hxx>
12 #include "CameraGroup.hxx"
13 #include "FGManipulator.hxx"
14 #include "WindowSystemAdapter.hxx"
16 #if !defined(X_DISPLAY_MISSING)
17 #define X_DOUBLE_SCROLL_BUG 1
22 const int displayStatsKey = 1;
23 const int printStatsKey = 2;
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
30 FGManipulator::FGManipulator() :
33 windowResizeHandler(0),
36 mouseMotionHandler(0),
37 statsHandler(new osgViewer::StatsHandler),
38 statsEvent(new osgGA::GUIEventAdapter),
39 statsType(osgViewer::StatsHandler::NO_STATS),
43 scrollButtonPressed(false)
45 using namespace osgGA;
46 statsHandler->setKeyEventTogglesOnScreenStats(displayStatsKey);
47 statsHandler->setKeyEventPrintsOutStats(printStatsKey);
48 statsEvent->setEventType(GUIEventAdapter::KEYDOWN);
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';
64 for (int i = 0; i < 128; i++)
68 void FGManipulator::setByMatrix(const osg::Matrixd& matrix)
71 position = matrix.getTrans();
72 attitude = matrix.getRotate();
75 osg::Matrixd FGManipulator::getMatrix() const
77 return osg::Matrixd::rotate(attitude) * osg::Matrixd::translate(position);
80 osg::Matrixd FGManipulator::getInverseMatrix() const
82 return (osg::Matrixd::translate(-position)
83 * osg::Matrixd::rotate(attitude.inverse())) ;
86 // Not used, but part of the interface.
87 void FGManipulator::setNode(osg::Node* node)
92 const osg::Node* FGManipulator::getNode() const
97 osg::Node* FGManipulator::getNode()
104 // Translate OSG modifier mask to FG modifier mask.
105 int osgToFGModifiers(int modifiers)
108 if (modifiers & osgGA::GUIEventAdapter::MODKEY_SHIFT)
109 result |= KEYMOD_SHIFT;
111 if (modifiers & osgGA::GUIEventAdapter::MODKEY_CTRL)
112 result |= KEYMOD_CTRL;
114 if (modifiers & osgGA::GUIEventAdapter::MODKEY_ALT)
115 result |= KEYMOD_ALT;
117 if (modifiers & osgGA::GUIEventAdapter::MODKEY_META)
118 result |= KEYMOD_META;
120 if (modifiers & osgGA::GUIEventAdapter::MODKEY_SUPER)
121 result |= KEYMOD_SUPER;
123 if (modifiers & osgGA::GUIEventAdapter::MODKEY_HYPER)
124 result |= KEYMOD_HYPER;
129 void FGManipulator::init(const osgGA::GUIEventAdapter& ea,
130 osgGA::GUIActionAdapter& us)
132 currentModifiers = osgToFGModifiers(ea.getModKeyMask());
133 (void)handle(ea, us);
136 // Calculate event coordinates in the viewport of the GUI camera, if
137 // possible. Otherwise return false and (-1, -1).
141 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
147 const osg::GraphicsContext* eventGC = ea.getGraphicsContext();
148 const osg::GraphicsContext::Traits* traits = eventGC->getTraits();
149 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
152 osg::Viewport* vport = guiCamera->getViewport();
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
166 y = vport->height() - (wy - vport->y());
174 bool FGManipulator::handle(const osgGA::GUIEventAdapter& ea,
175 osgGA::GUIActionAdapter& us)
180 switch (ea.getEventType()) {
181 case osgGA::GUIEventAdapter::FRAME:
189 case osgGA::GUIEventAdapter::KEYDOWN:
190 case osgGA::GUIEventAdapter::KEYUP:
193 handleKey(ea, key, modmask);
194 eventToViewport(ea, us, x, y);
196 (*keyHandler)(key, modmask, x, y);
199 case osgGA::GUIEventAdapter::PUSH:
200 case osgGA::GUIEventAdapter::RELEASE:
202 bool mainWindow = eventToViewport(ea, us, x, y);
204 switch (ea.getButton()) {
205 case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
208 case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
211 case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
215 if (mouseClickHandler)
216 (*mouseClickHandler)(button,
218 == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
221 case osgGA::GUIEventAdapter::SCROLL:
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
230 if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
234 if (mouseClickHandler) {
235 (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
236 (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
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.
248 if (eventToViewport(ea, us, x, y) && mouseMotionHandler)
249 (*mouseMotionHandler)(x, y);
251 case osgGA::GUIEventAdapter::RESIZE:
252 if (resizable && windowResizeHandler)
253 (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
255 case osgGA::GUIEventAdapter::CLOSE_WINDOW:
256 case osgGA::GUIEventAdapter::QUIT_APPLICATION:
264 void FGManipulator::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
267 using namespace osgGA;
269 // XXX Probably other translations are needed too.
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;
304 osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
306 std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
308 if (numPadIter != numlockKeyMap.end()) {
309 if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK) {
310 key = numPadIter->second;
314 modifiers = osgToFGModifiers(ea.getModKeyMask());
315 currentModifiers = modifiers;
316 if (eventType == osgGA::GUIEventAdapter::KEYUP)
317 modifiers |= KEYMOD_RELEASED;
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
322 if (key >= 0 && key < 128) {
323 if (modifiers & KEYMOD_RELEASED) {
324 key = release_keys[key];
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;
341 void FGManipulator::handleStats(osgGA::GUIActionAdapter& us)
343 static SGPropertyNode_ptr display = fgGetNode("/sim/rendering/on-screen-statistics", true);
344 static SGPropertyNode_ptr print = fgGetNode("/sim/rendering/print-statistics", true);
346 int type = display->getIntValue() % osgViewer::StatsHandler::LAST;
347 if (type != statsType) {
348 statsEvent->setKey(displayStatsKey);
350 statsType = (statsType + 1) % osgViewer::StatsHandler::LAST;
351 statsHandler->handle(*statsEvent, us);
352 } while (statsType != type);
354 display->setIntValue(statsType);
357 if (print->getBoolValue()) {
358 statsEvent->setKey(printStatsKey);
359 statsHandler->handle(*statsEvent, us);
360 print->setBoolValue(false);
364 void eventToWindowCoords(const osgGA::GUIEventAdapter* ea,
365 double& x, double& y)
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;
379 void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea,
380 double& x, double& y)
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;