]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalCanvas.cxx
Canvas MouseEvent now provides client and screen position
[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/MouseEvent.hxx>
37
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>
42
43 extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
44
45 namespace sc = simgear::canvas;
46
47 template<class Element>
48 naRef elementGetNode(naContext c, Element& element)
49 {
50   return propNodeGhostCreate(c, element.getProps());
51 }
52
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;
58
59 SGPropertyNode* from_nasal_helper(naContext c, naRef ref, SGPropertyNode**)
60 {
61   SGPropertyNode* props = ghostToPropNode(ref);
62   if( !props )
63     naRuntimeError(c, "Not a SGPropertyNode ghost.");
64
65   return props;
66 }
67
68 CanvasMgr& requireCanvasMgr(naContext c)
69 {
70   CanvasMgr* canvas_mgr =
71     static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
72   if( !canvas_mgr )
73     naRuntimeError(c, "Failed to get Canvas subsystem");
74
75   return *canvas_mgr;
76 }
77
78 /**
79  * Create new Canvas and get ghost for it.
80  */
81 static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
82 {
83   return NasalCanvas::create(c, requireCanvasMgr(c).createCanvas());
84 }
85
86 /**
87  * Get ghost for existing Canvas.
88  */
89 static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
90 {
91   nasal::CallContext ctx(c, argc, args);
92   SGPropertyNode& props = *ctx.requireArg<SGPropertyNode*>(0);
93   CanvasMgr& canvas_mgr = requireCanvasMgr(c);
94
95   sc::CanvasPtr canvas;
96   if( canvas_mgr.getPropertyRoot() == props.getParent() )
97   {
98     // get a canvas specified by its root node
99     canvas = canvas_mgr.getCanvas( props.getIndex() );
100     if( !canvas || canvas->getProps() != &props )
101       return naNil();
102   }
103   else
104   {
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") );
110   }
111
112   return NasalCanvas::create(c, canvas);
113 }
114
115 naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
116 {
117   return NasalGroup::create
118   (
119     ctx.c,
120     canvas.createGroup( ctx.getArg<std::string>(0) )
121   );
122 }
123
124 naRef f_elementGetTransformedBounds(sc::Element& el, const nasal::CallContext& ctx)
125 {
126   osg::BoundingBox bb = el.getTransformedBounds( osg::Matrix::identity() );
127
128   std::vector<float> bb_vec(4);
129   bb_vec[0] = bb._min.x();
130   bb_vec[1] = bb._min.y();
131   bb_vec[2] = bb._max.x();
132   bb_vec[3] = bb._max.y();
133
134   return nasal::to_nasal(ctx.c, bb_vec);
135 }
136
137 naRef f_groupCreateChild(sc::Group& group, const nasal::CallContext& ctx)
138 {
139   return NasalElement::create
140   (
141     ctx.c,
142     group.createChild( ctx.requireArg<std::string>(0),
143                        ctx.getArg<std::string>(1) )
144   );
145 }
146
147 naRef f_groupGetChild(sc::Group& group, const nasal::CallContext& ctx)
148 {
149   return NasalElement::create
150   (
151     ctx.c,
152     group.getChild( ctx.requireArg<SGPropertyNode*>(0) )
153   );
154 }
155
156 naRef f_groupGetElementById(sc::Group& group, const nasal::CallContext& ctx)
157 {
158   return NasalElement::create
159   (
160     ctx.c,
161     group.getElementById( ctx.requireArg<std::string>(0) )
162   );
163 }
164
165 // TODO allow directly exposing functions without parameters and return type
166 naRef f_eventStopPropagation(sc::Event& event, const nasal::CallContext& ctx)
167 {
168   if( ctx.argc != 0 )
169     naRuntimeError(ctx.c, "Event::stopPropagation no argument expected");
170   event.stopPropagation();
171   return naNil();
172 }
173
174 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
175 {
176   NasalEvent::init("canvas.Event")
177     .member("type", &sc::Event::getTypeString)
178     .method_func<&f_eventStopPropagation>("stopPropagation");
179   NasalMouseEvent::init("canvas.MouseEvent")
180     .bases<NasalEvent>()
181     .member("screenX", &sc::MouseEvent::getScreenX)
182     .member("screenY", &sc::MouseEvent::getScreenY)
183     .member("clientX", &sc::MouseEvent::getClientX)
184     .member("clientY", &sc::MouseEvent::getClientY)
185     .member("deltaX", &sc::MouseEvent::getDeltaX)
186     .member("deltaY", &sc::MouseEvent::getDeltaY)
187     .member("click_count", &sc::MouseEvent::getCurrentClickCount);
188   NasalCanvas::init("Canvas")
189     .member("_node_ghost", &elementGetNode<sc::Canvas>)
190     .member("size_x", &sc::Canvas::getSizeX)
191     .member("size_y", &sc::Canvas::getSizeY)
192     .method_func<&f_canvasCreateGroup>("_createGroup")
193     .method<&sc::Canvas::addEventListener>("addEventListener");
194   NasalElement::init("canvas.Element")
195     .member("_node_ghost", &elementGetNode<sc::Element>)
196     .method<&sc::Element::addEventListener>("addEventListener")
197     .method_func<&f_elementGetTransformedBounds>("getTransformedBounds");
198   NasalGroup::init("canvas.Group")
199     .bases<NasalElement>()
200     .method_func<&f_groupCreateChild>("_createChild")
201     .method_func<&f_groupGetChild>("_getChild")
202     .method_func<&f_groupGetElementById>("_getElementById");
203
204   nasal::Hash globals_module(globals, c),
205               canvas_module = globals_module.createHash("canvas");
206
207   canvas_module.set("_newCanvasGhost", f_createCanvas);
208   canvas_module.set("_getCanvasGhost", f_getCanvas);
209
210   return naNil();
211 }