]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/window.cxx
Canvas: Basic support for window stacking
[flightgear.git] / src / Canvas / window.cxx
1 // Window for placing a Canvas onto it (for dialogs, menus, etc.)
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 "window.hxx"
20 #include <Canvas/canvas.hxx>
21
22 #include <osgGA/GUIEventHandler>
23
24 #include <boost/foreach.hpp>
25
26 namespace canvas
27 {
28   //----------------------------------------------------------------------------
29   Window::Window(SGPropertyNode* node):
30     PropertyBasedElement(node),
31     _image(node)
32   {
33     // TODO probably better remove default position and size
34     node->setFloatValue("x", 50);
35     node->setFloatValue("y", 100);
36     node->setFloatValue("size[0]", 400);
37     node->setFloatValue("size[1]", 300);
38
39     node->setFloatValue("source/right", 1);
40     node->setFloatValue("source/bottom", 1);
41     node->setBoolValue("source/normalized", true);
42   }
43
44   //----------------------------------------------------------------------------
45   Window::~Window()
46   {
47     BOOST_FOREACH(osg::Group* parent, getGroup()->getParents())
48     {
49       parent->removeChild(getGroup());
50     }
51   }
52
53   //----------------------------------------------------------------------------
54   void Window::update(double delta_time_sec)
55   {
56     _image.update(delta_time_sec);
57   }
58
59   //----------------------------------------------------------------------------
60   void Window::valueChanged(SGPropertyNode * node)
61   {
62     if( node->getParent() == _node && node->getNameString() == "raise-top" )
63       doRaise(node);
64     else
65       _image.valueChanged(node);
66   }
67
68   //----------------------------------------------------------------------------
69   osg::Group* Window::getGroup()
70   {
71     return _image.getMatrixTransform();
72   }
73
74   //----------------------------------------------------------------------------
75   const Rect<float>& Window::getRegion() const
76   {
77     return _image.getRegion();
78   }
79
80   //----------------------------------------------------------------------------
81   void Window::setCanvas(CanvasPtr canvas)
82   {
83     _image.setCanvas(canvas);
84   }
85
86   //----------------------------------------------------------------------------
87   CanvasWeakPtr Window::getCanvas() const
88   {
89     return _image.getCanvas();
90   }
91
92   //----------------------------------------------------------------------------
93   bool Window::handleMouseEvent(const MouseEvent& event)
94   {
95     if( !getCanvas().expired() )
96       return getCanvas().lock()->handleMouseEvent(event);
97     else
98       return false;
99   }
100
101   //----------------------------------------------------------------------------
102   void Window::doRaise(SGPropertyNode* node_raise)
103   {
104     if( !node_raise->getBoolValue() )
105       return;
106
107     BOOST_FOREACH(osg::Group* parent, getGroup()->getParents())
108     {
109       // Remove window...
110       parent->removeChild(getGroup());
111
112       // ...and add again as topmost window
113       parent->addChild(getGroup());
114     }
115
116     node_raise->setBoolValue(false);
117   }
118
119 } // namespace canvas