]> git.mxchange.org Git - flightgear.git/blob - src/GUI/CanvasWidget.cxx
CanvasWidget: automatically set auto-viewport.
[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   // automatically resize viewport of canvas if no size is given
34   _auto_viewport( !props->hasChild("view") )
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
62   SGPropertyNode *nasal = props->getNode("nasal");
63   if( !nasal )
64     return;
65
66   FGNasalSys *nas = dynamic_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
67   if( !nas )
68     SG_LOG( SG_GENERAL,
69             SG_ALERT,
70             "CanvasWidget: Failed to get nasal subsystem!" );
71
72   const std::string file = std::string("__canvas:")
73                          + cprops->getStringValue("name");
74
75   SGPropertyNode *load = nasal->getNode("load");
76   if( load )
77   {
78     const char *s = load->getStringValue();
79     nas->handleCommand(module.c_str(), file.c_str(), s, cprops);
80   }
81 }
82
83 //------------------------------------------------------------------------------
84 CanvasWidget::~CanvasWidget()
85 {
86   if( _canvas )
87     // TODO check if really not in use anymore
88     _canvas->getProps()
89            ->getParent()
90            ->removeChild( _canvas->getProps()->getName(),
91                           _canvas->getProps()->getIndex(),
92                           false );
93 }
94
95 // Old versions of PUI are missing this defines...
96 #ifndef PU_SCROLL_UP_BUTTON
97 # define PU_SCROLL_UP_BUTTON     3
98 #endif
99 #ifndef PU_SCROLL_DOWN_BUTTON
100 # define PU_SCROLL_DOWN_BUTTON   4
101 #endif
102
103 //------------------------------------------------------------------------------
104 void CanvasWidget::doHit(int button, int updown, int x, int y)
105 {
106   puObject::doHit(button, updown, x, y);
107
108   // CTRL allows resizing and SHIFT allows moving the window
109   if( fgGetKeyModifiers() & (KEYMOD_CTRL | KEYMOD_SHIFT) )
110     return;
111
112   namespace sc = simgear::canvas;
113   sc::MouseEventPtr event(new sc::MouseEvent);
114
115   if( !_time )
116     _time = globals->get_props()->getNode("/sim/time/elapsed-sec");
117   event->time = _time->getDoubleValue();
118
119   if( !_view_height )
120     _view_height = globals->get_props()->getNode("/sim/gui/canvas/size[1]");
121   event->screen_pos.set(x, _view_height->getIntValue() - y);
122
123   event->client_pos.set(x - abox.min[0], abox.max[1] - y);
124   event->delta.set( event->getScreenX() - _last_x,
125                     event->getScreenY() - _last_y );
126
127   _last_x = event->getScreenX();
128   _last_y = event->getScreenY();
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
179 //------------------------------------------------------------------------------
180 int CanvasWidget::checkKey(int key, int updown)
181 {
182   return puObject::checkKey(key, updown);
183 }
184
185 //------------------------------------------------------------------------------
186 void CanvasWidget::setSize(int w, int h)
187 {
188   puObject::setSize(w, h);
189
190   if( _auto_viewport )
191   {
192     _canvas->getProps()->setIntValue("view[0]", w);
193     _canvas->getProps()->setIntValue("view[1]", h);
194   }
195 }
196
197 //------------------------------------------------------------------------------
198 void CanvasWidget::draw(int dx, int dy)
199 {
200   glEnable(GL_TEXTURE_2D);
201   glBindTexture(GL_TEXTURE_2D, _canvas_mgr->getCanvasTexId(_canvas));
202   glBegin( GL_QUADS );
203     glColor3f(1,1,1);
204     glTexCoord2f(0,0); glVertex2f(dx + abox.min[0], dy + abox.min[1]);
205     glTexCoord2f(1,0); glVertex2f(dx + abox.max[0], dy + abox.min[1]);
206     glTexCoord2f(1,1); glVertex2f(dx + abox.max[0], dy + abox.max[1]);
207     glTexCoord2f(0,1); glVertex2f(dx + abox.min[0], dy + abox.max[1]);
208   glEnd();
209   glDisable(GL_TEXTURE_2D);
210 }