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 canvas->setLayout(_layout);
135 // Placement within decoration canvas
136 _image_content->setSrcCanvas(canvas);
138 setSrcCanvas(canvas);
141 //----------------------------------------------------------------------------
142 CanvasWeakPtr Window::getCanvasContent() const
144 return _canvas_content;
147 //----------------------------------------------------------------------------
148 void Window::setLayout(const LayoutRef& layout)
150 CanvasPtr canvas = _canvas_content.lock();
152 canvas->setLayout(layout);
154 _layout = layout; // keep layout until content canvas is set
157 //----------------------------------------------------------------------------
158 CanvasPtr Window::getCanvasDecoration() const
160 return _canvas_decoration;
163 //----------------------------------------------------------------------------
164 bool Window::isResizable() const
169 //----------------------------------------------------------------------------
170 bool Window::isCapturingEvents() const
172 return _capture_events;
175 //----------------------------------------------------------------------------
178 // on writing the z-index the window always is moved to the top of all other
179 // windows with the same z-index.
180 set<int>("z-index", get<int>("z-index", 0));
183 //----------------------------------------------------------------------------
184 void Window::handleResize( uint8_t mode,
185 const osg::Vec2f& offset )
192 else if( mode & INIT )
194 _resize_top = getRegion().t();
195 _resize_right = getRegion().r();
196 _resize_bottom = getRegion().b();
197 _resize_left = getRegion().l();
202 _resize_bottom = getRegion().b() + offset.y();
203 else if( mode & TOP )
204 _resize_top = getRegion().t() + offset.y();
206 if( mode & canvas::Window::RIGHT )
207 _resize_right = getRegion().r() + offset.x();
208 else if( mode & canvas::Window::LEFT )
209 _resize_left = getRegion().l() + offset.x();
212 //----------------------------------------------------------------------------
213 void Window::parseDecorationBorder(const std::string& str)
215 _decoration_border = simgear::CSSBorder::parse(str);
216 _attributes_dirty |= DECORATION;
219 //----------------------------------------------------------------------------
220 void Window::updateDecoration()
222 int shadow_radius = get<float>("shadow-radius") + 0.5;
223 if( shadow_radius < 2 )
226 CanvasPtr content = _canvas_content.lock();
227 SGRect<int> content_view
231 get<int>("content-size[0]", content ? content->getViewWidth() : 400),
232 get<int>("content-size[1]", content ? content->getViewHeight() : 300)
235 if( _decoration_border.isNone() && !shadow_radius )
237 setSrcCanvas(content);
238 set<int>("size[0]", content_view.width());
239 set<int>("size[1]", content_view.height());
241 _image_content.reset();
242 _image_shadow.reset();
243 if( _canvas_decoration )
244 _canvas_decoration->destroy();
245 _canvas_decoration.reset();
249 if( !_canvas_decoration )
251 CanvasMgr* mgr = dynamic_cast<CanvasMgr*>(
252 Canvas::getSystemAdapter()->getSubsystem("Canvas")
257 SG_LOG(SG_GENERAL, SG_WARN, "canvas::Window: no canvas manager!");
261 _canvas_decoration = mgr->createCanvas("window-decoration");
262 _canvas_decoration->getProps()
263 ->setStringValue("background", "rgba(0,0,0,0)");
264 setSrcCanvas(_canvas_decoration);
266 _image_content = _canvas_decoration->getRootGroup()
267 ->createChild<Image>("content");
268 _image_content->setSrcCanvas(content);
270 // Draw content on top of decoration
271 _image_content->set<int>("z-index", 1);
274 GroupPtr group_decoration =
275 _canvas_decoration->getOrCreateGroup("decoration");
276 group_decoration->set<int>("tf/t[0]", shadow_radius);
277 group_decoration->set<int>("tf/t[1]", shadow_radius);
278 // TODO do we need clipping or shall we trust the decorator not to draw over
281 CSSBorder::Offsets const border =
282 _decoration_border.getAbsOffsets(content_view);
284 int shad2 = 2 * shadow_radius,
285 outer_width = border.l + content_view.width() + border.r + shad2,
286 outer_height = border.t + content_view.height() + border.b + shad2;
288 _canvas_decoration->setSizeX( outer_width );
289 _canvas_decoration->setSizeY( outer_height );
290 _canvas_decoration->setViewWidth( outer_width );
291 _canvas_decoration->setViewHeight( outer_height );
293 set<int>("size[0]", outer_width - shad2);
294 set<int>("size[1]", outer_height - shad2);
295 set<int>("outset", shadow_radius);
297 assert(_image_content);
298 _image_content->set<int>("x", shadow_radius + border.l);
299 _image_content->set<int>("y", shadow_radius + border.t);
300 _image_content->set<int>("size[0]", content_view.width());
301 _image_content->set<int>("size[1]", content_view.height());
307 _image_shadow->destroy();
308 _image_shadow.reset();
313 int shadow_inset = std::max<int>(get<float>("shadow-inset") + 0.5, 0),
314 slice_width = shadow_radius + shadow_inset;
316 _image_shadow = _canvas_decoration->getRootGroup()
317 ->getOrCreateChild<Image>("shadow");
318 _image_shadow->set<std::string>("src", "gui/images/shadow.png");
319 _image_shadow->set<float>("slice", 7);
320 _image_shadow->set<std::string>("fill", "#000000");
321 _image_shadow->set<float>("slice-width", slice_width);
322 _image_shadow->set<int>("size[0]", outer_width);
323 _image_shadow->set<int>("size[1]", outer_height);
325 // Draw shadow below decoration
326 _image_shadow->set<int>("z-index", -1);
329 } // namespace canvas
330 } // namespace simgear