| 1 | #ifdef HAVE_CONFIG_H |
| 2 | # include <config.h> |
| 3 | #endif |
| 4 | #include <osg/Camera> |
| 5 | #include <osg/GraphicsContext> |
| 6 | #include <osg/Math> |
| 7 | #include <osg/Viewport> |
| 8 | #include <osgViewer/Viewer> |
| 9 | |
| 10 | #include <GUI/FlightGear_pu.h> |
| 11 | #include <Main/fg_props.hxx> |
| 12 | #include "CameraGroup.hxx" |
| 13 | #include "FGEventHandler.hxx" |
| 14 | #include "WindowSystemAdapter.hxx" |
| 15 | #include "WindowBuilder.hxx" |
| 16 | #include "renderer.hxx" |
| 17 | #include "sview.hxx" |
| 18 | |
| 19 | #ifdef SG_MAC |
| 20 | // hack - during interactive resize on Mac, OSG queues and then flushes |
| 21 | // a large number of resize events, without doing any drawing. |
| 22 | extern void puCleanUpJunk ( void ) ; |
| 23 | #endif |
| 24 | |
| 25 | namespace flightgear |
| 26 | { |
| 27 | const int displayStatsKey = 1; |
| 28 | const int printStatsKey = 2; |
| 29 | |
| 30 | // The manipulator is responsible for updating a Viewer's camera. Its |
| 31 | // event handling method is also a convenient place to run the FG idle |
| 32 | // and draw handlers. |
| 33 | |
| 34 | FGEventHandler::FGEventHandler() : |
| 35 | idleHandler(0), |
| 36 | keyHandler(0), |
| 37 | mouseClickHandler(0), |
| 38 | mouseMotionHandler(0), |
| 39 | statsHandler(new FGStatsHandler), |
| 40 | statsEvent(new osgGA::GUIEventAdapter), |
| 41 | statsType(osgViewer::StatsHandler::NO_STATS), |
| 42 | currentModifiers(0), |
| 43 | resizable(true), |
| 44 | mouseWarped(false), |
| 45 | scrollButtonPressed(false), |
| 46 | changeStatsCameraRenderOrder(false), |
| 47 | m_composite_viewer_enabled(fgGetNode("/sim/rendering/composite-viewer-enabled", true)) |
| 48 | { |
| 49 | using namespace osgGA; |
| 50 | statsHandler->setKeyEventTogglesOnScreenStats(displayStatsKey); |
| 51 | statsHandler->setKeyEventPrintsOutStats(printStatsKey); |
| 52 | statsEvent->setEventType(GUIEventAdapter::KEYDOWN); |
| 53 | |
| 54 | for (int i = 0; i < 128; i++) |
| 55 | release_keys[i] = i; |
| 56 | |
| 57 | _display = fgGetNode("/sim/rendering/on-screen-statistics", true); |
| 58 | _print = fgGetNode("/sim/rendering/print-statistics", true); |
| 59 | } |
| 60 | |
| 61 | void FGEventHandler::clear() |
| 62 | { |
| 63 | _display.clear(); |
| 64 | _print.clear(); |
| 65 | } |
| 66 | |
| 67 | void FGEventHandler::reset() |
| 68 | { |
| 69 | _display = fgGetNode("/sim/rendering/on-screen-statistics", true); |
| 70 | _print = fgGetNode("/sim/rendering/print-statistics", true); |
| 71 | statsHandler->reset(); |
| 72 | } |
| 73 | |
| 74 | #if 0 |
| 75 | void FGEventHandler::init(const osgGA::GUIEventAdapter& ea, |
| 76 | osgGA::GUIActionAdapter& us) |
| 77 | { |
| 78 | currentModifiers = osgToFGModifiers(ea.getModKeyMask()); |
| 79 | (void)handle(ea, us); |
| 80 | } |
| 81 | #endif |
| 82 | |
| 83 | // Calculate event coordinates in the viewport of the GUI camera, if |
| 84 | // possible. Otherwise sets (x, y) to (-1, -1). |
| 85 | FGEventHandler::WindowType |
| 86 | FGEventHandler::eventToViewport(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us, |
| 87 | int& x, int& y) |
| 88 | { |
| 89 | WindowType ret = WindowType_NONE; |
| 90 | |
| 91 | flightgear::WindowBuilder* window_builder = flightgear::WindowBuilder::getWindowBuilder(); |
| 92 | flightgear::GraphicsWindow* main_window = window_builder->getDefaultWindow(); |
| 93 | |
| 94 | x = -1; |
| 95 | y = -1; |
| 96 | |
| 97 | const osg::GraphicsContext* eventGC = ea.getGraphicsContext(); |
| 98 | if( !eventGC ) |
| 99 | return WindowType_NONE; // TODO how can this happen? |
| 100 | const osg::GraphicsContext::Traits* traits = eventGC->getTraits(); |
| 101 | |
| 102 | osg::Viewport* vport; |
| 103 | |
| 104 | if (m_composite_viewer_enabled->getBoolValue() |
| 105 | && eventGC != main_window->gc.get()) { |
| 106 | // CompositeViewer is enabled and this is not the main window. |
| 107 | simgear::compositor::Compositor* compositor = SviewGetEventViewport(ea); |
| 108 | if (!compositor) { |
| 109 | SG_LOG(SG_GENERAL, SG_DEBUG, "SviewGetEventViewport() returned nullptr"); |
| 110 | return WindowType_NONE; |
| 111 | } |
| 112 | vport = compositor->getViewport(); |
| 113 | if (!vport) { |
| 114 | SG_LOG(SG_GENERAL, SG_ALERT, "compositor->getViewport() is nullptr"); |
| 115 | return WindowType_NONE; |
| 116 | } |
| 117 | ret = WindowType_SVIEW; |
| 118 | } |
| 119 | else { |
| 120 | osg::Camera* guiCamera = getGUICamera(CameraGroup::getDefault()); |
| 121 | if (!guiCamera) |
| 122 | return WindowType_NONE; |
| 123 | vport = guiCamera->getViewport(); |
| 124 | if (!vport) |
| 125 | return WindowType_NONE; |
| 126 | ret = WindowType_MAIN; |
| 127 | } |
| 128 | |
| 129 | // Scale x, y to the dimensions of the window |
| 130 | double wx = (((ea.getX() - ea.getXmin()) / (ea.getXmax() - ea.getXmin())) |
| 131 | * (float)traits->width); |
| 132 | double wy = (((ea.getY() - ea.getYmin()) / (ea.getYmax() - ea.getYmin())) |
| 133 | * (float)traits->height); |
| 134 | if (vport->x() <= wx && wx <= vport->x() + vport->width() |
| 135 | && vport->y() <= wy && wy <= vport->y() + vport->height()) { |
| 136 | // Finally, into viewport coordinates. Change y to "increasing |
| 137 | // downwards". |
| 138 | x = wx - vport->x(); |
| 139 | y = vport->height() - (wy - vport->y()); |
| 140 | } |
| 141 | return ret; |
| 142 | } |
| 143 | |
| 144 | /* A hack for when we are linked with OSG-3.4 and CompositeViewer is |
| 145 | enabled. It seems that OSG-3.4 incorrectly calls our event handler for |
| 146 | extra view windows (e.g. resize/close events), so we try to detect |
| 147 | this. Unfortunately OSG also messes up <ea>'s graphics context pointer so this |
| 148 | does't alwys work. */ |
| 149 | bool FGEventHandler::isMainWindow(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& us) |
| 150 | { |
| 151 | int x; |
| 152 | int y; |
| 153 | return eventToViewport(ea, us, x, y) == WindowType_MAIN; |
| 154 | } |
| 155 | |
| 156 | void FGEventHandler::setWindowRectangleInteriorWithCorrection(osgViewer::GraphicsWindow* window, int x, int y, int width, int height) |
| 157 | { |
| 158 | // Store (x y) in our state so that our handle() event handler can |
| 159 | // compare the requested position with the actual position and update |
| 160 | // (m_setWindowRectangle_delta_x m_setWindowRectangle_delta_y) accordingly. |
| 161 | // |
| 162 | SG_LOG(SG_VIEW, SG_DEBUG, "FGEventHandler::setWindowRectangle(): pos=(" << x << " " << y << ")" |
| 163 | << " delta=(" << m_setWindowRectangle_delta_x << " " << m_setWindowRectangle_delta_y << ")" |
| 164 | ); |
| 165 | m_setWindowRectangle_called = true; |
| 166 | m_setWindowRectangle_called_x = x; |
| 167 | m_setWindowRectangle_called_y = y; |
| 168 | window->setWindowRectangle( |
| 169 | x - m_setWindowRectangle_delta_x, |
| 170 | y - m_setWindowRectangle_delta_y, |
| 171 | width, |
| 172 | height |
| 173 | ); |
| 174 | } |
| 175 | |
| 176 | bool FGEventHandler::handle(const osgGA::GUIEventAdapter& ea, |
| 177 | osgGA::GUIActionAdapter& us) |
| 178 | { |
| 179 | // Event handlers seem to be called even if the according event has already |
| 180 | // been handled. Already handled events shouldn't be handled multiple times |
| 181 | // so we need to exit here manually. |
| 182 | if (ea.getHandled() |
| 183 | // Let mouse move events pass to correctly handle mouse cursor hide |
| 184 | // timeout while moving just on the canvas gui. |
| 185 | // TODO We should clean up the whole mouse input and make hide |
| 186 | // timeout independent of the event handler which consumed the |
| 187 | // event. |
| 188 | |
| 189 | // if we see a release, still process for active pick callbacks |
| 190 | // https://sourceforge.net/p/flightgear/codetickets/2347/ |
| 191 | |
| 192 | && ea.getEventType() != osgGA::GUIEventAdapter::MOVE && ea.getEventType() != osgGA::GUIEventAdapter::DRAG && ea.getEventType() != osgGA::GUIEventAdapter::RELEASE) { |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | int x = 0; |
| 197 | int y = 0; |
| 198 | |
| 199 | switch (ea.getEventType()) { |
| 200 | case osgGA::GUIEventAdapter::FRAME: |
| 201 | mouseWarped = false; |
| 202 | handleStats(us); |
| 203 | return true; |
| 204 | case osgGA::GUIEventAdapter::KEYDOWN: |
| 205 | case osgGA::GUIEventAdapter::KEYUP: |
| 206 | { |
| 207 | int key, modmask; |
| 208 | handleKey(ea, key, modmask); |
| 209 | eventToViewport(ea, us, x, y); |
| 210 | if (keyHandler) |
| 211 | (*keyHandler)(key, modmask, x, y); |
| 212 | return true; |
| 213 | } |
| 214 | case osgGA::GUIEventAdapter::PUSH: |
| 215 | case osgGA::GUIEventAdapter::RELEASE: |
| 216 | { |
| 217 | WindowType window_type = eventToViewport(ea, us, x, y); |
| 218 | bool mainWindow = (window_type == WindowType_MAIN); |
| 219 | int button = 0; |
| 220 | switch (ea.getButton()) { |
| 221 | case osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON: |
| 222 | button = 0; |
| 223 | break; |
| 224 | case osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON: |
| 225 | button = 1; |
| 226 | break; |
| 227 | case osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON: |
| 228 | button = 2; |
| 229 | break; |
| 230 | } |
| 231 | if (mouseClickHandler) |
| 232 | (*mouseClickHandler)(button, |
| 233 | (ea.getEventType() |
| 234 | == osgGA::GUIEventAdapter::RELEASE), x, y, mainWindow, &ea); |
| 235 | return true; |
| 236 | } |
| 237 | case osgGA::GUIEventAdapter::SCROLL: |
| 238 | { |
| 239 | WindowType window_type = eventToViewport(ea, us, x, y); |
| 240 | bool mainWindow = (window_type == WindowType_MAIN); |
| 241 | |
| 242 | int button; |
| 243 | if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_2D) { |
| 244 | if (ea.getScrollingDeltaY() > 0) |
| 245 | button = 3; |
| 246 | else if (ea.getScrollingDeltaY() < 0) |
| 247 | button = 4; |
| 248 | else |
| 249 | button = -1; |
| 250 | |
| 251 | #if defined(SG_MAC) |
| 252 | // bug https://code.google.com/p/flightgear-bugs/issues/detail?id=1286 |
| 253 | // Mac (Cocoa) interprets shuft+wheel as horizontal scroll |
| 254 | if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_SHIFT) { |
| 255 | if (ea.getScrollingDeltaX() > 0) |
| 256 | button = 3; |
| 257 | else if (ea.getScrollingDeltaX() < 0) |
| 258 | button = 4; |
| 259 | } |
| 260 | #endif |
| 261 | |
| 262 | } else if (ea.getScrollingMotion() == osgGA::GUIEventAdapter::SCROLL_UP) |
| 263 | button = 3; |
| 264 | else |
| 265 | button = 4; |
| 266 | if (mouseClickHandler && button != -1) { |
| 267 | (*mouseClickHandler)(button, 0, x, y, mainWindow, &ea); |
| 268 | (*mouseClickHandler)(button, 1, x, y, mainWindow, &ea); |
| 269 | } |
| 270 | return true; |
| 271 | } |
| 272 | case osgGA::GUIEventAdapter::MOVE: |
| 273 | case osgGA::GUIEventAdapter::DRAG: |
| 274 | // If we warped the mouse, then disregard all pointer motion |
| 275 | // events for this frame. We really want to flush the event |
| 276 | // queue of mouse events, but don't have the ability to do |
| 277 | // that with osgViewer. |
| 278 | if (mouseWarped) |
| 279 | return true; |
| 280 | if (eventToViewport(ea, us, x, y) != WindowType_NONE && mouseMotionHandler) |
| 281 | (*mouseMotionHandler)(x, y, &ea); |
| 282 | return true; |
| 283 | case osgGA::GUIEventAdapter::RESIZE: |
| 284 | SG_LOG(SG_VIEW, SG_DEBUG, "FGEventHandler::handle: RESIZE event " << ea.getWindowHeight() << " x " << ea.getWindowWidth() << ", resizable: " << resizable); |
| 285 | if (!isMainWindow(ea, us)) { |
| 286 | return true; |
| 287 | } |
| 288 | CameraGroup::getDefault()->resized(); |
| 289 | if (resizable) { |
| 290 | if (m_setWindowRectangle_called) { |
| 291 | // Update m_setWindowRectangle_delta_x and |
| 292 | // m_setWindowRectangle_delta_y so that our |
| 293 | // setWindowRectangle() compensates for the window furniture |
| 294 | // differences in future calls. |
| 295 | // |
| 296 | m_setWindowRectangle_called = false; |
| 297 | int error_x = ea.getWindowX() - m_setWindowRectangle_called_x; |
| 298 | int error_y = ea.getWindowY() - m_setWindowRectangle_called_y; |
| 299 | SG_LOG(SG_VIEW, SG_BULK, "m_setWindowRectangle_called is set:" |
| 300 | << " m_setWindowRectangle_delta=(" |
| 301 | << m_setWindowRectangle_delta_x << " " << m_setWindowRectangle_delta_y << ")" |
| 302 | << " m_setWindowRectangle_called_=(" |
| 303 | << m_setWindowRectangle_called_x << " " << m_setWindowRectangle_called_y << ")" |
| 304 | << " ea.getWindow=(" << ea.getWindowX() << " " << ea.getWindowY() << ")" |
| 305 | << " error=(" << error_x << " " << error_y << ")" |
| 306 | ); |
| 307 | m_setWindowRectangle_delta_x += error_x; |
| 308 | m_setWindowRectangle_delta_y += error_y; |
| 309 | } |
| 310 | globals->get_renderer()->resize(ea.getWindowWidth(), ea.getWindowHeight(), ea.getWindowX(), ea.getWindowY()); |
| 311 | } |
| 312 | statsHandler->handle(ea, us); |
| 313 | #ifdef SG_MAC |
| 314 | // work around OSG Cocoa-Viewer issue with resize event handling, |
| 315 | // where resize events are queued up, then dispatched in a batch, with |
| 316 | // no interveningd drawing calls. |
| 317 | puCleanUpJunk(); |
| 318 | #endif |
| 319 | return true; |
| 320 | case osgGA::GUIEventAdapter::CLOSE_WINDOW: |
| 321 | if (!isMainWindow(ea, us)) { |
| 322 | return true; |
| 323 | } |
| 324 | // Fall through. |
| 325 | case osgGA::GUIEventAdapter::QUIT_APPLICATION: |
| 326 | fgOSExit(0); |
| 327 | return true; |
| 328 | default: |
| 329 | return false; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | int FGEventHandler::translateKey(const osgGA::GUIEventAdapter& ea) |
| 334 | { |
| 335 | using namespace osgGA; |
| 336 | |
| 337 | static std::map<int, int> numlockKeyMap; |
| 338 | static std::map<int, int> noNumlockKeyMap; |
| 339 | |
| 340 | if (numlockKeyMap.empty()) { |
| 341 | // init these first time around |
| 342 | |
| 343 | // OSG reports NumPad keycodes independent of the NumLock modifier. |
| 344 | // Both KP-4 and KP-Left are reported as KEY_KP_Left (0xff96), so we |
| 345 | // have to generate the locked keys ourselves. |
| 346 | numlockKeyMap[GUIEventAdapter::KEY_KP_Insert] = '0'; |
| 347 | numlockKeyMap[GUIEventAdapter::KEY_KP_End] = '1'; |
| 348 | numlockKeyMap[GUIEventAdapter::KEY_KP_Down] = '2'; |
| 349 | numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down] = '3'; |
| 350 | numlockKeyMap[GUIEventAdapter::KEY_KP_Left] = '4'; |
| 351 | numlockKeyMap[GUIEventAdapter::KEY_KP_Begin] = '5'; |
| 352 | numlockKeyMap[GUIEventAdapter::KEY_KP_Right] = '6'; |
| 353 | numlockKeyMap[GUIEventAdapter::KEY_KP_Home] = '7'; |
| 354 | numlockKeyMap[GUIEventAdapter::KEY_KP_Up] = '8'; |
| 355 | numlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up] = '9'; |
| 356 | numlockKeyMap[GUIEventAdapter::KEY_KP_Delete] = '.'; |
| 357 | |
| 358 | // The comment above is incorrect on Mac osgViewer, at least. So we |
| 359 | // need to map the 'num-locked' key codes to real values. |
| 360 | numlockKeyMap[GUIEventAdapter::KEY_KP_0] = '0'; |
| 361 | numlockKeyMap[GUIEventAdapter::KEY_KP_1] = '1'; |
| 362 | numlockKeyMap[GUIEventAdapter::KEY_KP_2] = '2'; |
| 363 | numlockKeyMap[GUIEventAdapter::KEY_KP_3] = '3'; |
| 364 | numlockKeyMap[GUIEventAdapter::KEY_KP_4] = '4'; |
| 365 | numlockKeyMap[GUIEventAdapter::KEY_KP_5] = '5'; |
| 366 | numlockKeyMap[GUIEventAdapter::KEY_KP_6] = '6'; |
| 367 | numlockKeyMap[GUIEventAdapter::KEY_KP_7] = '7'; |
| 368 | numlockKeyMap[GUIEventAdapter::KEY_KP_8] = '8'; |
| 369 | numlockKeyMap[GUIEventAdapter::KEY_KP_9] = '9'; |
| 370 | numlockKeyMap[GUIEventAdapter::KEY_KP_Decimal] = '.'; |
| 371 | |
| 372 | // mapping when NumLock is off |
| 373 | noNumlockKeyMap[GUIEventAdapter::KEY_KP_Insert] = PU_KEY_INSERT; |
| 374 | noNumlockKeyMap[GUIEventAdapter::KEY_KP_End] = PU_KEY_END; |
| 375 | noNumlockKeyMap[GUIEventAdapter::KEY_KP_Down] = PU_KEY_DOWN; |
| 376 | noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Down] = PU_KEY_PAGE_DOWN; |
| 377 | noNumlockKeyMap[GUIEventAdapter::KEY_KP_Left] = PU_KEY_LEFT; |
| 378 | noNumlockKeyMap[GUIEventAdapter::KEY_KP_Begin] = '5'; |
| 379 | noNumlockKeyMap[GUIEventAdapter::KEY_KP_Right] = PU_KEY_RIGHT; |
| 380 | noNumlockKeyMap[GUIEventAdapter::KEY_KP_Home] = PU_KEY_HOME; |
| 381 | noNumlockKeyMap[GUIEventAdapter::KEY_KP_Up] = PU_KEY_UP; |
| 382 | noNumlockKeyMap[GUIEventAdapter::KEY_KP_Page_Up] = PU_KEY_PAGE_UP; |
| 383 | noNumlockKeyMap[GUIEventAdapter::KEY_KP_Delete] = 127; |
| 384 | } |
| 385 | |
| 386 | int key = ea.getKey(); |
| 387 | // XXX Probably other translations are needed too. |
| 388 | switch (key) { |
| 389 | case GUIEventAdapter::KEY_Escape: key = 0x1b; break; |
| 390 | case GUIEventAdapter::KEY_Return: key = '\n'; break; |
| 391 | case GUIEventAdapter::KEY_BackSpace: key = '\b'; break; |
| 392 | case GUIEventAdapter::KEY_Delete: key = 0x7f; break; |
| 393 | case GUIEventAdapter::KEY_Tab: key = '\t'; break; |
| 394 | case GUIEventAdapter::KEY_Left: key = PU_KEY_LEFT; break; |
| 395 | case GUIEventAdapter::KEY_Up: key = PU_KEY_UP; break; |
| 396 | case GUIEventAdapter::KEY_Right: key = PU_KEY_RIGHT; break; |
| 397 | case GUIEventAdapter::KEY_Down: key = PU_KEY_DOWN; break; |
| 398 | case GUIEventAdapter::KEY_Page_Up: key = PU_KEY_PAGE_UP; break; |
| 399 | case GUIEventAdapter::KEY_Page_Down: key = PU_KEY_PAGE_DOWN; break; |
| 400 | case GUIEventAdapter::KEY_Home: key = PU_KEY_HOME; break; |
| 401 | case GUIEventAdapter::KEY_End: key = PU_KEY_END; break; |
| 402 | case GUIEventAdapter::KEY_Insert: key = PU_KEY_INSERT; break; |
| 403 | case GUIEventAdapter::KEY_F1: key = PU_KEY_F1; break; |
| 404 | case GUIEventAdapter::KEY_F2: key = PU_KEY_F2; break; |
| 405 | case GUIEventAdapter::KEY_F3: key = PU_KEY_F3; break; |
| 406 | case GUIEventAdapter::KEY_F4: key = PU_KEY_F4; break; |
| 407 | case GUIEventAdapter::KEY_F5: key = PU_KEY_F5; break; |
| 408 | case GUIEventAdapter::KEY_F6: key = PU_KEY_F6; break; |
| 409 | case GUIEventAdapter::KEY_F7: key = PU_KEY_F7; break; |
| 410 | case GUIEventAdapter::KEY_F8: key = PU_KEY_F8; break; |
| 411 | case GUIEventAdapter::KEY_F9: key = PU_KEY_F9; break; |
| 412 | case GUIEventAdapter::KEY_F10: key = PU_KEY_F10; break; |
| 413 | case GUIEventAdapter::KEY_F11: key = PU_KEY_F11; break; |
| 414 | case GUIEventAdapter::KEY_F12: key = PU_KEY_F12; break; |
| 415 | case GUIEventAdapter::KEY_KP_Enter: key = '\r'; break; |
| 416 | case GUIEventAdapter::KEY_KP_Add: key = '+'; break; |
| 417 | case GUIEventAdapter::KEY_KP_Divide: key = '/'; break; |
| 418 | case GUIEventAdapter::KEY_KP_Multiply: key = '*'; break; |
| 419 | case GUIEventAdapter::KEY_KP_Subtract: key = '-'; break; |
| 420 | } |
| 421 | |
| 422 | #ifdef __APPLE__ |
| 423 | // Num Lock is always true on Mac |
| 424 | auto numPadIter = numlockKeyMap.find(key); |
| 425 | if (numPadIter != numlockKeyMap.end()) { |
| 426 | key = numPadIter->second; |
| 427 | } |
| 428 | #else |
| 429 | if (ea.getModKeyMask() & osgGA::GUIEventAdapter::MODKEY_NUM_LOCK) { |
| 430 | // NumLock on: map to numeric keys |
| 431 | auto numPadIter = numlockKeyMap.find(key); |
| 432 | if (numPadIter != numlockKeyMap.end()) { |
| 433 | key = numPadIter->second; |
| 434 | } |
| 435 | } else { |
| 436 | // NumLock off: map to PU arrow keys |
| 437 | auto numPadIter = noNumlockKeyMap.find(key); |
| 438 | if (numPadIter != noNumlockKeyMap.end()) { |
| 439 | key = numPadIter->second; |
| 440 | } |
| 441 | } |
| 442 | #endif |
| 443 | return key; |
| 444 | } |
| 445 | |
| 446 | int FGEventHandler::translateModifiers(const osgGA::GUIEventAdapter& ea) |
| 447 | { |
| 448 | int result = 0; |
| 449 | const auto modifiers = ea.getModKeyMask(); |
| 450 | if (modifiers & osgGA::GUIEventAdapter::MODKEY_SHIFT) |
| 451 | result |= KEYMOD_SHIFT; |
| 452 | |
| 453 | if (modifiers & osgGA::GUIEventAdapter::MODKEY_CTRL) |
| 454 | result |= KEYMOD_CTRL; |
| 455 | |
| 456 | if (modifiers & osgGA::GUIEventAdapter::MODKEY_ALT) |
| 457 | result |= KEYMOD_ALT; |
| 458 | |
| 459 | if (modifiers & osgGA::GUIEventAdapter::MODKEY_META) |
| 460 | result |= KEYMOD_META; |
| 461 | |
| 462 | if (modifiers & osgGA::GUIEventAdapter::MODKEY_SUPER) |
| 463 | result |= KEYMOD_SUPER; |
| 464 | |
| 465 | if (modifiers & osgGA::GUIEventAdapter::MODKEY_HYPER) |
| 466 | result |= KEYMOD_HYPER; |
| 467 | return result; |
| 468 | } |
| 469 | |
| 470 | void FGEventHandler::handleKey(const osgGA::GUIEventAdapter& ea, int& key, |
| 471 | int& modifiers) |
| 472 | { |
| 473 | key = translateKey(ea); |
| 474 | modifiers = translateModifiers(ea); |
| 475 | currentModifiers = modifiers; |
| 476 | const auto eventType = ea.getEventType(); |
| 477 | if (eventType == osgGA::GUIEventAdapter::KEYUP) |
| 478 | modifiers |= KEYMOD_RELEASED; |
| 479 | |
| 480 | // Release the letter key, for which the key press was reported. This |
| 481 | // is to deal with Ctrl-press -> a-press -> Ctrl-release -> a-release |
| 482 | // correctly. |
| 483 | if (key >= 0 && key < 128) { |
| 484 | if (modifiers & KEYMOD_RELEASED) { |
| 485 | key = release_keys[key]; |
| 486 | } else { |
| 487 | release_keys[key] = key; |
| 488 | if (key >= 1 && key <= 26) { |
| 489 | release_keys[key + '@'] = key; |
| 490 | release_keys[key + '`'] = key; |
| 491 | } else if (key >= 'A' && key <= 'Z') { |
| 492 | release_keys[key - '@'] = key; |
| 493 | release_keys[tolower(key)] = key; |
| 494 | } else if (key >= 'a' && key <= 'z') { |
| 495 | release_keys[key - '`'] = key; |
| 496 | release_keys[toupper(key)] = key; |
| 497 | } |
| 498 | } |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | void FGEventHandler::handleStats(osgGA::GUIActionAdapter& us) |
| 503 | { |
| 504 | int type = _display->getIntValue() % osgViewer::StatsHandler::LAST; |
| 505 | if (type != statsType) { |
| 506 | statsEvent->setKey(displayStatsKey); |
| 507 | do { |
| 508 | statsType = (statsType + 1) % osgViewer::StatsHandler::LAST; |
| 509 | statsHandler->handle(*statsEvent, us); |
| 510 | if (changeStatsCameraRenderOrder) { |
| 511 | statsHandler->getCamera()->setRenderOrder(osg::Camera::POST_RENDER, 99999); |
| 512 | changeStatsCameraRenderOrder = false; |
| 513 | } |
| 514 | } while (statsType != type); |
| 515 | |
| 516 | _display->setIntValue(statsType); |
| 517 | } |
| 518 | |
| 519 | if (_print->getBoolValue()) { |
| 520 | statsEvent->setKey(printStatsKey); |
| 521 | statsHandler->handle(*statsEvent, us); |
| 522 | _print->setBoolValue(false); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | bool eventToWindowCoords(const osgGA::GUIEventAdapter* ea, |
| 527 | double& x, double& y) |
| 528 | { |
| 529 | using namespace osg; |
| 530 | const GraphicsContext* gc = ea->getGraphicsContext(); |
| 531 | if (!gc || !gc->getTraits()) { |
| 532 | return false; |
| 533 | } |
| 534 | |
| 535 | const GraphicsContext::Traits* traits = gc->getTraits() ; |
| 536 | // Scale x, y to the dimensions of the window |
| 537 | x = (((ea->getX() - ea->getXmin()) / (ea->getXmax() - ea->getXmin())) |
| 538 | * (double)traits->width); |
| 539 | y = (((ea->getY() - ea->getYmin()) / (ea->getYmax() - ea->getYmin())) |
| 540 | * (double)traits->height); |
| 541 | if (ea->getMouseYOrientation() == osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS) |
| 542 | y = (double)traits->height - y; |
| 543 | |
| 544 | return true; |
| 545 | } |
| 546 | |
| 547 | } |