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>
36 #include <simgear/canvas/MouseEvent.hxx>
38 #include <simgear/nasal/cppbind/from_nasal.hxx>
39 #include <simgear/nasal/cppbind/to_nasal.hxx>
40 #include <simgear/nasal/cppbind/NasalHash.hxx>
41 #include <simgear/nasal/cppbind/Ghost.hxx>
43 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
45 namespace sc = simgear::canvas;
47 template<class Element>
48 naRef elementGetNode(naContext c, Element& element)
50 return propNodeGhostCreate(c, element.getProps());
53 typedef nasal::Ghost<sc::EventPtr> NasalEvent;
54 typedef nasal::Ghost<sc::MouseEventPtr> NasalMouseEvent;
55 typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
56 typedef nasal::Ghost<sc::ElementPtr> NasalElement;
57 typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
59 SGPropertyNode* from_nasal_helper(naContext c, naRef ref, SGPropertyNode**)
61 SGPropertyNode* props = ghostToPropNode(ref);
63 naRuntimeError(c, "Not a SGPropertyNode ghost.");
68 CanvasMgr& requireCanvasMgr(naContext c)
70 CanvasMgr* canvas_mgr =
71 static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
73 naRuntimeError(c, "Failed to get Canvas subsystem");
79 * Create new Canvas and get ghost for it.
81 static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
83 return NasalCanvas::create(c, requireCanvasMgr(c).createCanvas());
87 * Get ghost for existing Canvas.
89 static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
91 nasal::CallContext ctx(c, argc, args);
92 SGPropertyNode& props = *ctx.requireArg<SGPropertyNode*>(0);
93 CanvasMgr& canvas_mgr = requireCanvasMgr(c);
96 if( canvas_mgr.getPropertyRoot() == props.getParent() )
98 // get a canvas specified by its root node
99 canvas = canvas_mgr.getCanvas( props.getIndex() );
100 if( !canvas || canvas->getProps() != &props )
105 // get a canvas by name
106 if( props.hasValue("name") )
107 canvas = canvas_mgr.getCanvas( props.getStringValue("name") );
108 else if( props.hasValue("index") )
109 canvas = canvas_mgr.getCanvas( props.getIntValue("index") );
112 return NasalCanvas::create(c, canvas);
115 naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
117 return NasalGroup::create
120 canvas.createGroup( ctx.getArg<std::string>(0) )
124 naRef f_groupCreateChild(sc::Group& group, const nasal::CallContext& ctx)
126 return NasalElement::create
129 group.createChild( ctx.requireArg<std::string>(0),
130 ctx.getArg<std::string>(1) )
134 naRef f_groupGetChild(sc::Group& group, const nasal::CallContext& ctx)
136 return NasalElement::create
139 group.getChild( ctx.requireArg<SGPropertyNode*>(0) )
143 naRef f_groupGetElementById(sc::Group& group, const nasal::CallContext& ctx)
145 return NasalElement::create
148 group.getElementById( ctx.requireArg<std::string>(0) )
152 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
154 NasalEvent::init("canvas.Event");
155 NasalMouseEvent::init("canvas.MouseEvent")
156 .bases<NasalEvent>();
157 NasalCanvas::init("Canvas")
158 .member("_node_ghost", &elementGetNode<sc::Canvas>)
159 .member("size_x", &sc::Canvas::getSizeX)
160 .member("size_y", &sc::Canvas::getSizeY)
161 .method_func<&f_canvasCreateGroup>("_createGroup");
162 NasalElement::init("canvas.Element")
163 .member("_node_ghost", &elementGetNode<sc::Element>)
164 .method<&sc::Element::addEventListener>("addEventListener");
165 NasalGroup::init("canvas.Group")
166 .bases<NasalElement>()
167 .method_func<&f_groupCreateChild>("_createChild")
168 .method_func<&f_groupGetChild>("_getChild")
169 .method_func<&f_groupGetElementById>("_getElementById");
171 nasal::Hash globals_module(globals, c),
172 canvas_module = globals_module.createHash("canvas");
174 canvas_module.set("_newCanvasGhost", f_createCanvas);
175 canvas_module.set("_getCanvasGhost", f_getCanvas);