Debian Codegraph @

src/GUI/CanvasWidget.cxx

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