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