]> git.mxchange.org Git - flightgear.git/blob - src/GUI/CanvasWidget.cxx
Typos, license headers...
[flightgear.git] / src / GUI / CanvasWidget.cxx
1 // Airports forward declarations
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 #ifdef HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
23 #include "CanvasWidget.hxx"
24
25 #include <Canvas/canvas_mgr.hxx>
26 #include <Main/fg_os.hxx>      // fgGetKeyModifiers()
27 #include <Scripting/NasalSys.hxx>
28
29 #include <simgear/canvas/Canvas.hxx>
30 #include <simgear/canvas/MouseEvent.hxx>
31
32 SGPropertyNode_ptr CanvasWidget::_time,
33                    CanvasWidget::_view_height;
34
35 //------------------------------------------------------------------------------
36 CanvasWidget::CanvasWidget( int x, int y,
37                             int width, int height,
38                             SGPropertyNode* props,
39                             const std::string& module ):
40   puObject(x, y, width, height),
41   _canvas_mgr( dynamic_cast<CanvasMgr*>(globals->get_subsystem("Canvas")) ),
42   _last_x(0),
43   _last_y(0),
44   // automatically resize viewport of canvas if no size is given
45   _auto_viewport( !props->hasChild("view") )
46 {
47   if( !_canvas_mgr )
48   {
49     SG_LOG(SG_GENERAL, SG_ALERT, "CanvasWidget: failed to get canvas manager!");
50     return;
51   }
52
53   _canvas = _canvas_mgr->createCanvas
54   (
55     props->getStringValue("name", "gui-anonymous")
56   );
57
58   int view[2] = {
59     // Get canvas viewport size. If not specified use the widget dimensions
60     props->getIntValue("view[0]", width),
61     props->getIntValue("view[1]", height)
62   };
63
64   SGPropertyNode* cprops = _canvas->getProps();
65   cprops->setIntValue("size[0]", view[0] * 2); // use higher resolution
66   cprops->setIntValue("size[1]", view[1] * 2); // for antialias
67   cprops->setIntValue("view[0]", view[0]);
68   cprops->setIntValue("view[1]", view[1]);
69   cprops->setBoolValue("render-always", true);
70   cprops->setStringValue( "name",
71                            props->getStringValue("name", "gui-anonymous") );
72
73   SGPropertyNode *nasal = props->getNode("nasal");
74   if( !nasal )
75     return;
76
77   FGNasalSys *nas = dynamic_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
78   if( !nas )
79     SG_LOG( SG_GENERAL,
80             SG_ALERT,
81             "CanvasWidget: Failed to get nasal subsystem!" );
82
83   const std::string file = std::string("__canvas:")
84                          + cprops->getStringValue("name");
85
86   SGPropertyNode *load = nasal->getNode("load");
87   if( load )
88   {
89     const char *s = load->getStringValue();
90     nas->handleCommand(module.c_str(), file.c_str(), s, cprops);
91   }
92 }
93
94 //------------------------------------------------------------------------------
95 CanvasWidget::~CanvasWidget()
96 {
97   if( _canvas )
98     // TODO check if really not in use anymore
99     _canvas->getProps()
100            ->getParent()
101            ->removeChild( _canvas->getProps()->getName(),
102                           _canvas->getProps()->getIndex(),
103                           false );
104 }
105
106 // Old versions of PUI are missing this defines...
107 #ifndef PU_SCROLL_UP_BUTTON
108 # define PU_SCROLL_UP_BUTTON     3
109 #endif
110 #ifndef PU_SCROLL_DOWN_BUTTON
111 # define PU_SCROLL_DOWN_BUTTON   4
112 #endif
113
114 //------------------------------------------------------------------------------
115 void CanvasWidget::doHit(int button, int updown, int x, int y)
116 {
117   puObject::doHit(button, updown, x, y);
118
119   // CTRL allows resizing and SHIFT allows moving the window
120   if( fgGetKeyModifiers() & (KEYMOD_CTRL | KEYMOD_SHIFT) )
121     return;
122
123   namespace sc = simgear::canvas;
124   sc::MouseEventPtr event(new sc::MouseEvent);
125
126   if( !_time )
127     _time = globals->get_props()->getNode("/sim/time/elapsed-sec");
128   event->time = _time->getDoubleValue();
129
130   if( !_view_height )
131     _view_height = globals->get_props()->getNode("/sim/gui/canvas/size[1]");
132   event->screen_pos.set(x, _view_height->getIntValue() - y);
133
134   event->client_pos.set(x - abox.min[0], abox.max[1] - y);
135   event->delta.set( event->getScreenX() - _last_x,
136                     event->getScreenY() - _last_y );
137
138   _last_x = event->getScreenX();
139   _last_y = event->getScreenY();
140
141   switch( button )
142   {
143     case PU_LEFT_BUTTON:
144       event->button = osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON;
145       break;
146     case PU_MIDDLE_BUTTON:
147       event->button = osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON;
148       break;
149     case PU_RIGHT_BUTTON:
150       event->button = osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON;
151       break;
152     case PU_SCROLL_UP_BUTTON:
153     case PU_SCROLL_DOWN_BUTTON:
154       // Only let PU_DOWN trigger a scroll wheel event
155       if( updown != PU_DOWN )
156         return;
157
158       event->type = sc::Event::WHEEL;
159       event->delta.y() = button == PU_SCROLL_UP_BUTTON ? 1 : -1;
160
161       _canvas->handleMouseEvent(event);
162
163       return;
164     default:
165       SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown button: " << button);
166       return;
167   }
168
169   switch( updown )
170   {
171     case PU_DOWN:
172       event->type = sc::Event::MOUSE_DOWN;
173       puSetActiveWidget(this, x, y);
174       break;
175     case PU_UP:
176       event->type = sc::Event::MOUSE_UP;
177       puDeactivateWidget();
178       break;
179     case PU_DRAG:
180       event->type = sc::Event::DRAG;
181       break;
182     default:
183       SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown updown: " << updown);
184       return;
185   }
186
187   _canvas->handleMouseEvent(event);
188 }
189
190 //------------------------------------------------------------------------------
191 int CanvasWidget::checkKey(int key, int updown)
192 {
193   return puObject::checkKey(key, updown);
194 }
195
196 //------------------------------------------------------------------------------
197 void CanvasWidget::setSize(int w, int h)
198 {
199   puObject::setSize(w, h);
200
201   if( _auto_viewport )
202   {
203     _canvas->getProps()->setIntValue("view[0]", w);
204     _canvas->getProps()->setIntValue("view[1]", h);
205   }
206 }
207
208 //------------------------------------------------------------------------------
209 void CanvasWidget::draw(int dx, int dy)
210 {
211   glEnable(GL_TEXTURE_2D);
212   glBindTexture(GL_TEXTURE_2D, _canvas_mgr->getCanvasTexId(_canvas));
213   glBegin( GL_QUADS );
214     glColor3f(1,1,1);
215     glTexCoord2f(0,0); glVertex2f(dx + abox.min[0], dy + abox.min[1]);
216     glTexCoord2f(1,0); glVertex2f(dx + abox.max[0], dy + abox.min[1]);
217     glTexCoord2f(1,1); glVertex2f(dx + abox.max[0], dy + abox.max[1]);
218     glTexCoord2f(0,1); glVertex2f(dx + abox.min[0], dy + abox.max[1]);
219   glEnd();
220   glDisable(GL_TEXTURE_2D);
221 }