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