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>
28 #include <Scripting/NasalSys.hxx>
30 #include <osgGA/GUIEventAdapter>
32 #include <simgear/sg_inlines.h>
34 #include <simgear/canvas/Canvas.hxx>
35 #include <simgear/canvas/elements/CanvasElement.hxx>
37 #include <simgear/nasal/cppbind/from_nasal.hxx>
38 #include <simgear/nasal/cppbind/to_nasal.hxx>
39 #include <simgear/nasal/cppbind/NasalHash.hxx>
40 #include <simgear/nasal/cppbind/Ghost.hxx>
42 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
44 namespace sc = simgear::canvas;
46 template<class Element>
47 naRef elementGetNode(naContext c, Element& element)
49 return propNodeGhostCreate(c, element.getProps());
52 typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
53 typedef nasal::Ghost<sc::ElementPtr> NasalElement;
54 typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
57 typedef osg::ref_ptr<osgGA::GUIEventAdapter> GUIEventPtr;
59 class NasalCanvasEvent:
60 public NasalObject<GUIEventPtr, NasalCanvasEvent>
64 naRef getEventType(naContext c, const GUIEventPtr& event)
66 #define RET_EVENT_STR(type, str)\
67 case osgGA::GUIEventAdapter::type:\
68 return nasal::to_nasal(c, str);
70 switch( event->getEventType() )
72 RET_EVENT_STR(PUSH, "push");
73 RET_EVENT_STR(RELEASE, "release");
74 RET_EVENT_STR(DOUBLECLICK, "double-click");
75 RET_EVENT_STR(DRAG, "drag");
76 RET_EVENT_STR(MOVE, "move");
77 RET_EVENT_STR(SCROLL, "scroll");
78 RET_EVENT_STR(KEYUP, "key-up");
79 RET_EVENT_STR(KEYDOWN, "key-down");
90 SGPropertyNode& requireArg(naContext c, int argc, naRef* args, int index = 0)
93 naRuntimeError(c, "missing argument #%d", index);
95 SGPropertyNode* props = ghostToPropNode(args[index]);
97 naRuntimeError(c, "arg #%d: not a SGPropertyNode ghost");
102 CanvasMgr& requireCanvasMgr(naContext c)
104 CanvasMgr* canvas_mgr =
105 static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
107 naRuntimeError(c, "Failed to get Canvas subsystem");
113 * Create new Canvas and get ghost for it.
115 static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
117 return NasalCanvas::create(c, requireCanvasMgr(c).createCanvas());
121 * Get ghost for existing Canvas.
123 static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
125 SGPropertyNode& props = requireArg(c, argc, args);
126 CanvasMgr& canvas_mgr = requireCanvasMgr(c);
128 sc::CanvasPtr canvas;
129 if( canvas_mgr.getPropertyRoot() == props.getParent() )
131 // get a canvas specified by its root node
132 canvas = canvas_mgr.getCanvas( props.getIndex() );
133 if( !canvas || canvas->getProps() != &props )
138 // get a canvas by name
139 if( props.hasValue("name") )
140 canvas = canvas_mgr.getCanvas( props.getStringValue("name") );
141 else if( props.hasValue("index") )
142 canvas = canvas_mgr.getCanvas( props.getIntValue("index") );
145 return NasalCanvas::create(c, canvas);
148 naRef f_canvasCreateGroup( sc::Canvas& canvas,
155 name = nasal::from_nasal<std::string>(c, args[0]);
157 return NasalGroup::create(c, canvas.createGroup(name));
160 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
162 NasalCanvas::init("Canvas")
163 .member("_node_ghost", &elementGetNode<sc::Canvas>)
164 .member("size_x", &sc::Canvas::getSizeX)
165 .member("size_y", &sc::Canvas::getSizeY)
166 .method_func<&f_canvasCreateGroup>("createGroup");
167 NasalElement::init("canvas.Element")
168 .member("_node_ghost", &elementGetNode<sc::Element>);
169 NasalGroup::init("canvas.Group")
170 .bases<NasalElement>();
172 nasal::Hash globals_module(globals, c),
173 canvas_module = globals_module.createHash("canvas");
175 canvas_module.set("_newCanvasGhost", f_createCanvas);
176 canvas_module.set("_getCanvasGhost", f_getCanvas);