]> git.mxchange.org Git - flightgear.git/blobdiff - src/Scripting/NasalCanvas.cxx
Canvas: store data inside canvas and elements.
[flightgear.git] / src / Scripting / NasalCanvas.cxx
index dba2851476b818302d2608296f97865d270e29a8..2e0b0c034ebc79a2c59cacbfff0a3ddda292f70e 100644 (file)
 #  include "config.h"
 #endif
 
-#include <memory>
-#include <string.h>
-
 #include "NasalCanvas.hxx"
 #include <Canvas/canvas_mgr.hxx>
+#include <Canvas/gui_mgr.hxx>
 #include <Main/globals.hxx>
+#include <Scripting/NasalSys.hxx>
 
-//#include <boost/python.hpp>
-#include <boost/foreach.hpp>
-#include <boost/function.hpp>
-#include <boost/utility/enable_if.hpp>
-#include <boost/algorithm/string/case_conv.hpp>
-#include <boost/make_shared.hpp>
 #include <osgGA/GUIEventAdapter>
 
 #include <simgear/sg_inlines.h>
 
 #include <simgear/canvas/Canvas.hxx>
+#include <simgear/canvas/CanvasWindow.hxx>
 #include <simgear/canvas/elements/CanvasElement.hxx>
+#include <simgear/canvas/elements/CanvasText.hxx>
+#include <simgear/canvas/events/CustomEvent.hxx>
+#include <simgear/canvas/events/MouseEvent.hxx>
 
-static naRef elementPrototype;
-static naRef propsNodePrototype;
+#include <simgear/nasal/cppbind/from_nasal.hxx>
+#include <simgear/nasal/cppbind/to_nasal.hxx>
+#include <simgear/nasal/cppbind/NasalHash.hxx>
+#include <simgear/nasal/cppbind/Ghost.hxx>
 
 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
 
-static void hashset(naContext c, naRef hash, const char* key, naRef val)
-{
-  naRef s = naNewString(c);
-  naStr_fromdata(s, (char*)key, strlen(key));
-  naHash_set(hash, s, val);
-}
+namespace sc = simgear::canvas;
 
-template<class T>
-typename boost::enable_if<boost::is_arithmetic<T>, naRef>::type
-convertToNasal(T val)
+template<class Element>
+naRef elementGetNode(Element& element, naContext c)
 {
-  return naNum(val);
+  return propNodeGhostCreate(c, element.getProps());
 }
 
-//void initCanvasPython()
-//{
-//  using namespace boost::python;
-//  class_<simgear::canvas::Canvas>("Canvas");
-//}
+typedef nasal::Ghost<sc::EventPtr> NasalEvent;
+typedef nasal::Ghost<sc::CustomEventPtr> NasalCustomEvent;
+typedef nasal::Ghost<sc::MouseEventPtr> NasalMouseEvent;
 
-namespace sc = simgear::canvas;
+struct CustomEventDetailWrapper;
+typedef SGSharedPtr<CustomEventDetailWrapper> CustomEventDetailPtr;
+typedef nasal::Ghost<CustomEventDetailPtr> NasalCustomEventDetail;
 
