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>
41 class GUIEventHandler:
42 public osgGA::GUIEventHandler
45 GUIEventHandler(GUIMgr* gui_mgr):
49 bool handle( const osgGA::GUIEventAdapter& ea,
50 osgGA::GUIActionAdapter& aa,
56 return _gui_mgr->handleEvent(ea);
64 * Track a canvas placement on a window
66 class WindowPlacement:
67 public simgear::canvas::Placement
70 WindowPlacement( SGPropertyNode* node,
71 canvas::WindowPtr window,
72 simgear::canvas::CanvasPtr canvas ):
79 * Remove placement from window
81 virtual ~WindowPlacement()
83 canvas::WindowPtr window = _window.lock();
84 simgear::canvas::CanvasPtr canvas = _canvas.lock();
86 if( window && canvas && canvas == window->getCanvas().lock() )
87 window->setCanvas( simgear::canvas::CanvasPtr() );
91 canvas::WindowWeakPtr _window;
92 simgear::canvas::CanvasWeakPtr _canvas;
95 //------------------------------------------------------------------------------
96 typedef boost::shared_ptr<canvas::Window> WindowPtr;
97 WindowPtr windowFactory(SGPropertyNode* node)
99 return WindowPtr(new canvas::Window(node));
102 //------------------------------------------------------------------------------
104 PropertyBasedMgr( fgGetNode("/sim/gui/canvas", true),
107 _event_handler( new GUIEventHandler(this) ),
108 _transform( new osg::MatrixTransform ),
109 _cb_mouse_mode( this,
110 &GUIMgr::handleMouseMode,
111 fgGetNode("/devices/status/mice/mouse[0]/mode") ),
112 _handle_events(true),
113 _width(_props, "size[0]"),
114 _height(_props, "size[1]"),
115 _resize(canvas::Window::NONE),
116 _last_cursor(MOUSE_CURSOR_NONE),
119 _width = _height = -1;
121 osg::Camera* camera =
122 flightgear::getGUICamera( flightgear::CameraGroup::getDefault() );
124 camera->addChild(_transform);
126 simgear::canvas::Canvas::addPlacementFactory
129 boost::bind(&GUIMgr::addPlacement, this, _1, _2)
132 osg::StateSet* stateSet = _transform->getOrCreateStateSet();
133 stateSet->setDataVariance(osg::Object::STATIC);
134 stateSet->setRenderBinDetails(1000, "RenderBin");
136 // speed optimization?
137 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
138 stateSet->setAttribute(new osg::BlendFunc(
139 osg::BlendFunc::SRC_ALPHA,
140 osg::BlendFunc::ONE_MINUS_SRC_ALPHA)
142 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
143 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
144 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
145 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
148 //------------------------------------------------------------------------------
149 canvas::WindowPtr GUIMgr::createWindow(const std::string& name)
151 return boost::static_pointer_cast<canvas::Window>( createElement(name) );
154 //------------------------------------------------------------------------------
161 fgGetInt("/sim/startup/xsize"),
162 fgGetInt("/sim/startup/ysize")
165 PropertyBasedMgr::init();
167 globals->get_renderer()
170 // GUI is on top of everything so lets install as first event handler
171 .push_front( _event_handler );
174 //------------------------------------------------------------------------------
175 void GUIMgr::shutdown()
177 PropertyBasedMgr::shutdown();
179 globals->get_renderer()
181 ->removeEventHandler( _event_handler );
184 //------------------------------------------------------------------------------
185 bool GUIMgr::handleEvent(const osgGA::GUIEventAdapter& ea)
187 switch( ea.getEventType() )
189 case osgGA::GUIEventAdapter::PUSH:
190 case osgGA::GUIEventAdapter::RELEASE:
191 // case osgGA::GUIEventAdapter::DOUBLECLICK:
192 // // DOUBLECLICK doesn't seem to be triggered...
193 case osgGA::GUIEventAdapter::DRAG:
194 case osgGA::GUIEventAdapter::MOVE:
195 case osgGA::GUIEventAdapter::SCROLL:
196 return handleMouse(ea);
197 case osgGA::GUIEventAdapter::RESIZE:
198 handleResize( ea.getWindowX(),
201 ea.getWindowHeight() );
202 return false; // Let other event handlers also consume resize events
208 //------------------------------------------------------------------------------
209 void GUIMgr::elementCreated(simgear::PropertyBasedElementPtr element)
211 canvas::WindowPtr window =
212 boost::static_pointer_cast<canvas::Window>(element);
214 size_t layer_index = std::max(0, window->getProps()->getIntValue("layer", 1));
215 osg::Group *layer = 0;
217 if( layer_index < _transform->getNumChildren() )
219 layer = _transform->getChild(layer_index)->asGroup();
224 while( _transform->getNumChildren() <= layer_index )
226 layer = new osg::Group;
227 _transform->addChild(layer);
231 layer->addChild(window->getGroup());
234 //------------------------------------------------------------------------------
235 canvas::WindowPtr GUIMgr::getWindow(size_t i)
237 return boost::static_pointer_cast<canvas::Window>(_elements[i]);
240 //------------------------------------------------------------------------------
241 simgear::canvas::Placements
242 GUIMgr::addPlacement( SGPropertyNode* node,
243 simgear::canvas::CanvasPtr canvas )
245 int placement_index = node->getIntValue("index", -1);
247 simgear::canvas::Placements placements;
248 for( size_t i = 0; i < _elements.size(); ++i )
250 if( placement_index >= 0 && static_cast<int>(i) != placement_index )
253 canvas::WindowPtr window = getWindow(i);
257 window->setCanvas(canvas);
258 placements.push_back(
259 simgear::canvas::PlacementPtr(new WindowPlacement(node, window, canvas))
269 | || | _ inside corner region (L-shaped part inside margin) both
270 |___||_|_ _ _/ directions can be resized (outside only one axis)
274 | || | } margin_neg \
275 | ========|== <-- window border |_ area where resize
276 | | } margin_pos | can be initiated
280 const float resize_margin_pos = 12;
281 const float resize_margin_neg = 2;
282 const float resize_corner = 20;
284 //------------------------------------------------------------------------------
285 bool GUIMgr::handleMouse(const osgGA::GUIEventAdapter& ea)
287 if( !_transform->getNumChildren() || !_handle_events )
290 namespace sc = simgear::canvas;
291 sc::MouseEventPtr event(new sc::MouseEvent(ea));
293 event->screen_pos.x() = 0.5 * (ea.getXnormalized() + 1) * _width + 0.5;
294 event->screen_pos.y() = 0.5 * (ea.getYnormalized() + 1) * _height + 0.5;
295 if( ea.getMouseYOrientation()
296 != osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS )
297 event->screen_pos.y() = _height - event->screen_pos.y();
299 event->delta.x() = event->getScreenX() - _last_x;
300 event->delta.y() = event->getScreenY() - _last_y;
302 _last_x = event->getScreenX();
303 _last_y = event->getScreenY();
305 event->local_pos = event->client_pos = event->screen_pos;
307 if( !_resize_window.expired() )
309 switch( ea.getEventType() )
311 case osgGA::GUIEventAdapter::RELEASE:
312 _resize_window.lock()->handleResize(canvas::Window::NONE);
313 _resize_window.reset();
315 case osgGA::GUIEventAdapter::DRAG:
316 _resize_window.lock()->handleResize(_resize, event->delta);
323 canvas::WindowPtr window_at_cursor;
324 for( int i = _transform->getNumChildren() - 1; i >= 0; --i )
326 osg::Group *layer = _transform->getChild(i)->asGroup();
328 if( !layer->getNumChildren() )
331 for( int j = layer->getNumChildren() - 1; j >= 0; --j )
333 assert(layer->getChild(j)->getUserData());
334 canvas::WindowPtr window =
335 boost::static_pointer_cast<canvas::Window>
337 static_cast<sc::Element::OSGUserData*>
339 layer->getChild(j)->getUserData()
343 if( !window->isCapturingEvents() || !window->isVisible() )
346 float margin = window->isResizable() ? resize_margin_pos : 0;
347 if( window->getScreenRegion().contains( event->getScreenX(),
351 window_at_cursor = window;
356 if( window_at_cursor )
360 if( window_at_cursor )
362 const SGRect<float>& reg = window_at_cursor->getScreenRegion();
364 if( window_at_cursor->isResizable()
365 && ( ea.getEventType() == osgGA::GUIEventAdapter::MOVE
366 || ea.getEventType() == osgGA::GUIEventAdapter::PUSH
367 || ea.getEventType() == osgGA::GUIEventAdapter::RELEASE
369 && !reg.contains( event->getScreenX(),
371 -resize_margin_neg ) )
374 _last_cursor = fgGetMouseCursor();
378 if( event->getScreenX() <= reg.l() + resize_corner )
379 _resize |= canvas::Window::LEFT;
380 else if( event->getScreenX() >= reg.r() - resize_corner )
381 _resize |= canvas::Window::RIGHT;
383 if( event->getScreenY() <= reg.t() + resize_corner )
384 _resize |= canvas::Window::TOP;
385 else if( event->getScreenY() >= reg.b() - resize_corner )
386 _resize |= canvas::Window::BOTTOM;
388 static const int cursor_mapping[] =
390 0, MOUSE_CURSOR_LEFTSIDE, MOUSE_CURSOR_RIGHTSIDE, 0,
391 MOUSE_CURSOR_TOPSIDE, MOUSE_CURSOR_TOPLEFT, MOUSE_CURSOR_TOPRIGHT, 0,
392 MOUSE_CURSOR_BOTTOMSIDE, MOUSE_CURSOR_BOTTOMLEFT, MOUSE_CURSOR_BOTTOMRIGHT,
395 if( !cursor_mapping[_resize] )
398 fgSetMouseCursor(cursor_mapping[_resize]);
400 if( ea.getEventType() == osgGA::GUIEventAdapter::PUSH )
402 _resize_window = window_at_cursor;
403 window_at_cursor->doRaise();
404 window_at_cursor->handleResize( _resize | canvas::Window::INIT,
414 fgSetMouseCursor(_last_cursor);
419 canvas::WindowPtr target_window = window_at_cursor;
420 switch( ea.getEventType() )
422 case osgGA::GUIEventAdapter::PUSH:
423 _last_push = window_at_cursor;
424 event->type = sc::Event::MOUSE_DOWN;
426 case osgGA::GUIEventAdapter::SCROLL:
427 switch( ea.getScrollingMotion() )
429 case osgGA::GUIEventAdapter::SCROLL_UP:
430 event->delta.y() = 1;
432 case osgGA::GUIEventAdapter::SCROLL_DOWN:
433 event->delta.y() = -1;
439 // osg sends two events for every scrolling motion. We don't need
440 // duplicate events, so lets ignore the second event with the same
442 if( _last_scroll_time == ea.getTime() )
443 return window_at_cursor ? true : false;
444 _last_scroll_time = ea.getTime();
446 event->type = sc::Event::WHEEL;
448 case osgGA::GUIEventAdapter::MOVE:
450 canvas::WindowPtr last_mouse_over = _last_mouse_over.lock();
451 if( last_mouse_over != window_at_cursor && last_mouse_over )
453 sc::MouseEventPtr move_event( new sc::MouseEvent(*event) );
454 move_event->type = sc::Event::MOUSE_LEAVE;
455 move_event->client_pos -= toOsg(last_mouse_over->getPosition());
456 move_event->local_pos = move_event->client_pos;
458 last_mouse_over->handleEvent(move_event);
460 _last_mouse_over = window_at_cursor;
461 event->type = sc::Event::MOUSE_MOVE;
464 case osgGA::GUIEventAdapter::RELEASE:
465 target_window = _last_push.lock();
467 event->type = sc::Event::MOUSE_UP;
470 case osgGA::GUIEventAdapter::DRAG:
471 target_window = _last_push.lock();
472 event->type = sc::Event::DRAG;
481 event->client_pos -= toOsg(target_window->getPosition());
482 event->local_pos = event->client_pos;
483 return target_window->handleEvent(event);
489 //------------------------------------------------------------------------------
490 void GUIMgr::handleResize(int x, int y, int width, int height)
492 if( _width == width && _height == height )
498 // Origin should be at top left corner, therefore we need to mirror the y-axis
499 _transform->setMatrix(osg::Matrix(
507 //------------------------------------------------------------------------------
508 void GUIMgr::handleMouseMode(SGPropertyNode* node)
510 // pass-through indicates events should pass through to the UI
511 _handle_events = fgGetNode("/input/mice/mouse[0]/mode", node->getIntValue())
512 ->getBoolValue("pass-through");