]> git.mxchange.org Git - flightgear.git/blob - src/GUI/CanvasWidget.cxx
Fix calculating CanvasWidget mouse coordinates
[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], abox.max[1] - y);
117   event->delta.set( event->pos.x() - _last_x,
118                     event->pos.y() - _last_y );
119
120   _last_x = event->pos.x();
121   _last_y = event->pos.y();
122
123   switch( button )
124   {
125     case PU_LEFT_BUTTON:
126       event->button = osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON;
127       break;
128     case PU_MIDDLE_BUTTON:
129       event->button = osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON;
130       break;
131     case PU_RIGHT_BUTTON:
132       event->button = osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON;
133       break;
134     case PU_SCROLL_UP_BUTTON:
135     case PU_SCROLL_DOWN_BUTTON:
136       // Only let PU_DOWN trigger a scroll wheel event
137       if( updown != PU_DOWN )
138         return;
139
140       event->type = sc::Event::WHEEL;
141       event->delta.y() = button == PU_SCROLL_UP_BUTTON ? 1 : -1;
142
143       _canvas->handleMouseEvent(event);
144
145       return;
146     default:
147       SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown button: " << button);
148       return;
149   }
150
151   switch( updown )
152   {
153     case PU_DOWN:
154       event->type = sc::Event::MOUSE_DOWN;
155       puSetActiveWidget(this, x, y);
156       break;
157     case PU_UP:
158       event->type = sc::Event::MOUSE_UP;
159       puDeactivateWidget();
160       break;
161     case PU_DRAG:
162       event->type = sc::Event::DRAG;
163       break;
164     default:
165       SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown updown: " << updown);
166       return;
167   }
168
169   _canvas->handleMouseEvent(event);
170
171   _mouse_x->setIntValue(x - abox.min[0]);
172   _mouse_y->setIntValue(abox.max[1] - y);
173
174   if( updown == PU_DRAG )
175     _mouse_drag->setIntValue(button);
176   else if( updown == PU_DOWN )
177     _mouse_down->setIntValue(button);
178 }
179
180 //------------------------------------------------------------------------------
181 int CanvasWidget::checkKey(int key, int updown)
182 {
183   return puObject::checkKey(key, updown);
184 }
185
186 //------------------------------------------------------------------------------
187 void CanvasWidget::setSize(int w, int h)
188 {
189   puObject::setSize(w, h);
190
191   _canvas->getProps()->setIntValue("view[0]", w);
192   _canvas->getProps()->setIntValue("view[1]", h);
193 }
194
195 //------------------------------------------------------------------------------
196 void CanvasWidget::draw(int dx, int dy)
197 {
198   if( !_tex_id )
199   {
200     _tex_id = _canvas_mgr->getCanvasTexId( _canvas->getProps()->getIndex() );
201
202     // Normally we should be able to get the texture after one frame. I don't
203     // know if there are circumstances where it can take longer, so we don't
204     // log a warning message until we have tried a few times.
205     if( !_tex_id )
206     {
207       if( ++_no_tex_cnt == 5 )
208         SG_LOG(SG_GENERAL, SG_WARN, "CanvasWidget: failed to get texture!");
209       return;
210     }
211     else
212     {
213       if( _no_tex_cnt >= 5 )
214         SG_LOG
215         (
216           SG_GENERAL,
217           SG_INFO,
218           "CanvasWidget: got texture after " << _no_tex_cnt << " tries."
219         );
220       _no_tex_cnt = 0;
221     }
222   }
223
224   glEnable(GL_TEXTURE_2D);
225   glBindTexture(GL_TEXTURE_2D, _tex_id);
226   glBegin( GL_QUADS );
227     glColor3f(1,1,1);
228     glTexCoord2f(0,0); glVertex2f(dx + abox.min[0], dy + abox.min[1]);
229     glTexCoord2f(1,0); glVertex2f(dx + abox.max[0], dy + abox.min[1]);
230     glTexCoord2f(1,1); glVertex2f(dx + abox.max[0], dy + abox.max[1]);
231     glTexCoord2f(0,1); glVertex2f(dx + abox.min[0], dy + abox.max[1]);
232   glEnd();
233   glDisable(GL_TEXTURE_2D);
234 }