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>
21 #include <Canvas/canvas.hxx>
23 #include <Main/globals.hxx>
24 #include <Viewer/CameraGroup.hxx>
25 #include <Viewer/renderer.hxx>
27 #include <osg/BlendFunc>
28 #include <osgViewer/Viewer>
29 #include <osgGA/GUIEventHandler>
31 #include <boost/bind.hpp>
36 class GUIEventHandler:
37 public osgGA::GUIEventHandler
40 GUIEventHandler(GUIMgr* gui_mgr):
44 bool handle( const osgGA::GUIEventAdapter& ea,
45 osgGA::GUIActionAdapter& aa,
49 return _gui_mgr->handleEvent(ea);
57 * Track a canvas placement on a window
59 class WindowPlacement:
60 public canvas::Placement
63 WindowPlacement( canvas::WindowPtr window,
70 * Remove placement from window
72 virtual ~WindowPlacement()
74 canvas::WindowPtr window = _window.lock();
75 CanvasPtr canvas = _canvas.lock();
77 if( window && canvas && canvas == window->getCanvas().lock() )
78 window->setCanvas( CanvasPtr() );
82 canvas::WindowWeakPtr _window;
83 CanvasWeakPtr _canvas;
87 * Store pointer to window as user data
90 public osg::Referenced
93 canvas::WindowWeakPtr window;
94 WindowUserData(canvas::WindowPtr window):
99 //------------------------------------------------------------------------------
100 typedef boost::shared_ptr<canvas::Window> WindowPtr;
101 WindowPtr windowFactory(SGPropertyNode* node)
103 return WindowPtr(new canvas::Window(node));
106 //------------------------------------------------------------------------------
108 PropertyBasedMgr("/sim/gui/canvas", "window", &windowFactory),
109 _event_handler( new GUIEventHandler(this) ),
110 _transform( new osg::MatrixTransform ),
111 _width(_props, "size[0]"),
112 _height(_props, "size[1]")
114 _width = _height = -1;
116 osg::Camera* camera =
117 flightgear::getGUICamera( flightgear::CameraGroup::getDefault() );
119 camera->addChild(_transform);
121 osg::Viewport* vp = camera->getViewport();
122 handleResize(vp->x(), vp->y(), vp->width(), vp->height());
124 Canvas::addPlacementFactory
127 boost::bind(&GUIMgr::addPlacement, this, _1, _2)
130 osg::StateSet* stateSet = _transform->getOrCreateStateSet();
131 stateSet->setDataVariance(osg::Object::STATIC);
132 stateSet->setRenderBinDetails(1000, "RenderBin");
134 // speed optimization?
135 stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
136 stateSet->setAttribute(new osg::BlendFunc(
137 osg::BlendFunc::SRC_ALPHA,
138 osg::BlendFunc::ONE_MINUS_SRC_ALPHA)
140 stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
141 stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
142 stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
143 stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
146 //------------------------------------------------------------------------------
149 PropertyBasedMgr::init();
151 globals->get_renderer()
153 ->addEventHandler( _event_handler );
156 //------------------------------------------------------------------------------
157 void GUIMgr::shutdown()
159 PropertyBasedMgr::shutdown();
161 globals->get_renderer()
163 ->removeEventHandler( _event_handler );
166 //------------------------------------------------------------------------------
167 void GUIMgr::elementCreated(PropertyBasedElementPtr element)
169 canvas::WindowPtr window =
170 boost::static_pointer_cast<canvas::Window>(element);
172 size_t layer_index = std::max(0, window->getProps()->getIntValue("layer", 1));
173 osg::Group *layer = 0;
175 if( layer_index < _transform->getNumChildren() )
177 layer = _transform->getChild(layer_index)->asGroup();
182 while( _transform->getNumChildren() <= layer_index )
184 layer = new osg::Group;
185 _transform->addChild(layer);
188 window->getGroup()->setUserData(new WindowUserData(window));
189 layer->addChild(window->getGroup());
192 //------------------------------------------------------------------------------
193 bool GUIMgr::handleEvent(const osgGA::GUIEventAdapter& ea)
195 switch( ea.getEventType() )
197 case osgGA::GUIEventAdapter::PUSH:
198 case osgGA::GUIEventAdapter::RELEASE:
199 // case osgGA::GUIEventAdapter::DOUBLECLICK:
200 // // DOUBLECLICK doesn't seem to be triggered...
201 case osgGA::GUIEventAdapter::DRAG:
202 case osgGA::GUIEventAdapter::MOVE:
203 case osgGA::GUIEventAdapter::SCROLL:
204 return handleMouse(ea);
205 // case osgGA::GUIEventAdapter::MOVE:
206 // std::cout << "MOVE" << std::endl;
208 case osgGA::GUIEventAdapter::RESIZE:
209 handleResize( ea.getWindowX(),
212 ea.getWindowHeight() );
219 //------------------------------------------------------------------------------
220 canvas::WindowPtr GUIMgr::getWindow(size_t i)
222 return boost::static_pointer_cast<canvas::Window>(_elements[i]);
225 //------------------------------------------------------------------------------
226 canvas::Placements GUIMgr::addPlacement( const SGPropertyNode* node,
229 int placement_index = node->getIntValue("index", -1);
231 canvas::Placements placements;
232 for( size_t i = 0; i < _elements.size(); ++i )
234 if( placement_index >= 0 && static_cast<int>(i) != placement_index )
237 canvas::WindowPtr window = getWindow(i);
241 window->setCanvas(canvas);
242 placements.push_back(
243 canvas::PlacementPtr(new WindowPlacement(window, canvas))
249 //------------------------------------------------------------------------------
250 bool GUIMgr::handleMouse(const osgGA::GUIEventAdapter& ea)
252 if( !_transform->getNumChildren() )
255 canvas::MouseEvent event( ea.getEventType() );
257 event.x = 0.5 * (ea.getXnormalized() + 1) * _width + 0.5;
258 event.y = 0.5 * (ea.getYnormalized() + 1) * _height + 0.5;
259 if( ea.getMouseYOrientation()
260 != osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS )
261 event.y = _height - event.y;
263 event.button = ea.getButton();
264 event.state = ea.getButtonMask();
265 event.mod = ea.getModKeyMask();
266 event.scroll = ea.getScrollingMotion();
268 canvas::WindowPtr window_at_cursor;
269 for( int i = _transform->getNumChildren() - 1; i >= 0; --i )
271 osg::Group *layer = _transform->getChild(i)->asGroup();
273 if( !layer->getNumChildren() )
276 for( int j = layer->getNumChildren() - 1; j >= 0; --j )
278 assert(layer->getChild(j)->getUserData());
279 canvas::WindowPtr window =
280 static_cast<WindowUserData*>(layer->getChild(j)->getUserData())
282 if( window->getRegion().contains(event.x, event.y) )
284 window_at_cursor = window;
289 if( window_at_cursor )
293 canvas::WindowPtr target_window = window_at_cursor;
294 switch( ea.getEventType() )
296 case osgGA::GUIEventAdapter::PUSH:
297 _last_push = window_at_cursor;
299 case osgGA::GUIEventAdapter::SCROLL:
300 case osgGA::GUIEventAdapter::MOVE:
303 case osgGA::GUIEventAdapter::RELEASE:
304 target_window = _last_push.lock();
308 case osgGA::GUIEventAdapter::DRAG:
309 target_window = _last_push.lock();
318 event.dx = event.x - _last_x;
319 event.dy = event.y - _last_y;
324 // Let the event position be always relative to the top left window corner
325 event.x -= target_window->getRegion().x();
326 event.y -= target_window->getRegion().y();
328 return target_window->handleMouseEvent(event);
334 //------------------------------------------------------------------------------
335 void GUIMgr::handleResize(int x, int y, int width, int height)
337 if( _width == width && _height == height )
343 // Origin should be at top left corner, therefore we need to mirror the y-axis
344 _transform->setMatrix(osg::Matrix(