]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalCanvas.cxx
Expose canvas Text::getNearestCursor to Nasal as replacement for old property based...
[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 "NasalCanvas.hxx"
26 #include <Canvas/canvas_mgr.hxx>
27 #include <Main/globals.hxx>
28 #include <Scripting/NasalSys.hxx>
29
30 #include <osgGA/GUIEventAdapter>
31
32 #include <simgear/sg_inlines.h>
33
34 #include <simgear/canvas/Canvas.hxx>
35 #include <simgear/canvas/elements/CanvasElement.hxx>
36 #include <simgear/canvas/elements/CanvasText.hxx>
37 #include <simgear/canvas/MouseEvent.hxx>
38
39 #include <simgear/nasal/cppbind/from_nasal.hxx>
40 #include <simgear/nasal/cppbind/to_nasal.hxx>
41 #include <simgear/nasal/cppbind/NasalHash.hxx>
42 #include <simgear/nasal/cppbind/Ghost.hxx>
43
44 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
45
46 namespace sc = simgear::canvas;
47
48 template<class Element>
49 naRef elementGetNode(naContext c, Element& element)
50 {
51   return propNodeGhostCreate(c, element.getProps());
52 }
53
54 typedef nasal::Ghost<sc::EventPtr> NasalEvent;
55 typedef nasal::Ghost<sc::MouseEventPtr> NasalMouseEvent;
56 typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
57 typedef nasal::Ghost<sc::ElementPtr> NasalElement;
58 typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
59 typedef nasal::Ghost<sc::TextPtr> NasalText;
60
61 SGPropertyNode* from_nasal_helper(naContext c, naRef ref, SGPropertyNode**)
62 {
63   SGPropertyNode* props = ghostToPropNode(ref);
64   if( !props )
65     naRuntimeError(c, "Not a SGPropertyNode ghost.");
66
67   return props;
68 }
69
70 CanvasMgr& requireCanvasMgr(naContext c)
71 {
72   CanvasMgr* canvas_mgr =
73     static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
74   if( !canvas_mgr )
75     naRuntimeError(c, "Failed to get Canvas subsystem");
76
77   return *canvas_mgr;
78 }
79
80 /**
81  * Create new Canvas and get ghost for it.
82  */
83 static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
84 {
85   return NasalCanvas::create(c, requireCanvasMgr(c).createCanvas());
86 }
87
88 /**
89  * Get ghost for existing Canvas.
90  */
91 static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
92 {
93   nasal::CallContext ctx(c, argc, args);
94   SGPropertyNode& props = *ctx.requireArg<SGPropertyNode*>(0);
95   CanvasMgr& canvas_mgr = requireCanvasMgr(c);
96
97   sc::CanvasPtr canvas;
98   if( canvas_mgr.getPropertyRoot() == props.getParent() )
99   {
100     // get a canvas specified by its root node
101     canvas = canvas_mgr.getCanvas( props.getIndex() );
102     if( !canvas || canvas->getProps() != &props )
103       return naNil();
104   }
105   else
106   {
107     // get a canvas by name
108     if( props.hasValue("name") )
109       canvas = canvas_mgr.getCanvas( props.getStringValue("name") );
110     else if( props.hasValue("index") )
111       canvas = canvas_mgr.getCanvas( props.getIntValue("index") );
112   }
113
114   return NasalCanvas::create(c, canvas);
115 }
116
117 naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
118 {
119   return NasalGroup::create
120   (
121     ctx.c,
122     canvas.createGroup( ctx.getArg<std::string>(0) )
123   );
124 }
125
126 naRef f_elementGetTransformedBounds(sc::Element& el, const nasal::CallContext& ctx)
127 {
128   osg::BoundingBox bb = el.getTransformedBounds( osg::Matrix::identity() );
129
130   std::vector<float> bb_vec(4);
131   bb_vec[0] = bb._min.x();
132   bb_vec[1] = bb._min.y();
133   bb_vec[2] = bb._max.x();
134   bb_vec[3] = bb._max.y();
135
136   return nasal::to_nasal(ctx.c, bb_vec);
137 }
138
139 naRef f_groupCreateChild(sc::Group& group, const nasal::CallContext& ctx)
140 {
141   return NasalElement::create
142   (
143     ctx.c,
144     group.createChild( ctx.requireArg<std::string>(0),
145                        ctx.getArg<std::string>(1) )
146   );
147 }
148
149 naRef f_groupGetChild(sc::Group& group, const nasal::CallContext& ctx)
150 {
151   return NasalElement::create
152   (
153     ctx.c,
154     group.getChild( ctx.requireArg<SGPropertyNode*>(0) )
155   );
156 }
157
158 naRef f_groupGetElementById(sc::Group& group, const nasal::CallContext& ctx)
159 {
160   return NasalElement::create
161   (
162     ctx.c,
163     group.getElementById( ctx.requireArg<std::string>(0) )
164   );
165 }
166
167 naRef f_textGetNearestCursor(sc::Text& text, const nasal::CallContext& ctx)
168 {
169   return nasal::to_nasal
170   (
171     ctx.c,
172     text.getNearestCursor( ctx.requireArg<osg::Vec2>(0) )
173   );
174 }
175
176 naRef f_eventGetTarget(naContext c, sc::Event& event)
177 {
178   return NasalElement::create(c, event.getTarget().lock());
179 }
180
181 // TODO allow directly exposing functions without parameters and return type
182 naRef f_eventStopPropagation(sc::Event& event, const nasal::CallContext& ctx)
183 {
184   if( ctx.argc != 0 )
185     naRuntimeError(ctx.c, "Event::stopPropagation no argument expected");
186   event.stopPropagation();
187   return naNil();
188 }
189
190 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
191 {
192   NasalEvent::init("canvas.Event")
193     .member("type", &sc::Event::getTypeString)
194     .member("target", &f_eventGetTarget)
195     .method_func<&f_eventStopPropagation>("stopPropagation");
196   NasalMouseEvent::init("canvas.MouseEvent")
197     .bases<NasalEvent>()
198     .member("screenX", &sc::MouseEvent::getScreenX)
199     .member("screenY", &sc::MouseEvent::getScreenY)
200     .member("clientX", &sc::MouseEvent::getClientX)
201     .member("clientY", &sc::MouseEvent::getClientY)
202     .member("deltaX", &sc::MouseEvent::getDeltaX)
203     .member("deltaY", &sc::MouseEvent::getDeltaY)
204     .member("click_count", &sc::MouseEvent::getCurrentClickCount);
205   NasalCanvas::init("Canvas")
206     .member("_node_ghost", &elementGetNode<sc::Canvas>)
207     .member("size_x", &sc::Canvas::getSizeX)
208     .member("size_y", &sc::Canvas::getSizeY)
209     .method_func<&f_canvasCreateGroup>("_createGroup")
210     .method<&sc::Canvas::addEventListener>("addEventListener");
211   NasalElement::init("canvas.Element")
212     .member("_node_ghost", &elementGetNode<sc::Element>)
213     .method<&sc::Element::addEventListener>("addEventListener")
214     .method_func<&f_elementGetTransformedBounds>("getTransformedBounds");
215   NasalGroup::init("canvas.Group")
216     .bases<NasalElement>()
217     .method_func<&f_groupCreateChild>("_createChild")
218     .method_func<&f_groupGetChild>("_getChild")
219     .method_func<&f_groupGetElementById>("_getElementById");
220   NasalText::init("canvas.Text")
221     .bases<NasalElement>()
222     .method_func<&f_textGetNearestCursor>("getNearestCursor");
223
224   nasal::Hash globals_module(globals, c),
225               canvas_module = globals_module.createHash("canvas");
226
227   canvas_module.set("_newCanvasGhost", f_createCanvas);
228   canvas_module.set("_getCanvasGhost", f_getCanvas);
229
230   return naNil();
231 }