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