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 <Canvas/gui_mgr.hxx>
28 #include <Canvas/window.hxx>
29 #include <Main/globals.hxx>
30 #include <Scripting/NasalSys.hxx>
32 #include <osgGA/GUIEventAdapter>
34 #include <simgear/sg_inlines.h>
36 #include <simgear/canvas/Canvas.hxx>
37 #include <simgear/canvas/elements/CanvasElement.hxx>
38 #include <simgear/canvas/elements/CanvasText.hxx>
39 #include <simgear/canvas/MouseEvent.hxx>
41 #include <simgear/nasal/cppbind/from_nasal.hxx>
42 #include <simgear/nasal/cppbind/to_nasal.hxx>
43 #include <simgear/nasal/cppbind/NasalHash.hxx>
44 #include <simgear/nasal/cppbind/Ghost.hxx>
46 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
48 namespace sc = simgear::canvas;
50 template<class Element>
51 naRef elementGetNode(naContext c, Element& element)
53 return propNodeGhostCreate(c, element.getProps());
56 typedef nasal::Ghost<sc::EventPtr> NasalEvent;
57 typedef nasal::Ghost<sc::MouseEventPtr> NasalMouseEvent;
58 typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
59 typedef nasal::Ghost<sc::ElementPtr> NasalElement;
60 typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
61 typedef nasal::Ghost<sc::TextPtr> NasalText;
62 typedef nasal::Ghost<canvas::WindowWeakPtr> NasalWindow;
64 SGPropertyNode* from_nasal_helper(naContext c, naRef ref, SGPropertyNode**)
66 SGPropertyNode* props = ghostToPropNode(ref);
68 naRuntimeError(c, "Not a SGPropertyNode ghost.");
73 CanvasMgr& requireCanvasMgr(naContext c)
75 CanvasMgr* canvas_mgr =
76 static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
78 naRuntimeError(c, "Failed to get Canvas subsystem");
83 GUIMgr& requireGUIMgr(naContext c)
86 static_cast<GUIMgr*>(globals->get_subsystem("CanvasGUI"));
88 naRuntimeError(c, "Failed to get CanvasGUI subsystem");
94 * Create new Canvas and get ghost for it.
96 static naRef f_createCanvas(const nasal::CallContext& ctx)
98 return NasalCanvas::create(ctx.c, requireCanvasMgr(ctx.c).createCanvas());
102 * Create new Window and get ghost for it.
104 static naRef f_createWindow(const nasal::CallContext& ctx)
106 return NasalWindow::create
109 requireGUIMgr(ctx.c).createWindow( ctx.getArg<std::string>(0) )
114 * Get ghost for existing Canvas.
116 static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
118 nasal::CallContext ctx(c, argc, args);
119 SGPropertyNode& props = *ctx.requireArg<SGPropertyNode*>(0);
120 CanvasMgr& canvas_mgr = requireCanvasMgr(c);
122 sc::CanvasPtr canvas;
123 if( canvas_mgr.getPropertyRoot() == props.getParent() )
125 // get a canvas specified by its root node
126 canvas = canvas_mgr.getCanvas( props.getIndex() );
127 if( !canvas || canvas->getProps() != &props )
132 // get a canvas by name
133 if( props.hasValue("name") )
134 canvas = canvas_mgr.getCanvas( props.getStringValue("name") );
135 else if( props.hasValue("index") )
136 canvas = canvas_mgr.getCanvas( props.getIntValue("index") );
139 return NasalCanvas::create(c, canvas);
142 naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
144 return NasalGroup::create
147 canvas.createGroup( ctx.getArg<std::string>(0) )
152 * Get group containing all gui windows
154 naRef f_getDesktop(naContext c, naRef me, int argc, naRef* args)
156 return NasalGroup::create(c, requireGUIMgr(c).getDesktop());
159 naRef f_elementGetTransformedBounds(sc::Element& el, const nasal::CallContext& ctx)
161 osg::BoundingBox bb = el.getTransformedBounds( osg::Matrix::identity() );
163 std::vector<float> bb_vec(4);
164 bb_vec[0] = bb._min.x();
165 bb_vec[1] = bb._min.y();
166 bb_vec[2] = bb._max.x();
167 bb_vec[3] = bb._max.y();
169 return nasal::to_nasal(ctx.c, bb_vec);
172 naRef f_groupCreateChild(sc::Group& group, const nasal::CallContext& ctx)
174 return NasalElement::create
177 group.createChild( ctx.requireArg<std::string>(0),
178 ctx.getArg<std::string>(1) )
182 naRef f_groupGetChild(sc::Group& group, const nasal::CallContext& ctx)
184 return NasalElement::create
187 group.getChild( ctx.requireArg<SGPropertyNode*>(0) )
191 naRef f_groupGetElementById(sc::Group& group, const nasal::CallContext& ctx)
193 return NasalElement::create
196 group.getElementById( ctx.requireArg<std::string>(0) )
200 naRef to_nasal_helper(naContext c, const sc::ElementWeakPtr& el)
202 return NasalElement::create(c, el.lock());
205 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
207 NasalEvent::init("canvas.Event")
208 .member("type", &sc::Event::getTypeString)
209 .member("target", &sc::Event::getTarget)
210 .member("currentTarget", &sc::Event::getCurrentTarget)
211 .method("stopPropagation", &sc::Event::stopPropagation);
212 NasalMouseEvent::init("canvas.MouseEvent")
214 .member("screenX", &sc::MouseEvent::getScreenX)
215 .member("screenY", &sc::MouseEvent::getScreenY)
216 .member("clientX", &sc::MouseEvent::getClientX)
217 .member("clientY", &sc::MouseEvent::getClientY)
218 .member("localX", &sc::MouseEvent::getLocalX)
219 .member("localY", &sc::MouseEvent::getLocalY)
220 .member("deltaX", &sc::MouseEvent::getDeltaX)
221 .member("deltaY", &sc::MouseEvent::getDeltaY)
222 .member("click_count", &sc::MouseEvent::getCurrentClickCount);
223 NasalCanvas::init("Canvas")
224 .member("_node_ghost", &elementGetNode<sc::Canvas>)
225 .member("size_x", &sc::Canvas::getSizeX)
226 .member("size_y", &sc::Canvas::getSizeY)
227 .method("_createGroup", &f_canvasCreateGroup)
228 .method("_getGroup", &sc::Canvas::getGroup)
229 .method("addEventListener", &sc::Canvas::addNasalEventListener);
230 NasalElement::init("canvas.Element")
231 .member("_node_ghost", &elementGetNode<sc::Element>)
232 .method("addEventListener", &sc::Element::addNasalEventListener)
233 .method("getTransformedBounds", &f_elementGetTransformedBounds);
234 NasalGroup::init("canvas.Group")
235 .bases<NasalElement>()
236 .method("_createChild", &f_groupCreateChild)
237 .method("_getChild", &f_groupGetChild)
238 .method("_getElementById", &f_groupGetElementById);
239 NasalText::init("canvas.Text")
240 .bases<NasalElement>()
241 .method("getNearestCursor", &sc::Text::getNearestCursor);
243 NasalWindow::init("canvas.Window")
244 .bases<NasalElement>()
245 .member("_node_ghost", &elementGetNode<canvas::Window>)
246 .method("_getCanvasDecoration", &canvas::Window::getCanvasDecoration);
248 nasal::Hash globals_module(globals, c),
249 canvas_module = globals_module.createHash("canvas");
251 canvas_module.set("_newCanvasGhost", f_createCanvas);
252 canvas_module.set("_newWindowGhost", f_createWindow);
253 canvas_module.set("_getCanvasGhost", f_getCanvas);
254 canvas_module.set("_getDesktopGhost", f_getDesktop);