-naRef canvasGetTexture(naContext c, sc::Canvas* canvas)
-{
-  //{ parents : [Node], _g : node }
-  naRef parents = naNewVector(c);
-  naVec_append(parents, propsNodePrototype);
+typedef nasal::Ghost<simgear::PropertyBasedElementPtr> NasalPropertyBasedElement;
+typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
+typedef nasal::Ghost<sc::ElementPtr> NasalElement;
+typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
+typedef nasal::Ghost<sc::TextPtr> NasalText;
+typedef nasal::Ghost<sc::WindowWeakPtr> NasalWindow;
 
-  naRef node = naNewHash(c);
-  hashset(c, node, "parents", parents);
-  hashset(c, node, "_g", propNodeGhostCreate(c, canvas->getProps()));
+naRef to_nasal_helper(naContext c, const osg::BoundingBox& bb)
+{
+  std::vector<float> bb_vec(4);
+  bb_vec[0] = bb._min.x();
+  bb_vec[1] = bb._min.y();
+  bb_vec[2] = bb._max.x();
+  bb_vec[3] = bb._max.y();
 
-  return node;
+  return nasal::to_nasal(c, bb_vec);
 }
 
-namespace nasal
+SGPropertyNode* from_nasal_helper(naContext c, naRef ref, SGPropertyNode**)
 {
+  SGPropertyNode* props = ghostToPropNode(ref);
+  if( !props )
+    naRuntimeError(c, "Not a SGPropertyNode ghost.");
 
-  template<class T>
-  struct class_traits
-  {
-    typedef boost::false_type::type is_shared;
-    typedef T raw_type;
-  };
-
-  template<class T>
-  struct class_traits<boost::shared_ptr<T> >
-  {
-    typedef boost::true_type::type is_shared;
-    typedef T raw_type;
-  };
-
-  template<class T>
-  struct class_traits<osg::ref_ptr<T> >
-  {
-    typedef boost::true_type::type is_shared;
-    typedef T raw_type;
-  };
-
-  template<class T>
-  struct class_traits<SGSharedPtr<T> >
-  {
-    typedef boost::true_type::type is_shared;
-    typedef T raw_type;
-  };
-
-  template<class T>
-  struct SharedPointerPolicy
-  {
-    typedef typename class_traits<T>::raw_type raw_type;
-
-    /**
-     * Create a shared pointer on the heap to handle the reference counting for
-     * the passed shared pointer while it is used in Nasal space.
-     */
-    static T* createInstance(const T& ptr)
-    {
-      return new T(ptr);
-    }
-
-    static raw_type* getRawPtr(void* ptr)
-    {
-      return static_cast<T*>(ptr)->get();
-    }
-  };
-
-  template<class T>
-  struct RawPointerPolicy
-  {
-    typedef typename class_traits<T>::raw_type raw_type;
-
-    static T* createInstance()
-    {
-      return new T();
-    }
-
-    static raw_type* getRawPtr(void* ptr)
-    {
-      BOOST_STATIC_ASSERT((boost::is_same<raw_type, T>::value));
-      return static_cast<T*>(ptr);
-    }
-  };
-
-  class class_metadata
-  {
-    public:
-      void addNasalBase(const naRef& parent)
-      {
-        assert( naIsHash(parent) );
-        _parents.push_back(parent);
-      }
-
-    protected:
-      const std::string             _name;
-      naGhostType                   _ghost_type;
-      std::vector<class_metadata*>  _base_classes;
-      std::vector<naRef>            _parents;
-
-      explicit class_metadata(const std::string& name):
-        _name(name)
-      {
-
-      }
-
-      void addBaseClass(class_metadata* base)
-      {
-        assert(base);
-        std::cout << _name << ": addBase(" << base->_name << ")" << std::endl;
-        _base_classes.push_back(base);
-      }
-
-      naRef getParents(naContext c)
-      {
-        naRef parents = naNewVector(c);
-        for(size_t i = 0; i < _parents.size(); ++i)
-          naVec_append(parents, _parents[i]);
-        return parents;
-      }
-  };
-
-  /**
-   * Class for exposing C++ objects to Nasal
-   */
-  template<class T>
-  class class_:
-    public class_metadata,
-    protected boost::mpl::if_< typename class_traits<T>::is_shared,
-                               SharedPointerPolicy<T>,
-                               RawPointerPolicy<T> >::type
-  {
-    private:
-      typedef typename class_traits<T>::raw_type raw_type;
-
-    public:
-
-      typedef boost::function<naRef(naContext c, raw_type*)> getter_t;
-      typedef std::map<std::string, getter_t> GetterMap;
-
-      static class_& init(const std::string& name)
-      {
-        getSingletonHolder().reset( new class_(name) );
-        return *getSingletonPtr();
-      }
-
-      static naRef f_create(naContext c, naRef me, int argc, naRef* args)
-      {
-        return create(c);
-      }
-
-      static naRef create( naContext c )
-      {
-        return makeGhost(c, class_::createInstance());
-      }
-
-      // TODO use variadic template when supporting C++11
-      template<class A1>
-      static naRef create( naContext c, const A1& a1 )
-      {
-        return makeGhost(c, class_::createInstance(a1));
-      }
-
-      class_& bases(const naRef& parent)
-      {
-        addNasalBase(parent);
-        return *this;
-      }
-
-      template<class Base>
-      typename boost::enable_if
-        < boost::is_convertible<Base, class_metadata>,
-          class_
-        >::type&
-      bases()
-      {
-        Base* base = Base::getSingletonPtr();
-        addBaseClass( base );
-
-        // Replace any getter that is not available in the current class.
-        // TODO check if this is the correct behavior of function overriding
-        const typename Base::GetterMap& base_getter = base->getGetter();
-        for( typename Base::GetterMap::const_iterator getter =
-               base_getter.begin();
-               getter != base_getter.end();
-             ++getter )
-        {
-          if( _getter.find(getter->first) == _getter.end() )
-            _getter.insert( *getter );
-        }
-
-        return *this;
-      }
-
-      template<class Type>
-      typename boost::enable_if_c
-        < !boost::is_convertible< Type, class_metadata>::value,
-          class_
-        >::type&
-      bases()
-      {
-        return bases< class_<Type> >();
-      }
-
-      template<class Var>
-      class_& def( const std::string& field,
-                   Var (raw_type::*getter)() const )
-      {
-        _getter[field] = boost::bind( &convertToNasal<Var>,
-                                      boost::bind(getter, _2) );
-        return *this;
-      }
-
-      class_& def_readonly( const std::string& field,
-                            getter_t getter )
-      {
-        _getter[field] = getter;
-        return *this;
-      }
-
-      /**
-       * Data member
-       */
-      template<class Var>
-      class_& def_readonly( const std::string& field,
-                            Var raw_type::*var )
-      {
-        _getter[field] = boost::bind( &convertToNasal<Var>,
-                                      boost::bind(var, _2) );
-        return *this;
-      }
-
-      static class_* getSingletonPtr()
-      {
-        return getSingletonHolder().get();
-      }
-
-      const GetterMap& getGetter() const
-      {
-        return _getter;
-      }
-
-    private:
-
-      typedef std::auto_ptr<class_> class_ptr;
-      GetterMap _getter;
-
-      explicit class_(const std::string& name):
-        class_metadata( name )
-      {
-        _ghost_type.destroy = &destroyGhost;
-        _ghost_type.name = _name.c_str();
-        _ghost_type.get_member = &getMember;
-        _ghost_type.set_member = 0;
-      }
-
-      static class_ptr& getSingletonHolder()
-      {
-        static class_ptr instance;
-        return instance;
-      }
-
-      static naRef makeGhost(naContext c, void *ptr)
-      {
-        std::cout << "makeGhost    " << ptr << std::endl;
-        return naNewGhost2(c, &getSingletonPtr()->_ghost_type, ptr);
-      }
-
-      static void destroyGhost(void *ptr)
-      {
-        std::cout << "destroyGhost " << ptr << std::endl;
-        delete (T*)ptr;
-      }
-
-      static const char* getMember(naContext c, void* g, naRef field, naRef* out)
-      {
-        const std::string key = naStr_data(field);
-        if( key == "parents" )
-        {
-          *out = getSingletonPtr()->getParents(c);
-          return "";
-        }
-
-        typename GetterMap::iterator getter =
-          getSingletonPtr()->_getter.find(key);
-
-        if( getter == getSingletonPtr()->_getter.end() )
-          return 0;
-
-        *out = getter->second(c, class_::getRawPtr(g));
-        return "";
-      }
-  };
+  return props;
 }
 
-struct Base
+sc::CanvasWeakPtr from_nasal_helper(naContext c, naRef ref, sc::CanvasWeakPtr const*)
 {
-  int getInt() const
-  {
-    return 8;
-  }
-};
+  return nasal::from_nasal<sc::CanvasPtr>(c, ref);
+}
 
