]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/window.cxx
Ensure immediate canvas update inside GUI windows.
[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     _resizable(false),
35     _capture_events(true),
36     _resize_top(node, "resize-top"),
37     _resize_right(node, "resize-right"),
38     _resize_bottom(node, "resize-bottom"),
39     _resize_left(node, "resize-left"),
40     _resize_status(node, "resize-status")
41   {
42     _image.removeListener();
43
44     // TODO probably better remove default position and size
45     node->setFloatValue("x", 50);
46     node->setFloatValue("y", 100);
47     node->setFloatValue("size[0]", 400);
48     node->setFloatValue("size[1]", 300);
49
50     node->setFloatValue("source/right", 1);
51     node->setFloatValue("source/bottom", 1);
52     node->setBoolValue("source/normalized", true);
53   }
54
55   //----------------------------------------------------------------------------
56   Window::~Window()
57   {
58
59   }
60
61   //----------------------------------------------------------------------------
62   void Window::update(double delta_time_sec)
63   {
64     _image.update(delta_time_sec);
65   }
66
67   //----------------------------------------------------------------------------
68   void Window::valueChanged(SGPropertyNode * node)
69   {
70     bool handled = false;
71     if( node->getParent() == _node )
72     {
73       handled = true;
74       if( node->getNameString() == "raise-top" )
75         doRaise(node);
76       else if( node->getNameString()  == "resize" )
77         _resizable = node->getBoolValue();
78       else if( node->getNameString() == "capture-events" )
79         _capture_events = node->getBoolValue();
80       else
81         handled = false;
82
83       // Ensure canvas is redrawn before the window is displayed after it has
84       // been hidden. We can't rely on the cull callback as it gets called too
85       // late (GUI camera is attached as POST_RENDER whereas canvas contents are
86       // rendered during PRE_RENDER).
87       if( node->getNameString() == "visible" && node->getBoolValue() )
88       {
89         simgear::canvas::CanvasPtr canvas = getCanvas().lock();
90         if( canvas )
91           canvas->enableRendering();
92       }
93     }
94
95     if( !handled )
96       _image.valueChanged(node);
97   }
98
99   //----------------------------------------------------------------------------
100   osg::Group* Window::getGroup()
101   {
102     return _image.getMatrixTransform();
103   }
104
105   //----------------------------------------------------------------------------
106   const SGRect<float>& Window::getRegion() const
107   {
108     return _image.getRegion();
109   }
110
111   //----------------------------------------------------------------------------
112   void Window::setCanvas(simgear::canvas::CanvasPtr canvas)
113   {
114     _image.setSrcCanvas(canvas);
115   }
116
117   //----------------------------------------------------------------------------
118   simgear::canvas::CanvasWeakPtr Window::getCanvas() const
119   {
120     return _image.getSrcCanvas();
121   }
122
123   //----------------------------------------------------------------------------
124   bool Window::isVisible() const
125   {
126     return _image.isVisible();
127   }
128
129   //----------------------------------------------------------------------------
130   bool Window::isResizable() const
131   {
132     return _resizable;
133   }
134
135   //----------------------------------------------------------------------------
136   bool Window::isCapturingEvents() const
137   {
138     return _capture_events;
139   }
140
141   //----------------------------------------------------------------------------
142   bool Window::handleMouseEvent(const simgear::canvas::MouseEventPtr& event)
143   {
144     return _image.handleMouseEvent(event);
145   }
146
147   //----------------------------------------------------------------------------
148   void Window::handleResize(uint8_t mode, const osg::Vec2f& delta)
149   {
150     if( mode == NONE )
151     {
152       _resize_status = 0;
153       return;
154     }
155     else if( mode & INIT )
156     {
157       _resize_top    = getRegion().t();
158       _resize_right  = getRegion().r();
159       _resize_bottom = getRegion().b();
160       _resize_left   = getRegion().l();
161       _resize_status = 1;
162     }
163
164     if( mode & BOTTOM )
165       _resize_bottom += delta.y();
166     else if( mode & TOP )
167       _resize_top += delta.y();
168
169     if( mode & canvas::Window::RIGHT )
170       _resize_right += delta.x();
171     else if( mode & canvas::Window::LEFT )
172       _resize_left += delta.x();
173   }
174
175   //----------------------------------------------------------------------------
176   void Window::doRaise(SGPropertyNode* node_raise)
177   {
178     if( node_raise && !node_raise->getBoolValue() )
179       return;
180
181     BOOST_FOREACH(osg::Group* parent, getGroup()->getParents())
182     {
183       // Remove window...
184       parent->removeChild(getGroup());
185
186       // ...and add again as topmost window
187       parent->addChild(getGroup());
188     }
189
190     if( node_raise )
191       node_raise->setBoolValue(false);
192   }
193
194 } // namespace canvas