]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/gui_mgr.cxx
f72cf49d67aa8b975538066c2ef4390fc89d7d4b
[flightgear.git] / src / Canvas / gui_mgr.cxx
1 // Canvas gui/dialog manager
2 //
3 // Copyright (C) 2012  Thomas Geymayer <tomgey@gmail.com>
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 #include <Canvas/window.hxx>
21
22 #include <Main/fg_props.hxx>
23 #include <Main/globals.hxx>
24 #include <Viewer/CameraGroup.hxx>
25 #include <Viewer/renderer.hxx>
26
27 #include <simgear/canvas/Canvas.hxx>
28 #include <simgear/canvas/CanvasPlacement.hxx>
29
30 #include <osg/BlendFunc>
31 #include <osgViewer/Viewer>
32 #include <osgGA/GUIEventHandler>
33
34 #include <boost/bind.hpp>
35
36 /**
37  * Event handler
38  */
39 class GUIEventHandler:
40   public osgGA::GUIEventHandler
41 {
42   public:
43     GUIEventHandler(GUIMgr* gui_mgr):
44       _gui_mgr( gui_mgr )
45     {}
46
47     bool handle( const osgGA::GUIEventAdapter& ea,
48                  osgGA::GUIActionAdapter& aa,
49                  osg::Object*,
50                  osg::NodeVisitor* )
51     {
52       return _gui_mgr->handleEvent(ea);
53     }
54
55   protected:
56     GUIMgr *_gui_mgr;
57 };
58
59 /**
60  * Track a canvas placement on a window
61  */
62 class WindowPlacement:
63   public simgear::canvas::Placement
64 {
65   public:
66     WindowPlacement( SGPropertyNode* node,
67                      canvas::WindowPtr window,
68                      simgear::canvas::CanvasPtr canvas ):
69       Placement(node),
70       _window(window),
71       _canvas(canvas)
72     {}
73
74     /**
75      * Remove placement from window
76      */
77     virtual ~WindowPlacement()
78     {
79       canvas::WindowPtr window = _window.lock();
80       simgear::canvas::CanvasPtr canvas = _canvas.lock();
81
82       if( window && canvas && canvas == window->getCanvas().lock() )
83         window->setCanvas( simgear::canvas::CanvasPtr() );
84     }
85
86   private:
87     canvas::WindowWeakPtr _window;
88     simgear::canvas::CanvasWeakPtr _canvas;
89 };
90
91 /**
92  * Store pointer to window as user data
93  */
94 class WindowUserData:
95   public osg::Referenced
96 {
97   public:
98     canvas::WindowWeakPtr window;
99     WindowUserData(canvas::WindowPtr window):
100       window(window)
101     {}
102 };
103
104 //------------------------------------------------------------------------------
105 typedef boost::shared_ptr<canvas::Window> WindowPtr;
106 WindowPtr windowFactory(SGPropertyNode* node)
107 {
108   return WindowPtr(new canvas::Window(node));
109 }
110
111 //------------------------------------------------------------------------------
112 GUIMgr::GUIMgr():
113   PropertyBasedMgr( fgGetNode("/sim/gui/canvas", true),
114                     "window",
115                     &windowFactory ),
116   _event_handler( new GUIEventHandler(this) ),
117   _transform( new osg::MatrixTransform ),
118   _width(_props, "size[0]"),
119   _height(_props, "size[1]")
120 {
121   _width = _height = -1;
122
123   osg::Camera* camera =
124     flightgear::getGUICamera( flightgear::CameraGroup::getDefault() );
125   assert(camera);
126   camera->addChild(_transform);
127
128   osg::Viewport* vp = camera->getViewport();
129   handleResize(vp->x(), vp->y(), vp->width(), vp->height());
130
131   simgear::canvas::Canvas::addPlacementFactory
132   (
133     "window",
134     boost::bind(&GUIMgr::addPlacement, this, _1, _2)
135   );
136
137   osg::StateSet* stateSet = _transform->getOrCreateStateSet();
138   stateSet->setDataVariance(osg::Object::STATIC);
139   stateSet->setRenderBinDetails(1000, "RenderBin");
140
141   // speed optimization?
142   stateSet->setMode(GL_CULL_FACE, osg::StateAttribute::OFF);
143   stateSet->setAttribute(new osg::BlendFunc(
144     osg::BlendFunc::SRC_ALPHA,
145     osg::BlendFunc::ONE_MINUS_SRC_ALPHA)
146   );
147   stateSet->setMode(GL_BLEND, osg::StateAttribute::ON);
148   stateSet->setMode(GL_LIGHTING, osg::StateAttribute::OFF);
149   stateSet->setMode(GL_FOG, osg::StateAttribute::OFF);
150   stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
151 }
152
153 //------------------------------------------------------------------------------
154 void GUIMgr::init()
155 {
156   PropertyBasedMgr::init();
157
158   globals->get_renderer()
159          ->getViewer()
160          ->addEventHandler( _event_handler );
161 }
162
163 //------------------------------------------------------------------------------
164 void GUIMgr::shutdown()
165 {
166   PropertyBasedMgr::shutdown();
167
168   globals->get_renderer()
169          ->getViewer()
170          ->removeEventHandler( _event_handler );
171 }
172
173 //------------------------------------------------------------------------------
174 void GUIMgr::elementCreated(simgear::PropertyBasedElementPtr element)
175 {
176   canvas::WindowPtr window =
177     boost::static_pointer_cast<canvas::Window>(element);
178
179   size_t layer_index = std::max(0, window->getProps()->getIntValue("layer", 1));
180   osg::Group *layer = 0;
181
182   if( layer_index < _transform->getNumChildren() )
183   {
184     layer = _transform->getChild(layer_index)->asGroup();
185     assert(layer);
186   }
187   else
188   {
189     while( _transform->getNumChildren() <= layer_index )
190     {
191       layer = new osg::Group;
192       _transform->addChild(layer);
193     }
194   }
195   window->getGroup()->setUserData(new WindowUserData(window));
196   layer->addChild(window->getGroup());
197 }
198
199 //------------------------------------------------------------------------------
200 bool GUIMgr::handleEvent(const osgGA::GUIEventAdapter& ea)
201 {
202   switch( ea.getEventType() )
203   {
204     case osgGA::GUIEventAdapter::PUSH:
205     case osgGA::GUIEventAdapter::RELEASE:
206 //    case osgGA::GUIEventAdapter::DOUBLECLICK:
207 //    // DOUBLECLICK doesn't seem to be triggered...
208     case osgGA::GUIEventAdapter::DRAG:
209     case osgGA::GUIEventAdapter::MOVE:
210     case osgGA::GUIEventAdapter::SCROLL:
211       return handleMouse(ea);
212 //        case osgGA::GUIEventAdapter::MOVE:
213 //          std::cout << "MOVE" << std::endl;
214 //          break;
215     case osgGA::GUIEventAdapter::RESIZE:
216       handleResize( ea.getWindowX(),
217                     ea.getWindowY(),
218                     ea.getWindowWidth(),
219                     ea.getWindowHeight() );
220       return true;
221     default:
222       return false;
223   }
224 }
225
226 //------------------------------------------------------------------------------
227 canvas::WindowPtr GUIMgr::getWindow(size_t i)
228 {
229   return boost::static_pointer_cast<canvas::Window>(_elements[i]);
230 }
231
232 //------------------------------------------------------------------------------
233 simgear::canvas::Placements
234 GUIMgr::addPlacement( SGPropertyNode* node,
235                       simgear::canvas::CanvasPtr canvas )
236 {
237   int placement_index = node->getIntValue("index", -1);
238
239   simgear::canvas::Placements placements;
240   for( size_t i = 0; i < _elements.size(); ++i )
241   {
242     if( placement_index >= 0 && static_cast<int>(i) != placement_index )
243       continue;
244
245     canvas::WindowPtr window = getWindow(i);
246     if( !window )
247       continue;
248
249     window->setCanvas(canvas);
250     placements.push_back(
251       simgear::canvas::PlacementPtr(new WindowPlacement(node, window, canvas))
252     );
253   }
254   return placements;
255 }
256
257 //------------------------------------------------------------------------------
258 bool GUIMgr::handleMouse(const osgGA::GUIEventAdapter& ea)
259 {
260   if( !_transform->getNumChildren() )
261     return false;
262
263   simgear::canvas::MouseEvent event( ea.getEventType() );
264
265   event.x = 0.5 * (ea.getXnormalized() + 1) * _width + 0.5;
266   event.y = 0.5 * (ea.getYnormalized() + 1) * _height + 0.5;
267   if(    ea.getMouseYOrientation()
268       != osgGA::GUIEventAdapter::Y_INCREASING_DOWNWARDS )
269     event.y = _height - event.y;
270
271   event.button = ea.getButton();
272   event.state = ea.getButtonMask();
273   event.mod = ea.getModKeyMask();
274   event.scroll = ea.getScrollingMotion();
275
276   canvas::WindowPtr window_at_cursor;
277   for( int i = _transform->getNumChildren() - 1; i >= 0; --i )
278   {
279     osg::Group *layer = _transform->getChild(i)->asGroup();
280     assert(layer);
281     if( !layer->getNumChildren() )
282       continue;
283
284     for( int j = layer->getNumChildren() - 1; j >= 0; --j )
285     {
286       assert(layer->getChild(j)->getUserData());
287       canvas::WindowPtr window =
288         static_cast<WindowUserData*>(layer->getChild(j)->getUserData())
289           ->window.lock();
290       if( window->getRegion().contains(event.x, event.y) )
291       {
292         window_at_cursor = window;
293         break;
294       }
295     }
296
297     if( window_at_cursor )
298       break;
299   }
300
301   canvas::WindowPtr target_window = window_at_cursor;
302   switch( ea.getEventType() )
303   {
304     case osgGA::GUIEventAdapter::PUSH:
305       _last_push = window_at_cursor;
306       break;
307     case osgGA::GUIEventAdapter::SCROLL:
308     case osgGA::GUIEventAdapter::MOVE:
309       break;
310
311     case osgGA::GUIEventAdapter::RELEASE:
312       target_window = _last_push.lock();
313       _last_push.reset();
314       break;
315
316     case osgGA::GUIEventAdapter::DRAG:
317       target_window = _last_push.lock();
318       break;
319
320     default:
321       return false;
322   }
323
324   if( target_window )
325   {
326     event.dx = event.x - _last_x;
327     event.dy = event.y - _last_y;
328
329     _last_x = event.x;
330     _last_y = event.y;
331
332     // Let the event position be always relative to the top left window corner
333     event.x -= target_window->getRegion().x();
334     event.y -= target_window->getRegion().y();
335
336     return target_window->handleMouseEvent(event);
337   }
338   else
339     return false;
340 }
341
342 //------------------------------------------------------------------------------
343 void GUIMgr::handleResize(int x, int y, int width, int height)
344 {
345   if( _width == width && _height == height )
346     return;
347
348   _width = width;
349   _height = height;
350
351   // Origin should be at top left corner, therefore we need to mirror the y-axis
352   _transform->setMatrix(osg::Matrix(
353     1,  0, 0, 0,
354     0, -1, 0, 0,
355     0,  0, 1, 0,
356     0, _height, 0, 1
357   ));
358 }