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:
157 case osgGA::GUIEventAdapter::KEYDOWN:
158 case osgGA::GUIEventAdapter::KEYUP:
161 handleKey(ea, key, modmask);
162 eventToViewport(ea, us, x, y);
164 (*keyHandler)(key, modmask, x, y);
167 case osgGA::GUIEventAdapter::PUSH:
168 case osgGA::GUIEventAdapter::RELEASE:
170 bool mainWindow = eventToViewport(ea, us, x, y);
172 switch (ea.getButton()) {
173 case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON:
176 case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON:
179 case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON:
183 if (mouseClickHandler)
184 (*mouseClickHandler)(button,
186 == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea);
189 case osgGA::GUIEventAdapter::SCROLL:
191 bool mainWindow = eventToViewport(ea, us, x, y);
192 #ifdef X_DOUBLE_SCROLL_BUG
193 scrollButtonPressed = !scrollButtonPressed;
194 if (!scrollButtonPressed) // Drop the button release event
198 if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP)
202 if (mouseClickHandler) {
203 (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea);
204 (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea);
208 case osgGA::GUIEventAdapter::MOVE:
209 case osgGA::GUIEventAdapter::DRAG:
210 // If we warped the mouse, then disregard all pointer motion
211 // events for this frame. We really want to flush the event
212 // queue of mouse events, but don't have the ability to do
213 // that with osgViewer.
216 if (eventToViewport(ea, us, x, y) && mouseMotionHandler)
217 (*mouseMotionHandler)(x, y);
219 case osgGA::GUIEventAdapter::RESIZE:
220 CameraGroup::getDefault()->resized();
221 if (resizable && windowResizeHandler)
222 (*windowResizeHandler)(ea.getWindowWidth(), ea.getWindowHeight());
224 case osgGA::GUIEventAdapter::CLOSE_WINDOW:
225 case osgGA::GUIEventAdapter::QUIT_APPLICATION:
233 void FGEventHandler::handleKey(const osgGA::GUIEventAdapter& ea, int& key,
236 using namespace osgGA;
238 // XXX Probably other translations are needed too.
240 case GUIEventAdapter::KEY_Escape: key = 0x1b; break;
241 case GUIEventAdapter::KEY_Return: key = '\n'; break;
242 case GUIEventAdapter::KEY_BackSpace: key = '\b'; break;
243 case GUIEventAdapter::KEY_Delete: key = 0x7f; break;
244 case GUIEventAdapter::KEY_Tab: key = '\t'; break;
245 case GUIEventAdapter::KEY_Left: key = PU_KEY_LEFT; break;
246 case GUIEventAdapter::KEY_Up: key = PU_KEY_UP; break;
247 case GUIEventAdapter::KEY_Right: key = PU_KEY_RIGHT; break;
248 case GUIEventAdapter::KEY_Down: key = PU_KEY_DOWN; break;
249 case GUIEventAdapter::KEY_Page_Up: key = PU_KEY_PAGE_UP; break;
250 case GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break;
251 case GUIEventAdapter::KEY_Home: key = PU_KEY_HOME; break;
252 case GUIEventAdapter::KEY_End: key = PU_KEY_END; break;
253 case GUIEventAdapter::KEY_Insert: key = PU_KEY_INSERT; break;
254 case GUIEventAdapter::KEY_F1: key = PU_KEY_F1; break;
255 case GUIEventAdapter::KEY_F2: key = PU_KEY_F2; break;
256 case GUIEventAdapter::KEY_F3: key = PU_KEY_F3; break;
257 case GUIEventAdapter::KEY_F4: key = PU_KEY_F4; break;
258 case GUIEventAdapter::KEY_F5: key = PU_KEY_F5; break;
259 case GUIEventAdapter::KEY_F6: key = PU_KEY_F6; break;
260 case GUIEventAdapter::KEY_F7: key = PU_KEY_F7; break;
261 case GUIEventAdapter::KEY_F8: key = PU_KEY_F8; break;
262 case GUIEventAdapter::KEY_F9: key = PU_KEY_F9; break;
263 case GUIEventAdapter::KEY_F10: key = PU_KEY_F10; break;
264 case GUIEventAdapter::KEY_F11: key = PU_KEY_F11; break;
265 case GUIEventAdapter::KEY_F12: key = PU_KEY_F12; break;
266 case GUIEventAdapter::KEY_KP_Delete: key = '.'; break;
267 case GUIEventAdapter::KEY_KP_Enter: key = '\r'; break;
268 case GUIEventAdapter::KEY_KP_Add: key = '+'; break;
269 case GUIEventAdapter::KEY_KP_Divide: key = '/'; break;
270 case GUIEventAdapter::KEY_KP_Multiply: key = '*'; break;
271 case GUIEventAdapter::KEY_KP_Subtract: key = '-'; break;
273 osgGA::GUIEventAdapter::EventType eventType = ea.getEventType();
275 std::map<int, int>::iterator numPadIter = numlockKeyMap.find(key);
277 if (numPadIter != numlockKeyMap.end()) {
278 if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK) {
279 key = numPadIter->second;
283 modifiers = osgToFGModifiers(ea.getModKeyMask());
284 currentModifiers = modifiers;
285 if (eventType == osgGA::GUIEventAdapter::KEYUP)
286 modifiers |= KEYMOD_RELEASED;
288 // Release the letter key, for which the key press was reported. This
289 // is to deal with Ctrl-press -> a-press -> Ctrl-release -> a-release
291 if (key >= 0 && key < 128) {
292 if (modifiers & KEYMOD_RELEASED) {
293 key = release_keys[key];
295 release_keys[key] = key;
296 if (key >= 1 && key <= 26) {
297 release_keys[key + '@'] = key;
298 release_keys[key + '`'] = key;
299 } else if (key >= 'A' && key <= 'Z') {
300 release_keys[key - '@'] = key;
301 release_keys[tolower(key)] = key;
302 } else if (key >= 'a' && key <= 'z') {
303 release_keys[key - '`'] = key;
304 release_keys[toupper(key)] = key;
310 void FGEventHandler::handleStats(osgGA::GUIActionAdapter& us)
312 static SGPropertyNode_ptr display = fgGetNode("/sim/rendering/on-screen-statistics", true);
313 static SGPropertyNode_ptr print = fgGetNode("/sim/rendering/print-statistics", true);
315 int type = display->getIntValue() % osgViewer::StatsHandler::LAST;
316 if (type != statsType) {
317 statsEvent->setKey(displayStatsKey);
319 statsType = (statsType + 1) % osgViewer::StatsHandler::LAST;
320 statsHandler->handle(*statsEvent, us);
321 } while (statsType != type);
323 display->setIntValue(statsType);
326 if (print->getBoolValue()) {
327 statsEvent->setKey(printStatsKey);
328 statsHandler->handle(*statsEvent, us);
329 print->setBoolValue(false);
333 void eventToWindowCoords(const osgGA::GUIEventAdapter* ea,
334 double& x, double& y)
337 const GraphicsContext* gc = ea->getGraphicsContext();
338 const GraphicsContext::Traits* traits = gc->getTraits() ;
339 // Scale x, y to the dimensions of the window
340 x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
341 * (double)traits->width);
342 y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
343 * (double)traits->height);
344 if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS)
345 y = (double)traits->height - y;
348 void eventToWindowCoordsYDown(const osgGA::GUIEventAdapter* ea,
349 double& x, double& y)
352 const GraphicsContext* gc = ea->getGraphicsContext();
353 const GraphicsContext::Traits* traits = gc->getTraits() ;
354 // Scale x, y to the dimensions of the window
355 x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin()))
356 * (double)traits->width);
357 y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin()))
358 * (double)traits->height);
359 if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_UPWARDS)
360 y = (double)traits->height - y;