]> git.mxchange.org Git - simgear.git/blob - simgear/canvas/CanvasWindow.cxx
canvas::Text: add heightForWidth method.
[simgear.git] / simgear / canvas / CanvasWindow.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 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.
9 //
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.
14 //
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
18
19 #include "CanvasMgr.hxx"
20 #include "CanvasSystemAdapter.hxx"
21 #include "CanvasWindow.hxx"
22
23 #include <simgear/canvas/Canvas.hxx>
24 #include <simgear/scene/util/OsgMath.hxx>
25
26 #include <osgGA/GUIEventHandler>
27
28 #include <boost/algorithm/string/predicate.hpp>
29 #include <boost/foreach.hpp>
30
31 namespace simgear
32 {
33 namespace canvas
34 {
35
36   //----------------------------------------------------------------------------
37   const std::string Window::TYPE_NAME = "window";
38
39   //----------------------------------------------------------------------------
40   Window::Window( const CanvasWeakPtr& canvas,
41                   const SGPropertyNode_ptr& node,
42                   const Style& parent_style,
43                   Element* parent ):
44     Image(canvas, node, parent_style, parent),
45     _attributes_dirty(0),
46     _resizable(false),
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")
53   {
54     node->setFloatValue("source/right", 1);
55     node->setFloatValue("source/bottom", 1);
56     node->setBoolValue("source/normalized", true);
57   }
58
59   //----------------------------------------------------------------------------
60   Window::~Window()
61   {
62     if( _canvas_decoration )
63       _canvas_decoration->destroy();
64   }
65
66   //----------------------------------------------------------------------------
67   void Window::update(double delta_time_sec)
68   {
69     if( _attributes_dirty & DECORATION )
70     {
71       updateDecoration();
72       _attributes_dirty &= ~DECORATION;
73     }
74
75     Image::update(delta_time_sec);
76   }
77
78   //----------------------------------------------------------------------------
79   void Window::valueChanged(SGPropertyNode * node)
80   {
81     bool handled = false;
82     if( node->getParent() == _node )
83     {
84       handled = true;
85       const std::string& name = node->getNameString();
86       if( name  == "resize" )
87         _resizable = node->getBoolValue();
88       else if( name == "update" )
89         update(0);
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;
97       else
98         handled = false;
99     }
100
101     if( !handled )
102       Image::valueChanged(node);
103   }
104
105   //----------------------------------------------------------------------------
106   osg::Group* Window::getGroup()
107   {
108     return getMatrixTransform();
109   }
110
111   //----------------------------------------------------------------------------
112   const SGVec2<float> Window::getPosition() const
113   {
114     const osg::Matrix& m = getMatrixTransform()->getMatrix();
115     return SGVec2<float>( m(3, 0), m(3, 1) );
116   }
117
118   //----------------------------------------------------------------------------
119   const SGRect<float> Window::getScreenRegion() const
120   {
121     return getPosition() + getRegion();
122   }
123
124   //----------------------------------------------------------------------------
125   void Window::setCanvasContent(CanvasPtr canvas)
126   {
127     _canvas_content = canvas;
128     if( _layout )
129     {
130       canvas->setLayout(_layout);
131       _layout.clear();
132     }
133
134     if( _image_content )
135       // Placement within decoration canvas
136       _image_content->setSrcCanvas(canvas);
137     else
138       setSrcCanvas(canvas);
139   }
140
141   //----------------------------------------------------------------------------
142   CanvasWeakPtr Window::getCanvasContent() const
143   {
144     return _canvas_content;
145   }
146
147   //----------------------------------------------------------------------------
148   void Window::setLayout(const LayoutRef& layout)
149   {
150     CanvasPtr canvas = _canvas_content.lock();
151     if( canvas )
152       canvas->setLayout(layout);
153     else
154       _layout = layout; // keep layout until content canvas is set
155   }
156
157   //----------------------------------------------------------------------------
158   CanvasPtr Window::getCanvasDecoration() const
159   {
160     return _canvas_decoration;
161   }
162
163   //----------------------------------------------------------------------------
164   bool Window::isResizable() const
165   {
166     return _resizable;
167   }
168
169   //----------------------------------------------------------------------------
170   bool Window::isCapturingEvents() const
171   {
172     return _capture_events;
173   }
174
175   //----------------------------------------------------------------------------
176   void Window::raise()
177   {
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));
181   }
182
183   //----------------------------------------------------------------------------
184   void Window::handleResize( uint8_t mode,
185                              const osg::Vec2f& offset )
186   {
187     if( mode == NONE )
188     {
189       _resize_status = 0;
190       return;
191     }
192     else if( mode & INIT )
193     {
194       _resize_top    = getRegion().t();
195       _resize_right  = getRegion().r();
196       _resize_bottom = getRegion().b();
197       _resize_left   = getRegion().l();
198       _resize_status = 1;
199     }
200
201     if( mode & BOTTOM )
202       _resize_bottom = getRegion().b() + offset.y();
203     else if( mode & TOP )
204       _resize_top = getRegion().t() + offset.y();
205
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();
210   }
211
212   //----------------------------------------------------------------------------
213   void Window::parseDecorationBorder(const std::string& str)
214   {
215     _decoration_border = simgear::CSSBorder::parse(str);
216     _attributes_dirty |= DECORATION;
217   }
218
219   //----------------------------------------------------------------------------
220   void Window::updateDecoration()
221   {
222     int shadow_radius = get<float>("shadow-radius") + 0.5;
223     if( shadow_radius < 2 )
224       shadow_radius = 0;
225
226     CanvasPtr content = _canvas_content.lock();
227     SGRect<int> content_view
228     (
229       0,
230       0,
231       get<int>("content-size[0]", content ? content->getViewWidth()  : 400),
232       get<int>("content-size[1]", content ? content->getViewHeight() : 300)
233     );
234
235     if( _decoration_border.isNone() && !shadow_radius )
236     {
237       setSrcCanvas(content);
238       set<int>("size[0]", content_view.width());
239       set<int>("size[1]", content_view.height());
240
241       _image_content.reset();
242       _image_shadow.reset();
243       if( _canvas_decoration )
244         _canvas_decoration->destroy();
245       _canvas_decoration.reset();
246       return;
247     }
248
249     if( !_canvas_decoration )
250     {
251       CanvasMgr* mgr = dynamic_cast<CanvasMgr*>(
252         Canvas::getSystemAdapter()->getSubsystem("Canvas")
253       );
254
255       if( !mgr )
256       {
257         SG_LOG(SG_GENERAL, SG_WARN, "canvas::Window: no canvas manager!");
258         return;
259       }
260
261       _canvas_decoration = mgr->createCanvas("window-decoration");
262       _canvas_decoration->getProps()
263                         ->setStringValue("background", "rgba(0,0,0,0)");
264       setSrcCanvas(_canvas_decoration);
265
266       _image_content = _canvas_decoration->getRootGroup()
267                                          ->createChild<Image>("content");
268       _image_content->setSrcCanvas(content);
269
270       // Draw content on top of decoration
271       _image_content->set<int>("z-index", 1);
272     }
273
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
279     //      the shadow?
280
281     CSSBorder::Offsets const border =
282       _decoration_border.getAbsOffsets(content_view);
283
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;
287
288     _canvas_decoration->setSizeX( outer_width );
289     _canvas_decoration->setSizeY( outer_height );
290     _canvas_decoration->setViewWidth( outer_width );
291     _canvas_decoration->setViewHeight( outer_height );
292
293     set<int>("size[0]", outer_width - shad2);
294     set<int>("size[1]", outer_height - shad2);
295     set<int>("outset", shadow_radius);
296
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());
302
303     if( !shadow_radius )
304     {
305       if( _image_shadow )
306       {
307         _image_shadow->destroy();
308         _image_shadow.reset();
309       }
310       return;
311     }
312
313     int shadow_inset = std::max<int>(get<float>("shadow-inset") + 0.5, 0),
314         slice_width  = shadow_radius + shadow_inset;
315
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);
324
325     // Draw shadow below decoration
326     _image_shadow->set<int>("z-index", -1);
327   }
328
329 } // namespace canvas
330 } // namespace simgear