-struct Test:
-  public Base
+CanvasMgr& requireCanvasMgr(naContext c)
 {
-  Test(): x(1) {}
-  int x;
-};
+  CanvasMgr* canvas_mgr =
+    static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
+  if( !canvas_mgr )
+    naRuntimeError(c, "Failed to get Canvas subsystem");
 
-typedef nasal::class_<sc::CanvasPtr> NasalCanvas;
+  return *canvas_mgr;
+}
 
-void initCanvas()
+GUIMgr& requireGUIMgr(naContext c)
 {
+  GUIMgr* mgr =
+    static_cast<GUIMgr*>(globals->get_subsystem("CanvasGUI"));
+  if( !mgr )
+    naRuntimeError(c, "Failed to get CanvasGUI subsystem");
 
-  NasalCanvas::init("Canvas")
-    .def_readonly("texture", &canvasGetTexture)
-    .def("size_x", &sc::Canvas::getSizeX)
-    .def("size_y", &sc::Canvas::getSizeY);
-  nasal::class_<sc::ElementPtr>::init("canvas.Element");
-  nasal::class_<sc::GroupPtr>::init("canvas.Group")
-    .bases<sc::ElementPtr>();
-
-  nasal::class_<Base>::init("BaseClass")
-    .def("int", &Base::getInt);
-  nasal::class_<Test>::init("TestClass")
-    .bases<Base>()
-    .def_readonly("x", &Test::x);
+  return *mgr;
 }
 
 /**
- * A Nasal Hash
+ * Create new Canvas and get ghost for it.
  */
-class Hash
+static naRef f_createCanvas(const nasal::CallContext& ctx)
 {
-  public:
-
-    /**
-     * Initialize from an existing Nasal Hash
-     *
-     * @param hash  Existing Nasal Hash
-     * @param c     Nasal context
-     */
-    Hash(const naRef& hash, naContext c):
-      _hash(hash),
-      _context(c)
-    {
-      assert( naIsHash(_hash) );
-    }
-
-    void set(const std::string& name, naRef val)
-    {
-      naHash_set(_hash, stringToNasal(name), val);
-    }
-
-    void set(const std::string& name, naCFunction func)
-    {
-      set(name, naNewFunc(_context, naNewCCode(_context, func)));
-    }
+  return NasalCanvas::create(ctx.c, requireCanvasMgr(ctx.c).createCanvas());
+}
 
-    void set(const std::string& name, const std::string& str)
-    {
-      set(name, stringToNasal(str));
-    }
+/**
+ * Create new Window and get ghost for it.
+ */
+static naRef f_createWindow(const nasal::CallContext& ctx)
+{
+  return NasalWindow::create
+  (
+    ctx.c,
+    requireGUIMgr(ctx.c).createWindow( ctx.getArg<std::string>(0) )
+  );
+}
 
