]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalCanvas.cxx
0c705e23067085ce318fbc85424b0e4ca2df133b
[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 <Main/globals.hxx>
29 #include <Scripting/NasalSys.hxx>
30
31 #include <osgGA/GUIEventAdapter>
32
33 #include <simgear/sg_inlines.h>
34
35 #include <simgear/canvas/Canvas.hxx>
36 #include <simgear/canvas/CanvasWindow.hxx>
37 #include <simgear/canvas/elements/CanvasElement.hxx>
38 #include <simgear/canvas/elements/CanvasText.hxx>
39 #include <simgear/canvas/events/CustomEvent.hxx>
40 #include <simgear/canvas/events/MouseEvent.hxx>
41
42 #include <simgear/nasal/cppbind/from_nasal.hxx>
43 #include <simgear/nasal/cppbind/to_nasal.hxx>
44 #include <simgear/nasal/cppbind/NasalHash.hxx>
45 #include <simgear/nasal/cppbind/Ghost.hxx>
46
47 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
48
49 namespace sc = simgear::canvas;
50
51 template<class Element>
52 naRef elementGetNode(Element& element, naContext c)
53 {
54   return propNodeGhostCreate(c, element.getProps());
55 }
56
57 typedef nasal::Ghost<sc::EventPtr> NasalEvent;
58 typedef nasal::Ghost<sc::CustomEventPtr> NasalCustomEvent;
59 typedef nasal::Ghost<sc::MouseEventPtr> NasalMouseEvent;
60
61 struct CustomEventDetailWrapper;
62 typedef SGSharedPtr<CustomEventDetailWrapper> CustomEventDetailPtr;
63 typedef nasal::Ghost<CustomEventDetailPtr> NasalCustomEventDetail;
64
65 typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
66 typedef nasal::Ghost<sc::ElementPtr> NasalElement;
67 typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
68 typedef nasal::Ghost<sc::TextPtr> NasalText;
69 typedef nasal::Ghost<sc::WindowWeakPtr> NasalWindow;
70
71 naRef to_nasal_helper(naContext c, const osg::BoundingBox& bb)
72 {
73   std::vector<float> bb_vec(4);
74   bb_vec[0] = bb._min.x();
75   bb_vec[1] = bb._min.y();
76   bb_vec[2] = bb._max.x();
77   bb_vec[3] = bb._max.y();
78
79   return nasal::to_nasal(c, bb_vec);
80 }
81
82 SGPropertyNode* from_nasal_helper(naContext c, naRef ref, SGPropertyNode**)
83 {
84   SGPropertyNode* props = ghostToPropNode(ref);
85   if( !props )
86     naRuntimeError(c, "Not a SGPropertyNode ghost.");
87
88   return props;
89 }
90
91 CanvasMgr& requireCanvasMgr(naContext c)
92 {
93   CanvasMgr* canvas_mgr =
94     static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
95   if( !canvas_mgr )
96     naRuntimeError(c, "Failed to get Canvas subsystem");
97
98   return *canvas_mgr;
99 }
100
101 GUIMgr& requireGUIMgr(naContext c)
102 {
103   GUIMgr* mgr =
104     static_cast<GUIMgr*>(globals->get_subsystem("CanvasGUI"));
105   if( !mgr )
106     naRuntimeError(c, "Failed to get CanvasGUI subsystem");
107
108   return *mgr;
109 }
110
111 /**
112  * Create new Canvas and get ghost for it.
113  */
114 static naRef f_createCanvas(const nasal::CallContext& ctx)
115 {
116   return NasalCanvas::create(ctx.c, requireCanvasMgr(ctx.c).createCanvas());
117 }
118
119 /**
120  * Create new Window and get ghost for it.
121  */
122 static naRef f_createWindow(const nasal::CallContext& ctx)
123 {
124   return NasalWindow::create
125   (
126     ctx.c,
127     requireGUIMgr(ctx.c).createWindow( ctx.getArg<std::string>(0) )
128   );
129 }
130
131 /**
132  * Get ghost for existing Canvas.
133  */
134 static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
135 {
136   nasal::CallContext ctx(c, argc, args);
137   SGPropertyNode& props = *ctx.requireArg<SGPropertyNode*>(0);
138   CanvasMgr& canvas_mgr = requireCanvasMgr(c);
139
140   sc::CanvasPtr canvas;
141   if( canvas_mgr.getPropertyRoot() == props.getParent() )
142   {
143     // get a canvas specified by its root node
144     canvas = canvas_mgr.getCanvas( props.getIndex() );
145     if( !canvas || canvas->getProps() != &props )
146       return naNil();
147   }
148   else
149   {
150     // get a canvas by name
151     if( props.hasValue("name") )
152       canvas = canvas_mgr.getCanvas( props.getStringValue("name") );
153     else if( props.hasValue("index") )
154       canvas = canvas_mgr.getCanvas( props.getIntValue("index") );
155   }
156
157   return NasalCanvas::create(c, canvas);
158 }
159
160 naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
161 {
162   return NasalGroup::create
163   (
164     ctx.c,
165     canvas.createGroup( ctx.getArg<std::string>(0) )
166   );
167 }
168
169 /**
170  * Get group containing all gui windows
171  */
172 naRef f_getDesktop(naContext c, naRef me, int argc, naRef* args)
173 {
174   return NasalGroup::create(c, requireGUIMgr(c).getDesktop());
175 }
176
177 naRef f_groupCreateChild(sc::Group& group, const nasal::CallContext& ctx)
178 {
179   return NasalElement::create
180   (
181     ctx.c,
182     group.createChild( ctx.requireArg<std::string>(0),
183                        ctx.getArg<std::string>(1) )
184   );
185 }
186
187 naRef f_groupGetChild(sc::Group& group, const nasal::CallContext& ctx)
188 {
189   return NasalElement::create
190   (
191     ctx.c,
192     group.getChild( ctx.requireArg<SGPropertyNode*>(0) )
193   );
194 }
195
196 naRef f_groupGetElementById(sc::Group& group, const nasal::CallContext& ctx)
197 {
198   return NasalElement::create
199   (
200     ctx.c,
201     group.getElementById( ctx.requireArg<std::string>(0) )
202   );
203 }
204
205 template<int Mask>
206 naRef f_eventGetModifier(sc::MouseEvent& event, naContext)
207 {
208   return naNum((event.getModifiers() & Mask) != 0);
209 }
210
211 static naRef f_createCustomEvent(const nasal::CallContext& ctx)
212 {
213   std::string const& type = ctx.requireArg<std::string>(0);
214   if( type.empty() )
215     return naNil();
216
217   simgear::StringMap data = ctx.getArg<simgear::StringMap>(1);
218   return NasalCustomEvent::create(
219     ctx.c,
220     sc::CustomEventPtr(new sc::CustomEvent(type, data))
221   );
222 }
223
224 struct CustomEventDetailWrapper:
225   public SGReferenced
226 {
227   sc::CustomEventPtr _event;
228
229   CustomEventDetailWrapper(const sc::CustomEventPtr& event):
230     _event(event)
231   {
232
233   }
234
235   bool _get( const std::string& key,
236              std::string& value_out ) const
237   {
238     if( !_event )
239       return false;
240
241     simgear::StringMap::const_iterator it = _event->detail.find(key);
242     if( it == _event->detail.end() )
243       return false;
244
245     value_out = it->second;
246     return true;
247   }
248
249   bool _set( const std::string& key,
250              const std::string& value )
251   {
252     if( !_event )
253       return false;
254
255     _event->detail[ key ] = value;
256     return true;
257   }
258 };
259
260 static naRef f_customEventGetDetail( sc::CustomEvent& event,
261                                      naContext c )
262 {
263   return nasal::to_nasal(
264     c,
265     CustomEventDetailPtr(new CustomEventDetailWrapper(&event))
266   );
267 }
268
269 naRef to_nasal_helper(naContext c, const sc::ElementWeakPtr& el)
270 {
271   return NasalElement::create(c, el.lock());
272 }
273
274 naRef initNasalCanvas(naRef globals, naContext c)
275 {
276   using osgGA::GUIEventAdapter;
277   NasalEvent::init("canvas.Event")
278     .member("type", &sc::Event::getTypeString)
279     .member("target", &sc::Event::getTarget)
280     .member("currentTarget", &sc::Event::getCurrentTarget)
281     .method("stopPropagation", &sc::Event::stopPropagation);
282
283   NasalCustomEvent::init("canvas.CustomEvent")
284     .bases<NasalEvent>()
285     .member("detail", &f_customEventGetDetail, &sc::CustomEvent::setDetail);
286   NasalCustomEventDetail::init("canvas.CustomEventDetail")
287     ._get(&CustomEventDetailWrapper::_get)
288     ._set(&CustomEventDetailWrapper::_set);
289
290   NasalMouseEvent::init("canvas.MouseEvent")
291     .bases<NasalEvent>()
292     .member("screenX", &sc::MouseEvent::getScreenX)
293     .member("screenY", &sc::MouseEvent::getScreenY)
294     .member("clientX", &sc::MouseEvent::getClientX)
295     .member("clientY", &sc::MouseEvent::getClientY)
296     .member("localX", &sc::MouseEvent::getLocalX)
297     .member("localY", &sc::MouseEvent::getLocalY)
298     .member("deltaX", &sc::MouseEvent::getDeltaX)
299     .member("deltaY", &sc::MouseEvent::getDeltaY)
300     .member("button", &sc::MouseEvent::getButton)
301     .member("buttons", &sc::MouseEvent::getButtonMask)
302     .member("modifiers", &sc::MouseEvent::getModifiers)
303     .member("ctrlKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_CTRL>)
304     .member("shiftKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_SHIFT>)
305     .member("altKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_ALT>)
306     .member("metaKey", &f_eventGetModifier<GUIEventAdapter::MODKEY_META>)
307     .member("click_count", &sc::MouseEvent::getCurrentClickCount);
308
309   NasalCanvas::init("Canvas")
310     .member("_node_ghost", &elementGetNode<sc::Canvas>)
311     .member("size_x", &sc::Canvas::getSizeX)
312     .member("size_y", &sc::Canvas::getSizeY)
313     .method("_createGroup", &f_canvasCreateGroup)
314     .method("_getGroup", &sc::Canvas::getGroup)
315     .method("addEventListener", &sc::Canvas::addEventListener);
316   NasalElement::init("canvas.Element")
317     .member("_node_ghost", &elementGetNode<sc::Element>)
318     .method("_getParent", &sc::Element::getParent)
319     .method("addEventListener", &sc::Element::addEventListener)
320     .method("dispatchEvent", &sc::Element::dispatchEvent)
321     .method("getBoundingBox", &sc::Element::getBoundingBox)
322     .method("getTightBoundingBox", &sc::Element::getTightBoundingBox);
323   NasalGroup::init("canvas.Group")
324     .bases<NasalElement>()
325     .method("_createChild", &f_groupCreateChild)
326     .method("_getChild", &f_groupGetChild)
327     .method("_getElementById", &f_groupGetElementById);
328   NasalText::init("canvas.Text")
329     .bases<NasalElement>()
330     .method("getNearestCursor", &sc::Text::getNearestCursor);
331
332   NasalWindow::init("canvas.Window")
333     .bases<NasalElement>()
334     .member("_node_ghost", &elementGetNode<sc::Window>)
335     .method("_getCanvasDecoration", &sc::Window::getCanvasDecoration);
336     
337   nasal::Hash globals_module(globals, c),
338               canvas_module = globals_module.createHash("canvas");
339
340   canvas_module.set("_newCanvasGhost", f_createCanvas);
341   canvas_module.set("_newWindowGhost", f_createWindow);
342   canvas_module.set("_getCanvasGhost", f_getCanvas);
343   canvas_module.set("_getDesktopGhost", f_getDesktop);
344
345   canvas_module.createHash("CustomEvent")
346                .set("new", &f_createCustomEvent);
347   return naNil();
348 }