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>
20 //------------------------------------------------------------------------------
21 CanvasWidget::CanvasWidget( int x, int y,
22 int width, int height,
23 SGPropertyNode* props,
24 const std::string& module ):
25 puObject(x, y, width, height),
26 _canvas_mgr( dynamic_cast<CanvasMgr*>(globals->get_subsystem("Canvas")) ),
32 SG_LOG(SG_GENERAL, SG_ALERT, "CanvasWidget: failed to get canvas manager!");
36 _canvas = _canvas_mgr->createCanvas
38 props->getStringValue("name", "gui-anonymous")
42 // Get canvas viewport size. If not specified use the widget dimensions
43 props->getIntValue("view[0]", width),
44 props->getIntValue("view[1]", height)
47 SGPropertyNode* cprops = _canvas->getProps();
48 cprops->setIntValue("size[0]", view[0] * 2); // use higher resolution
49 cprops->setIntValue("size[1]", view[1] * 2); // for antialias
50 cprops->setIntValue("view[0]", view[0]);
51 cprops->setIntValue("view[1]", view[1]);
52 cprops->setBoolValue("render-always", true);
53 cprops->setStringValue( "name",
54 props->getStringValue("name", "gui-anonymous") );
55 SGPropertyNode* input = cprops->getChild("input", 0, true);
56 _mouse_x = input->getChild("mouse-x", 0, true);
57 _mouse_y = input->getChild("mouse-y", 0, true);
58 _mouse_down = input->getChild("mouse-down", 0, true);
59 _mouse_drag = input->getChild("mouse-drag", 0, true);
61 SGPropertyNode *nasal = props->getNode("nasal");
65 FGNasalSys *nas = dynamic_cast<FGNasalSys*>(globals->get_subsystem("nasal"));
69 "CanvasWidget: Failed to get nasal subsystem!" );
71 const std::string file = std::string("__canvas:")
72 + cprops->getStringValue("name");
74 SGPropertyNode *load = nasal->getNode("load");
77 const char *s = load->getStringValue();
78 nas->handleCommand(module.c_str(), file.c_str(), s, cprops);
82 //------------------------------------------------------------------------------
83 CanvasWidget::~CanvasWidget()
86 // TODO check if really not in use anymore
89 ->removeChild( _canvas->getProps()->getName(),
90 _canvas->getProps()->getIndex(),
94 //------------------------------------------------------------------------------
95 void CanvasWidget::doHit(int button, int updown, int x, int y)
97 puObject::doHit(button, updown, x, y);
99 // CTRL allows resizing and SHIFT allows moving the window
100 if( fgGetKeyModifiers() & (KEYMOD_CTRL | KEYMOD_SHIFT) )
103 _mouse_x->setIntValue(x - abox.min[0]);
104 _mouse_y->setIntValue(abox.max[1] - y);
106 if( updown == PU_DRAG )
107 _mouse_drag->setIntValue(button);
108 else if( updown == PU_DOWN )
109 _mouse_down->setIntValue(button);
111 if( button != active_mouse_button )
115 puDeactivateWidget();
116 else if (updown == PU_DOWN)
117 puSetActiveWidget(this, x, y);
120 //------------------------------------------------------------------------------
121 int CanvasWidget::checkKey(int key, int updown)
123 return puObject::checkKey(key, updown);
126 //------------------------------------------------------------------------------
127 void CanvasWidget::setSize(int w, int h)
129 puObject::setSize(w, h);
131 _canvas->getProps()->setIntValue("view[0]", w);
132 _canvas->getProps()->setIntValue("view[1]", h);
135 //------------------------------------------------------------------------------
136 void CanvasWidget::draw(int dx, int dy)
140 _tex_id = _canvas_mgr->getCanvasTexId( _canvas->getProps()->getIndex() );
142 // Normally we should be able to get the texture after one frame. I don't
143 // know if there are circumstances where it can take longer, so we don't
144 // log a warning message until we have tried a few times.
147 if( ++_no_tex_cnt == 5 )
148 SG_LOG(SG_GENERAL, SG_WARN, "CanvasWidget: failed to get texture!");
153 if( _no_tex_cnt >= 5 )
158 "CanvasWidget: got texture after " << _no_tex_cnt << " tries."
164 glEnable(GL_TEXTURE_2D);
165 glBindTexture(GL_TEXTURE_2D, _tex_id);
168 glTexCoord2f(0,0); glVertex2f(dx + abox.min[0], dy + abox.min[1]);
169 glTexCoord2f(1,0); glVertex2f(dx + abox.max[0], dy + abox.min[1]);
170 glTexCoord2f(1,1); glVertex2f(dx + abox.max[0], dy + abox.max[1]);
171 glTexCoord2f(0,1); glVertex2f(dx + abox.min[0], dy + abox.max[1]);
173 glDisable(GL_TEXTURE_2D);