-    void set(const std::string& name, double num)
-    {
-      set(name, naNum(num));
-    }
+/**
+ * Get ghost for existing Canvas.
+ */
+static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
+{
+  nasal::CallContext ctx(c, argc, args);
+  SGPropertyNode& props = *ctx.requireArg<SGPropertyNode*>(0);
+  CanvasMgr& canvas_mgr = requireCanvasMgr(c);
 
-    /**
-     * Create a new child hash (module)
-     *
-     * @param name  Name of the new hash inside this hash
-     */
-    Hash createHash(const std::string& name)
-    {
-      naRef hash = naNewHash(_context);
-      set(name, hash);
-      return Hash(hash, _context);
-    }
+  sc::CanvasPtr canvas;
+  if( canvas_mgr.getPropertyRoot() == props.getParent() )
+  {
+    // get a canvas specified by its root node
+    canvas = canvas_mgr.getCanvas( props.getIndex() );
+    if( !canvas || canvas->getProps() != &props )
+      return naNil();
+  }
+  else
+  {
+    // get a canvas by name
+    if( props.hasValue("name") )
+      canvas = canvas_mgr.getCanvas( props.getStringValue("name") );
+    else if( props.hasValue("index") )
+      canvas = canvas_mgr.getCanvas( props.getIntValue("index") );
+  }
 
-  protected:
-    naRef _hash;
-    naContext _context;
+  return NasalCanvas::create(c, canvas);
+}
 
-    naRef stringToNasal(const std::string& str)
-    {
-      naRef s = naNewString(_context);
-      naStr_fromdata(s, str.c_str(), str.size());
-      return s;
-    }
-};
+naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
+{
+  return NasalGroup::create
+  (
+    ctx.c,
+    canvas.createGroup( ctx.getArg<std::string>(0) )
+  );
+}
 
 /**
- * Class for exposing C++ objects to Nasal
+ * Get group containing all gui windows
  */
-template<class T, class Derived>
-class NasalObject
+naRef f_getDesktop(naContext c, naRef me, int argc, naRef* args)
 {
-  public:
-    // TODO use variadic template when supporting C++11
-    template<class A1>
-    static naRef create( naContext c, const A1& a1 )
-    {
-      return makeGhost(c, new T(a1));
-    }
-
-    template<class A1, class A2>
-    static naRef create( naContext c, const A1& a1,
-                                      const A2& a2 )
-    {
-      return makeGhost(c, new T(a1, a2));
-    }
-
-    template<class A1, class A2, class A3>
-    static naRef create( naContext c, const A1& a1,
-                                      const A2& a2,
-                                      const A3& a3 )
-    {
-      return makeGhost(c, new T(a1, a2, a3));
-    }
+  return NasalGroup::create(c, requireGUIMgr(c).getDesktop());
+}
 
-    template<class A1, class A2, class A3, class A4>
-    static naRef create( naContext c, const A1& a1,
-                                      const A2& a2,
-                                      const A3& a3,
-                                      const A4& a4 )
-    {
-      return makeGhost(c, new T(a1, a2, a3, a4));
-    }
+naRef f_groupCreateChild(sc::Group& group, const nasal::CallContext& ctx)
+{
+  return NasalElement::create
+  (
+    ctx.c,
+    group.createChild( ctx.requireArg<std::string>(0),
+                       ctx.getArg<std::string>(1) )
+  );
+}
 
-    template<class A1, class A2, class A3, class A4, class A5>
-    static naRef create( naContext c, const A1& a1,
-                                      const A2& a2,
-                                      const A3& a3,
-                                      const A4& a4,
-                                      const A5& a5 )
-    {
-      return makeGhost(c, new T(a1, a2, a3, a4, a5));
-    }
+naRef f_groupGetChild(sc::Group& group, const nasal::CallContext& ctx)
+{
+  return NasalElement::create
+  (
+    ctx.c,
+    group.getChild( ctx.requireArg<SGPropertyNode*>(0) )
+  );
+}
 
-    // TODO If you need more arguments just do some copy&paste :)
+naRef f_groupGetElementById(sc::Group& group, const nasal::CallContext& ctx)
+{
+  return NasalElement::create
+  (
+    ctx.c,
+    group.getElementById( ctx.requireArg<std::string>(0) )
+  );
+}
 
