1 // Window for placing a Canvas onto it (for dialogs, menus, etc.)
3 // Copyright (C) 2012 Thomas Geymayer <tomgey@gmail.com>
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Library General Public
7 // License as published by the Free Software Foundation; either
8 // version 2 of the License, or (at your option) any later version.
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Library General Public License for more details.
15 // You should have received a copy of the GNU Library General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include "CanvasMgr.hxx"
20 #include "CanvasSystemAdapter.hxx"
21 #include "CanvasWindow.hxx"
23 #include <simgear/canvas/Canvas.hxx>
24 #include <simgear/scene/util/OsgMath.hxx>
26 #include <osgGA/GUIEventHandler>
28 #include <boost/algorithm/string/predicate.hpp>
29 #include <boost/foreach.hpp>
36 //----------------------------------------------------------------------------
37 const std::string Window::TYPE_NAME = "window";
39 //----------------------------------------------------------------------------
40 Window::Window( const CanvasWeakPtr& canvas,
41 const SGPropertyNode_ptr& node,
42 const Style& parent_style,
44 Image(canvas, node, parent_style, parent),
47 _capture_events(true),
48 _resize_top(node, "resize-top"),
49 _resize_right(node, "resize-right"),
50 _resize_bottom(node, "resize-bottom"),
51 _resize_left(node, "resize-left"),
52 _resize_status(node, "resize-status")
54 node->setFloatValue("source/right", 1);
55 node->setFloatValue("source/bottom", 1);
56 node->setBoolValue("source/normalized", true);
59 //----------------------------------------------------------------------------
62 if( _canvas_decoration )
63 _canvas_decoration->destroy();
66 //----------------------------------------------------------------------------
67 void Window::update(double delta_time_sec)
69 if( _attributes_dirty & DECORATION )
72 _attributes_dirty &= ~DECORATION;
75 Image::update(delta_time_sec);
78 //----------------------------------------------------------------------------
79 void Window::valueChanged(SGPropertyNode * node)
82 if( node->getParent() == _node )
85 const std::string& name = node->getNameString();
86 if( name == "resize" )
87 _resizable = node->getBoolValue();
88 else if( name == "update" )
90 else if( name == "capture-events" )
91 _capture_events = node->getBoolValue();
92 else if( name == "decoration-border" )
93 parseDecorationBorder(node->getStringValue());
94 else if( boost::starts_with(name, "shadow-")
95 || name == "content-size" )
96 _attributes_dirty |= DECORATION;
102 Image::valueChanged(node);
105 //----------------------------------------------------------------------------
106 osg::Group* Window::getGroup()
108 return getMatrixTransform();
111 //----------------------------------------------------------------------------
112 const SGVec2<float> Window::getPosition() const
114 const osg::Matrix& m = getMatrixTransform()->getMatrix();
115 return SGVec2<float>( m(3, 0), m(3, 1) );
118 //----------------------------------------------------------------------------
119 const SGRect<float> Window::getScreenRegion() const
121 return getPosition() + getRegion();
124 //----------------------------------------------------------------------------
125 void Window::setCanvasContent(CanvasPtr canvas)
127 _canvas_content = canvas;
130 // Placement within decoration canvas
131 _image_content->setSrcCanvas(canvas);
133 setSrcCanvas(canvas);
136 //----------------------------------------------------------------------------
137 CanvasWeakPtr Window::getCanvasContent() const
139 return _canvas_content;
142 //----------------------------------------------------------------------------
143 CanvasPtr Window::getCanvasDecoration() const
145 return _canvas_decoration;
148 //----------------------------------------------------------------------------
149 bool Window::isResizable() const
154 //----------------------------------------------------------------------------
155 bool Window::isCapturingEvents() const
157 return _capture_events;
160 //----------------------------------------------------------------------------
163 // on writing the z-index the window always is moved to the top of all other
164 // windows with the same z-index.
165 set<int>("z-index", get<int>("z-index", 0));
168 //----------------------------------------------------------------------------
169 void Window::handleResize( uint8_t mode,
170 const osg::Vec2f& offset )
177 else if( mode & INIT )
179 _resize_top = getRegion().t();
180 _resize_right = getRegion().r();
181 _resize_bottom = getRegion().b();
182 _resize_left = getRegion().l();
187 _resize_bottom = getRegion().b() + offset.y();
188 else if( mode & TOP )
189 _resize_top = getRegion().t() + offset.y();
191 if( mode & canvas::Window::RIGHT )
192 _resize_right = getRegion().r() + offset.x();
193 else if( mode & canvas::Window::LEFT )
194 _resize_left = getRegion().l() + offset.x();
197 //----------------------------------------------------------------------------
198 void Window::parseDecorationBorder(const std::string& str)
200 _decoration_border = simgear::CSSBorder::parse(str);
201 _attributes_dirty |= DECORATION;
204 //----------------------------------------------------------------------------
205 void Window::updateDecoration()
207 int shadow_radius = get<float>("shadow-radius") + 0.5;
208 if( shadow_radius < 2 )
211 CanvasPtr content = _canvas_content.lock();
212 SGRect<int> content_view
216 get<int>("content-size[0]", content ? content->getViewWidth() : 400),
217 get<int>("content-size[1]", content ? content->getViewHeight() : 300)
220 if( _decoration_border.isNone() && !shadow_radius )
222 setSrcCanvas(content);
223 set<int>("size[0]", content_view.width());
224 set<int>("size[1]", content_view.height());
226 _image_content.reset();
227 _image_shadow.reset();
228 if( _canvas_decoration )
229 _canvas_decoration->destroy();
230 _canvas_decoration.reset();
234 if( !_canvas_decoration )
236 CanvasMgr* mgr = dynamic_cast<CanvasMgr*>(
237 Canvas::getSystemAdapter()->getSubsystem("Canvas")
242 SG_LOG(SG_GENERAL, SG_WARN, "canvas::Window: no canvas manager!");
246 _canvas_decoration = mgr->createCanvas("window-decoration");
247 _canvas_decoration->getProps()
248 ->setStringValue("background", "rgba(0,0,0,0)");
249 setSrcCanvas(_canvas_decoration);
251 _image_content = _canvas_decoration->getRootGroup()
252 ->createChild<Image>("content");
253 _image_content->setSrcCanvas(content);
255 // Draw content on top of decoration
256 _image_content->set<int>("z-index", 1);
259 GroupPtr group_decoration =
260 _canvas_decoration->getOrCreateGroup("decoration");
261 group_decoration->set<int>("tf/t[0]", shadow_radius);
262 group_decoration->set<int>("tf/t[1]", shadow_radius);
263 // TODO do we need clipping or shall we trust the decorator not to draw over
266 CSSBorder::Offsets const border =
267 _decoration_border.getAbsOffsets(content_view);
269 int shad2 = 2 * shadow_radius,
270 outer_width = border.l + content_view.width() + border.r + shad2,
271 outer_height = border.t + content_view.height() + border.b + shad2;
273 _canvas_decoration->setSizeX( outer_width );
274 _canvas_decoration->setSizeY( outer_height );
275 _canvas_decoration->setViewWidth( outer_width );
276 _canvas_decoration->setViewHeight( outer_height );
278 set<int>("size[0]", outer_width - shad2);
279 set<int>("size[1]", outer_height - shad2);
280 set<int>("outset", shadow_radius);
282 assert(_image_content);
283 _image_content->set<int>("x", shadow_radius + border.l);
284 _image_content->set<int>("y", shadow_radius + border.t);
285 _image_content->set<int>("size[0]", content_view.width());
286 _image_content->set<int>("size[1]", content_view.height());
292 _image_shadow->destroy();
293 _image_shadow.reset();
298 int shadow_inset = std::max<int>(get<float>("shadow-inset") + 0.5, 0),
299 slice_width = shadow_radius + shadow_inset;
301 _image_shadow = _canvas_decoration->getRootGroup()
302 ->getOrCreateChild<Image>("shadow");
303 _image_shadow->set<std::string>("src", "gui/images/shadow.png");
304 _image_shadow->set<float>("slice", 7);
305 _image_shadow->set<std::string>("fill", "#000000");
306 _image_shadow->set<float>("slice-width", slice_width);
307 _image_shadow->set<int>("size[0]", outer_width);
308 _image_shadow->set<int>("size[1]", outer_height);
310 // Draw shadow below decoration
311 _image_shadow->set<int>("z-index", -1);
314 } // namespace canvas
315 } // namespace simgear