1 // Canvas gui/dialog manager
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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.
19 #include "gui_mgr.hxx"
20 #include <Canvas/window.hxx>
22 #include <Main/fg_os.hxx>
23 #include <Main/fg_props.hxx>
24 #include <Main/globals.hxx>
25 #include <Viewer/CameraGroup.hxx>
26 #include <Viewer/renderer.hxx>
28 #include <simgear/canvas/Canvas.hxx>
29 #include <simgear/canvas/CanvasPlacement.hxx>
30 #include <simgear/scene/util/OsgMath.hxx>
32 #include <osg/BlendFunc>
33 #include <osgViewer/Viewer>
34 #include <osgGA/GUIEventHandler>
36 #include <boost/bind.hpp>
37 #include <boost/lexical_cast.hpp>
40 typedef boost::shared_ptr<DesktopGroup> DesktopPtr;
41 typedef boost::weak_ptr<DesktopGroup> DesktopWeakPtr;
46 class GUIEventHandler:
47 public osgGA::GUIEventHandler
50 GUIEventHandler(const DesktopWeakPtr& desktop_group);
52 bool handle( const osgGA::GUIEventAdapter& ea,
53 osgGA::GUIActionAdapter&,
58 DesktopWeakPtr _desktop;
62 * Track a canvas placement on a window
64 class WindowPlacement:
65 public simgear::canvas::Placement
68 WindowPlacement( SGPropertyNode* node,
69 canvas::WindowPtr window,
70 simgear::canvas::CanvasPtr canvas ):
77 * Remove placement from window
79 virtual ~WindowPlacement()
81 canvas::WindowPtr window = _window.lock();
82 simgear::canvas::CanvasPtr canvas = _canvas.lock();
84 if( window && canvas && canvas == window->getCanvasContent().lock() )
85 window->setCanvasContent( simgear::canvas::CanvasPtr() );
89 canvas::WindowWeakPtr _window;
90 simgear::canvas::CanvasWeakPtr _canvas;
97 public simgear::canvas::Group
101 bool handleEvent(const osgGA::GUIEventAdapter& ea);
107 SGPropertyChangeCallback<DesktopGroup> _cb_mouse_mode;
110 simgear::PropertyObject<int> _width,
113 canvas::WindowWeakPtr _last_push,
119 osg::Vec2 _drag_start;
122 double _last_scroll_time;
124 bool handleMouse(const osgGA::GUIEventAdapter& ea);
125 void handleResize(int x, int y, int width, int height);
126 void handleMouseMode(SGPropertyNode* node);
131 simgear::canvas::ElementFactory
132 getChildFactory(const std::string& type) const
134 if( type == "window" )
135 return &Element::create<canvas::Window>;
137 return Group::getChildFactory(type);
143 simgear::canvas::Placements
144 addPlacement(SGPropertyNode* node, simgear::canvas::CanvasPtr canvas)
146 const std::string& id = node->getStringValue("id");
148 simgear::canvas::Placements placements;
149 canvas::WindowPtr window = getChild<canvas::Window>(id);
152 window->setCanvasContent(canvas);
153 placements.push_back(
154 simgear::canvas::PlacementPtr(
155 new WindowPlacement(node, window, canvas)
162 //------------------------------------------------------------------------------
163 GUIEventHandler::GUIEventHandler(const DesktopWeakPtr& desktop_group):
164 _desktop( desktop_group )
169 //------------------------------------------------------------------------------
170 bool GUIEventHandler::handle( const osgGA::GUIEventAdapter& ea,
171 osgGA::GUIActionAdapter&,
175 if( ea.getHandled() )
178 DesktopPtr desktop = _desktop.lock();
179 return desktop && desktop->handleEvent(ea);
182 //------------------------------------------------------------------------------
183 DesktopGroup::DesktopGroup():
184 Group(simgear::canvas::CanvasPtr(), fgGetNode("/sim/gui/canvas", true)),
185 _cb_mouse_mode( this,
186 &DesktopGroup::handleMouseMode,
187 fgGetNode("/devices/status/mice/mouse[0]/mode") ),
188 _handle_events(true),
189 _width(_node, "size[0]"),
190 _height(_node, "size[1]"),
191 _resize(canvas::Window::NONE),
192 _last_cursor(MOUSE_CURSOR_NONE),
197 osg::Camera* camera =
198 flightgear::getGUICamera( flightgear::CameraGroup::getDefault() );
200 camera->addChild( getMatrixTransform() );
202 simgear::canvas::Canvas::addPlacementFactory
205 boost::bind(&DesktopGroup::addPlacement, this, _1, _2)
209 osg::StateSet* stateSet = _transform->getOrCreateStateSet();
210 stateSet->setDataVariance(osg::Object::STATIC);
211 stateSet->setRenderBinDetails(1000, "RenderBin");
213 // speed optimization?
214 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
215 stateSet->setAttribute(new osg::BlendFunc(
216 osg::BlendFunc::SRC_ALPHA,
217 osg::BlendFunc::ONE_MINUS_SRC_ALPHA)
219 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
220 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
221 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
222 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
224 _width = _height = -1;
226 // Do not change values on reinit
227 _width.node()->setAttribute(SGPropertyNode::PRESERVE, true);
228 _height.node()->setAttribute(SGPropertyNode::PRESERVE, true);
231 //------------------------------------------------------------------------------
232 bool DesktopGroup::handleEvent(const osgGA::GUIEventAdapter& ea)
234 switch( ea.getEventType() )
236 case osgGA::GUIEventAdapter::PUSH:
237 case osgGA::GUIEventAdapter::RELEASE:
238 // case osgGA::GUIEventAdapter::DOUBLECLICK:
239 // // DOUBLECLICK doesn't seem to be triggered...
240 case osgGA::GUIEventAdapter::DRAG:
241 case osgGA::GUIEventAdapter::MOVE:
242 case osgGA::GUIEventAdapter::SCROLL:
243 return handleMouse(ea);
244 case osgGA::GUIEventAdapter::RESIZE:
245 handleResize( ea.getWindowX(),
248 ea.getWindowHeight() );
249 return false; // Let other event handlers also consume resize events
259 | || | _ inside corner region (L-shaped part inside margin) both
260 |___||_|_ _ _/ directions can be resized (outside only one axis)
264 | || | } margin_neg \
265 | ========|== <-- window border |_ area where resize
266 | | } margin_pos | can be initiated
270 const float resize_margin_pos = 12;
271 const float resize_margin_neg = 2;
272 const float resize_corner = 20;
274 //------------------------------------------------------------------------------
275 bool DesktopGroup::handleMouse(const osgGA::GUIEventAdapter& ea)
277 if( !_transform->getNumChildren() || !_handle_events )
280 namespace sc = simgear::canvas;
281 sc::MouseEventPtr event(new sc::MouseEvent(ea));
283 event->screen_pos.x() = 0.5 * (ea.getXnormalized() + 1) * _width + 0.5;
284 event->screen_pos.y() = 0.5 * (ea.getYnormalized() + 1) * _height + 0.5;
285 if( ea.getMouseYOrientation()
286 != osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS )
287 event->screen_pos.y() = _height - event->screen_pos.y();
289 event->delta.x() = event->getScreenX() - _last_x;
290 event->delta.y() = event->getScreenY() - _last_y;
292 _last_x = event->getScreenX();
293 _last_y = event->getScreenY();
295 event->local_pos = event->client_pos = event->screen_pos;
297 if( !_resize_window.expired() )
299 switch( ea.getEventType() )
301 case osgGA::GUIEventAdapter::RELEASE:
302 _resize_window.lock()->handleResize(canvas::Window::NONE);
303 _resize_window.reset();
305 case osgGA::GUIEventAdapter::DRAG:
306 _resize_window.lock()->handleResize( _resize,
307 event->screen_pos - _drag_start );
314 canvas::WindowPtr window_at_cursor;
315 for( int i = _transform->getNumChildren() - 1; i >= 0; --i )
317 osg::Group *element = _transform->getChild(i)->asGroup();
320 assert(element->getUserData());
322 canvas::WindowPtr window =
323 boost::dynamic_pointer_cast<canvas::Window>
325 static_cast<sc::Element::OSGUserData*>(element->getUserData())->element
328 if( !window || !window->isCapturingEvents() || !window->isVisible() )
331 float margin = window->isResizable() ? resize_margin_pos : 0;
332 if( window->getScreenRegion().contains( event->getScreenX(),
336 window_at_cursor = window;
341 if( window_at_cursor )
343 const SGRect<float>& reg = window_at_cursor->getScreenRegion();
345 if( window_at_cursor->isResizable()
346 && ( ea.getEventType() == osgGA::GUIEventAdapter::MOVE
347 || ea.getEventType() == osgGA::GUIEventAdapter::PUSH
348 || ea.getEventType() == osgGA::GUIEventAdapter::RELEASE
350 && !reg.contains( event->getScreenX(),
352 -resize_margin_neg ) )
355 _last_cursor = fgGetMouseCursor();
359 if( event->getScreenX() <= reg.l() + resize_corner )
360 _resize |= canvas::Window::LEFT;
361 else if( event->getScreenX() >= reg.r() - resize_corner )
362 _resize |= canvas::Window::RIGHT;
364 if( event->getScreenY() <= reg.t() + resize_corner )
365 _resize |= canvas::Window::TOP;
366 else if( event->getScreenY() >= reg.b() - resize_corner )
367 _resize |= canvas::Window::BOTTOM;
369 static const int cursor_mapping[] =
371 0, MOUSE_CURSOR_LEFTSIDE, MOUSE_CURSOR_RIGHTSIDE, 0,
372 MOUSE_CURSOR_TOPSIDE, MOUSE_CURSOR_TOPLEFT, MOUSE_CURSOR_TOPRIGHT, 0,
373 MOUSE_CURSOR_BOTTOMSIDE, MOUSE_CURSOR_BOTTOMLEFT, MOUSE_CURSOR_BOTTOMRIGHT,
376 if( !cursor_mapping[_resize] )
379 fgSetMouseCursor(cursor_mapping[_resize]);
381 if( ea.getEventType() == osgGA::GUIEventAdapter::PUSH )
383 _resize_window = window_at_cursor;
384 _drag_start = event->screen_pos;
386 window_at_cursor->raise();
387 window_at_cursor->handleResize(_resize | canvas::Window::INIT);
396 fgSetMouseCursor(_last_cursor);
401 canvas::WindowPtr target_window = window_at_cursor;
402 switch( ea.getEventType() )
404 case osgGA::GUIEventAdapter::PUSH:
405 _last_push = window_at_cursor;
406 event->type = sc::Event::MOUSE_DOWN;
408 case osgGA::GUIEventAdapter::SCROLL:
409 switch( ea.getScrollingMotion() )
411 case osgGA::GUIEventAdapter::SCROLL_UP:
412 event->delta.y() = 1;
414 case osgGA::GUIEventAdapter::SCROLL_DOWN:
415 event->delta.y() = -1;
421 // osg sends two events for every scrolling motion. We don't need
422 // duplicate events, so lets ignore the second event with the same
424 if( _last_scroll_time == ea.getTime() )
425 return window_at_cursor ? true : false;
426 _last_scroll_time = ea.getTime();
428 event->type = sc::Event::WHEEL;
430 case osgGA::GUIEventAdapter::MOVE:
432 canvas::WindowPtr last_mouse_over = _last_mouse_over.lock();
433 if( last_mouse_over != window_at_cursor && last_mouse_over )
435 sc::MouseEventPtr move_event( new sc::MouseEvent(*event) );
436 move_event->type = sc::Event::MOUSE_LEAVE;
437 move_event->client_pos -= toOsg(last_mouse_over->getPosition());
438 move_event->local_pos = move_event->client_pos;
440 last_mouse_over->handleEvent(move_event);
442 _last_mouse_over = window_at_cursor;
443 event->type = sc::Event::MOUSE_MOVE;
446 case osgGA::GUIEventAdapter::RELEASE:
448 event->type = sc::Event::MOUSE_UP;
450 canvas::WindowPtr last_push = _last_push.lock();
453 if( last_push && last_push != target_window )
456 sc::MouseEventPtr leave_event( new sc::MouseEvent(*event) );
457 leave_event->type = sc::Event::MOUSE_LEAVE;
458 leave_event->client_pos -= toOsg(last_push->getPosition());
459 leave_event->local_pos = leave_event->client_pos;
461 last_push->handleEvent(leave_event);
465 case osgGA::GUIEventAdapter::DRAG:
466 target_window = _last_push.lock();
467 event->type = sc::Event::DRAG;
476 event->client_pos -= toOsg(target_window->getPosition());
477 event->local_pos = event->client_pos;
478 return target_window->handleEvent(event);
484 //------------------------------------------------------------------------------
485 void DesktopGroup::handleResize(int x, int y, int width, int height)
487 if( _width == width && _height == height )
493 // Origin should be at top left corner, therefore we need to mirror the y-axis
494 _transform->setMatrix(osg::Matrix(
502 //------------------------------------------------------------------------------
503 void DesktopGroup::handleMouseMode(SGPropertyNode* node)
505 // pass-through indicates events should pass through to the UI
506 _handle_events = fgGetNode("/input/mice/mouse[0]/mode", node->getIntValue())
507 ->getBoolValue("pass-through");
510 //------------------------------------------------------------------------------
512 _desktop( new DesktopGroup ),
513 _event_handler( new GUIEventHandler(
514 boost::static_pointer_cast<DesktopGroup>(_desktop)
517 // We handle the property listener manually within ::init and ::shutdown.
518 _desktop->removeListener();
521 //------------------------------------------------------------------------------
522 canvas::WindowPtr GUIMgr::createWindow(const std::string& name)
524 canvas::WindowPtr window = _desktop->createChild<canvas::Window>(name);
526 window->set<std::string>
529 boost::lexical_cast<std::string>(window->getProps()->getIndex())
534 //------------------------------------------------------------------------------
537 boost::static_pointer_cast<DesktopGroup>(_desktop)->handleResize
541 fgGetInt("/sim/startup/xsize"),
542 fgGetInt("/sim/startup/ysize")
545 globals->get_renderer()
548 // GUI is on top of everything so lets install as first event handler
549 .push_front( _event_handler );
551 _desktop->getProps()->addChangeListener(_desktop.get());
552 _desktop->getProps()->fireCreatedRecursive();
555 //------------------------------------------------------------------------------
556 void GUIMgr::shutdown()
558 _desktop->getProps()->removeChangeListener(_desktop.get());
560 globals->get_renderer()
562 ->removeEventHandler( _event_handler );
565 //------------------------------------------------------------------------------
566 void GUIMgr::update(double dt)
568 _desktop->update(dt);
571 //------------------------------------------------------------------------------
572 simgear::canvas::GroupPtr GUIMgr::getDesktop()