-    static Derived& getInstance()
-    {
-      static Derived instance;
-      return instance;
-    }
+static void propElementSetData( simgear::PropertyBasedElement& el,
+                                const std::string& name,
+                                naContext c,
+                                naRef ref )
+{
+  if( naIsNil(ref) )
+    return el.removeDataProp(name);
 
-    void setParent(const naRef& parent)
-    {
-      // TODO check if we need to take care of reference counting/gc
-      _parents.resize(1);
-      _parents[0] = parent;
-    }
+  std::string val = nasal::from_nasal<std::string>(c, ref);
 
-  protected:
+  char* end = NULL;
 
-    // TODO switch to boost::/std::function (with C++11 lambdas this can make
-    //      adding setters easier and shorter)
-    typedef naRef (Derived::*getter_t)(naContext, const T&);
-    typedef std::map<std::string, getter_t> GetterMap;
+  long val_long = strtol(val.c_str(), &end, 10);
+  if( !*end )
+    return el.setDataProp(name, val_long);
 
-    const std::string   _ghost_name;
-    std::vector<naRef>  _parents;
-    GetterMap           _getter;
+  double val_double = strtod(val.c_str(), &end);
+  if( !*end )
+    return el.setDataProp(name, val_double);
 
-    NasalObject(const std::string& ghost_name):
-      _ghost_name( ghost_name )
-    {
-      _ghost_type.destroy = &destroyGhost;
-      _ghost_type.name = _ghost_name.c_str();
-      _ghost_type.get_member = &Derived::getMember;
-      _ghost_type.set_member = 0;
+  el.setDataProp(name, val);
+}
 
-      _getter["parents"] = &NasalObject::getParents;
-    }
+/**
+ * Accessor for HTML5 data properties.
+ *
+ * # set single property:
+ * el.data("myKey", 5);
+ *
+ * # set multiple properties
+ * el.data({myProp1: 12, myProp2: "test"});
+ *
+ * # get value of properties
+ * el.data("myKey");   # 5
+ * el.data("myProp2"); # "test"
+ *
+ * # remove a single property
+ * el.data("myKey", nil);
+ *
+ * # remove multiple properties
+ * el.data({myProp1: nil, myProp2: nil});
+ *
+ * # set and remove multiple properties
+ * el.data({newProp: "some text...", removeProp: nil});
+ *
+ *
+ * @see http://api.jquery.com/data/
+ */
+static naRef f_propElementData( simgear::PropertyBasedElement& el,
+                                const nasal::CallContext& ctx )
+{
+  if( ctx.isHash(0) )
+  {
+    // Add/delete properties given as hash
+    nasal::Hash obj = ctx.requireArg<nasal::Hash>(0);
+    for(nasal::Hash::iterator it = obj.begin(); it != obj.end(); ++it)
+      propElementSetData(el, it->getKey(), ctx.c, it->getValue<naRef>());
 
-    naRef getParents(naContext c, const T&)
-    {
-      naRef parents = naNewVector(c);
-      for(size_t i = 0; i < _parents.size(); ++i)
-        naVec_append(parents, _parents[i]);
-      return parents;
-    }
+    return ctx.to_nasal(&el);
+  }
 
-    static naRef makeGhost(naContext c, void *ptr)
+  std::string name = ctx.getArg<std::string>(0);
+  if( !name.empty() )
+  {
+    if( ctx.argc == 1 )
     {
-      std::cout << "create  " << ptr << std::endl;
-      return naNewGhost2(c, &(getInstance()._ghost_type), ptr);
-    }
+      // name + additional argument -> add/delete property
+      SGPropertyNode* node = el.getDataProp<SGPropertyNode*>(name);
+      if( !node )
+        return naNil();
 
-    static void destroyGhost(void *ptr)
-    {
-      std::cout << "destroy " << ptr << std::endl;
-      delete (T*)ptr;
+      return ctx.to_nasal( node->getStringValue() );
     }
-
-    static const char* getMember(naContext c, void* g, naRef field, naRef* out)
+    else
     {
-      typename GetterMap::iterator getter =
-        getInstance()._getter.find(naStr_data(field));
-
-      if( getter == getInstance()._getter.end() )
-        return 0;
-
-      *out = (getInstance().*getter->second)(c, *static_cast<T*>(g));
-      return "";
+      // only name -> get property
+      propElementSetData(el, name, ctx.c, ctx.requireArg<naRef>(1));
+      return ctx.to_nasal(&el);
     }
+  }
 
-  private:
-
-    naGhostType _ghost_type;
-
-};
+  return naNil();
+}
 
-static naRef stringToNasal(naContext c, const std::string& s)
+template<int Mask>
+naRef f_eventGetModifier(sc::MouseEvent& event, naContext)
 {
-    return naStr_fromdata(naNewString(c),
-                   const_cast<char *>(s.c_str()),
-                   s.length());
+  return naNum((event.getModifiers() & Mask) != 0);
 }
 
