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 <Main/globals.hxx>
29 #include <osgGA/GUIEventAdapter>
31 #include <simgear/sg_inlines.h>
33 #include <simgear/canvas/Canvas.hxx>
34 #include <simgear/canvas/elements/CanvasElement.hxx>
36 #include <simgear/nasal/cppbind/from_nasal.hxx>
37 #include <simgear/nasal/cppbind/to_nasal.hxx>
38 #include <simgear/nasal/cppbind/NasalHash.hxx>
39 #include <simgear/nasal/cppbind/Ghost.hxx>
41 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
43 namespace sc = simgear::canvas;
45 template<class Element>
46 naRef elementGetNode(naContext c, Element& element)
48 return propNodeGhostCreate(c, element.getProps());
51 typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
52 typedef nasal::Ghost<sc::ElementPtr> NasalElement;
53 typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
56 typedef osg::ref_ptr<osgGA::GUIEventAdapter> GUIEventPtr;
58 class NasalCanvasEvent:
59 public NasalObject<GUIEventPtr, NasalCanvasEvent>
63 naRef getEventType(naContext c, const GUIEventPtr& event)
65 #define RET_EVENT_STR(type, str)\
66 case osgGA::GUIEventAdapter::type:\
67 return nasal::to_nasal(c, str);
69 switch( event->getEventType() )
71 RET_EVENT_STR(PUSH, "push");
72 RET_EVENT_STR(RELEASE, "release");
73 RET_EVENT_STR(DOUBLECLICK, "double-click");
74 RET_EVENT_STR(DRAG, "drag");
75 RET_EVENT_STR(MOVE, "move");
76 RET_EVENT_STR(SCROLL, "scroll");
77 RET_EVENT_STR(KEYUP, "key-up");
78 RET_EVENT_STR(KEYDOWN, "key-down");
89 static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
91 CanvasMgr* canvas_mgr =
92 static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
96 return NasalCanvas::create(c, canvas_mgr->createCanvas());
99 naRef f_canvasCreateGroup( sc::Canvas& canvas,
106 name = nasal::from_nasal<std::string>(c, args[0]);
108 return NasalGroup::create(c, canvas.createGroup(name));
111 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
113 NasalCanvas::init("Canvas")
114 .member("_node_ghost", &elementGetNode<sc::Canvas>)
115 .member("size_x", &sc::Canvas::getSizeX)
116 .member("size_y", &sc::Canvas::getSizeY)
117 .method_func<&f_canvasCreateGroup>("createGroup");
118 NasalElement::init("canvas.Element")
119 .member("_node_ghost", &elementGetNode<sc::Element>);
120 NasalGroup::init("canvas.Group")
121 .bases<NasalElement>();
123 nasal::Hash globals_module(globals, c),
124 canvas_module = globals_module.createHash("canvas");
126 canvas_module.set("_newCanvasGhost", f_createCanvas);