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