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