| 1 | // Canvas gui/dialog manager |
| 2 | // |
| 3 | // Copyright (C) 2012 Thomas Geymayer <[email protected]> |
| 4 | // |
| 5 | // This program is free software; you can redistribute it and/or |
| 6 | // modify it under the terms of the GNU General Public License as |
| 7 | // published by the Free Software Foundation; either version 2 of the |
| 8 | // License, or (at your option) any later version. |
| 9 | // |
| 10 | // This program is distributed in the hope that it will be useful, but |
| 11 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | // General Public License for more details. |
| 14 | // |
| 15 | // You should have received a copy of the GNU General Public License |
| 16 | // along with this program; if not, write to the Free Software |
| 17 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 18 | |
| 19 | #include "gui_mgr.hxx" |
| 20 | |
| 21 | #include <Main/fg_os.hxx> |
| 22 | #include <Main/fg_props.hxx> |
| 23 | #include <Main/globals.hxx> |
| 24 | |
| 25 | #include <simgear/canvas/Canvas.hxx> |
| 26 | #include <simgear/canvas/CanvasPlacement.hxx> |
| 27 | #include <simgear/canvas/CanvasWindow.hxx> |
| 28 | #include <simgear/canvas/events/KeyboardEvent.hxx> |
| 29 | #include <simgear/scene/util/OsgMath.hxx> |
| 30 | |
| 31 | #include <osg/BlendFunc> |
| 32 | #include <osgViewer/Viewer> |
| 33 | #include <osgViewer/View> |
| 34 | #include <osgGA/GUIEventHandler> |
| 35 | |
| 36 | class DesktopGroup; |
| 37 | typedef SGSharedPtr<DesktopGroup> DesktopPtr; |
| 38 | typedef SGWeakPtr<DesktopGroup> DesktopWeakPtr; |
| 39 | |
| 40 | namespace sc = simgear::canvas; |
| 41 | using osgEA = osgGA::GUIEventAdapter; |
| 42 | |
| 43 | /* |
| 44 | RESIZE AREAS |
| 45 | ============ |
| 46 | |
| 47 | | || | _ inside corner region (L-shaped part inside margin) both |
| 48 | |___||_|_ _ _/ directions can be resized (outside only one axis) |
| 49 | | || | | |
| 50 | | || | |
| 51 | | || |_____|__ _ |
| 52 | | || | } margin_neg \ |
| 53 | | ========|== <-- window border |_ area where resize |
| 54 | | | } margin_pos | can be initiated |
| 55 | |____________|__/ _/ |
| 56 | |<- corner ->| |
| 57 | */ |
| 58 | const float RESIZE_MARGIN_POS = 12; |
| 59 | const float RESIZE_MARGIN_NEG = 2; |
| 60 | const float RESIZE_CORNER = 20; |
| 61 | |
| 62 | /** |
| 63 | * Event handler |
| 64 | */ |
| 65 | class GUIEventHandler: |
| 66 | public osgGA::GUIEventHandler |
| 67 | { |
| 68 | public: |
| 69 | GUIEventHandler(const DesktopWeakPtr& desktop_group); |
| 70 | |
| 71 | bool handle( const osgEA& ea, |
| 72 | osgGA::GUIActionAdapter&, |
| 73 | osg::Object*, |
| 74 | osg::NodeVisitor* ); |
| 75 | |
| 76 | protected: |
| 77 | DesktopWeakPtr _desktop; |
| 78 | }; |
| 79 | |
| 80 | /** |
| 81 | * Track a canvas placement on a window |
| 82 | */ |
| 83 | class WindowPlacement: |
| 84 | public sc::Placement |
| 85 | { |
| 86 | public: |
| 87 | WindowPlacement( SGPropertyNode* node, |
| 88 | sc::WindowPtr window, |
| 89 | sc::CanvasPtr canvas ): |
| 90 | Placement(node), |
| 91 | _window(window), |
| 92 | _canvas(canvas) |
| 93 | {} |
| 94 | |
| 95 | /** |
| 96 | * Remove placement from window |
| 97 | */ |
| 98 | virtual ~WindowPlacement() |
| 99 | { |
| 100 | sc::WindowPtr window = _window.lock(); |
| 101 | sc::CanvasPtr canvas = _canvas.lock(); |
| 102 | |
| 103 | if( window && canvas && canvas == window->getCanvasContent().lock() ) |
| 104 | window->setCanvasContent( sc::CanvasPtr() ); |
| 105 | } |
| 106 | |
| 107 | private: |
| 108 | sc::WindowWeakPtr _window; |
| 109 | sc::CanvasWeakPtr _canvas; |
| 110 | }; |
| 111 | |
| 112 | /** |
| 113 | * Desktop root group |
| 114 | */ |
| 115 | class DesktopGroup: |
| 116 | public sc::Group |
| 117 | { |
| 118 | public: |
| 119 | DesktopGroup(osg::Camera* camera); |
| 120 | |
| 121 | void setFocusWindow(const sc::WindowPtr& window); |
| 122 | |
| 123 | bool grabPointer(const sc::WindowPtr& window); |
| 124 | void ungrabPointer(const sc::WindowPtr& window); |
| 125 | |
| 126 | sc::WindowPtr windowAtPosition(const osg::Vec2f& screen_pos); |
| 127 | osg::Vec2f toScreenPos(const osgEA& ea) const; |
| 128 | |
| 129 | bool handleOsgEvent(const osgEA& ea); |
| 130 | |
| 131 | protected: |
| 132 | |
| 133 | friend class GUIMgr; |
| 134 | |
| 135 | SGPropertyChangeCallback<DesktopGroup> _cb_mouse_mode; |
| 136 | bool _handle_events {true}; |
| 137 | |
| 138 | simgear::PropertyObject<int> _width, |
| 139 | _height; |
| 140 | |
| 141 | sc::WindowWeakPtr _last_push, |
| 142 | _last_drag, |
| 143 | _last_mouse_over, |
| 144 | _resize_window, |
| 145 | _focus_window, |
| 146 | _pointer_grab_window; |
| 147 | |
| 148 | uint8_t _resize {sc::Window::NONE}; |
| 149 | FGMouseCursor::Cursor _last_cursor{FGMouseCursor::CURSOR_NONE}; |
| 150 | bool _drag_finished {false}; |
| 151 | |
| 152 | osg::Vec2f _drag_start, |
| 153 | _last_mouse_pos; |
| 154 | double _last_scroll_time {0}; |
| 155 | |
| 156 | uint32_t _last_key_down_no_mod {~0u}; // Key repeat for non modifier keys |
| 157 | |
| 158 | bool canHandleInput() const; |
| 159 | bool handleMouse(const osgEA& ea); |
| 160 | bool handleKeyboard(const osgEA& ea); |
| 161 | |
| 162 | bool propagateEvent( const sc::EventPtr& event, |
| 163 | const sc::WindowPtr& active_window ); |
| 164 | bool propagateRootEvent(const sc::EventPtr& event); |
| 165 | |
| 166 | void handleResize(int x, int y, int width, int height); |
| 167 | bool handleDrag(const sc::EventPtr& event); |
| 168 | void finishDrag(const sc::WindowPtr& drag_src, const sc::EventPtr& event); |
| 169 | void handleMouseMode(SGPropertyNode* node); |
| 170 | |
| 171 | /** |
| 172 | * |
| 173 | */ |
| 174 | sc::ElementFactory |
| 175 | getChildFactory(const std::string& type) const |
| 176 | { |
| 177 | if( type == "window" ) |
| 178 | return &Element::create<sc::Window>; |
| 179 | |
| 180 | return Group::getChildFactory(type); |
| 181 | } |
| 182 | }; |
| 183 | |
| 184 | //------------------------------------------------------------------------------ |
| 185 | GUIEventHandler::GUIEventHandler(const DesktopWeakPtr& desktop_group): |
| 186 | _desktop( desktop_group ) |
| 187 | { |
| 188 | |
| 189 | } |
| 190 | |
| 191 | //------------------------------------------------------------------------------ |
| 192 | bool GUIEventHandler::handle( const osgEA& ea, |
| 193 | osgGA::GUIActionAdapter&, |
| 194 | osg::Object*, |
| 195 | osg::NodeVisitor* ) |
| 196 | { |
| 197 | if( ea.getHandled() ) |
| 198 | return false; |
| 199 | |
| 200 | DesktopPtr desktop = _desktop.lock(); |
| 201 | return desktop && desktop->handleOsgEvent(ea); |
| 202 | } |
| 203 | |
| 204 | //------------------------------------------------------------------------------ |
| 205 | DesktopGroup::DesktopGroup(osg::Camera* camera): |
| 206 | Group(sc::CanvasPtr(), fgGetNode("/sim/gui/canvas", true)), |
| 207 | _cb_mouse_mode( this, |
| 208 | &DesktopGroup::handleMouseMode, |
| 209 | fgGetNode("/devices/status/mice/mouse[0]/mode") ), |
| 210 | _width(_node, "size[0]"), |
| 211 | _height(_node, "size[1]") |
| 212 | { |
| 213 | if( !camera ) |
| 214 | { |
| 215 | SG_LOG(SG_GUI, SG_WARN, "DesktopGroup: failed to get GUI camera."); |
| 216 | return; |
| 217 | } |
| 218 | |
| 219 | camera->addChild(_scene_group.get()); |
| 220 | |
| 221 | osg::StateSet* stateSet = _scene_group->getOrCreateStateSet(); |
| 222 | stateSet->setDataVariance(osg::Object::STATIC); |
| 223 | stateSet->setRenderBinDetails(1000, "RenderBin"); |
| 224 | |
| 225 | // speed optimization? |
| 226 | stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF); |
| 227 | stateSet->setAttribute(new osg::BlendFunc( |
| 228 | osg::BlendFunc::SRC_ALPHA, |
| 229 | osg::BlendFunc::ONE_MINUS_SRC_ALPHA) |
| 230 | ); |
| 231 | stateSet->setMode(GL_BLEND, osg::StateAttribute::ON); |
| 232 | stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF); |
| 233 | stateSet->setMode(GL_FOG, osg::StateAttribute::OFF); |
| 234 | stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF); |
| 235 | |
| 236 | _width = _height = -1; |
| 237 | } |
| 238 | |
| 239 | //------------------------------------------------------------------------------ |
| 240 | void DesktopGroup::setFocusWindow(const sc::WindowPtr& window) |
| 241 | { |
| 242 | _focus_window = window; |
| 243 | } |
| 244 | |
| 245 | //------------------------------------------------------------------------------ |
| 246 | bool DesktopGroup::grabPointer(const sc::WindowPtr& window) |
| 247 | { |
| 248 | sc::WindowPtr resize = _resize_window.lock(); |
| 249 | if( (resize && resize != window) || !_pointer_grab_window.expired() ) |
| 250 | // Already grabbed (resize -> implicit grab) |
| 251 | return false; |
| 252 | |
| 253 | _pointer_grab_window = window; |
| 254 | return true; |
| 255 | } |
| 256 | |
| 257 | //------------------------------------------------------------------------------ |
| 258 | void DesktopGroup::ungrabPointer(const sc::WindowPtr& window) |
| 259 | { |
| 260 | if( _pointer_grab_window.expired() ) |
| 261 | SG_LOG(SG_GUI, SG_WARN, "ungrabPointer: no active grab."); |
| 262 | else if( window != _pointer_grab_window.lock() ) |
| 263 | SG_LOG(SG_GUI, SG_WARN, "ungrabPointer: window is not owner of the grab."); |
| 264 | else |
| 265 | _pointer_grab_window.reset(); |
| 266 | } |
| 267 | |
| 268 | //------------------------------------------------------------------------------ |
| 269 | sc::WindowPtr DesktopGroup::windowAtPosition(const osg::Vec2f& screen_pos) |
| 270 | { |
| 271 | for( int i = _scene_group->getNumChildren() - 1; i >= 0; --i ) |
| 272 | { |
| 273 | osg::Group *element = _scene_group->getChild(i)->asGroup(); |
| 274 | |
| 275 | if( !element || !element->getUserData() ) |
| 276 | continue; // TODO: warn/log? |
| 277 | |
| 278 | sc::WindowPtr window = |
| 279 | dynamic_cast<sc::Window*> |
| 280 | ( |
| 281 | static_cast<sc::Element::OSGUserData*>( |
| 282 | element->getUserData() |
| 283 | )->element.get() |
| 284 | ); |
| 285 | |
| 286 | if( !window || !window->isCapturingEvents() || !window->isVisible() ) |
| 287 | continue; |
| 288 | |
| 289 | float margin = window->isResizable() ? RESIZE_MARGIN_POS : 0; |
| 290 | if( window->getScreenRegion().contains( screen_pos.x(), |
| 291 | screen_pos.y(), |
| 292 | margin ) ) |
| 293 | { |
| 294 | return window; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | return {}; |
| 299 | } |
| 300 | |
| 301 | //------------------------------------------------------------------------------ |
| 302 | osg::Vec2f DesktopGroup::toScreenPos(const osgEA& ea) const |
| 303 | { |
| 304 | float x = SGMiscf::round(0.5 * (ea.getXnormalized() + 1) * _width); |
| 305 | float y = SGMiscf::round(0.5 * (ea.getYnormalized() + 1) * _height); |
| 306 | |
| 307 | if( ea.getMouseYOrientation() != osgEA::Y_INCREASING_DOWNWARDS ) |
| 308 | y = _height - y; |
| 309 | |
| 310 | return {x, y}; |
| 311 | } |
| 312 | |
| 313 | //------------------------------------------------------------------------------ |
| 314 | bool DesktopGroup::handleOsgEvent(const osgEA& ea) |
| 315 | { |
| 316 | switch( ea.getEventType() ) |
| 317 | { |
| 318 | case osgEA::PUSH: |
| 319 | case osgEA::RELEASE: |
| 320 | // case osgEA::DOUBLECLICK: |
| 321 | // // DOUBLECLICK doesn't seem to be triggered... |
| 322 | case osgEA::DRAG: |
| 323 | case osgEA::MOVE: |
| 324 | case osgEA::SCROLL: |
| 325 | return handleMouse(ea); |
| 326 | case osgEA::KEYDOWN: |
| 327 | case osgEA::KEYUP: |
| 328 | return handleKeyboard(ea); |
| 329 | case osgEA::RESIZE: |
| 330 | handleResize( ea.getWindowX(), |
| 331 | ea.getWindowY(), |
| 332 | ea.getWindowWidth(), |
| 333 | ea.getWindowHeight() ); |
| 334 | return false; // Let other event handlers also consume resize events |
| 335 | default: |
| 336 | return false; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | //------------------------------------------------------------------------------ |
| 341 | bool DesktopGroup::canHandleInput() const |
| 342 | { |
| 343 | return _handle_events |
| 344 | && _scene_group.valid() |
| 345 | && _scene_group->getNumChildren() > 0; |
| 346 | } |
| 347 | |
| 348 | //------------------------------------------------------------------------------ |
| 349 | bool DesktopGroup::handleMouse(const osgEA& ea) |
| 350 | { |
| 351 | if( !canHandleInput() ) |
| 352 | return false; |
| 353 | |
| 354 | osg::Vec2f mouse_pos = toScreenPos(ea), |
| 355 | delta = mouse_pos - _last_mouse_pos; |
| 356 | _last_mouse_pos = mouse_pos; |
| 357 | |
| 358 | if( auto resize_window = _resize_window.lock() ) |
| 359 | { |
| 360 | switch( ea.getEventType() ) |
| 361 | { |
| 362 | case osgEA::RELEASE: |
| 363 | resize_window->handleResize(sc::Window::NONE); |
| 364 | _resize_window.reset(); |
| 365 | break; |
| 366 | case osgEA::DRAG: |
| 367 | resize_window->handleResize(_resize, mouse_pos - _drag_start); |
| 368 | return true; |
| 369 | default: |
| 370 | // Ignore all other events while resizing |
| 371 | return true; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | sc::MouseEventPtr event(new sc::MouseEvent(ea)); |
| 376 | event->screen_pos = mouse_pos; |
| 377 | event->delta = delta; |
| 378 | |
| 379 | if( !_drag_finished && ea.getEventType() == osgEA::DRAG ) |
| 380 | return handleDrag(event); |
| 381 | |
| 382 | if( auto last_drag = _last_drag.lock() ) |
| 383 | { |
| 384 | if( ea.getEventType() == osgEA::RELEASE ) |
| 385 | finishDrag(last_drag, event); |
| 386 | else |
| 387 | // While dragging ignore all other mouse events |
| 388 | return true; |
| 389 | } |
| 390 | |
| 391 | sc::WindowPtr window_at_cursor = _pointer_grab_window.lock(); |
| 392 | if( !window_at_cursor ) |
| 393 | window_at_cursor = windowAtPosition(event->screen_pos); |
| 394 | |
| 395 | if( window_at_cursor ) |
| 396 | { |
| 397 | const SGRect<float>& reg = window_at_cursor->getScreenRegion(); |
| 398 | |
| 399 | if( window_at_cursor->isResizable() |
| 400 | && !reg.contains( event->getScreenX(), |
| 401 | event->getScreenY(), |
| 402 | -RESIZE_MARGIN_NEG ) ) |
| 403 | { |
| 404 | if( !_last_cursor ) |
| 405 | _last_cursor = fgGetMouseCursor(); |
| 406 | |
| 407 | _resize = 0; |
| 408 | |
| 409 | if( event->getScreenX() <= reg.l() + RESIZE_CORNER ) |
| 410 | _resize |= sc::Window::LEFT; |
| 411 | else if( event->getScreenX() >= reg.r() - RESIZE_CORNER ) |
| 412 | _resize |= sc::Window::RIGHT; |
| 413 | |
| 414 | if( event->getScreenY() <= reg.t() + RESIZE_CORNER ) |
| 415 | _resize |= sc::Window::TOP; |
| 416 | else if( event->getScreenY() >= reg.b() - RESIZE_CORNER ) |
| 417 | _resize |= sc::Window::BOTTOM; |
| 418 | |
| 419 | static const FGMouseCursor::Cursor cursor_mapping[] = |
| 420 | { |
| 421 | FGMouseCursor::CURSOR_NONE, |
| 422 | FGMouseCursor::CURSOR_LEFT_SIDE, |
| 423 | FGMouseCursor::CURSOR_RIGHT_SIDE, |
| 424 | FGMouseCursor::CURSOR_NONE, |
| 425 | FGMouseCursor::CURSOR_TOP_SIDE, |
| 426 | FGMouseCursor::CURSOR_TOP_LEFT, |
| 427 | FGMouseCursor::CURSOR_TOP_RIGHT, |
| 428 | FGMouseCursor::CURSOR_NONE, |
| 429 | FGMouseCursor::CURSOR_BOTTOM_SIDE, |
| 430 | FGMouseCursor::CURSOR_BOTTOM_LEFT, |
| 431 | FGMouseCursor::CURSOR_BOTTOM_RIGHT, |
| 432 | }; |
| 433 | |
| 434 | if( !cursor_mapping[_resize] ) |
| 435 | return false; |
| 436 | |
| 437 | fgSetMouseCursor(cursor_mapping[_resize]); |
| 438 | |
| 439 | if( ea.getEventType() == osgEA::PUSH ) |
| 440 | { |
| 441 | _resize_window = window_at_cursor; |
| 442 | _drag_start = event->screen_pos; |
| 443 | |
| 444 | window_at_cursor->raise(); |
| 445 | window_at_cursor->handleResize(_resize | sc::Window::INIT); |
| 446 | } |
| 447 | |
| 448 | return true; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | if( _last_cursor ) |
| 453 | { |
| 454 | fgSetMouseCursor(_last_cursor); |
| 455 | _last_cursor = FGMouseCursor::CURSOR_NONE; |
| 456 | return true; |
| 457 | } |
| 458 | |
| 459 | switch( ea.getEventType() ) |
| 460 | { |
| 461 | case osgEA::PUSH: |
| 462 | _last_push = window_at_cursor; |
| 463 | _drag_finished = false; |
| 464 | event->type = sc::Event::MOUSE_DOWN; |
| 465 | break; |
| 466 | case osgEA::SCROLL: |
| 467 | switch( ea.getScrollingMotion() ) |
| 468 | { |
| 469 | case osgEA::SCROLL_UP: |
| 470 | event->delta.y() = 1; |
| 471 | break; |
| 472 | case osgEA::SCROLL_DOWN: |
| 473 | event->delta.y() = -1; |
| 474 | break; |
| 475 | default: |
| 476 | return false; |
| 477 | } |
| 478 | |
| 479 | // osg sends two events for every scrolling motion. We don't need |
| 480 | // duplicate events, so lets ignore the second event with the same |
| 481 | // timestamp. |
| 482 | if( _last_scroll_time == ea.getTime() ) |
| 483 | return window_at_cursor ? true : false; |
| 484 | _last_scroll_time = ea.getTime(); |
| 485 | |
| 486 | event->type = sc::Event::WHEEL; |
| 487 | break; |
| 488 | |
| 489 | // If drag has not been handled yet it has been aborted. So let's treat it |
| 490 | // like a normal mouse movement. |
| 491 | case osgEA::DRAG: |
| 492 | case osgEA::MOVE: |
| 493 | { |
| 494 | sc::WindowPtr last_mouse_over = _last_mouse_over.lock(); |
| 495 | if( last_mouse_over && last_mouse_over != window_at_cursor ) |
| 496 | last_mouse_over->handleEvent(event->clone(sc::Event::MOUSE_LEAVE)); |
| 497 | |
| 498 | _last_mouse_over = window_at_cursor; |
| 499 | event->type = sc::Event::MOUSE_MOVE; |
| 500 | break; |
| 501 | } |
| 502 | case osgEA::RELEASE: |
| 503 | { |
| 504 | sc::WindowPtr last_push = _last_push.lock(); |
| 505 | if( last_push && last_push != window_at_cursor ) |
| 506 | { |
| 507 | // Leave old window |
| 508 | last_push->handleEvent(event->clone(sc::Event::MOUSE_LEAVE)); |
| 509 | } |
| 510 | |
| 511 | _last_push.reset(); |
| 512 | event->type = sc::Event::MOUSE_UP; |
| 513 | break; |
| 514 | } |
| 515 | |
| 516 | default: |
| 517 | return false; |
| 518 | } |
| 519 | |
| 520 | return propagateEvent(event, window_at_cursor); |
| 521 | } |
| 522 | |
| 523 | //------------------------------------------------------------------------------ |
| 524 | bool DesktopGroup::handleKeyboard(const osgEA& ea) |
| 525 | { |
| 526 | if( !canHandleInput() ) |
| 527 | return false; |
| 528 | |
| 529 | sc::KeyboardEventPtr event(new sc::KeyboardEvent(ea)); |
| 530 | |
| 531 | if( auto drag = _last_drag.lock() ) |
| 532 | { |
| 533 | if( ea.getKey() == osgEA::KEY_Escape ) |
| 534 | finishDrag(drag, event); |
| 535 | |
| 536 | // While dragging ignore all key events |
| 537 | return true; |
| 538 | } |
| 539 | |
| 540 | // Detect key repeat (of non modifier keys) |
| 541 | if( !event->isModifier() ) |
| 542 | { |
| 543 | if( event->getType() == sc::Event::KEY_DOWN ) |
| 544 | { |
| 545 | if( event->keyCode() == _last_key_down_no_mod ) |
| 546 | event->setRepeat(true); |
| 547 | _last_key_down_no_mod = event->keyCode(); |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | if( event->keyCode() == _last_key_down_no_mod ) |
| 552 | _last_key_down_no_mod = ~0u; |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | sc::WindowPtr active_window = _focus_window.lock(); |
| 557 | bool handled = propagateEvent(event, active_window); |
| 558 | |
| 559 | if( event->getType() == sc::Event::KEY_DOWN |
| 560 | && !event->defaultPrevented() |
| 561 | && event->isPrint() ) |
| 562 | { |
| 563 | handled |= propagateEvent(event->clone(sc::Event::KEY_PRESS), active_window); |
| 564 | } |
| 565 | |
| 566 | return handled; |
| 567 | } |
| 568 | |
| 569 | //------------------------------------------------------------------------------ |
| 570 | bool DesktopGroup::propagateEvent( const sc::EventPtr& event, |
| 571 | const sc::WindowPtr& active_window ) |
| 572 | { |
| 573 | return active_window |
| 574 | ? active_window->handleEvent(event) |
| 575 | : propagateRootEvent(event); |
| 576 | } |
| 577 | |
| 578 | //------------------------------------------------------------------------------ |
| 579 | bool DesktopGroup::propagateRootEvent(const sc::EventPtr& event) |
| 580 | { |
| 581 | handleEvent(event); |
| 582 | |
| 583 | // stopPropagation() on DesktopGroup stops propagation to internal event |
| 584 | // handling. |
| 585 | return event->propagation_stopped; |
| 586 | } |
| 587 | |
| 588 | //------------------------------------------------------------------------------ |
| 589 | void DesktopGroup::handleResize(int x, int y, int width, int height) |
| 590 | { |
| 591 | if( _width == width && _height == height ) |
| 592 | return; |
| 593 | |
| 594 | _width = width; |
| 595 | _height = height; |
| 596 | |
| 597 | if( _scene_group.valid() ) |
| 598 | { |
| 599 | // Origin should be at top left corner, therefore we need to mirror the |
| 600 | // y-axis |
| 601 | _scene_group->setMatrix(osg::Matrix( |
| 602 | 1, 0, 0, 0, |
| 603 | 0, -1, 0, 0, |
| 604 | 0, 0, 1, 0, |
| 605 | 0, _height, 0, 1 |
| 606 | )); |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | //------------------------------------------------------------------------------ |
| 611 | bool DesktopGroup::handleDrag(const sc::EventPtr& event) |
| 612 | { |
| 613 | event->type = sc::Event::DRAG; |
| 614 | |
| 615 | auto drag_window = _last_drag.lock(); |
| 616 | if( !drag_window ) |
| 617 | { |
| 618 | _last_drag = drag_window = _last_push.lock(); |
| 619 | |
| 620 | if( drag_window ) |
| 621 | drag_window->handleEvent(event->clone(sc::Event::DRAG_START)); |
| 622 | } |
| 623 | |
| 624 | // TODO: dragover |
| 625 | return drag_window && drag_window->handleEvent(event); |
| 626 | } |
| 627 | |
| 628 | //------------------------------------------------------------------------------ |
| 629 | void DesktopGroup::finishDrag( const sc::WindowPtr& drag_src, |
| 630 | const sc::EventPtr& event ) |
| 631 | { |
| 632 | drag_src->handleEvent(event->clone(sc::Event::DRAG_END)); |
| 633 | _last_drag.reset(); |
| 634 | _drag_finished = true; |
| 635 | } |
| 636 | |
| 637 | //------------------------------------------------------------------------------ |
| 638 | void DesktopGroup::handleMouseMode(SGPropertyNode* node) |
| 639 | { |
| 640 | // pass-through indicates events should pass through to the UI |
| 641 | _handle_events = fgGetNode("/input/mice/mouse[0]/mode", node->getIntValue()) |
| 642 | ->getBoolValue("pass-through"); |
| 643 | } |
| 644 | |
| 645 | //------------------------------------------------------------------------------ |
| 646 | GUIMgr::GUIMgr() |
| 647 | { |
| 648 | |
| 649 | } |
| 650 | |
| 651 | //------------------------------------------------------------------------------ |
| 652 | void GUIMgr::setGUIViewAndCamera(osgViewer::View* view, osg::Camera* cam) |
| 653 | { |
| 654 | _viewerView = view; |
| 655 | _camera = cam; |
| 656 | } |
| 657 | |
| 658 | //------------------------------------------------------------------------------ |
| 659 | sc::WindowPtr GUIMgr::createWindow(const std::string& name) |
| 660 | { |
| 661 | sc::WindowPtr window = _desktop->createChild<sc::Window>(name); |
| 662 | if( name.empty() ) |
| 663 | window->set("id", std::to_string(window->getProps()->getIndex())); |
| 664 | return window; |
| 665 | } |
| 666 | |
| 667 | //------------------------------------------------------------------------------ |
| 668 | void GUIMgr::init() |
| 669 | { |
| 670 | assert(_viewerView); |
| 671 | if( _desktop && _event_handler ) |
| 672 | { |
| 673 | SG_LOG(SG_GUI, SG_WARN, "GUIMgr::init() already initialized."); |
| 674 | return; |
| 675 | } |
| 676 | |
| 677 | auto camera = _camera; |
| 678 | if (!camera) { |
| 679 | camera = _viewerView->getCamera(); |
| 680 | } |
| 681 | |
| 682 | DesktopPtr desktop( new DesktopGroup(camera) ); |
| 683 | desktop->handleResize |
| 684 | ( |
| 685 | 0, |
| 686 | 0, |
| 687 | fgGetInt("/sim/startup/xsize"), |
| 688 | fgGetInt("/sim/startup/ysize") |
| 689 | ); |
| 690 | _desktop = desktop; |
| 691 | |
| 692 | _event_handler = new GUIEventHandler(desktop); |
| 693 | _viewerView->getEventHandlers() |
| 694 | // GUI is on top of everything so lets install as first event handler |
| 695 | .push_front( _event_handler ); |
| 696 | |
| 697 | sc::Canvas::addPlacementFactory |
| 698 | ( |
| 699 | "window", |
| 700 | std::bind(&GUIMgr::addWindowPlacement, this, std::placeholders::_1, std::placeholders::_2) |
| 701 | ); |
| 702 | |
| 703 | _desktop->getProps()->fireCreatedRecursive(); |
| 704 | } |
| 705 | |
| 706 | //------------------------------------------------------------------------------ |
| 707 | void GUIMgr::shutdown() |
| 708 | { |
| 709 | if( !_desktop && !_event_handler ) |
| 710 | { |
| 711 | SG_LOG(SG_GUI, SG_WARN, "GUIMgr::shutdown() not running."); |
| 712 | return; |
| 713 | } |
| 714 | |
| 715 | sc::Canvas::removePlacementFactory("window"); |
| 716 | |
| 717 | if( _desktop ) |
| 718 | { |
| 719 | _desktop->destroy(); |
| 720 | _desktop.reset(); |
| 721 | } |
| 722 | |
| 723 | if( _event_handler ) |
| 724 | { |
| 725 | _viewerView->removeEventHandler( _event_handler ); |
| 726 | _event_handler = 0; |
| 727 | } |
| 728 | |
| 729 | _viewerView = nullptr; |
| 730 | } |
| 731 | |
| 732 | //------------------------------------------------------------------------------ |
| 733 | void GUIMgr::update(double dt) |
| 734 | { |
| 735 | _desktop->update(dt); |
| 736 | } |
| 737 | |
| 738 | //------------------------------------------------------------------------------ |
| 739 | sc::GroupPtr GUIMgr::getDesktop() |
| 740 | { |
| 741 | return _desktop; |
| 742 | } |
| 743 | |
| 744 | //------------------------------------------------------------------------------ |
| 745 | void GUIMgr::setInputFocus(const simgear::canvas::WindowPtr& window) |
| 746 | { |
| 747 | static_cast<DesktopGroup*>(_desktop.get())->setFocusWindow(window); |
| 748 | } |
| 749 | |
| 750 | //------------------------------------------------------------------------------ |
| 751 | bool GUIMgr::grabPointer(const sc::WindowPtr& window) |
| 752 | { |
| 753 | return static_cast<DesktopGroup*>(_desktop.get())->grabPointer(window); |
| 754 | } |
| 755 | |
| 756 | //------------------------------------------------------------------------------ |
| 757 | void GUIMgr::ungrabPointer(const sc::WindowPtr& window) |
| 758 | { |
| 759 | static_cast<DesktopGroup*>(_desktop.get())->ungrabPointer(window); |
| 760 | } |
| 761 | |
| 762 | //------------------------------------------------------------------------------ |
| 763 | sc::Placements |
| 764 | GUIMgr::addWindowPlacement( SGPropertyNode* placement, |
| 765 | sc::CanvasPtr canvas ) |
| 766 | { |
| 767 | const std::string& id = placement->getStringValue("id"); |
| 768 | |
| 769 | sc::Placements placements; |
| 770 | sc::WindowPtr window = _desktop->getChild<sc::Window>(id); |
| 771 | if( window ) |
| 772 | { |
| 773 | window->setCanvasContent(canvas); |
| 774 | placements.push_back( |
| 775 | sc::PlacementPtr( |
| 776 | new WindowPlacement(placement, window, canvas) |
| 777 | )); |
| 778 | } |
| 779 | return placements; |
| 780 | } |
| 781 | |
| 782 | |
| 783 | // Register the subsystem. |
| 784 | SGSubsystemMgr::Registrant<GUIMgr> registrantGUIMgr( |
| 785 | SGSubsystemMgr::DISPLAY, |
| 786 | { |
| 787 | {"viewer", SGSubsystemMgr::Dependency::HARD}, |
| 788 | }); |