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.
20 #include <simgear/canvas/Canvas.hxx>
22 #include <osgGA/GUIEventHandler>
24 #include <boost/foreach.hpp>
28 //----------------------------------------------------------------------------
29 Window::Window(SGPropertyNode* node):
30 PropertyBasedElement(node),
31 _image( simgear::canvas::CanvasPtr(),
33 simgear::canvas::Style() ),
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")
42 _image.removeListener();
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);
50 node->setFloatValue("source/right", 1);
51 node->setFloatValue("source/bottom", 1);
52 node->setBoolValue("source/normalized", true);
55 //----------------------------------------------------------------------------
61 //----------------------------------------------------------------------------
62 void Window::update(double delta_time_sec)
64 _image.update(delta_time_sec);
67 //----------------------------------------------------------------------------
68 void Window::valueChanged(SGPropertyNode * node)
71 if( node->getParent() == _node )
74 if( node->getNameString() == "raise-top" )
76 else if( node->getNameString() == "resize" )
77 _resizable = node->getBoolValue();
78 else if( node->getNameString() == "capture-events" )
79 _capture_events = node->getBoolValue();
85 _image.valueChanged(node);
88 //----------------------------------------------------------------------------
89 osg::Group* Window::getGroup()
91 return _image.getMatrixTransform();
94 //----------------------------------------------------------------------------
95 const SGRect<float>& Window::getRegion() const
97 return _image.getRegion();
100 //----------------------------------------------------------------------------
101 void Window::setCanvas(simgear::canvas::CanvasPtr canvas)
103 _image.setSrcCanvas(canvas);
106 //----------------------------------------------------------------------------
107 simgear::canvas::CanvasWeakPtr Window::getCanvas() const
109 return _image.getSrcCanvas();
112 //----------------------------------------------------------------------------
113 bool Window::isVisible() const
115 return _image.isVisible();
118 //----------------------------------------------------------------------------
119 bool Window::isResizable() const
124 //----------------------------------------------------------------------------
125 bool Window::isCapturingEvents() const
127 return _capture_events;
130 //----------------------------------------------------------------------------
131 bool Window::handleMouseEvent(const simgear::canvas::MouseEventPtr& event)
133 return _image.handleMouseEvent(event);
136 //----------------------------------------------------------------------------
137 void Window::handleResize(uint8_t mode, const osg::Vec2f& delta)
144 else if( mode & INIT )
146 _resize_top = getRegion().t();
147 _resize_right = getRegion().r();
148 _resize_bottom = getRegion().b();
149 _resize_left = getRegion().l();
154 _resize_bottom += delta.y();
155 else if( mode & TOP )
156 _resize_top += delta.y();
158 if( mode & canvas::Window::RIGHT )
159 _resize_right += delta.x();
160 else if( mode & canvas::Window::LEFT )
161 _resize_left += delta.x();
164 //----------------------------------------------------------------------------
165 void Window::doRaise(SGPropertyNode* node_raise)
167 if( node_raise && !node_raise->getBoolValue() )
170 BOOST_FOREACH(osg::Group* parent, getGroup()->getParents())
173 parent->removeChild(getGroup());
175 // ...and add again as topmost window
176 parent->addChild(getGroup());
180 node_raise->setBoolValue(false);
183 } // namespace canvas