-typedef osg::ref_ptr<osgGA::GUIEventAdapter> GUIEventPtr;
-
-class NasalCanvasEvent:
-  public NasalObject<GUIEventPtr, NasalCanvasEvent>
+static naRef f_createCustomEvent(const nasal::CallContext& ctx)
 {
-  public:
-
-    NasalCanvasEvent():
-      NasalObject("CanvasEvent")
-    {
-      _getter["type"] = &NasalCanvasEvent::getEventType;
-    }
-
-    naRef getEventType(naContext c, const GUIEventPtr& event)
-    {
-#define RET_EVENT_STR(type, str)\
-  case osgGA::GUIEventAdapter::type:\
-    return stringToNasal(c, str);
-
-      switch( event->getEventType() )
-      {
-        RET_EVENT_STR(PUSH,         "push");
-        RET_EVENT_STR(RELEASE,      "release");
-        RET_EVENT_STR(DOUBLECLICK,  "double-click");
-        RET_EVENT_STR(DRAG,         "drag");
-        RET_EVENT_STR(MOVE,         "move");
-        RET_EVENT_STR(SCROLL,       "scroll");
-        RET_EVENT_STR(KEYUP,        "key-up");
-        RET_EVENT_STR(KEYDOWN,      "key-down");
-
-#undef RET_EVENT_STR
-
-        default:
-          return naNil();
-      }
-    }
-};
-
-//using simgear::canvas::CanvasPtr;
-//
-///**
-// * Expose Canvas to Nasal
-// */
-//class NasalCanvas:
-//  public NasalObject<CanvasPtr, NasalCanvas>
-//{
-//  public:
-//
-//    NasalCanvas():
-//      NasalObject("Canvas")
-//    {
-//      _getter["texture"] = &NasalCanvas::getTexture;
-//      _getter["size_x"] = &NasalCanvas::getSizeX;
-//      _getter["size_y"] = &NasalCanvas::getSizeY;
-//    }
-//
-//    static naRef f_create(naContext c, naRef me, int argc, naRef* args)
-//    {
-//      std::cout << "NasalCanvas::create" << std::endl;
-//
-//      CanvasMgr* canvas_mgr =
-//        static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
-//      if( !canvas_mgr )
-//        return naNil();
-//
-//      return create(c, canvas_mgr->createCanvas());
-//    }
-//
-//    static naRef f_setPrototype(naContext c, naRef me, int argc, naRef* args)
-//    {
-//      if( argc != 1 || !naIsHash(args[0]) )
-//        naRuntimeError(c, "Invalid argument(s)");
-//
-//      getInstance().setParent(args[0]);
-//
-//      return naNil();
-//    }
-//
-//    naRef getTexture(naContext c, const CanvasPtr& canvas)
-//    {
-//      //{ parents : [Node], _g : node }
-//      naRef parents = naNewVector(c);
-//      naVec_append(parents, propsNodePrototype);
-//
-//      naRef node = naNewHash(c);
-//      hashset(c, node, "parents", parents);
-//      hashset(c, node, "_g", propNodeGhostCreate(c, canvas->getProps()));
-//
-//      return node;
-//    }
-//
-//    naRef getSizeX(naContext c, const CanvasPtr& canvas)
-//    {
-//      return naNum(canvas->getSizeX());
-//    }
-//
-//    naRef getSizeY(naContext c, const CanvasPtr& canvas)
-//    {
-//      return naNum(canvas->getSizeY());
-//    }
-//};
-
-static void elementGhostDestroy(void* g);
+  std::string const& type = ctx.requireArg<std::string>(0);
+  if( type.empty() )
+    return naNil();
 
-static const char* elementGhostGetMember(naContext c, void* g, naRef field, naRef* out);
-static void elementGhostSetMember(naContext c, void* g, naRef field, naRef value);
-naGhostType ElementGhostType = { elementGhostDestroy, "canvas.element", 
-    elementGhostGetMember, elementGhostSetMember };
+  simgear::StringMap detail;
+  if( ctx.isHash(1) )
+  {
+    nasal::Hash const& cfg = ctx.requireArg<nasal::Hash>(1);
+    naRef na_detail = cfg.get("detail");
+    if( naIsHash(na_detail) )
+      detail = ctx.from_nasal<simgear::StringMap>(na_detail);
+  }
 
-static simgear::canvas::Element* elementGhost(naRef r)
-{
-  if (naGhost_type(r) == &ElementGhostType)
-    return (simgear::canvas::Element*) naGhost_ptr(r);
-  return 0;
+  return NasalCustomEvent::create(
+    ctx.c,
+    sc::CustomEventPtr(new sc::CustomEvent(type, detail))
+  );
 }
 
-static void elementGhostDestroy(void* g)
+struct CustomEventDetailWrapper:
+  public SGReferenced
 {
-}
+  sc::CustomEventPtr _event;
 
