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.
28 #include "NasalCanvas.hxx"
29 #include <Canvas/canvas_mgr.hxx>
30 #include <Main/globals.hxx>
32 //#include <boost/python.hpp>
33 #include <boost/foreach.hpp>
34 #include <boost/algorithm/string/case_conv.hpp>
35 #include <boost/make_shared.hpp>
36 #include <osgGA/GUIEventAdapter>
38 #include <simgear/sg_inlines.h>
40 #include <simgear/canvas/Canvas.hxx>
41 #include <simgear/canvas/elements/CanvasElement.hxx>
43 #include <simgear/nasal/cppbind/from_nasal.hxx>
44 #include <simgear/nasal/cppbind/to_nasal.hxx>
45 #include <simgear/nasal/cppbind/NasalHash.hxx>
46 #include <simgear/nasal/cppbind/Ghost.hxx>
48 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
50 //void initCanvasPython()
52 // using namespace boost::python;
53 // class_<simgear::canvas::Canvas>("Canvas");
56 namespace sc = simgear::canvas;
58 naRef canvasGetNode(naContext c, sc::Canvas* canvas)
60 return propNodeGhostCreate(c, canvas->getProps());
76 void setX(int x_) { x = x_; }
77 naRef test(int argc, naRef* args)
83 typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
85 void initCanvas(naContext c)
88 NasalCanvas::init("Canvas")
89 .member("_node_ghost", &canvasGetNode)
90 .member("size_x", &sc::Canvas::getSizeX)
91 .member("size_y", &sc::Canvas::getSizeY);
92 nasal::Ghost<sc::ElementPtr>::init("canvas.Element");
93 nasal::Ghost<sc::GroupPtr>::init("canvas.Group")
94 .bases<sc::ElementPtr>();
96 nasal::Ghost<Base>::init("BaseClass")
97 .member("int", &Base::getInt);
98 nasal::Ghost<Test>::init("TestClass")
100 .member("x", &Test::setX)
101 .method<&Test::test>("test");
106 * Class for exposing C++ objects to Nasal
108 template<class T, class Derived>
112 // TODO use variadic template when supporting C++11
114 static naRef create( naContext c, const A1& a1 )
116 return makeGhost(c, new T(a1));
119 template<class A1, class A2>
120 static naRef create( naContext c, const A1& a1,
123 return makeGhost(c, new T(a1, a2));
126 template<class A1, class A2, class A3>
127 static naRef create( naContext c, const A1& a1,
131 return makeGhost(c, new T(a1, a2, a3));
134 template<class A1, class A2, class A3, class A4>
135 static naRef create( naContext c, const A1& a1,
140 return makeGhost(c, new T(a1, a2, a3, a4));
143 template<class A1, class A2, class A3, class A4, class A5>
144 static naRef create( naContext c, const A1& a1,
150 return makeGhost(c, new T(a1, a2, a3, a4, a5));
153 // TODO If you need more arguments just do some copy&paste :)
155 static Derived& getInstance()
157 static Derived instance;
161 void setParent(const naRef& parent)
163 // TODO check if we need to take care of reference counting/gc
165 _parents[0] = parent;
170 // TODO switch to boost::/std::function (with C++11 lambdas this can make
171 // adding setters easier and shorter)
172 typedef naRef (Derived::*getter_t)(naContext, const T&);
173 typedef std::map<std::string, getter_t> MemberMap;
175 const std::string _ghost_name;
176 std::vector<naRef> _parents;
179 NasalObject(const std::string& ghost_name):
180 _ghost_name( ghost_name )
182 _ghost_type.destroy = &destroyGhost;
183 _ghost_type.name = _ghost_name.c_str();
184 _ghost_type.get_member = &Derived::getMember;
185 _ghost_type.set_member = 0;
187 _members["parents"] = &NasalObject::getParents;
190 naRef getParents(naContext c, const T&)
192 naRef parents = naNewVector(c);
193 for(size_t i = 0; i < _parents.size(); ++i)
194 naVec_append(parents, _parents[i]);
198 static naRef makeGhost(naContext c, void *ptr)
200 std::cout << "create " << ptr << std::endl;
201 return naNewGhost2(c, &(getInstance()._ghost_type), ptr);
204 static void destroyGhost(void *ptr)
206 std::cout << "destroy " << ptr << std::endl;
210 static const char* getMember(naContext c, void* g, naRef field, naRef* out)
212 typename MemberMap::iterator getter =
213 getInstance()._members.find(naStr_data(field));
215 if( getter == getInstance()._members.end() )
218 *out = (getInstance().*getter->second)(c, *static_cast<T*>(g));
224 naGhostType _ghost_type;
228 typedef osg::ref_ptr<osgGA::GUIEventAdapter> GUIEventPtr;
230 class NasalCanvasEvent:
231 public NasalObject<GUIEventPtr, NasalCanvasEvent>
236 NasalObject("CanvasEvent")
238 _members["type"] = &NasalCanvasEvent::getEventType;
241 naRef getEventType(naContext c, const GUIEventPtr& event)
243 #define RET_EVENT_STR(type, str)\
244 case osgGA::GUIEventAdapter::type:\
245 return nasal::to_nasal(c, str);
247 switch( event->getEventType() )
249 RET_EVENT_STR(PUSH, "push");
250 RET_EVENT_STR(RELEASE, "release");
251 RET_EVENT_STR(DOUBLECLICK, "double-click");
252 RET_EVENT_STR(DRAG, "drag");
253 RET_EVENT_STR(MOVE, "move");
254 RET_EVENT_STR(SCROLL, "scroll");
255 RET_EVENT_STR(KEYUP, "key-up");
256 RET_EVENT_STR(KEYDOWN, "key-down");
267 static const char* eventGhostGetMember(naContext c, void* g, naRef field, naRef* out)
269 const char* fieldName = naStr_data(field);
270 osgGA::GUIEventAdapter* gea = (osgGA::GUIEventAdapter*) g;
272 if (!strcmp(fieldName, "windowX")) *out = naNum(gea->getWindowX());
273 else if (!strcmp(fieldName, "windowY")) *out = naNum(gea->getWindowY());
274 else if (!strcmp(fieldName, "time")) *out = naNum(gea->getTime());
275 else if (!strcmp(fieldName, "button")) *out = naNum(gea->getButton());
283 static naRef f_element_addButtonCallback(naContext c, naRef me, int argc, naRef* args)
285 simgear::canvas::Element* e = elementGhost(me);
287 naRuntimeError(c, "element.addButtonCallback called on non-canvas-element object");
293 static naRef f_element_addDragCallback(naContext c, naRef me, int argc, naRef* args)
295 simgear::canvas::Element* e = elementGhost(me);
297 naRuntimeError(c, "element.addDragCallback called on non-canvas-element object");
303 static naRef f_element_addMoveCallback(naContext c, naRef me, int argc, naRef* args)
305 simgear::canvas::Element* e = elementGhost(me);
307 naRuntimeError(c, "element.addMoveCallback called on non-canvas-element object");
313 static naRef f_element_addScrollCallback(naContext c, naRef me, int argc, naRef* args)
315 simgear::canvas::Element* e = elementGhost(me);
317 naRuntimeError(c, "element.addScrollCallback called on non-canvas-element object");
324 static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
326 std::cout << "f_createCanvas" << std::endl;
328 CanvasMgr* canvas_mgr =
329 static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
333 return NasalCanvas::create(c, canvas_mgr->createCanvas());
336 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
339 hashset(c, gcSave, "canvasProto", canvasPrototype);
341 hashset(c, canvasPrototype, "getElement", naNewFunc(c, naNewCCode(c, f_canvas_getElement)));*/
342 // set any event methods
345 elementPrototype = naNewHash(c);
346 hashset(c, gcSave, "elementProto", elementPrototype);
348 hashset(c, elementPrototype, "addButtonCallback", naNewFunc(c, naNewCCode(c, f_element_addButtonCallback)));
349 hashset(c, elementPrototype, "addDragCallback", naNewFunc(c, naNewCCode(c, f_element_addDragCallback)));
350 hashset(c, elementPrototype, "addMoveCallback", naNewFunc(c, naNewCCode(c, f_element_addMoveCallback)));
351 hashset(c, elementPrototype, "addScrollCallback", naNewFunc(c, naNewCCode(c, f_element_addScrollCallback)));
353 nasal::Hash globals_module(globals, c),
354 canvas_module = globals_module.createHash("canvas");
356 canvas_module.set("_new", f_createCanvas);
357 canvas_module.set("testClass", nasal::Ghost<Test>::f_create);