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 //------------------------------------------------------------------------------
98 void CanvasWidget::doHit(int button, int updown, int x, int y)
100 puObject::doHit(button, updown, x, y);
102 // CTRL allows resizing and SHIFT allows moving the window
103 if( fgGetKeyModifiers() & (KEYMOD_CTRL | KEYMOD_SHIFT) )
106 namespace sc = simgear::canvas;
107 sc::MouseEventPtr event(new sc::MouseEvent);
108 event->pos.set(x - abox.min[0], y - abox.min[1]);
109 event->delta.set(x - _last_x, y - _last_y);
117 event->button = osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON;
119 case PU_MIDDLE_BUTTON:
120 event->button = osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON;
122 case PU_RIGHT_BUTTON:
123 event->button = osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON;
125 case PU_SCROLL_UP_BUTTON:
126 case PU_SCROLL_DOWN_BUTTON:
127 // Only let PU_DOWN trigger a scroll wheel event
128 if( updown != PU_DOWN )
131 event->type = sc::Event::WHEEL;
132 event->delta.y() = button == PU_SCROLL_UP_BUTTON ? 1 : -1;
134 _canvas->handleMouseEvent(event);
138 SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown button: " << button);
145 event->type = sc::Event::MOUSE_DOWN;
146 puSetActiveWidget(this, x, y);
149 event->type = sc::Event::MOUSE_UP;
150 puDeactivateWidget();
153 event->type = sc::Event::DRAG;
156 SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown updown: " << updown);
160 _canvas->handleMouseEvent(event);
162 _mouse_x->setIntValue(x - abox.min[0]);
163 _mouse_y->setIntValue(abox.max[1] - y);
165 if( updown == PU_DRAG )
166 _mouse_drag->setIntValue(button);
167 else if( updown == PU_DOWN )
168 _mouse_down->setIntValue(button);
171 //------------------------------------------------------------------------------
172 int CanvasWidget::checkKey(int key, int updown)
174 return puObject::checkKey(key, updown);
177 //------------------------------------------------------------------------------
178 void CanvasWidget::setSize(int w, int h)
180 puObject::setSize(w, h);
182 _canvas->getProps()->setIntValue("view[0]", w);
183 _canvas->getProps()->setIntValue("view[1]", h);
186 //------------------------------------------------------------------------------
187 void CanvasWidget::draw(int dx, int dy)
191 _tex_id = _canvas_mgr->getCanvasTexId( _canvas->getProps()->getIndex() );
193 // Normally we should be able to get the texture after one frame. I don't
194 // know if there are circumstances where it can take longer, so we don't
195 // log a warning message until we have tried a few times.
198 if( ++_no_tex_cnt == 5 )
199 SG_LOG(SG_GENERAL, SG_WARN, "CanvasWidget: failed to get texture!");
204 if( _no_tex_cnt >= 5 )
209 "CanvasWidget: got texture after " << _no_tex_cnt << " tries."
215 glEnable(GL_TEXTURE_2D);
216 glBindTexture(GL_TEXTURE_2D, _tex_id);
219 glTexCoord2f(0,0); glVertex2f(dx + abox.min[0], dy + abox.min[1]);
220 glTexCoord2f(1,0); glVertex2f(dx + abox.max[0], dy + abox.min[1]);
221 glTexCoord2f(1,1); glVertex2f(dx + abox.max[0], dy + abox.max[1]);
222 glTexCoord2f(0,1); glVertex2f(dx + abox.min[0], dy + abox.max[1]);
224 glDisable(GL_TEXTURE_2D);