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,
22 CanvasWidget::_view_height;
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")) ),
33 _auto_viewport( props->getBoolValue("auto-viewport", true) )
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") );
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 // Old versions of PUI are missing this defines...
95 #ifndef PU_SCROLL_UP_BUTTON
96 # define PU_SCROLL_UP_BUTTON 3
98 #ifndef PU_SCROLL_DOWN_BUTTON
99 # define PU_SCROLL_DOWN_BUTTON 4
102 //------------------------------------------------------------------------------
103 void CanvasWidget::doHit(int button, int updown, int x, int y)
105 puObject::doHit(button, updown, x, y);
107 // CTRL allows resizing and SHIFT allows moving the window
108 if( fgGetKeyModifiers() & (KEYMOD_CTRL | KEYMOD_SHIFT) )
111 namespace sc = simgear::canvas;
112 sc::MouseEventPtr event(new sc::MouseEvent);
115 _time = globals->get_props()->getNode("/sim/time/elapsed-sec");
116 event->time = _time->getDoubleValue();
119 _view_height = globals->get_props()->getNode("/sim/gui/canvas/size[1]");
120 event->screen_pos.set(x, _view_height->getIntValue() - y);
122 event->client_pos.set(x - abox.min[0], abox.max[1] - y);
123 event->delta.set( event->getScreenX() - _last_x,
124 event->getScreenY() - _last_y );
126 _last_x = event->getScreenX();
127 _last_y = event->getScreenY();
132 event->button = osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON;
134 case PU_MIDDLE_BUTTON:
135 event->button = osgGA::GUIEventAdapter::MIDDLE_MOUSE_BUTTON;
137 case PU_RIGHT_BUTTON:
138 event->button = osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON;
140 case PU_SCROLL_UP_BUTTON:
141 case PU_SCROLL_DOWN_BUTTON:
142 // Only let PU_DOWN trigger a scroll wheel event
143 if( updown != PU_DOWN )
146 event->type = sc::Event::WHEEL;
147 event->delta.y() = button == PU_SCROLL_UP_BUTTON ? 1 : -1;
149 _canvas->handleMouseEvent(event);
153 SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown button: " << button);
160 event->type = sc::Event::MOUSE_DOWN;
161 puSetActiveWidget(this, x, y);
164 event->type = sc::Event::MOUSE_UP;
165 puDeactivateWidget();
168 event->type = sc::Event::DRAG;
171 SG_LOG(SG_INPUT, SG_WARN, "CanvasWidget: Unknown updown: " << updown);
175 _canvas->handleMouseEvent(event);
178 //------------------------------------------------------------------------------
179 int CanvasWidget::checkKey(int key, int updown)
181 return puObject::checkKey(key, updown);
184 //------------------------------------------------------------------------------
185 void CanvasWidget::setSize(int w, int h)
187 puObject::setSize(w, h);
191 _canvas->getProps()->setIntValue("view[0]", w);
192 _canvas->getProps()->setIntValue("view[1]", h);
196 //------------------------------------------------------------------------------
197 void CanvasWidget::draw(int dx, int dy)
199 glEnable(GL_TEXTURE_2D);
200 glBindTexture(GL_TEXTURE_2D, _canvas_mgr->getCanvasTexId(_canvas));
203 glTexCoord2f(0,0); glVertex2f(dx + abox.min[0], dy + abox.min[1]);
204 glTexCoord2f(1,0); glVertex2f(dx + abox.max[0], dy + abox.min[1]);
205 glTexCoord2f(1,1); glVertex2f(dx + abox.max[0], dy + abox.max[1]);
206 glTexCoord2f(0,1); glVertex2f(dx + abox.min[0], dy + abox.max[1]);
208 glDisable(GL_TEXTURE_2D);