1 // Window for placing a Canvas onto it (for dialogs, menus, etc.)
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
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.
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.
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.
19 #include "canvas_mgr.hxx"
21 #include <Main/globals.hxx>
22 #include <simgear/canvas/Canvas.hxx>
23 #include <simgear/scene/util/OsgMath.hxx>
25 #include <osgGA/GUIEventHandler>
27 #include <boost/algorithm/string/predicate.hpp>
28 #include <boost/foreach.hpp>
32 namespace sc = simgear::canvas;
34 //----------------------------------------------------------------------------
35 const std::string Window::TYPE_NAME = "window";
37 //----------------------------------------------------------------------------
38 Window::Window( const simgear::canvas::CanvasWeakPtr& canvas,
39 const SGPropertyNode_ptr& node,
40 const Style& parent_style,
42 Image(canvas, node, parent_style, parent),
45 _capture_events(true),
46 _resize_top(node, "resize-top"),
47 _resize_right(node, "resize-right"),
48 _resize_bottom(node, "resize-bottom"),
49 _resize_left(node, "resize-left"),
50 _resize_status(node, "resize-status")
52 node->setFloatValue("source/right", 1);
53 node->setFloatValue("source/bottom", 1);
54 node->setBoolValue("source/normalized", true);
57 //----------------------------------------------------------------------------
60 if( _canvas_decoration )
61 _canvas_decoration->destroy();
64 //----------------------------------------------------------------------------
65 void Window::update(double delta_time_sec)
67 if( _attributes_dirty & DECORATION )
70 _attributes_dirty &= ~DECORATION;
73 Image::update(delta_time_sec);
76 //----------------------------------------------------------------------------
77 void Window::valueChanged(SGPropertyNode * node)
80 if( node->getParent() == _node )
83 const std::string& name = node->getNameString();
84 if( name == "resize" )
85 _resizable = node->getBoolValue();
86 else if( name == "update" )
88 else if( name == "capture-events" )
89 _capture_events = node->getBoolValue();
90 else if( name == "decoration-border" )
91 parseDecorationBorder(node->getStringValue());
92 else if( boost::starts_with(name, "shadow-") )
93 _attributes_dirty |= DECORATION;
99 Image::valueChanged(node);
102 //----------------------------------------------------------------------------
103 osg::Group* Window::getGroup()
105 return getMatrixTransform();
108 //----------------------------------------------------------------------------
109 const SGVec2<float> Window::getPosition() const
111 const osg::Matrix& m = getMatrixTransform()->getMatrix();
112 return SGVec2<float>( m(3, 0), m(3, 1) );
115 //----------------------------------------------------------------------------
116 const SGRect<float> Window::getScreenRegion() const
118 return getPosition() + getRegion();
121 //----------------------------------------------------------------------------
122 void Window::setCanvasContent(sc::CanvasPtr canvas)
124 _canvas_content = canvas;
127 // Placement within decoration canvas
128 _image_content->setSrcCanvas(canvas);
130 setSrcCanvas(canvas);
133 //----------------------------------------------------------------------------
134 sc::CanvasWeakPtr Window::getCanvasContent() const
136 return _canvas_content;
139 //----------------------------------------------------------------------------
140 sc::CanvasPtr Window::getCanvasDecoration()
142 return _canvas_decoration;
145 //----------------------------------------------------------------------------
146 bool Window::isResizable() const
151 //----------------------------------------------------------------------------
152 bool Window::isCapturingEvents() const
154 return _capture_events;
157 //----------------------------------------------------------------------------
160 // on writing the z-index the window always is moved to the top of all other
161 // windows with the same z-index.
162 set<int>("z-index", get<int>("z-index", 0));
165 //----------------------------------------------------------------------------
166 void Window::handleResize(uint8_t mode, const osg::Vec2f& delta)
173 else if( mode & INIT )
175 _resize_top = getRegion().t();
176 _resize_right = getRegion().r();
177 _resize_bottom = getRegion().b();
178 _resize_left = getRegion().l();
183 _resize_bottom += delta.y();
184 else if( mode & TOP )
185 _resize_top += delta.y();
187 if( mode & canvas::Window::RIGHT )
188 _resize_right += delta.x();
189 else if( mode & canvas::Window::LEFT )
190 _resize_left += delta.x();
193 //----------------------------------------------------------------------------
194 void Window::parseDecorationBorder(const std::string& str)
196 _decoration_border = simgear::CSSBorder::parse(str);
197 _attributes_dirty |= DECORATION;
200 //----------------------------------------------------------------------------
201 void Window::updateDecoration()
203 int shadow_radius = get<float>("shadow-radius") + 0.5;
204 if( shadow_radius < 2 )
207 if( _decoration_border.isNone() && !shadow_radius )
209 sc::CanvasPtr canvas_content = _canvas_content.lock();
210 setSrcCanvas(canvas_content);
211 set<int>("size[0]", canvas_content->getViewWidth());
212 set<int>("size[1]", canvas_content->getViewHeight());
214 _image_content.reset();
215 _image_shadow.reset();
216 _canvas_decoration->destroy();
217 _canvas_decoration.reset();
221 sc::CanvasPtr content = _canvas_content.lock();
222 if( !_canvas_decoration )
225 dynamic_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
229 SG_LOG(SG_GENERAL, SG_WARN, "canvas::Window: no canvas manager!");
233 _canvas_decoration = mgr->createCanvas("window-decoration");
234 _canvas_decoration->getProps()
235 ->setStringValue("background", "rgba(0,0,0,0)");
236 setSrcCanvas(_canvas_decoration);
238 _image_content = _canvas_decoration->getRootGroup()
239 ->createChild<sc::Image>("content");
240 _image_content->setSrcCanvas(content);
242 // Draw content on top of decoration
243 _image_content->set<int>("z-index", 1);
246 sc::GroupPtr group_decoration =
247 _canvas_decoration->getOrCreateGroup("decoration");
248 group_decoration->set<int>("tf/t[0]", shadow_radius);
249 group_decoration->set<int>("tf/t[1]", shadow_radius);
250 // TODO do we need clipping or shall we trust the decorator not to draw over
253 simgear::CSSBorder::Offsets const border =
254 _decoration_border.getAbsOffsets(content->getViewport());
256 int shad2 = 2 * shadow_radius,
257 outer_width = border.l + content->getViewWidth() + border.r + shad2,
258 outer_height = border.t + content->getViewHeight() + border.b + shad2;
260 _canvas_decoration->setSizeX( outer_width );
261 _canvas_decoration->setSizeY( outer_height );
262 _canvas_decoration->setViewWidth( outer_width );
263 _canvas_decoration->setViewHeight( outer_height );
265 set<int>("size[0]", outer_width - shad2);
266 set<int>("size[1]", outer_height - shad2);
267 set<int>("outset", shadow_radius);
269 assert(_image_content);
270 _image_content->set<int>("x", shadow_radius + border.l);
271 _image_content->set<int>("y", shadow_radius + border.t);
277 _image_shadow->destroy();
278 _image_shadow.reset();
283 int shadow_inset = std::max<int>(get<float>("shadow-inset") + 0.5, 0),
284 slice_width = shadow_radius + shadow_inset;
286 _image_shadow = _canvas_decoration->getRootGroup()
287 ->getOrCreateChild<sc::Image>("shadow");
288 _image_shadow->set<std::string>("file", "gui/images/shadow.png");
289 _image_shadow->set<float>("slice", 7);
290 _image_shadow->set<std::string>("fill", "#000000");
291 _image_shadow->set<float>("slice-width", slice_width);
292 _image_shadow->set<int>("size[0]", outer_width);
293 _image_shadow->set<int>("size[1]", outer_height);
295 // Draw shadow below decoration
296 _image_shadow->set<int>("z-index", -1);
299 } // namespace canvas