]> git.mxchange.org Git - flightgear.git/blob - src/Canvas/window.cxx
Allow Canvas Windows to be resized by dragging
[flightgear.git] / src / Canvas / window.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 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.
9 //
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.
14 //
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.
18
19 #include "window.hxx"
20 #include <simgear/canvas/Canvas.hxx>
21
22 #include <osgGA/GUIEventHandler>
23
24 #include <boost/foreach.hpp>
25
26 namespace canvas
27 {
28   //----------------------------------------------------------------------------
29   Window::Window(SGPropertyNode* node):
30     PropertyBasedElement(node),
31     _image( simgear::canvas::CanvasPtr(),
32             node,
33             simgear::canvas::Style() ),
34     _resizable(false),
35     _resize_top(node, "resize-top"),
36     _resize_right(node, "resize-right"),
37     _resize_bottom(node, "resize-bottom"),
38     _resize_left(node, "resize-left"),
39     _resize_status(node, "resize-status")
40   {
41     _image.removeListener();
42
43     // TODO probably better remove default position and size
44     node->setFloatValue("x", 50);
45     node->setFloatValue("y", 100);
46     node->setFloatValue("size[0]", 400);
47     node->setFloatValue("size[1]", 300);
48
49     node->setFloatValue("source/right", 1);
50     node->setFloatValue("source/bottom", 1);
51     node->setBoolValue("source/normalized", true);
52   }
53
54   //----------------------------------------------------------------------------
55   Window::~Window()
56   {
57
58   }
59
60   //----------------------------------------------------------------------------
61   void Window::update(double delta_time_sec)
62   {
63     _image.update(delta_time_sec);
64   }
65
66   //----------------------------------------------------------------------------
67   void Window::valueChanged(SGPropertyNode * node)
68   {
69     bool handled = false;
70     if( node->getParent() == _node )
71     {
72       handled = true;
73       if( node->getNameString() == "raise-top" )
74         doRaise(node);
75       else if( node->getNameString()  == "resize" )
76         _resizable = node->getBoolValue();
77       else
78         handled = false;
79     }
80
81     if( !handled )
82       _image.valueChanged(node);
83   }
84
85   //----------------------------------------------------------------------------
86   osg::Group* Window::getGroup()
87   {
88     return _image.getMatrixTransform();
89   }
90
91   //----------------------------------------------------------------------------
92   const simgear::Rect<float>& Window::getRegion() const
93   {
94     return _image.getRegion();
95   }
96
97   //----------------------------------------------------------------------------
98   void Window::setCanvas(simgear::canvas::CanvasPtr canvas)
99   {
100     _image.setSrcCanvas(canvas);
101   }
102
103   //----------------------------------------------------------------------------
104   simgear::canvas::CanvasWeakPtr Window::getCanvas() const
105   {
106     return _image.getSrcCanvas();
107   }
108
109   //----------------------------------------------------------------------------
110   bool Window::isResizable() const
111   {
112     return _resizable;
113   }
114
115   //----------------------------------------------------------------------------
116   bool Window::handleMouseEvent(const simgear::canvas::MouseEventPtr& event)
117   {
118     if( !getCanvas().expired() )
119       return getCanvas().lock()->handleMouseEvent(event);
120     else
121       return false;
122   }
123
124   //----------------------------------------------------------------------------
125   void Window::handleResize(uint8_t mode, const osg::Vec2f& delta)
126   {
127     if( mode == NONE )
128     {
129       _resize_status = 0;
130       return;
131     }
132     else if( mode & INIT )
133     {
134       _resize_top    = getRegion().t();
135       _resize_right  = getRegion().r();
136       _resize_bottom = getRegion().b();
137       _resize_left   = getRegion().l();
138       _resize_status = 1;
139     }
140
141     if( mode & BOTTOM )
142       _resize_bottom += delta.y();
143     else if( mode & TOP )
144       _resize_top += delta.y();
145
146     if( mode & canvas::Window::RIGHT )
147       _resize_right += delta.x();
148     else if( mode & canvas::Window::LEFT )
149       _resize_left += delta.x();
150   }
151
152   //----------------------------------------------------------------------------
153   void Window::doRaise(SGPropertyNode* node_raise)
154   {
155     if( node_raise && !node_raise->getBoolValue() )
156       return;
157
158     BOOST_FOREACH(osg::Group* parent, getGroup()->getParents())
159     {
160       // Remove window...
161       parent->removeChild(getGroup());
162
163       // ...and add again as topmost window
164       parent->addChild(getGroup());
165     }
166
167     if( node_raise )
168       node_raise->setBoolValue(false);
169   }
170
171 } // namespace canvas