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"
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 FGEventHandler::FGEventHandler() :
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';
63 numlockKeyMap[GUIEventAdapter::KEY_KP_Delete] = '.';
65 // mapping when NumLock is off
66 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Insert] = PU_KEY_INSERT;
67 noNumlockKeyMap[GUIEventAdapter::KEY_KP_End] = PU_KEY_END;
68 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Down] = PU_KEY_DOWN;
69 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down] = PU_KEY_PAGE_DOWN;
70 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Left] = PU_KEY_LEFT;
71 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Begin] = '5';
72 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Right] = PU_KEY_RIGHT;
73 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Home] = PU_KEY_HOME;
74 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Up] = PU_KEY_UP;
75 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up] = PU_KEY_PAGE_UP;
76 noNumlockKeyMap[GUIEventAdapter::KEY_KP_Delete] = 127;
78 for (int i = 0; i < 128; i++)
84 // Translate OSG modifier mask to FG modifier mask.
85 int osgToFGModifiers(int modifiers)
88 if (modifiers & osgGA::GUIEventAdapter::MODKEY_SHIFT)
89 result |= KEYMOD_SHIFT;
91 if (modifiers & osgGA::GUIEventAdapter::MODKEY_CTRL)
92 result |= KEYMOD_CTRL;
94 if (modifiers & osgGA::GUIEventAdapter::MODKEY_ALT)
97 if (modifiers & osgGA::GUIEventAdapter::MODKEY_META)
98 result |= KEYMOD_META;
100 if (modifiers & osgGA::GUIEventAdapter::MODKEY_SUPER)
101 result |= KEYMOD_SUPER;
103 if (modifiers & osgGA::GUIEventAdapter::MODKEY_HYPER)
104 result |= KEYMOD_HYPER;
110 void FGEventHandler::init(const osgGA::GUIEventAdapter& ea,
111 osgGA::GUIActionAdapter& us)
113 currentModifiers = osgToFGModifiers(ea.getModKeyMask());
114 (void)handle(ea, us);
118 // Calculate event coordinates in the viewport of the GUI camera, if
119 // possible. Otherwise return false and (-1, -1).
123 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
129 const osg::GraphicsContext* eventGC = ea.getGraphicsContext();
130 const osg::GraphicsContext::Traits* traits = eventGC->getTraits();
131 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
134 osg::Viewport* vport = guiCamera->getViewport();
138 // Scale x, y to the dimensions of the window
139 double wx = (((ea.getX() - ea.getXmin()) / (ea.getXmax() - ea.getXmin()))
140 * (float)traits->width);
141 double wy = (((ea.getY() - ea.getYmin()) / (ea.getYmax() - ea.getYmin()))
142 * (float)traits->height);
143 if (vport->x() <= wx && wx <= vport->x() + vport->width()
144 && vport->y() <= wy && wy <= vport->y() + vport->height()) {
145 // Finally, into viewport coordinates. Change y to "increasing
148 y = vport->height() - (wy - vport->y());
156 bool FGEventHandler::handle(const osgGA::GUIEventAdapter& ea,
157 osgGA::GUIActionAdapter& us)
162 switch (ea.getEventType()) {
163 case osgGA::GUIEventAdapter::FRAME:
167 case osgGA::GUIEventAdapter::KEYDOWN:
168 case osgGA::GUIEventAdapter::KEYUP:
171 handleKey(ea, key, modmask);
172 eventToViewport(ea, us, x, y);
174 (*keyHandler)(key, modmask, x, y);
177 case osgGA::GUIEventAdapter::PUSH:
178 case osgGA::GUIEventAdapter::RELEASE:
180 bool mainWindow = eventToViewport(ea, us, x, y);
182 switch (ea.getButton()) {
183 case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
186 case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
189 case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
193 if (mouseClickHandler)
194 (*mouseClickHandler)(button,
196 == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
199 case osgGA::GUIEventAdapter::SCROLL:
201 bool mainWindow = eventToViewport(ea, us, x, y);
202 #ifdef X_DOUBLE_SCROLL_BUG
203 scrollButtonPressed = !scrollButtonPressed;
204 if (!scrollButtonPressed) // Drop the button release event
208 if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_2D) {
209 if (ea.getScrollingDeltaY() > 0)
211 else if (ea.getScrollingDeltaY() < 0)
213 } else if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
217 if (mouseClickHandler) {
218 (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
219 (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
223 case osgGA::GUIEventAdapter::MOVE:
224 case osgGA::GUIEventAdapter::DRAG:
225 // If we warped the mouse, then disregard all pointer motion
226 // events for this frame. We really want to flush the event
227 // queue of mouse events, but don't have the ability to do
228 // that with osgViewer.
231 if (eventToViewport(ea, us, x, y) && mouseMotionHandler)
232 (*mouseMotionHandler)(x, y);
234 case osgGA::GUIEventAdapter::RESIZE:
235 CameraGroup::getDefault()->resized();
236 if (resizable && windowResizeHandler)
237 (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
239 case osgGA::GUIEventAdapter::CLOSE_WINDOW:
240 case osgGA::GUIEventAdapter::QUIT_APPLICATION:
248 void FGEventHandler::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
251 using namespace osgGA;
253 // XXX Probably other translations are needed too.
255 case GUIEventAdapter::KEY_Escape: key = 0x1b; break;
256 case GUIEventAdapter::KEY_Return: key = '\n'; break;
257 case GUIEventAdapter::KEY_BackSpace: key = '\b'; break;
258 case GUIEventAdapter::KEY_Delete: key = 0x7f; break;
259 case GUIEventAdapter::KEY_Tab: key = '\t'; break;
260 case GUIEventAdapter::KEY_Left: key = PU_KEY_LEFT; break;
261 case GUIEventAdapter::KEY_Up: key = PU_KEY_UP; break;
262 case GUIEventAdapter::KEY_Right: key = PU_KEY_RIGHT; break;
263 case GUIEventAdapter::KEY_Down: key = PU_KEY_DOWN; break;
264 case GUIEventAdapter::KEY_Page_Up: key = PU_KEY_PAGE_UP; break;
265 case GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break;
266 case GUIEventAdapter::KEY_Home: key = PU_KEY_HOME; break;
267 case GUIEventAdapter::KEY_End: key = PU_KEY_END; break;
268 case GUIEventAdapter::KEY_Insert: key = PU_KEY_INSERT; break;
269 case GUIEventAdapter::KEY_F1: key = PU_KEY_F1; break;
270 case GUIEventAdapter::KEY_F2: key = PU_KEY_F2; break;
271 case GUIEventAdapter::KEY_F3: key = PU_KEY_F3; break;
272 case GUIEventAdapter::KEY_F4: key = PU_KEY_F4; break;
273 case GUIEventAdapter::KEY_F5: key = PU_KEY_F5; break;
274 case GUIEventAdapter::KEY_F6: key = PU_KEY_F6; break;
275 case GUIEventAdapter::KEY_F7: key = PU_KEY_F7; break;
276 case GUIEventAdapter::KEY_F8: key = PU_KEY_F8; break;
277 case GUIEventAdapter::KEY_F9: key = PU_KEY_F9; break;
278 case GUIEventAdapter::KEY_F10: key = PU_KEY_F10; break;
279 case GUIEventAdapter::KEY_F11: key = PU_KEY_F11; break;
280 case GUIEventAdapter::KEY_F12: key = PU_KEY_F12; break;
281 case GUIEventAdapter::KEY_KP_Enter: key = '\r'; break;
282 case GUIEventAdapter::KEY_KP_Add: key = '+'; break;
283 case GUIEventAdapter::KEY_KP_Divide: key = '/'; break;
284 case GUIEventAdapter::KEY_KP_Multiply: key = '*'; break;
285 case GUIEventAdapter::KEY_KP_Subtract: key = '-'; break;
287 osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
290 // Num Lock is always true on Mac
291 std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
292 if (numPadIter != numlockKeyMap.end()) {
293 key = numPadIter->second;
296 if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK)
298 // NumLock on: map to numeric keys
299 std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
300 if (numPadIter != numlockKeyMap.end()) {
301 key = numPadIter->second;
306 // NumLock off: map to PU arrow keys
307 std::map<int, int>::iterator numPadIter = noNumlockKeyMap.find(key);
308 if (numPadIter != noNumlockKeyMap.end()) {
309 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 FGEventHandler::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;