1 // NasalCanvas.cxx -- expose Canvas classes to Nasal
3 // Written by James Turner, started 2012.
5 // Copyright (C) 2012 James Turner
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Public License for more details.
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "NasalCanvas.hxx"
26 #include <Canvas/canvas_mgr.hxx>
27 #include <Canvas/gui_mgr.hxx>
28 #include <Main/globals.hxx>
29 #include <Scripting/NasalSys.hxx>
31 #include <osgGA/GUIEventAdapter>
33 #include <simgear/sg_inlines.h>
35 #include <simgear/canvas/Canvas.hxx>
36 #include <simgear/canvas/CanvasWindow.hxx>
37 #include <simgear/canvas/elements/CanvasElement.hxx>
38 #include <simgear/canvas/elements/CanvasText.hxx>
39 #include <simgear/canvas/layout/BoxLayout.hxx>
40 #include <simgear/canvas/layout/NasalWidget.hxx>
41 #include <simgear/canvas/events/CustomEvent.hxx>
42 #include <simgear/canvas/events/MouseEvent.hxx>
44 #include <simgear/nasal/cppbind/from_nasal.hxx>
45 #include <simgear/nasal/cppbind/to_nasal.hxx>
46 #include <simgear/nasal/cppbind/NasalHash.hxx>
47 #include <simgear/nasal/cppbind/Ghost.hxx>
49 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
51 namespace sc = simgear::canvas;
53 template<class Element>
54 naRef elementGetNode(Element& element, naContext c)
56 return propNodeGhostCreate(c, element.getProps());
59 typedef nasal::Ghost<sc::EventPtr> NasalEvent;
60 typedef nasal::Ghost<sc::CustomEventPtr> NasalCustomEvent;
61 typedef nasal::Ghost<sc::MouseEventPtr> NasalMouseEvent;
63 struct CustomEventDetailWrapper;
64 typedef SGSharedPtr<CustomEventDetailWrapper> CustomEventDetailPtr;
65 typedef nasal::Ghost<CustomEventDetailPtr> NasalCustomEventDetail;
67 typedef nasal::Ghost<simgear::PropertyBasedElementPtr> NasalPropertyBasedElement;
68 typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
69 typedef nasal::Ghost<sc::ElementPtr> NasalElement;
70 typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
71 typedef nasal::Ghost<sc::TextPtr> NasalText;
73 typedef nasal::Ghost<sc::LayoutItemRef> NasalLayoutItem;
74 typedef nasal::Ghost<sc::LayoutRef> NasalLayout;
75 typedef nasal::Ghost<sc::BoxLayoutRef> NasalBoxLayout;
77 typedef nasal::Ghost<sc::WindowPtr> NasalWindow;
79 naRef to_nasal_helper(naContext c, const osg::BoundingBox& bb)
81 std::vector<float> bb_vec(4);
82 bb_vec[0] = bb._min.x();
83 bb_vec[1] = bb._min.y();
84 bb_vec[2] = bb._max.x();
85 bb_vec[3] = bb._max.y();
87 return nasal::to_nasal(c, bb_vec);
90 SGPropertyNode* from_nasal_helper(naContext c, naRef ref, SGPropertyNode**)
92 SGPropertyNode* props = ghostToPropNode(ref);
94 naRuntimeError(c, "Not a SGPropertyNode ghost.");
99 CanvasMgr& requireCanvasMgr(naContext c)
101 CanvasMgr* canvas_mgr =
102 static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
104 naRuntimeError(c, "Failed to get Canvas subsystem");
109 GUIMgr& requireGUIMgr(naContext c)
112 static_cast<GUIMgr*>(globals->get_subsystem("CanvasGUI"));
114 naRuntimeError(c, "Failed to get CanvasGUI subsystem");
120 * Create new Canvas and get ghost for it.
122 static naRef f_createCanvas(const nasal::CallContext& ctx)
124 return ctx.to_nasal(requireCanvasMgr(ctx.c).createCanvas());
128 * Create new Window and get ghost for it.
130 static naRef f_createWindow(const nasal::CallContext& ctx)
132 return nasal::to_nasal<sc::WindowWeakPtr>
135 requireGUIMgr(ctx.c).createWindow( ctx.getArg<std::string>(0) )
140 * Get ghost for existing Canvas.
142 static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
144 nasal::CallContext ctx(c, argc, args);
145 SGPropertyNode& props = *ctx.requireArg<SGPropertyNode*>(0);
146 CanvasMgr& canvas_mgr = requireCanvasMgr(c);
148 sc::CanvasPtr canvas;
149 if( canvas_mgr.getPropertyRoot() == props.getParent() )
151 // get a canvas specified by its root node
152 canvas = canvas_mgr.getCanvas( props.getIndex() );
153 if( !canvas || canvas->getProps() != &props )
158 // get a canvas by name
159 if( props.hasValue("name") )
160 canvas = canvas_mgr.getCanvas( props.getStringValue("name") );
161 else if( props.hasValue("index") )
162 canvas = canvas_mgr.getCanvas( props.getIntValue("index") );
165 return nasal::to_nasal(c, canvas);
168 naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
170 return ctx.to_nasal( canvas.createGroup(ctx.getArg<std::string>(0)) );
174 * Get group containing all gui windows
176 naRef f_getDesktop(naContext c, naRef me, int argc, naRef* args)
178 return nasal::to_nasal(c, requireGUIMgr(c).getDesktop());
181 static naRef f_groupCreateChild(sc::Group& group, const nasal::CallContext& ctx)
183 return ctx.to_nasal( group.createChild( ctx.requireArg<std::string>(0),
184 ctx.getArg<std::string>(1) ) );
187 static sc::ElementPtr f_groupGetChild(sc::Group& group, SGPropertyNode* node)
189 return group.getChild(node);
192 static void propElementSetData( simgear::PropertyBasedElement& el,
193 const std::string& name,
198 return el.removeDataProp(name);
200 std::string val = nasal::from_nasal<std::string>(c, ref);
204 long val_long = strtol(val.c_str(), &end, 10);
206 return el.setDataProp(name, val_long);
208 double val_double = strtod(val.c_str(), &end);
210 return el.setDataProp(name, val_double);
212 el.setDataProp(name, val);
216 * Accessor for HTML5 data properties.
218 * # set single property:
219 * el.data("myKey", 5);
221 * # set multiple properties
222 * el.data({myProp1: 12, myProp2: "test"});
224 * # get value of properties
225 * el.data("myKey"); # 5
226 * el.data("myProp2"); # "test"
228 * # remove a single property
229 * el.data("myKey", nil);
231 * # remove multiple properties
232 * el.data({myProp1: nil, myProp2: nil});
234 * # set and remove multiple properties
235 * el.data({newProp: "some text...", removeProp: nil});
238 * @see http://api.jquery.com/data/
240 static naRef f_propElementData( simgear::PropertyBasedElement& el,
241 const nasal::CallContext& ctx )
245 // Add/delete properties given as hash
246 nasal::Hash obj = ctx.requireArg<nasal::Hash>(0);
247 for(nasal::Hash::iterator it = obj.begin(); it != obj.end(); ++it)
248 propElementSetData(el, it->getKey(), ctx.c, it->getValue<naRef>());
250 return ctx.to_nasal(&el);
253 std::string name = ctx.getArg<std::string>(0);
258 // name + additional argument -> add/delete property
259 SGPropertyNode* node = el.getDataProp<SGPropertyNode*>(name);
263 return ctx.to_nasal( node->getStringValue() );
267 // only name -> get property
268 propElementSetData(el, name, ctx.c, ctx.requireArg<naRef>(1));
269 return ctx.to_nasal(&el);
277 naRef f_eventGetModifier(sc::MouseEvent& event, naContext)
279 return naNum((event.getModifiers() & Mask) != 0);
282 static naRef f_createCustomEvent(const nasal::CallContext& ctx)
284 std::string const& type = ctx.requireArg<std::string>(0);
288 bool bubbles = false;
289 simgear::StringMap detail;
292 nasal::Hash const& cfg = ctx.requireArg<nasal::Hash>(1);
293 naRef na_detail = cfg.get("detail");
294 if( naIsHash(na_detail) )
295 detail = ctx.from_nasal<simgear::StringMap>(na_detail);
296 bubbles = cfg.get<bool>("bubbles");
300 sc::CustomEventPtr(new sc::CustomEvent(type, bubbles, detail))
304 struct CustomEventDetailWrapper:
307 sc::CustomEventPtr _event;
309 CustomEventDetailWrapper(const sc::CustomEventPtr& event):
315 bool _get( const std::string& key,
316 std::string& value_out ) const
321 simgear::StringMap::const_iterator it = _event->detail.find(key);
322 if( it == _event->detail.end() )
325 value_out = it->second;
329 bool _set( const std::string& key,
330 const std::string& value )
335 _event->detail[ key ] = value;
340 static naRef f_customEventGetDetail( sc::CustomEvent& event,
343 return nasal::to_nasal(
345 CustomEventDetailPtr(new CustomEventDetailWrapper(&event))
349 static naRef f_boxLayoutAddItem( sc::BoxLayout& box,
350 const nasal::CallContext& ctx )
352 box.addItem( ctx.requireArg<sc::LayoutItemRef>(0),
353 ctx.getArg<int>(1) );
356 static naRef f_boxLayoutInsertItem( sc::BoxLayout& box,
357 const nasal::CallContext& ctx )
359 box.insertItem( ctx.requireArg<int>(0),
360 ctx.requireArg<sc::LayoutItemRef>(1),
361 ctx.getArg<int>(2) );
365 static naRef f_boxLayoutAddStretch( sc::BoxLayout& box,
366 const nasal::CallContext& ctx )
368 box.addStretch( ctx.getArg<int>(0) );
371 static naRef f_boxLayoutInsertStretch( sc::BoxLayout& box,
372 const nasal::CallContext& ctx )
374 box.insertStretch( ctx.requireArg<int>(0),
375 ctx.getArg<int>(1) );
379 template<class Type, class Base>
380 static naRef f_newAsBase(const nasal::CallContext& ctx)
382 return ctx.to_nasal<Base*>(new Type());
385 naRef initNasalCanvas(naRef globals, naContext c)
387 nasal::Hash globals_module(globals, c),
388 canvas_module = globals_module.createHash("canvas");
390 nasal::Object::setupGhost();
392 //----------------------------------------------------------------------------
395 using osgGA::GUIEventAdapter;
396 NasalEvent::init("canvas.Event")
397 .member("type", &sc::Event::getTypeString)
398 .member("target", &sc::Event::getTarget)
399 .member("currentTarget", &sc::Event::getCurrentTarget)
400 .method("stopPropagation", &sc::Event::stopPropagation);
402 NasalCustomEvent::init("canvas.CustomEvent")
404 .member("detail", &f_customEventGetDetail, &sc::CustomEvent::setDetail);
405 NasalCustomEventDetail::init("canvas.CustomEventDetail")
406 ._get(&CustomEventDetailWrapper::_get)
407 ._set(&CustomEventDetailWrapper::_set);
409 canvas_module.createHash("CustomEvent")
410 .set("new", &f_createCustomEvent);
412 NasalMouseEvent::init("canvas.MouseEvent")
414 .member("screenX", &sc::MouseEvent::getScreenX)
415 .member("screenY", &sc::MouseEvent::getScreenY)
416 .member("clientX", &sc::MouseEvent::getClientX)
417 .member("clientY", &sc::MouseEvent::getClientY)
418 .member("localX", &sc::MouseEvent::getLocalX)
419 .member("localY", &sc::MouseEvent::getLocalY)
420 .member("deltaX", &sc::MouseEvent::getDeltaX)
421 .member("deltaY", &sc::MouseEvent::getDeltaY)
422 .member("button", &sc::MouseEvent::getButton)
423 .member("buttons", &sc::MouseEvent::getButtonMask)
424 .member("modifiers", &sc::MouseEvent::getModifiers)
425 .member("ctrlKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_CTRL>)
426 .member("shiftKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_SHIFT>)
427 .member("altKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_ALT>)
428 .member("metaKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_META>)
429 .member("click_count", &sc::MouseEvent::getCurrentClickCount);
431 //----------------------------------------------------------------------------
434 NasalPropertyBasedElement::init("PropertyBasedElement")
435 .method("data", &f_propElementData);
436 NasalCanvas::init("Canvas")
437 .bases<NasalPropertyBasedElement>()
438 .bases<nasal::ObjectRef>()
439 .member("_node_ghost", &elementGetNode<sc::Canvas>)
440 .member("size_x", &sc::Canvas::getSizeX)
441 .member("size_y", &sc::Canvas::getSizeY)
442 .method("_createGroup", &f_canvasCreateGroup)
443 .method("_getGroup", &sc::Canvas::getGroup)
444 .method( "addEventListener",
445 static_cast<bool (sc::Canvas::*)( const std::string&,
446 const sc::EventListener& )>
447 (&sc::Canvas::addEventListener) )
448 .method("dispatchEvent", &sc::Canvas::dispatchEvent);
450 canvas_module.set("_newCanvasGhost", f_createCanvas);
451 canvas_module.set("_getCanvasGhost", f_getCanvas);
453 NasalElement::init("canvas.Element")
454 .bases<NasalPropertyBasedElement>()
455 .member("_node_ghost", &elementGetNode<sc::Element>)
456 .method("_getParent", &sc::Element::getParent)
457 .method("_getCanvas", &sc::Element::getCanvas)
458 .method("addEventListener", &sc::Element::addEventListener)
459 .method("dispatchEvent", &sc::Element::dispatchEvent)
460 .method("getBoundingBox", &sc::Element::getBoundingBox)
461 .method("getTightBoundingBox", &sc::Element::getTightBoundingBox);
463 NasalGroup::init("canvas.Group")
464 .bases<NasalElement>()
465 .method("_createChild", &f_groupCreateChild)
466 .method( "_getChild", &f_groupGetChild)
467 .method("_getElementById", &sc::Group::getElementById);
468 NasalText::init("canvas.Text")
469 .bases<NasalElement>()
470 .method("heightForWidth", &sc::Text::heightForWidth)
471 .method("maxWidth", &sc::Text::maxWidth)
472 .method("getNearestCursor", &sc::Text::getNearestCursor);
474 //----------------------------------------------------------------------------
477 NasalLayoutItem::init("canvas.LayoutItem")
478 .method("getCanvas", &sc::LayoutItem::getCanvas)
479 .method("setCanvas", &sc::LayoutItem::setCanvas)
480 .method("getParent", &sc::LayoutItem::getParent)
481 .method("setParent", &sc::LayoutItem::setParent)
482 .method("sizeHint", &sc::LayoutItem::sizeHint)
483 .method("minimumSize", &sc::LayoutItem::minimumSize)
484 .method("maximumSize", &sc::LayoutItem::maximumSize)
485 .method("hasHeightForWidth", &sc::LayoutItem::hasHeightForWidth)
486 .method("heightForWidth", &sc::LayoutItem::heightForWidth)
487 .method("minimumHeightForWidth", &sc::LayoutItem::minimumHeightForWidth)
488 .method("setGeometry", &sc::LayoutItem::setGeometry)
489 .method("geometry", &sc::LayoutItem::geometry);
490 sc::NasalWidget::setupGhost(canvas_module);
492 NasalLayout::init("canvas.Layout")
493 .bases<NasalLayoutItem>()
494 .method("addItem", &sc::Layout::addItem)
495 .method("count", &sc::Layout::count)
496 .method("itemAt", &sc::Layout::itemAt)
497 .method("takeAt", &sc::Layout::takeAt)
498 .method("removeItem", &sc::Layout::removeItem)
499 .method("clear", &sc::Layout::clear);
501 NasalBoxLayout::init("canvas.BoxLayout")
502 .bases<NasalLayout>()
503 .method("setSpacing", &sc::BoxLayout::setSpacing)
504 .method("addItem", &f_boxLayoutAddItem)
505 .method("addSpacing", &sc::BoxLayout::addSpacing)
506 .method("addStretch", &f_boxLayoutAddStretch)
507 .method("insertItem", &f_boxLayoutInsertItem)
508 .method("insertSpacing", &sc::BoxLayout::insertSpacing)
509 .method("insertStretch", &f_boxLayoutInsertStretch);
511 canvas_module.createHash("HBoxLayout")
512 .set("new", &f_newAsBase<sc::HBoxLayout, sc::BoxLayout>);
513 canvas_module.createHash("VBoxLayout")
514 .set("new", &f_newAsBase<sc::VBoxLayout, sc::BoxLayout>);
516 //----------------------------------------------------------------------------
519 NasalWindow::init("canvas.Window")
520 .bases<NasalElement>()
521 .member("_node_ghost", &elementGetNode<sc::Window>)
522 .method("_getCanvasDecoration", &sc::Window::getCanvasDecoration)
523 .method("setLayout", &sc::Window::setLayout);
525 canvas_module.set("_newWindowGhost", f_createWindow);
526 canvas_module.set("_getDesktopGhost", f_getDesktop);