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';
64 for (int i = 0; i < 128; i++)
70 // Translate OSG modifier mask to FG modifier mask.
71 int osgToFGModifiers(int modifiers)
74 if (modifiers & osgGA::GUIEventAdapter::MODKEY_SHIFT)
75 result |= KEYMOD_SHIFT;
77 if (modifiers & osgGA::GUIEventAdapter::MODKEY_CTRL)
78 result |= KEYMOD_CTRL;
80 if (modifiers & osgGA::GUIEventAdapter::MODKEY_ALT)
83 if (modifiers & osgGA::GUIEventAdapter::MODKEY_META)
84 result |= KEYMOD_META;
86 if (modifiers & osgGA::GUIEventAdapter::MODKEY_SUPER)
87 result |= KEYMOD_SUPER;
89 if (modifiers & osgGA::GUIEventAdapter::MODKEY_HYPER)
90 result |= KEYMOD_HYPER;
96 void FGEventHandler::init(const osgGA::GUIEventAdapter& ea,
97 osgGA::GUIActionAdapter& us)
99 currentModifiers = osgToFGModifiers(ea.getModKeyMask());
100 (void)handle(ea, us);
104 // Calculate event coordinates in the viewport of the GUI camera, if
105 // possible. Otherwise return false and (-1, -1).
109 eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us,
115 const osg::GraphicsContext* eventGC = ea.getGraphicsContext();
116 const osg::GraphicsContext::Traits* traits = eventGC->getTraits();
117 osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault());
120 osg::Viewport* vport = guiCamera->getViewport();
124 // Scale x, y to the dimensions of the window
125 double wx = (((ea.getX() - ea.getXmin()) / (ea.getXmax() - ea.getXmin()))
126 * (float)traits->width);
127 double wy = (((ea.getY() - ea.getYmin()) / (ea.getYmax() - ea.getYmin()))
128 * (float)traits->height);
129 if (vport->x() <= wx && wx <= vport->x() + vport->width()
130 && vport->y() <= wy && wy <= vport->y() + vport->height()) {
131 // Finally, into viewport coordinates. Change y to "increasing
134 y = vport->height() - (wy - vport->y());
142 bool FGEventHandler::handle(const osgGA::GUIEventAdapter& ea,
143 osgGA::GUIActionAdapter& us)
148 switch (ea.getEventType()) {
149 case osgGA::GUIEventAdapter::FRAME:
153 case osgGA::GUIEventAdapter::KEYDOWN:
154 case osgGA::GUIEventAdapter::KEYUP:
157 handleKey(ea, key, modmask);
158 eventToViewport(ea, us, x, y);
160 (*keyHandler)(key, modmask, x, y);
163 case osgGA::GUIEventAdapter::PUSH:
164 case osgGA::GUIEventAdapter::RELEASE:
166 bool mainWindow = eventToViewport(ea, us, x, y);
168 switch (ea.getButton()) {
169 case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
172 case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
175 case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
179 if (mouseClickHandler)
180 (*mouseClickHandler)(button,
182 == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
185 case osgGA::GUIEventAdapter::SCROLL:
187 bool mainWindow = eventToViewport(ea, us, x, y);
188 #ifdef X_DOUBLE_SCROLL_BUG
189 scrollButtonPressed = !scrollButtonPressed;
190 if (!scrollButtonPressed) // Drop the button release event
194 if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
198 if (mouseClickHandler) {
199 (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
200 (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
204 case osgGA::GUIEventAdapter::MOVE:
205 case osgGA::GUIEventAdapter::DRAG:
206 // If we warped the mouse, then disregard all pointer motion
207 // events for this frame. We really want to flush the event
208 // queue of mouse events, but don't have the ability to do
209 // that with osgViewer.
212 if (eventToViewport(ea, us, x, y) && mouseMotionHandler)
213 (*mouseMotionHandler)(x, y);
215 case osgGA::GUIEventAdapter::RESIZE:
216 CameraGroup::getDefault()->resized();
217 if (resizable && windowResizeHandler)
218 (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
220 case osgGA::GUIEventAdapter::CLOSE_WINDOW:
221 case osgGA::GUIEventAdapter::QUIT_APPLICATION:
229 void FGEventHandler::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
232 using namespace osgGA;
234 // XXX Probably other translations are needed too.
236 case GUIEventAdapter::KEY_Escape: key = 0x1b; break;
237 case GUIEventAdapter::KEY_Return: key = '\n'; break;
238 case GUIEventAdapter::KEY_BackSpace: key = '\b'; break;
239 case GUIEventAdapter::KEY_Delete: key = 0x7f; break;
240 case GUIEventAdapter::KEY_Tab: key = '\t'; break;
241 case GUIEventAdapter::KEY_Left: key = PU_KEY_LEFT; break;
242 case GUIEventAdapter::KEY_Up: key = PU_KEY_UP; break;
243 case GUIEventAdapter::KEY_Right: key = PU_KEY_RIGHT; break;
244 case GUIEventAdapter::KEY_Down: key = PU_KEY_DOWN; break;
245 case GUIEventAdapter::KEY_Page_Up: key = PU_KEY_PAGE_UP; break;
246 case GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break;
247 case GUIEventAdapter::KEY_Home: key = PU_KEY_HOME; break;
248 case GUIEventAdapter::KEY_End: key = PU_KEY_END; break;
249 case GUIEventAdapter::KEY_Insert: key = PU_KEY_INSERT; break;
250 case GUIEventAdapter::KEY_F1: key = PU_KEY_F1; break;
251 case GUIEventAdapter::KEY_F2: key = PU_KEY_F2; break;
252 case GUIEventAdapter::KEY_F3: key = PU_KEY_F3; break;
253 case GUIEventAdapter::KEY_F4: key = PU_KEY_F4; break;
254 case GUIEventAdapter::KEY_F5: key = PU_KEY_F5; break;
255 case GUIEventAdapter::KEY_F6: key = PU_KEY_F6; break;
256 case GUIEventAdapter::KEY_F7: key = PU_KEY_F7; break;
257 case GUIEventAdapter::KEY_F8: key = PU_KEY_F8; break;
258 case GUIEventAdapter::KEY_F9: key = PU_KEY_F9; break;
259 case GUIEventAdapter::KEY_F10: key = PU_KEY_F10; break;
260 case GUIEventAdapter::KEY_F11: key = PU_KEY_F11; break;
261 case GUIEventAdapter::KEY_F12: key = PU_KEY_F12; break;
262 case GUIEventAdapter::KEY_KP_Delete: key = '.'; break;
263 case GUIEventAdapter::KEY_KP_Enter: key = '\r'; break;
264 case GUIEventAdapter::KEY_KP_Add: key = '+'; break;
265 case GUIEventAdapter::KEY_KP_Divide: key = '/'; break;
266 case GUIEventAdapter::KEY_KP_Multiply: key = '*'; break;
267 case GUIEventAdapter::KEY_KP_Subtract: key = '-'; break;
269 osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
271 std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
273 if (numPadIter != numlockKeyMap.end()) {
275 // Num Lock is always true on Mac
276 key = numPadIter->second;
278 if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK) {
279 key = numPadIter->second;
284 modifiers = osgToFGModifiers(ea.getModKeyMask());
285 currentModifiers = modifiers;
286 if (eventType == osgGA::GUIEventAdapter::KEYUP)
287 modifiers |= KEYMOD_RELEASED;
289 // Release the letter key, for which the key press was reported. This
290 // is to deal with Ctrl-press -> a-press -> Ctrl-release -> a-release
292 if (key >= 0 && key < 128) {
293 if (modifiers & KEYMOD_RELEASED) {
294 key = release_keys[key];
296 release_keys[key] = key;
297 if (key >= 1 && key <= 26) {
298 release_keys[key + '@'] = key;
299 release_keys[key + '`'] = key;
300 } else if (key >= 'A' && key <= 'Z') {
301 release_keys[key - '@'] = key;
302 release_keys[tolower(key)] = key;
303 } else if (key >= 'a' && key <= 'z') {
304 release_keys[key - '`'] = key;
305 release_keys[toupper(key)] = key;
311 void FGEventHandler::handleStats(osgGA::GUIActionAdapter& us)
313 static SGPropertyNode_ptr display = fgGetNode("/sim/rendering/on-screen-statistics", true);
314 static SGPropertyNode_ptr print = fgGetNode("/sim/rendering/print-statistics", true);
316 int type = display->getIntValue() % osgViewer::StatsHandler::LAST;
317 if (type != statsType) {
318 statsEvent->setKey(displayStatsKey);
320 statsType = (statsType + 1) % osgViewer::StatsHandler::LAST;
321 statsHandler->handle(*statsEvent, us);
322 } while (statsType != type);
324 display->setIntValue(statsType);
327 if (print->getBoolValue()) {
328 statsEvent->setKey(printStatsKey);
329 statsHandler->handle(*statsEvent, us);
330 print->setBoolValue(false);
334 void eventToWindowCoords(const osgGA::GUIEventAdapter* ea,
335 double& x, double& y)
338 const GraphicsContext* gc = ea->getGraphicsContext();
339 const GraphicsContext::Traits* traits = gc->getTraits() ;
340 // Scale x, y to the dimensions of the window
341 x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
342 * (double)traits->width);
343 y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
344 * (double)traits->height);
345 if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS)
346 y = (double)traits->height - y;
349 void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea,
350 double& x, double& y)
353 const GraphicsContext* gc = ea->getGraphicsContext();
354 const GraphicsContext::Traits* traits = gc->getTraits() ;
355 // Scale x, y to the dimensions of the window
356 x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
357 * (double)traits->width);
358 y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
359 * (double)traits->height);
360 if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
361 y = (double)traits->height - y;