]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalCanvas.cxx
2a9faaa1b48e31ba5b51afc43f3947755f79ae35
[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 #if 0
60 typedef osg::ref_ptr<osgGA::GUIEventAdapter> GUIEventPtr;
61
62 class NasalCanvasEvent:
63   public NasalObject<GUIEventPtr, NasalCanvasEvent>
64 {
65   public:
66
67     naRef getEventType(naContext c, const GUIEventPtr& event)
68     {
69 #define RET_EVENT_STR(type, str)\
70   case osgGA::GUIEventAdapter::type:\
71     return nasal::to_nasal(c, str);
72
73       switch( event->getEventType() )
74       {
75         RET_EVENT_STR(PUSH,         "push");
76         RET_EVENT_STR(RELEASE,      "release");
77         RET_EVENT_STR(DOUBLECLICK,  "double-click");
78         RET_EVENT_STR(DRAG,         "drag");
79         RET_EVENT_STR(MOVE,         "move");
80         RET_EVENT_STR(SCROLL,       "scroll");
81         RET_EVENT_STR(KEYUP,        "key-up");
82         RET_EVENT_STR(KEYDOWN,      "key-down");
83
84 #undef RET_EVENT_STR
85
86         default:
87           return naNil();
88       }
89     }
90 };
91 #endif
92
93 SGPropertyNode& requireArg(naContext c, int argc, naRef* args, int index = 0)
94 {
95   if( argc <= index )
96     naRuntimeError(c, "missing argument #%d", index);
97
98   SGPropertyNode* props = ghostToPropNode(args[index]);
99   if( !props )
100     naRuntimeError(c, "arg #%d: not a SGPropertyNode ghost");
101
102   return *props;
103 }
104
105 CanvasMgr& requireCanvasMgr(naContext c)
106 {
107   CanvasMgr* canvas_mgr =
108     static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
109   if( !canvas_mgr )
110     naRuntimeError(c, "Failed to get Canvas subsystem");
111
112   return *canvas_mgr;
113 }
114
115 /**
116  * Create new Canvas and get ghost for it.
117  */
118 static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
119 {
120   return NasalCanvas::create(c, requireCanvasMgr(c).createCanvas());
121 }
122
123 /**
124  * Get ghost for existing Canvas.
125  */
126 static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
127 {
128   SGPropertyNode& props = requireArg(c, argc, args);
129   CanvasMgr& canvas_mgr = requireCanvasMgr(c);
130
131   sc::CanvasPtr canvas;
132   if( canvas_mgr.getPropertyRoot() == props.getParent() )
133   {
134     // get a canvas specified by its root node
135     canvas = canvas_mgr.getCanvas( props.getIndex() );
136     if( !canvas || canvas->getProps() != &props )
137       return naNil();
138   }
139   else
140   {
141     // get a canvas by name
142     if( props.hasValue("name") )
143       canvas = canvas_mgr.getCanvas( props.getStringValue("name") );
144     else if( props.hasValue("index") )
145       canvas = canvas_mgr.getCanvas( props.getIntValue("index") );
146   }
147
148   return NasalCanvas::create(c, canvas);
149 }
150
151 naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
152 {
153   std::string name;
154   if( ctx.argc > 0 )
155     name = nasal::from_nasal<std::string>(ctx.c, ctx.args[0]);
156
157   return NasalGroup::create(ctx.c, canvas.createGroup(name));
158 }
159
160 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
161 {
162   NasalEvent::init("canvas.Event");
163   NasalMouseEvent::init("canvas.MouseEvent")
164     .bases<NasalEvent>();
165   NasalCanvas::init("Canvas")
166     .member("_node_ghost", &elementGetNode<sc::Canvas>)
167     .member("size_x", &sc::Canvas::getSizeX)
168     .member("size_y", &sc::Canvas::getSizeY)
169     .method_func<&f_canvasCreateGroup>("createGroup");
170   NasalElement::init("canvas.Element")
171     .member("_node_ghost", &elementGetNode<sc::Element>)
172     .method<&sc::Element::addEventListener>("addEventListener");
173   NasalGroup::init("canvas.Group")
174     .bases<NasalElement>();
175
176   nasal::Hash globals_module(globals, c),
177               canvas_module = globals_module.createHash("canvas");
178
179   canvas_module.set("_newCanvasGhost", f_createCanvas);
180   canvas_module.set("_getCanvasGhost", f_getCanvas);
181
182   return naNil();
183 }