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