5 #include <osg/GraphicsContext>
7 #include <osg/Viewport>
8 #include <osgViewer/Viewer>
11 #include <Main/fg_props.hxx>
12 #include "CameraGroup.hxx"
13 #include "FGEventHandler.hxx"
14 #include "WindowSystemAdapter.hxx"
15 #include "renderer.hxx"
17 #if !defined(X_DISPLAY_MISSING)
18 #define X_DOUBLE_SCROLL_BUG 1
23 const int displayStatsKey = 1;
24 const int printStatsKey = 2;
27 // The manipulator is responsible for updating a Viewer's camera. Its
28 // event handling method is also a convenient place to run the FG idle
31 FGEventHandler::FGEventHandler() :
35 mouseMotionHandler(0),
36 statsHandler(new osgViewer::StatsHandler),
37 statsEvent(new osgGA::GUIEventAdapter),
38 statsType(osgViewer::StatsHandler::NO_STATS),
42 scrollButtonPressed(false)
44 using namespace osgGA;
45 statsHandler->setKeyEventTogglesOnScreenStats(displayStatsKey);
46 statsHandler->setKeyEventPrintsOutStats(printStatsKey);
47 statsEvent->setEventType(GUIEventAdapter::KEYDOWN);
49 // OSG reports NumPad keycodes independent of the NumLock modifier.
50 // Both KP-4 and KP-Left are reported as KEY_KP_Left (0xff96), so we
51 // have to generate the locked keys ourselves.
52 numlockKeyMap[GUIEventAdapter::KEY_KP_Insert] = '0';
53 numlockKeyMap[GUIEventAdapter::KEY_KP_End] = '1';
54 numlockKeyMap[GUIEventAdapter::KEY_KP_Down] = '2';
55 numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down] = '3';
56 numlockKeyMap[GUIEventAdapter::KEY_KP_Left] = '4';
57 numlockKeyMap[GUIEventAdapter::KEY_KP_Begin] = '5';
58 numlockKeyMap[GUIEventAdapter::KEY_KP_Right] = '6';
59 numlockKeyMap[GUIEventAdapter::KEY_KP_Home] = '7';
60 numlockKeyMap[GUIEventAdapter::KEY_KP_Up] = '8';
61 numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up] = '9';
62 numlockKeyMap[GUIEventAdapter::KEY_KP_Delete] = '.';
64 // mapping when NumLock is off
65 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Insert] = PU_KEY_INSERT;
66 noNumlockKeyMap[GUIEventAdapter::KEY_KP_End] = PU_KEY_END;
67 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Down] = PU_KEY_DOWN;
68 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down] = PU_KEY_PAGE_DOWN;
69 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Left] = PU_KEY_LEFT;
70 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Begin] = '5';
71 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Right] = PU_KEY_RIGHT;
72 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Home] = PU_KEY_HOME;
73 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Up] = PU_KEY_UP;
74 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up] = PU_KEY_PAGE_UP;
75 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Delete] = 127;
77 for (int i = 0; i < 128; i++)
83 // Translate OSG modifier mask to FG modifier mask.
84 int osgToFGModifiers(int modifiers)
87 if (modifiers & osgGA::GUIEventAdapter::MODKEY_SHIFT)
88 result |= KEYMOD_SHIFT;
90 if (modifiers & osgGA::GUIEventAdapter::MODKEY_CTRL)
91 result |= KEYMOD_CTRL;
93 if (modifiers & osgGA::GUIEventAdapter::MODKEY_ALT)
96 if (modifiers & osgGA::GUIEventAdapter::MODKEY_META)
97 result |= KEYMOD_META;
99 if (modifiers & osgGA::GUIEventAdapter::MODKEY_SUPER)
100 result |= KEYMOD_SUPER;
102 if (modifiers & osgGA::GUIEventAdapter::MODKEY_HYPER)
103 result |= KEYMOD_HYPER;
109 void FGEventHandler::init(const osgGA::GUIEventAdapter& ea,
110 osgGA::GUIActionAdapter& us)
112 currentModifiers = osgToFGModifiers(ea.getModKeyMask());
113 (void)handle(ea, us);
117 // Calculate event coordinates in the viewport of the GUI camera, if
118 // possible. Otherwise return false and (-1, -1).
122 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
128 const osg::GraphicsContext* eventGC = ea.getGraphicsContext();
129 const osg::GraphicsContext::Traits* traits = eventGC->getTraits();
130 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
133 osg::Viewport* vport = guiCamera->getViewport();
137 // Scale x, y to the dimensions of the window
138 double wx = (((ea.getX() - ea.getXmin()) / (ea.getXmax() - ea.getXmin()))
139 * (float)traits->width);
140 double wy = (((ea.getY() - ea.getYmin()) / (ea.getYmax() - ea.getYmin()))
141 * (float)traits->height);
142 if (vport->x() <= wx && wx <= vport->x() + vport->width()
143 && vport->y() <= wy && wy <= vport->y() + vport->height()) {
144 // Finally, into viewport coordinates. Change y to "increasing
147 y = vport->height() - (wy - vport->y());
155 bool FGEventHandler::handle(const osgGA::GUIEventAdapter& ea,
156 osgGA::GUIActionAdapter& us)
161 switch (ea.getEventType()) {
162 case osgGA::GUIEventAdapter::FRAME:
166 case osgGA::GUIEventAdapter::KEYDOWN:
167 case osgGA::GUIEventAdapter::KEYUP:
170 handleKey(ea, key, modmask);
171 eventToViewport(ea, us, x, y);
173 (*keyHandler)(key, modmask, x, y);
176 case osgGA::GUIEventAdapter::PUSH:
177 case osgGA::GUIEventAdapter::RELEASE:
179 bool mainWindow = eventToViewport(ea, us, x, y);
181 switch (ea.getButton()) {
182 case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
185 case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
188 case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
192 if (mouseClickHandler)
193 (*mouseClickHandler)(button,
195 == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
198 case osgGA::GUIEventAdapter::SCROLL:
200 bool mainWindow = eventToViewport(ea, us, x, y);
201 #ifdef X_DOUBLE_SCROLL_BUG
202 scrollButtonPressed = !scrollButtonPressed;
203 if (!scrollButtonPressed) // Drop the button release event
207 if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_2D) {
208 if (ea.getScrollingDeltaY() > 0)
210 else if (ea.getScrollingDeltaY() < 0)
212 } else if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
216 if (mouseClickHandler) {
217 (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
218 (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
222 case osgGA::GUIEventAdapter::MOVE:
223 case osgGA::GUIEventAdapter::DRAG:
224 // If we warped the mouse, then disregard all pointer motion
225 // events for this frame. We really want to flush the event
226 // queue of mouse events, but don't have the ability to do
227 // that with osgViewer.
230 if (eventToViewport(ea, us, x, y) && mouseMotionHandler)
231 (*mouseMotionHandler)(x, y);
233 case osgGA::GUIEventAdapter::RESIZE:
234 CameraGroup::getDefault()->resized();
236 globals->get_renderer()->resize(ea.getWindowWidth(), ea.getWindowHeight());
238 case osgGA::GUIEventAdapter::CLOSE_WINDOW:
239 case osgGA::GUIEventAdapter::QUIT_APPLICATION:
247 void FGEventHandler::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
250 using namespace osgGA;
252 // XXX Probably other translations are needed too.
254 case GUIEventAdapter::KEY_Escape: key = 0x1b; break;
255 case GUIEventAdapter::KEY_Return: key = '\n'; break;
256 case GUIEventAdapter::KEY_BackSpace: key = '\b'; break;
257 case GUIEventAdapter::KEY_Delete: key = 0x7f; break;
258 case GUIEventAdapter::KEY_Tab: key = '\t'; break;
259 case GUIEventAdapter::KEY_Left: key = PU_KEY_LEFT; break;
260 case GUIEventAdapter::KEY_Up: key = PU_KEY_UP; break;
261 case GUIEventAdapter::KEY_Right: key = PU_KEY_RIGHT; break;
262 case GUIEventAdapter::KEY_Down: key = PU_KEY_DOWN; break;
263 case GUIEventAdapter::KEY_Page_Up: key = PU_KEY_PAGE_UP; break;
264 case GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break;
265 case GUIEventAdapter::KEY_Home: key = PU_KEY_HOME; break;
266 case GUIEventAdapter::KEY_End: key = PU_KEY_END; break;
267 case GUIEventAdapter::KEY_Insert: key = PU_KEY_INSERT; break;
268 case GUIEventAdapter::KEY_F1: key = PU_KEY_F1; break;
269 case GUIEventAdapter::KEY_F2: key = PU_KEY_F2; break;
270 case GUIEventAdapter::KEY_F3: key = PU_KEY_F3; break;
271 case GUIEventAdapter::KEY_F4: key = PU_KEY_F4; break;
272 case GUIEventAdapter::KEY_F5: key = PU_KEY_F5; break;
273 case GUIEventAdapter::KEY_F6: key = PU_KEY_F6; break;
274 case GUIEventAdapter::KEY_F7: key = PU_KEY_F7; break;
275 case GUIEventAdapter::KEY_F8: key = PU_KEY_F8; break;
276 case GUIEventAdapter::KEY_F9: key = PU_KEY_F9; break;
277 case GUIEventAdapter::KEY_F10: key = PU_KEY_F10; break;
278 case GUIEventAdapter::KEY_F11: key = PU_KEY_F11; break;
279 case GUIEventAdapter::KEY_F12: key = PU_KEY_F12; break;
280 case GUIEventAdapter::KEY_KP_Enter: key = '\r'; break;
281 case GUIEventAdapter::KEY_KP_Add: key = '+'; break;
282 case GUIEventAdapter::KEY_KP_Divide: key = '/'; break;
283 case GUIEventAdapter::KEY_KP_Multiply: key = '*'; break;
284 case GUIEventAdapter::KEY_KP_Subtract: key = '-'; break;
286 osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
289 // Num Lock is always true on Mac
290 std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
291 if (numPadIter != numlockKeyMap.end()) {
292 key = numPadIter->second;
295 if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK)
297 // NumLock on: map to numeric keys
298 std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
299 if (numPadIter != numlockKeyMap.end()) {
300 key = numPadIter->second;
305 // NumLock off: map to PU arrow keys
306 std::map<int, int>::iterator numPadIter = noNumlockKeyMap.find(key);
307 if (numPadIter != noNumlockKeyMap.end()) {
308 key = numPadIter->second;
313 modifiers = osgToFGModifiers(ea.getModKeyMask());
314 currentModifiers = modifiers;
315 if (eventType == osgGA::GUIEventAdapter::KEYUP)
316 modifiers |= KEYMOD_RELEASED;
318 // Release the letter key, for which the key press was reported. This
319 // is to deal with Ctrl-press -> a-press -> Ctrl-release -> a-release
321 if (key >= 0 && key < 128) {
322 if (modifiers & KEYMOD_RELEASED) {
323 key = release_keys[key];
325 release_keys[key] = key;
326 if (key >= 1 && key <= 26) {
327 release_keys[key + '@'] = key;
328 release_keys[key + '`'] = key;
329 } else if (key >= 'A' && key <= 'Z') {
330 release_keys[key - '@'] = key;
331 release_keys[tolower(key)] = key;
332 } else if (key >= 'a' && key <= 'z') {
333 release_keys[key - '`'] = key;
334 release_keys[toupper(key)] = key;
340 void FGEventHandler::handleStats(osgGA::GUIActionAdapter& us)
342 static SGPropertyNode_ptr display = fgGetNode("/sim/rendering/on-screen-statistics", true);
343 static SGPropertyNode_ptr print = fgGetNode("/sim/rendering/print-statistics", true);
345 int type = display->getIntValue() % osgViewer::StatsHandler::LAST;
346 if (type != statsType) {
347 statsEvent->setKey(displayStatsKey);
349 statsType = (statsType + 1) % osgViewer::StatsHandler::LAST;
350 statsHandler->handle(*statsEvent, us);
351 } while (statsType != type);
353 display->setIntValue(statsType);
356 if (print->getBoolValue()) {
357 statsEvent->setKey(printStatsKey);
358 statsHandler->handle(*statsEvent, us);
359 print->setBoolValue(false);
363 void eventToWindowCoords(const osgGA::GUIEventAdapter* ea,
364 double& x, double& y)
367 const GraphicsContext* gc = ea->getGraphicsContext();
368 const GraphicsContext::Traits* traits = gc->getTraits() ;
369 // Scale x, y to the dimensions of the window
370 x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
371 * (double)traits->width);
372 y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
373 * (double)traits->height);
374 if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS)
375 y = (double)traits->height - y;
378 void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea,
379 double& x, double& y)
382 const GraphicsContext* gc = ea->getGraphicsContext();
383 const GraphicsContext::Traits* traits = gc->getTraits() ;
384 // Scale x, y to the dimensions of the window
385 x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
386 * (double)traits->width);
387 y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
388 * (double)traits->height);
389 if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
390 y = (double)traits->height - y;