4 * Created on: 03.07.2012
12 #include "CanvasWidget.hxx"
14 #include <Canvas/canvas_mgr.hxx>
15 #include <Main/fg_os.hxx> // fgGetKeyModifiers()
16 #include <Scripting/NasalSys.hxx>
18 #include <simgear/canvas/Canvas.hxx>
19 #include <simgear/canvas/MouseEvent.hxx>
21 SGPropertyNode_ptr CanvasWidget::_time;
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")) ),
37 SG_LOG(SG_GENERAL, SG_ALERT, "CanvasWidget: failed to get canvas manager!");
41 _canvas = _canvas_mgr->createCanvas
43 props->getStringValue("name", "gui-anonymous")
47 // Get canvas viewport size. If not specified use the widget dimensions
48 props->getIntValue("view[0]", width),
49 props->getIntValue("view[1]", height)
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);
66 SGPropertyNode *nasal = props->getNode("nasal");
70 FGNasalSys *nas = dynamic_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
74 "CanvasWidget: Failed to get nasal subsystem!" );
76 const std::string file = std::string("__canvas:")
77 + cprops->getStringValue("name");
79 SGPropertyNode *load = nasal->getNode("load");
82 const char *s = load->getStringValue();
83 nas->handleCommand(module.c_str(), file.c_str(), s, cprops);
87 //------------------------------------------------------------------------------
88 CanvasWidget::~CanvasWidget()
91 // TODO check if really not in use anymore
94 ->removeChild( _canvas->getProps()->getName(),
95 _canvas->getProps()->getIndex(),
99 // Old versions of PUI are missing this defines...
100 #ifndef PU_SCROLL_UP_BUTTON
101 # define PU_SCROLL_UP_BUTTON 3
103 #ifndef PU_SCROLL_DOWN_BUTTON
104 # define PU_SCROLL_DOWN_BUTTON 4
107 //------------------------------------------------------------------------------
108 void CanvasWidget::doHit(int button, int updown, int x, int y)
110 puObject::doHit(button, updown, x, y);
112 // CTRL allows resizing and SHIFT allows moving the window
113 if( fgGetKeyModifiers() & (KEYMOD_CTRL | KEYMOD_SHIFT) )
116 namespace sc = simgear::canvas;
117 sc::MouseEventPtr event(new sc::MouseEvent);
120 _time = globals->get_props()->getNode("/sim/time/elapsed-sec");
121 event->time = _time->getDoubleValue();
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 );
127 _last_x = event->pos.x();
128 _last_y = event->pos.y();
133 event->button = osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON;
135 case PU_MIDDLE_BUTTON:
136 event->button = osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON;
138 case PU_RIGHT_BUTTON:
139 event->button = osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON;
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 )
147 event->type = sc::Event::WHEEL;
148 event->delta.y() = button == PU_SCROLL_UP_BUTTON ? 1 : -1;
150 _canvas->handleMouseEvent(event);
154 SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown button: " << button);
161 event->type = sc::Event::MOUSE_DOWN;
162 puSetActiveWidget(this, x, y);
165 event->type = sc::Event::MOUSE_UP;
166 puDeactivateWidget();
169 event->type = sc::Event::DRAG;
172 SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown updown: " << updown);
176 _canvas->handleMouseEvent(event);
178 _mouse_x->setIntValue(x - abox.min[0]);
179 _mouse_y->setIntValue(abox.max[1] - y);
181 if( updown == PU_DRAG )
182 _mouse_drag->setIntValue(button);
183 else if( updown == PU_DOWN )
184 _mouse_down->setIntValue(button);
187 //------------------------------------------------------------------------------
188 int CanvasWidget::checkKey(int key, int updown)
190 return puObject::checkKey(key, updown);
193 //------------------------------------------------------------------------------
194 void CanvasWidget::setSize(int w, int h)
196 puObject::setSize(w, h);
198 _canvas->getProps()->setIntValue("view[0]", w);
199 _canvas->getProps()->setIntValue("view[1]", h);
202 //------------------------------------------------------------------------------
203 void CanvasWidget::draw(int dx, int dy)
207 _tex_id = _canvas_mgr->getCanvasTexId( _canvas->getProps()->getIndex() );
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.
214 if( ++_no_tex_cnt == 5 )
215 SG_LOG(SG_GENERAL, SG_WARN, "CanvasWidget: failed to get texture!");
220 if( _no_tex_cnt >= 5 )
225 "CanvasWidget: got texture after " << _no_tex_cnt << " tries."
231 glEnable(GL_TEXTURE_2D);
232 glBindTexture(GL_TEXTURE_2D, _tex_id);
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]);
240 glDisable(GL_TEXTURE_2D);