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 //------------------------------------------------------------------------------
22 CanvasWidget::CanvasWidget( int x, int y,
23 int width, int height,
24 SGPropertyNode* props,
25 const std::string& module ):
26 puObject(x, y, width, height),
27 _canvas_mgr( dynamic_cast<CanvasMgr*>(globals->get_subsystem("Canvas")) ),
35 SG_LOG(SG_GENERAL, SG_ALERT, "CanvasWidget: failed to get canvas manager!");
39 _canvas = _canvas_mgr->createCanvas
41 props->getStringValue("name", "gui-anonymous")
45 // Get canvas viewport size. If not specified use the widget dimensions
46 props->getIntValue("view[0]", width),
47 props->getIntValue("view[1]", height)
50 SGPropertyNode* cprops = _canvas->getProps();
51 cprops->setIntValue("size[0]", view[0] * 2); // use higher resolution
52 cprops->setIntValue("size[1]", view[1] * 2); // for antialias
53 cprops->setIntValue("view[0]", view[0]);
54 cprops->setIntValue("view[1]", view[1]);
55 cprops->setBoolValue("render-always", true);
56 cprops->setStringValue( "name",
57 props->getStringValue("name", "gui-anonymous") );
58 SGPropertyNode* input = cprops->getChild("input", 0, true);
59 _mouse_x = input->getChild("mouse-x", 0, true);
60 _mouse_y = input->getChild("mouse-y", 0, true);
61 _mouse_down = input->getChild("mouse-down", 0, true);
62 _mouse_drag = input->getChild("mouse-drag", 0, true);
64 SGPropertyNode *nasal = props->getNode("nasal");
68 FGNasalSys *nas = dynamic_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
72 "CanvasWidget: Failed to get nasal subsystem!" );
74 const std::string file = std::string("__canvas:")
75 + cprops->getStringValue("name");
77 SGPropertyNode *load = nasal->getNode("load");
80 const char *s = load->getStringValue();
81 nas->handleCommand(module.c_str(), file.c_str(), s, cprops);
85 //------------------------------------------------------------------------------
86 CanvasWidget::~CanvasWidget()
89 // TODO check if really not in use anymore
92 ->removeChild( _canvas->getProps()->getName(),
93 _canvas->getProps()->getIndex(),
97 // Old versions of PUI are missing this defines...
98 #ifndef PU_SCROLL_UP_BUTTON
99 # define PU_SCROLL_UP_BUTTON 3
101 #ifndef PU_SCROLL_DOWN_BUTTON
102 # define PU_SCROLL_DOWN_BUTTON 4
105 //------------------------------------------------------------------------------
106 void CanvasWidget::doHit(int button, int updown, int x, int y)
108 puObject::doHit(button, updown, x, y);
110 // CTRL allows resizing and SHIFT allows moving the window
111 if( fgGetKeyModifiers() & (KEYMOD_CTRL | KEYMOD_SHIFT) )
114 namespace sc = simgear::canvas;
115 sc::MouseEventPtr event(new sc::MouseEvent);
116 event->pos.set(x - abox.min[0], y - abox.min[1]);
117 event->delta.set(x - _last_x, y - _last_y);
125 event->button = osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON;
127 case PU_MIDDLE_BUTTON:
128 event->button = osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON;
130 case PU_RIGHT_BUTTON:
131 event->button = osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON;
133 case PU_SCROLL_UP_BUTTON:
134 case PU_SCROLL_DOWN_BUTTON:
135 // Only let PU_DOWN trigger a scroll wheel event
136 if( updown != PU_DOWN )
139 event->type = sc::Event::WHEEL;
140 event->delta.y() = button == PU_SCROLL_UP_BUTTON ? 1 : -1;
142 _canvas->handleMouseEvent(event);
146 SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown button: " << button);
153 event->type = sc::Event::MOUSE_DOWN;
154 puSetActiveWidget(this, x, y);
157 event->type = sc::Event::MOUSE_UP;
158 puDeactivateWidget();
161 event->type = sc::Event::DRAG;
164 SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown updown: " << updown);
168 _canvas->handleMouseEvent(event);
170 _mouse_x->setIntValue(x - abox.min[0]);
171 _mouse_y->setIntValue(abox.max[1] - y);
173 if( updown == PU_DRAG )
174 _mouse_drag->setIntValue(button);
175 else if( updown == PU_DOWN )
176 _mouse_down->setIntValue(button);
179 //------------------------------------------------------------------------------
180 int CanvasWidget::checkKey(int key, int updown)
182 return puObject::checkKey(key, updown);
185 //------------------------------------------------------------------------------
186 void CanvasWidget::setSize(int w, int h)
188 puObject::setSize(w, h);
190 _canvas->getProps()->setIntValue("view[0]", w);
191 _canvas->getProps()->setIntValue("view[1]", h);
194 //------------------------------------------------------------------------------
195 void CanvasWidget::draw(int dx, int dy)
199 _tex_id = _canvas_mgr->getCanvasTexId( _canvas->getProps()->getIndex() );
201 // Normally we should be able to get the texture after one frame. I don't
202 // know if there are circumstances where it can take longer, so we don't
203 // log a warning message until we have tried a few times.
206 if( ++_no_tex_cnt == 5 )
207 SG_LOG(SG_GENERAL, SG_WARN, "CanvasWidget: failed to get texture!");
212 if( _no_tex_cnt >= 5 )
217 "CanvasWidget: got texture after " << _no_tex_cnt << " tries."
223 glEnable(GL_TEXTURE_2D);
224 glBindTexture(GL_TEXTURE_2D, _tex_id);
227 glTexCoord2f(0,0); glVertex2f(dx + abox.min[0], dy + abox.min[1]);
228 glTexCoord2f(1,0); glVertex2f(dx + abox.max[0], dy + abox.min[1]);
229 glTexCoord2f(1,1); glVertex2f(dx + abox.max[0], dy + abox.max[1]);
230 glTexCoord2f(0,1); glVertex2f(dx + abox.min[0], dy + abox.max[1]);
232 glDisable(GL_TEXTURE_2D);