]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalCanvas.cxx
6ef49d19a447c383c7f56f4d14a1f7c3b6328666
[flightgear.git] / src / Scripting / NasalCanvas.cxx
1 // NasalCanvas.cxx -- expose Canvas classes to Nasal
2 //
3 // Written by James Turner, started 2012.
4 //
5 // Copyright (C) 2012 James Turner
6 //
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.
11 //
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.
16 //
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.
20
21 #ifdef HAVE_CONFIG_H
22 #  include "config.h"
23 #endif
24
25 #include <memory>
26 #include <string.h>
27
28 #include "NasalCanvas.hxx"
29 #include <Canvas/canvas_mgr.hxx>
30 #include <Main/globals.hxx>
31
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>
37
38 #include <simgear/sg_inlines.h>
39
40 #include <simgear/canvas/Canvas.hxx>
41 #include <simgear/canvas/elements/CanvasElement.hxx>
42
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>
47
48 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
49
50 //void initCanvasPython()
51 //{
52 //  using namespace boost::python;
53 //  class_<simgear::canvas::Canvas>("Canvas");
54 //}
55
56 namespace sc = simgear::canvas;
57
58 naRef canvasGetNode(naContext c, sc::Canvas* canvas)
59 {
60   return propNodeGhostCreate(c, canvas->getProps());
61 }
62
63 struct Base
64 {
65   int getInt() const
66   {
67     return 8;
68   }
69 };
70
71 struct Test:
72   public Base
73 {
74   Test(): x(1) {}
75   int x;
76   void setX(int x_) { x = x_; }
77   naRef test(int argc, naRef* args)
78   {
79     return naNil();
80   }
81 };
82
83 typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
84
85 void initCanvas(naContext c)
86 {
87
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>();
95
96   nasal::Ghost<Base>::init("BaseClass")
97     .member("int", &Base::getInt);
98   nasal::Ghost<Test>::init("TestClass")
99     .bases<Base>()
100     .member("x", &Test::setX)
101     .method<&Test::test>("test");
102 }
103
104 #if 0
105 /**
106  * Class for exposing C++ objects to Nasal
107  */
108 template<class T, class Derived>
109 class NasalObject
110 {
111   public:
112     // TODO use variadic template when supporting C++11
113     template<class A1>
114     static naRef create( naContext c, const A1& a1 )
115     {
116       return makeGhost(c, new T(a1));
117     }
118
119     template<class A1, class A2>
120     static naRef create( naContext c, const A1& a1,
121                                       const A2& a2 )
122     {
123       return makeGhost(c, new T(a1, a2));
124     }
125
126     template<class A1, class A2, class A3>
127     static naRef create( naContext c, const A1& a1,
128                                       const A2& a2,
129                                       const A3& a3 )
130     {
131       return makeGhost(c, new T(a1, a2, a3));
132     }
133
134     template<class A1, class A2, class A3, class A4>
135     static naRef create( naContext c, const A1& a1,
136                                       const A2& a2,
137                                       const A3& a3,
138                                       const A4& a4 )
139     {
140       return makeGhost(c, new T(a1, a2, a3, a4));
141     }
142
143     template<class A1, class A2, class A3, class A4, class A5>
144     static naRef create( naContext c, const A1& a1,
145                                       const A2& a2,
146                                       const A3& a3,
147                                       const A4& a4,
148                                       const A5& a5 )
149     {
150       return makeGhost(c, new T(a1, a2, a3, a4, a5));
151     }
152
153     // TODO If you need more arguments just do some copy&paste :)
154
155     static Derived& getInstance()
156     {
157       static Derived instance;
158       return instance;
159     }
160
161     void setParent(const naRef& parent)
162     {
163       // TODO check if we need to take care of reference counting/gc
164       _parents.resize(1);
165       _parents[0] = parent;
166     }
167
168   protected:
169
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;
174
175     const std::string   _ghost_name;
176     std::vector<naRef>  _parents;
177     MemberMap           _members;
178
179     NasalObject(const std::string& ghost_name):
180       _ghost_name( ghost_name )
181     {
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;
186
187       _members["parents"] = &NasalObject::getParents;
188     }
189
190     naRef getParents(naContext c, const T&)
191     {
192       naRef parents = naNewVector(c);
193       for(size_t i = 0; i < _parents.size(); ++i)
194         naVec_append(parents, _parents[i]);
195       return parents;
196     }
197
198     static naRef makeGhost(naContext c, void *ptr)
199     {
200       std::cout << "create  " << ptr << std::endl;
201       return naNewGhost2(c, &(getInstance()._ghost_type), ptr);
202     }
203
204     static void destroyGhost(void *ptr)
205     {
206       std::cout << "destroy " << ptr << std::endl;
207       delete (T*)ptr;
208     }
209
210     static const char* getMember(naContext c, void* g, naRef field, naRef* out)
211     {
212       typename MemberMap::iterator getter =
213         getInstance()._members.find(naStr_data(field));
214
215       if( getter == getInstance()._members.end() )
216         return 0;
217
218       *out = (getInstance().*getter->second)(c, *static_cast<T*>(g));
219       return "";
220     }
221
222   private:
223
224     naGhostType _ghost_type;
225
226 };
227
228 typedef osg::ref_ptr<osgGA::GUIEventAdapter> GUIEventPtr;
229
230 class NasalCanvasEvent:
231   public NasalObject<GUIEventPtr, NasalCanvasEvent>
232 {
233   public:
234
235     NasalCanvasEvent():
236       NasalObject("CanvasEvent")
237     {
238       _members["type"] = &NasalCanvasEvent::getEventType;
239     }
240
241     naRef getEventType(naContext c, const GUIEventPtr& event)
242     {
243 #define RET_EVENT_STR(type, str)\
244   case osgGA::GUIEventAdapter::type:\
245     return nasal::to_nasal(c, str);
246
247       switch( event->getEventType() )
248       {
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");
257
258 #undef RET_EVENT_STR
259
260         default:
261           return naNil();
262       }
263     }
264 };
265 #endif
266 #if 0
267 static const char* eventGhostGetMember(naContext c, void* g, naRef field, naRef* out)
268 {
269   const char* fieldName = naStr_data(field);
270   osgGA::GUIEventAdapter* gea = (osgGA::GUIEventAdapter*) g;
271
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());
276   else {
277     return 0;
278   }
279
280   return "";
281 }
282
283 static naRef f_element_addButtonCallback(naContext c, naRef me, int argc, naRef* args)
284 {
285   simgear::canvas::Element* e = elementGhost(me);
286   if (!e) {
287     naRuntimeError(c, "element.addButtonCallback called on non-canvas-element object");
288   }
289   
290   return naNil();
291 }
292
293 static naRef f_element_addDragCallback(naContext c, naRef me, int argc, naRef* args)
294 {
295   simgear::canvas::Element* e = elementGhost(me);
296   if (!e) {
297     naRuntimeError(c, "element.addDragCallback called on non-canvas-element object");
298   }
299   
300   return naNil();
301 }
302
303 static naRef f_element_addMoveCallback(naContext c, naRef me, int argc, naRef* args)
304 {
305   simgear::canvas::Element* e = elementGhost(me);
306   if (!e) {
307     naRuntimeError(c, "element.addMoveCallback called on non-canvas-element object");
308   }
309   
310   return naNil();
311 }
312
313 static naRef f_element_addScrollCallback(naContext c, naRef me, int argc, naRef* args)
314 {
315   simgear::canvas::Element* e = elementGhost(me);
316   if (!e) {
317     naRuntimeError(c, "element.addScrollCallback called on non-canvas-element object");
318   }
319   
320   return naNil();
321 }
322 #endif
323
324 static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
325 {
326   std::cout << "f_createCanvas" << std::endl;
327
328   CanvasMgr* canvas_mgr =
329     static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
330   if( !canvas_mgr )
331     return naNil();
332
333   return NasalCanvas::create(c, canvas_mgr->createCanvas());
334 }
335
336 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
337 {
338       /*naNewHash(c);
339     hashset(c, gcSave, "canvasProto", canvasPrototype);
340   
341     hashset(c, canvasPrototype, "getElement", naNewFunc(c, naNewCCode(c, f_canvas_getElement)));*/
342     // set any event methods
343   
344 #if 0
345     elementPrototype = naNewHash(c);
346     hashset(c, gcSave, "elementProto", elementPrototype);
347     
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)));
352 #endif
353   nasal::Hash globals_module(globals, c),
354               canvas_module = globals_module.createHash("canvas");
355
356   canvas_module.set("_new", f_createCanvas);
357   canvas_module.set("testClass", nasal::Ghost<Test>::f_create);
358
359   initCanvas(c);
360
361   return naNil();
362 }