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