-template<class T, class Derived>
-class NasalObject
-{
- 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));
- }
-
- 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));
- }
-
- 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));
- }
-
- // TODO If you need more arguments just do some copy&paste :)
-
- static Derived& getInstance()
- {
- static Derived instance;
- return instance;
- }
-
- void setParent(const naRef& parent)
- {
- // TODO check if we need to take care of reference counting/gc
- _parents.resize(1);
- _parents[0] = parent;
- }
-
- protected:
-
- // 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;
-
- const std::string _ghost_name;
- std::vector<naRef> _parents;
- GetterMap _getter;
-
- 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;
-
- _getter["parents"] = &NasalObject::getParents;
- }
-
- 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;
- }
-
- static naRef makeGhost(naContext c, void *ptr)
- {
- std::cout << "create " << ptr << std::endl;
- return naNewGhost2(c, &(getInstance()._ghost_type), ptr);
- }
-
- static void destroyGhost(void *ptr)
- {
- std::cout << "destroy " << ptr << std::endl;
- delete (T*)ptr;
- }
-
- static const char* getMember(naContext c, void* g, naRef field, naRef* out)
- {
- 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 "";
- }
-
- private:
-
- naGhostType _ghost_type;
-
-};
-
-static naRef stringToNasal(naContext c, const std::string& s)
-{
- return naStr_fromdata(naNewString(c),
- const_cast<char *>(s.c_str()),
- s.length());
-}
-
-typedef osg::ref_ptr<osgGA::GUIEventAdapter> GUIEventPtr;
-
-class NasalCanvasEvent:
- public NasalObject<GUIEventPtr, NasalCanvasEvent>
-{
- 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);
-
-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 };
-
-static simgear::canvas::Element* elementGhost(naRef r)