-static const char* eventGhostGetMember(naContext c, void* g, naRef field, naRef* out)
-{
-  const char* fieldName = naStr_data(field);
-  osgGA::GUIEventAdapter* gea = (osgGA::GUIEventAdapter*) g;
-
-  if (!strcmp(fieldName, "windowX")) *out = naNum(gea->getWindowX());
-  else if (!strcmp(fieldName, "windowY")) *out = naNum(gea->getWindowY());
-  else if (!strcmp(fieldName, "time")) *out = naNum(gea->getTime());
-  else if (!strcmp(fieldName, "button")) *out = naNum(gea->getButton());
-  else {
-    return 0;
-  }
-  
-  return "";
-}
+  CustomEventDetailWrapper(const sc::CustomEventPtr& event):
+    _event(event)
+  {
 
-static const char* elementGhostGetMember(naContext c, void* g, naRef field, naRef* out)
-{
-  const char* fieldName = naStr_data(field);
-  simgear::canvas::Element* e = (simgear::canvas::Element*) g;
-  SG_UNUSED(e);
-  
-  if (!strcmp(fieldName, "parents")) {
-    *out = naNewVector(c);
-    naVec_append(*out, elementPrototype);
-  } else {
-    return 0;
   }
-  
-  return "";
-}
-
-static void elementGhostSetMember(naContext c, void* g, naRef field, naRef value)
-{
-  const char* fieldName = naStr_data(field);
-  simgear::canvas::Element* e = (simgear::canvas::Element*) g;
-  SG_UNUSED(fieldName);
-  SG_UNUSED(e);
-}
 
+  bool _get( const std::string& key,
+             std::string& value_out ) const
+  {
+    if( !_event )
+      return false;
 
-static naRef f_canvas_getElement(naContext c, naRef me, int argc, naRef* args)
-{
-//  simgear::canvas::Canvas* cvs = canvasGhost(me);
-//  if (!cvs) {
-//    naRuntimeError(c, "canvas.getElement called on non-canvas object");
-//  }
-  
-  return naNil();
-}
+    simgear::StringMap::const_iterator it = _event->detail.find(key);
+    if( it == _event->detail.end() )
+      return false;
 
-static naRef f_element_addButtonCallback(naContext c, naRef me, int argc, naRef* args)
-{
-  simgear::canvas::Element* e = elementGhost(me);
-  if (!e) {
-    naRuntimeError(c, "element.addButtonCallback called on non-canvas-element object");
+    value_out = it->second;
+    return true;
   }
-  
-  return naNil();
-}
 
-static naRef f_element_addDragCallback(naContext c, naRef me, int argc, naRef* args)
-{
-  simgear::canvas::Element* e = elementGhost(me);
-  if (!e) {
-    naRuntimeError(c, "element.addDragCallback called on non-canvas-element object");
-  }
-  
-  return naNil();
-}
+  bool _set( const std::string& key,
+             const std::string& value )
+  {
+    if( !_event )
+      return false;
 
-static naRef f_element_addMoveCallback(naContext c, naRef me, int argc, naRef* args)
-{
-  simgear::canvas::Element* e = elementGhost(me);
-  if (!e) {
-    naRuntimeError(c, "element.addMoveCallback called on non-canvas-element object");
+    _event->detail[ key ] = value;
+    return true;
   }
-  
-  return naNil();
-}
+};
 
-static naRef f_element_addScrollCallback(naContext c, naRef me, int argc, naRef* args)
+static naRef f_customEventGetDetail( sc::CustomEvent& event,
+                                     naContext c )
 {
-  simgear::canvas::Element* e = elementGhost(me);
-  if (!e) {
-    naRuntimeError(c, "element.addScrollCallback called on non-canvas-element object");
-  }
-  
-  return naNil();
+  return nasal::to_nasal(
+    c,
+    CustomEventDetailPtr(new CustomEventDetailWrapper(&event))
+  );
 }
 
-static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
+naRef to_nasal_helper(naContext c, const sc::ElementWeakPtr& el)
 {
-  std::cout << "f_createCanvas" << std::endl;
-
-  CanvasMgr* canvas_mgr =
-    static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
-  if( !canvas_mgr )
-    return naNil();
-
-  return NasalCanvas::create(c, canvas_mgr->createCanvas());
+  return NasalElement::create(c, el.lock());
 }
 
-static naRef f_setCanvasPrototype(naContext c, naRef me, int argc, naRef* args)
+naRef to_nasal_helper(naContext c, const sc::CanvasWeakPtr& canvas)
 {
-  if( argc != 1 || !naIsHash(args[0]) )
-    naRuntimeError(c, "Invalid argument(s)");
-
-  NasalCanvas::getSingletonPtr()->addNasalBase(args[0]);
-
-  return naNil();
+  return NasalCanvas::create(c, canvas.lock());
 }
 
-naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
+naRef initNasalCanvas(naRef globals, naContext c)
 {
-  naRef props_module = naHash_cget(globals, (char*)"props");
-  if( naIsNil(props_module) )
-    std::cerr << "No props module" << std::endl;
-
-  propsNodePrototype = naHash_cget(props_module, (char*)"Node");
-  if( naIsNil(propsNodePrototype) )
-    std::cerr << "Failed to get props.Node" << std::endl;
-
-      /*naNewHash(c);
-    hashset(c, gcSave, "canvasProto", canvasPrototype);
-  
-    hashset(c, canvasPrototype, "getElement", naNewFunc(c, naNewCCode(c, f_canvas_getElement)));*/
-    // set any event methods
-  
-    elementPrototype = naNewHash(c);
-    hashset(c, gcSave, "elementProto", elementPrototype);
+  using osgGA::GUIEventAdapter;
+  NasalEvent::init("canvas.Event")
+    .member("type", &sc::Event::getTypeString)
+    .member("target", &sc::Event::getTarget)
+    .member("currentTarget", &sc::Event::getCurrentTarget)
+    .method("stopPropagation", &sc::Event::stopPropagation);
+
+  NasalCustomEvent::init("canvas.CustomEvent")
+    .bases<NasalEvent>()
+    .member("detail", &f_customEventGetDetail, &sc::CustomEvent::setDetail);
+  NasalCustomEventDetail::init("canvas.CustomEventDetail")
+    ._get(&CustomEventDetailWrapper::_get)
+    ._set(&CustomEventDetailWrapper::_set);
+
+  NasalMouseEvent::init("canvas.MouseEvent")
+    .bases<NasalEvent>()
+    .member("screenX", &sc::MouseEvent::getScreenX)
+    .member("screenY", &sc::MouseEvent::getScreenY)
+    .member("clientX", &sc::MouseEvent::getClientX)
+    .member("clientY", &sc::MouseEvent::getClientY)
+    .member("localX", &sc::MouseEvent::getLocalX)
+    .member("localY", &sc::MouseEvent::getLocalY)
+    .member("deltaX", &sc::MouseEvent::getDeltaX)
+    .member("deltaY", &sc::MouseEvent::getDeltaY)
+    .member("button", &sc::MouseEvent::getButton)
+    .member("buttons", &sc::MouseEvent::getButtonMask)
+    .member("modifiers", &sc::MouseEvent::getModifiers)
+    .member("ctrlKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_CTRL>)
+    .member("shiftKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_SHIFT>)
+    .member("altKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_ALT>)
+    .member("metaKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_META>)
+    .member("click_count", &sc::MouseEvent::getCurrentClickCount);
+
+  NasalPropertyBasedElement::init("PropertyBasedElement")
+    .method("data", &f_propElementData);
+  NasalCanvas::init("Canvas")
+    .bases<NasalPropertyBasedElement>()
+    .member("_node_ghost", &elementGetNode<sc::Canvas>)
+    .member("size_x", &sc::Canvas::getSizeX)
+    .member("size_y", &sc::Canvas::getSizeY)
+    .method("_createGroup", &f_canvasCreateGroup)
+    .method("_getGroup", &sc::Canvas::getGroup)
+    .method("addEventListener", &sc::Canvas::addEventListener)
+    .method("dispatchEvent", &sc::Canvas::dispatchEvent);
+  NasalElement::init("canvas.Element")
+    .bases<NasalPropertyBasedElement>()
+    .member("_node_ghost", &elementGetNode<sc::Element>)
+    .method("_getParent", &sc::Element::getParent)
+    .method("_getCanvas", &sc::Element::getCanvas)
+    .method("addEventListener", &sc::Element::addEventListener)
+    .method("dispatchEvent", &sc::Element::dispatchEvent)
+    .method("getBoundingBox", &sc::Element::getBoundingBox)
+    .method("getTightBoundingBox", &sc::Element::getTightBoundingBox);
+  NasalGroup::init("canvas.Group")
+    .bases<NasalElement>()
+    .method("_createChild", &f_groupCreateChild)
+    .method("_getChild", &f_groupGetChild)
+    .method("_getElementById", &f_groupGetElementById);
+  NasalText::init("canvas.Text")
+    .bases<NasalElement>()
+    .method("getNearestCursor", &sc::Text::getNearestCursor);
+
+  NasalWindow::init("canvas.Window")
+    .bases<NasalElement>()
+    .member("_node_ghost", &elementGetNode<sc::Window>)
+    .method("_getCanvasDecoration", &sc::Window::getCanvasDecoration);
     
-    hashset(c, elementPrototype, "addButtonCallback", naNewFunc(c, naNewCCode(c, f_element_addButtonCallback)));
-    hashset(c, elementPrototype, "addDragCallback", naNewFunc(c, naNewCCode(c, f_element_addDragCallback)));
-    hashset(c, elementPrototype, "addMoveCallback", naNewFunc(c, naNewCCode(c, f_element_addMoveCallback)));
-    hashset(c, elementPrototype, "addScrollCallback", naNewFunc(c, naNewCCode(c, f_element_addScrollCallback)));
-
-  Hash globals_module(globals, c),
-       canvas_module = globals_module.createHash("canvas");
-
-  canvas_module.set("_new", f_createCanvas);
-  canvas_module.set("_setPrototype", f_setCanvasPrototype);
-  canvas_module.set("testClass", nasal::class_<Test>::f_create);
+  nasal::Hash globals_module(globals, c),
+              canvas_module = globals_module.createHash("canvas");
 
-  initCanvas();
+  canvas_module.set("_newCanvasGhost", f_createCanvas);
+  canvas_module.set("_newWindowGhost", f_createWindow);
+  canvas_module.set("_getCanvasGhost", f_getCanvas);
+  canvas_module.set("_getDesktopGhost", f_getDesktop);
 
+  canvas_module.createHash("CustomEvent")
+               .set("new", &f_createCustomEvent);
   return naNil();
 }