]> git.mxchange.org Git - flightgear.git/blob - src/GUI/CanvasWidget.cxx
Fix for old PUI versions
[flightgear.git] / src / GUI / CanvasWidget.cxx
1 /*
2  * CanvasWidget.cxx
3  *
4  *  Created on: 03.07.2012
5  *      Author: tom
6  */
7
8 #ifdef HAVE_CONFIG_H
9 #  include <config.h>
10 #endif
11
12 #include "CanvasWidget.hxx"
13
14 #include <Canvas/canvas_mgr.hxx>
15 #include <Main/fg_os.hxx>      // fgGetKeyModifiers()
16 #include <Scripting/NasalSys.hxx>
17
18 #include <simgear/canvas/Canvas.hxx>
19 #include <simgear/canvas/MouseEvent.hxx>
20
21 //------------------------------------------------------------------------------
22 CanvasWidget::CanvasWidget( int x, int y,
23                             int width, int height,
24                             SGPropertyNode* props,
25                             const std::string& module ):
26   puObject(x, y, width, height),
27   _canvas_mgr( dynamic_cast<CanvasMgr*>(globals->get_subsystem("Canvas")) ),
28   _tex_id(0),
29   _no_tex_cnt(0),
30   _last_x(0),
31   _last_y(0)
32 {
33   if( !_canvas_mgr )
34   {
35     SG_LOG(SG_GENERAL, SG_ALERT, "CanvasWidget: failed to get canvas manager!");
36     return;
37   }
38
39   _canvas = _canvas_mgr->createCanvas
40   (
41     props->getStringValue("name", "gui-anonymous")
42   );
43
44   int view[2] = {
45     // Get canvas viewport size. If not specified use the widget dimensions
46     props->getIntValue("view[0]", width),
47     props->getIntValue("view[1]", height)
48   };
49
50   SGPropertyNode* cprops = _canvas->getProps();
51   cprops->setIntValue("size[0]", view[0] * 2); // use higher resolution
52   cprops->setIntValue("size[1]", view[1] * 2); // for antialias
53   cprops->setIntValue("view[0]", view[0]);
54   cprops->setIntValue("view[1]", view[1]);
55   cprops->setBoolValue("render-always", true);
56   cprops->setStringValue( "name",
57                            props->getStringValue("name", "gui-anonymous") );
58   SGPropertyNode* input = cprops->getChild("input", 0, true);
59   _mouse_x = input->getChild("mouse-x", 0, true);
60   _mouse_y = input->getChild("mouse-y", 0, true);
61   _mouse_down = input->getChild("mouse-down", 0, true);
62   _mouse_drag = input->getChild("mouse-drag", 0, true);
63
64   SGPropertyNode *nasal = props->getNode("nasal");
65   if( !nasal )
66     return;
67
68   FGNasalSys *nas = dynamic_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
69   if( !nas )
70     SG_LOG( SG_GENERAL,
71             SG_ALERT,
72             "CanvasWidget: Failed to get nasal subsystem!" );
73
74   const std::string file = std::string("__canvas:")
75                          + cprops->getStringValue("name");
76
77   SGPropertyNode *load = nasal->getNode("load");
78   if( load )
79   {
80     const char *s = load->getStringValue();
81     nas->handleCommand(module.c_str(), file.c_str(), s, cprops);
82   }
83 }
84
85 //------------------------------------------------------------------------------
86 CanvasWidget::~CanvasWidget()
87 {
88   if( _canvas )
89     // TODO check if really not in use anymore
90     _canvas->getProps()
91            ->getParent()
92            ->removeChild( _canvas->getProps()->getName(),
93                           _canvas->getProps()->getIndex(),
94                           false );
95 }
96
97 // Old versions of PUI are missing this defines...
98 #ifndef PU_SCROLL_UP_BUTTON
99 # define PU_SCROLL_UP_BUTTON     3
100 #endif
101 #ifndef PU_SCROLL_DOWN_BUTTON
102 # define PU_SCROLL_DOWN_BUTTON   4
103 #endif
104
105 //------------------------------------------------------------------------------
106 void CanvasWidget::doHit(int button, int updown, int x, int y)
107 {
108   puObject::doHit(button, updown, x, y);
109
110   // CTRL allows resizing and SHIFT allows moving the window
111   if( fgGetKeyModifiers() & (KEYMOD_CTRL | KEYMOD_SHIFT) )
112     return;
113
114   namespace sc = simgear::canvas;
115   sc::MouseEventPtr event(new sc::MouseEvent);
116   event->pos.set(x - abox.min[0], y - abox.min[1]);
117   event->delta.set(x - _last_x, y - _last_y);
118
119   _last_x = x;
120   _last_y = y;
121
122   switch( button )
123   {
124     case PU_LEFT_BUTTON:
125       event->button = osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON;
126       break;
127     case PU_MIDDLE_BUTTON:
128       event->button = osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON;
129       break;
130     case PU_RIGHT_BUTTON:
131       event->button = osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON;
132       break;
133     case PU_SCROLL_UP_BUTTON:
134     case PU_SCROLL_DOWN_BUTTON:
135       // Only let PU_DOWN trigger a scroll wheel event
136       if( updown != PU_DOWN )
137         return;
138
139       event->type = sc::Event::WHEEL;
140       event->delta.y() = button == PU_SCROLL_UP_BUTTON ? 1 : -1;
141
142       _canvas->handleMouseEvent(event);
143
144       return;
145     default:
146       SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown button: " << button);
147       return;
148   }
149
150   switch( updown )
151   {
152     case PU_DOWN:
153       event->type = sc::Event::MOUSE_DOWN;
154       puSetActiveWidget(this, x, y);
155       break;
156     case PU_UP:
157       event->type = sc::Event::MOUSE_UP;
158       puDeactivateWidget();
159       break;
160     case PU_DRAG:
161       event->type = sc::Event::DRAG;
162       break;
163     default:
164       SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown updown: " << updown);
165       return;
166   }
167
168   _canvas->handleMouseEvent(event);
169
170   _mouse_x->setIntValue(x - abox.min[0]);
171   _mouse_y->setIntValue(abox.max[1] - y);
172
173   if( updown == PU_DRAG )
174     _mouse_drag->setIntValue(button);
175   else if( updown == PU_DOWN )
176     _mouse_down->setIntValue(button);
177 }
178
179 //------------------------------------------------------------------------------
180 int CanvasWidget::checkKey(int key, int updown)
181 {
182   return puObject::checkKey(key, updown);
183 }
184
185 //------------------------------------------------------------------------------
186 void CanvasWidget::setSize(int w, int h)
187 {
188   puObject::setSize(w, h);
189
190   _canvas->getProps()->setIntValue("view[0]", w);
191   _canvas->getProps()->setIntValue("view[1]", h);
192 }
193
194 //------------------------------------------------------------------------------
195 void CanvasWidget::draw(int dx, int dy)
196 {
197   if( !_tex_id )
198   {
199     _tex_id = _canvas_mgr->getCanvasTexId( _canvas->getProps()->getIndex() );
200
201     // Normally we should be able to get the texture after one frame. I don't
202     // know if there are circumstances where it can take longer, so we don't
203     // log a warning message until we have tried a few times.
204     if( !_tex_id )
205     {
206       if( ++_no_tex_cnt == 5 )
207         SG_LOG(SG_GENERAL, SG_WARN, "CanvasWidget: failed to get texture!");
208       return;
209     }
210     else
211     {
212       if( _no_tex_cnt >= 5 )
213         SG_LOG
214         (
215           SG_GENERAL,
216           SG_INFO,
217           "CanvasWidget: got texture after " << _no_tex_cnt << " tries."
218         );
219       _no_tex_cnt = 0;
220     }
221   }
222
223   glEnable(GL_TEXTURE_2D);
224   glBindTexture(GL_TEXTURE_2D, _tex_id);
225   glBegin( GL_QUADS );
226     glColor3f(1,1,1);
227     glTexCoord2f(0,0); glVertex2f(dx + abox.min[0], dy + abox.min[1]);
228     glTexCoord2f(1,0); glVertex2f(dx + abox.max[0], dy + abox.min[1]);
229     glTexCoord2f(1,1); glVertex2f(dx + abox.max[0], dy + abox.max[1]);
230     glTexCoord2f(0,1); glVertex2f(dx + abox.min[0], dy + abox.max[1]);
231   glEnd();
232   glDisable(GL_TEXTURE_2D);
233 }