]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalCanvas.cxx
Canvas: also apply matrix of element to getTransformedBounds.
[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 <Canvas/gui_mgr.hxx>
28 #include <Canvas/window.hxx>
29 #include <Main/globals.hxx>
30 #include <Scripting/NasalSys.hxx>
31
32 #include <osgGA/GUIEventAdapter>
33
34 #include <simgear/sg_inlines.h>
35
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>
40
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>
45
46 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
47
48 namespace sc = simgear::canvas;
49
50 template<class Element>
51 naRef elementGetNode(naContext c, Element& element)
52 {
53   return propNodeGhostCreate(c, element.getProps());
54 }
55
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;
63
64 SGPropertyNode* from_nasal_helper(naContext c, naRef ref, SGPropertyNode**)
65 {
66   SGPropertyNode* props = ghostToPropNode(ref);
67   if( !props )
68     naRuntimeError(c, "Not a SGPropertyNode ghost.");
69
70   return props;
71 }
72
73 CanvasMgr& requireCanvasMgr(naContext c)
74 {
75   CanvasMgr* canvas_mgr =
76     static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
77   if( !canvas_mgr )
78     naRuntimeError(c, "Failed to get Canvas subsystem");
79
80   return *canvas_mgr;
81 }
82
83 GUIMgr& requireGUIMgr(naContext c)
84 {
85   GUIMgr* mgr =
86     static_cast<GUIMgr*>(globals->get_subsystem("CanvasGUI"));
87   if( !mgr )
88     naRuntimeError(c, "Failed to get CanvasGUI subsystem");
89
90   return *mgr;
91 }
92
93 /**
94  * Create new Canvas and get ghost for it.
95  */
96 static naRef f_createCanvas(const nasal::CallContext& ctx)
97 {
98   return NasalCanvas::create(ctx.c, requireCanvasMgr(ctx.c).createCanvas());
99 }
100
101 /**
102  * Create new Window and get ghost for it.
103  */
104 static naRef f_createWindow(const nasal::CallContext& ctx)
105 {
106   return NasalWindow::create
107   (
108     ctx.c,
109     requireGUIMgr(ctx.c).createWindow( ctx.getArg<std::string>(0) )
110   );
111 }
112
113 /**
114  * Get ghost for existing Canvas.
115  */
116 static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
117 {
118   nasal::CallContext ctx(c, argc, args);
119   SGPropertyNode& props = *ctx.requireArg<SGPropertyNode*>(0);
120   CanvasMgr& canvas_mgr = requireCanvasMgr(c);
121
122   sc::CanvasPtr canvas;
123   if( canvas_mgr.getPropertyRoot() == props.getParent() )
124   {
125     // get a canvas specified by its root node
126     canvas = canvas_mgr.getCanvas( props.getIndex() );
127     if( !canvas || canvas->getProps() != &props )
128       return naNil();
129   }
130   else
131   {
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") );
137   }
138
139   return NasalCanvas::create(c, canvas);
140 }
141
142 naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
143 {
144   return NasalGroup::create
145   (
146     ctx.c,
147     canvas.createGroup( ctx.getArg<std::string>(0) )
148   );
149 }
150
151 /**
152  * Get group containing all gui windows
153  */
154 naRef f_getDesktop(naContext c, naRef me, int argc, naRef* args)
155 {
156   return NasalGroup::create(c, requireGUIMgr(c).getDesktop());
157 }
158
159 naRef f_elementGetTransformedBounds(sc::Element& el, const nasal::CallContext& ctx)
160 {
161   osg::BoundingBox bb = el.getTransformedBounds(el.getMatrix());
162
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();
168
169   return nasal::to_nasal(ctx.c, bb_vec);
170 }
171
172 naRef f_groupCreateChild(sc::Group& group, const nasal::CallContext& ctx)
173 {
174   return NasalElement::create
175   (
176     ctx.c,
177     group.createChild( ctx.requireArg<std::string>(0),
178                        ctx.getArg<std::string>(1) )
179   );
180 }
181
182 naRef f_groupGetChild(sc::Group& group, const nasal::CallContext& ctx)
183 {
184   return NasalElement::create
185   (
186     ctx.c,
187     group.getChild( ctx.requireArg<SGPropertyNode*>(0) )
188   );
189 }
190
191 naRef f_groupGetElementById(sc::Group& group, const nasal::CallContext& ctx)
192 {
193   return NasalElement::create
194   (
195     ctx.c,
196     group.getElementById( ctx.requireArg<std::string>(0) )
197   );
198 }
199
200 naRef to_nasal_helper(naContext c, const sc::ElementWeakPtr& el)
201 {
202   return NasalElement::create(c, el.lock());
203 }
204
205 naRef initNasalCanvas(naRef globals, naContext c)
206 {
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")
213     .bases<NasalEvent>()
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::addEventListener);
230   NasalElement::init("canvas.Element")
231     .member("_node_ghost", &elementGetNode<sc::Element>)
232     .method("_getParent", &sc::Element::getParent)
233     .method("addEventListener", &sc::Element::addEventListener)
234     .method("getTransformedBounds", &f_elementGetTransformedBounds);
235   NasalGroup::init("canvas.Group")
236     .bases<NasalElement>()
237     .method("_createChild", &f_groupCreateChild)
238     .method("_getChild", &f_groupGetChild)
239     .method("_getElementById", &f_groupGetElementById);
240   NasalText::init("canvas.Text")
241     .bases<NasalElement>()
242     .method("getNearestCursor", &sc::Text::getNearestCursor);
243
244   NasalWindow::init("canvas.Window")
245     .bases<NasalElement>()
246     .member("_node_ghost", &elementGetNode<canvas::Window>)
247     .method("_getCanvasDecoration", &canvas::Window::getCanvasDecoration);
248     
249   nasal::Hash globals_module(globals, c),
250               canvas_module = globals_module.createHash("canvas");
251
252   canvas_module.set("_newCanvasGhost", f_createCanvas);
253   canvas_module.set("_newWindowGhost", f_createWindow);
254   canvas_module.set("_getCanvasGhost", f_getCanvas);
255   canvas_module.set("_getDesktopGhost", f_getDesktop);
256
257   return naNil();
258 }