]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalCanvas.cxx
Canvas: Proper fix for OpenVG init handling.
[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 <string.h>
26
27 #include "NasalCanvas.hxx"
28
29 #include <boost/foreach.hpp>
30 #include <boost/algorithm/string/case_conv.hpp>
31
32 #include <osgGA/GUIEventAdapter>
33
34 #include <simgear/sg_inlines.h>
35
36 #include <Canvas/canvas.hxx>
37 #include <Canvas/elements/element.hxx>
38
39 static naRef canvasPrototype;
40 static naRef elementPrototype;
41 static naRef eventPrototype;
42
43 static void canvasGhostDestroy(void* g);
44 static void elementGhostDestroy(void* g);
45 static void eventGhostDestroy(void* g);
46
47 static const char* canvasGhostGetMember(naContext c, void* g, naRef field, naRef* out);
48 naGhostType CanvasGhostType = { canvasGhostDestroy, "canvas", canvasGhostGetMember, 0 };
49
50 static const char* elementGhostGetMember(naContext c, void* g, naRef field, naRef* out);
51 static void elementGhostSetMember(naContext c, void* g, naRef field, naRef value);
52 naGhostType ElementGhostType = { elementGhostDestroy, "canvas.element", 
53     elementGhostGetMember, elementGhostSetMember };
54
55 static const char* eventGhostGetMember(naContext c, void* g, naRef field, naRef* out);
56 naGhostType EventGhostType = { eventGhostDestroy, "ga-event", eventGhostGetMember, 0 };
57
58 static void hashset(naContext c, naRef hash, const char* key, naRef val)
59 {
60   naRef s = naNewString(c);
61   naStr_fromdata(s, (char*)key, strlen(key));
62   naHash_set(hash, s, val);
63 }
64
65 static naRef stringToNasal(naContext c, const std::string& s)
66 {
67     return naStr_fromdata(naNewString(c),
68                    const_cast<char *>(s.c_str()), 
69                    s.length());
70 }
71
72 static naRef eventTypeToNasal(naContext c, osgGA::GUIEventAdapter::EventType ty)
73 {
74   switch (ty) {
75   case osgGA::GUIEventAdapter::PUSH: return stringToNasal(c, "push");
76   case osgGA::GUIEventAdapter::RELEASE: return stringToNasal(c, "release");
77   case osgGA::GUIEventAdapter::DOUBLECLICK: return stringToNasal(c, "double-click");
78   case osgGA::GUIEventAdapter::DRAG: return stringToNasal(c, "drag");
79   case osgGA::GUIEventAdapter::MOVE: return stringToNasal(c, "move");
80   case osgGA::GUIEventAdapter::SCROLL: return stringToNasal(c, "scroll");
81   case osgGA::GUIEventAdapter::KEYUP: return stringToNasal(c, "key-up");
82   case osgGA::GUIEventAdapter::KEYDOWN: return stringToNasal(c, "key-down");
83
84   default:
85       break;
86   }
87   
88   return naNil();
89 }
90
91 static canvas::Element* elementGhost(naRef r)
92 {
93   if (naGhost_type(r) == &ElementGhostType)
94     return (canvas::Element*) naGhost_ptr(r);
95   return 0;
96 }
97
98 static Canvas* canvasGhost(naRef r)
99 {
100   if (naGhost_type(r) == &CanvasGhostType)
101     return (Canvas*) naGhost_ptr(r);
102   return 0;
103 }
104
105 static void elementGhostDestroy(void* g)
106 {
107 }
108
109 static void canvasGhostDestroy(void* g)
110 {
111 }
112
113 static void eventGhostDestroy(void* g)
114 {
115     osgGA::GUIEventAdapter* gea = static_cast<osgGA::GUIEventAdapter*>(g);
116     gea->unref();
117 }
118
119 static const char* eventGhostGetMember(naContext c, void* g, naRef field, naRef* out)
120 {
121   const char* fieldName = naStr_data(field);
122   osgGA::GUIEventAdapter* gea = (osgGA::GUIEventAdapter*) g;
123   
124   if (!strcmp(fieldName, "parents")) {
125     *out = naNewVector(c);
126     naVec_append(*out, eventPrototype);
127   } else if (!strcmp(fieldName, "type")) *out = eventTypeToNasal(c, gea->getEventType());
128   else if (!strcmp(fieldName, "windowX")) *out = naNum(gea->getWindowX());
129   else if (!strcmp(fieldName, "windowY")) *out = naNum(gea->getWindowY());
130   else if (!strcmp(fieldName, "time")) *out = naNum(gea->getTime());
131   else if (!strcmp(fieldName, "button")) *out = naNum(gea->getButton());
132   else {
133     return 0;
134   }
135   
136   return "";
137 }
138
139 static const char* canvasGhostGetMember(naContext c, void* g, naRef field, naRef* out)
140 {
141   const char* fieldName = naStr_data(field);
142   Canvas* cvs = (Canvas*) g;
143   
144   if (!strcmp(fieldName, "parents")) {
145     *out = naNewVector(c);
146     naVec_append(*out, canvasPrototype);
147   } else if (!strcmp(fieldName, "sizeX")) *out = naNum(cvs->getSizeX());
148   else if (!strcmp(fieldName, "sizeY")) *out = naNum(cvs->getSizeY());
149   else {
150     return 0;
151   }
152   
153   return "";
154 }
155
156 static const char* elementGhostGetMember(naContext c, void* g, naRef field, naRef* out)
157 {
158   const char* fieldName = naStr_data(field);
159   canvas::Element* e = (canvas::Element*) g;
160   SG_UNUSED(e);
161   
162   if (!strcmp(fieldName, "parents")) {
163     *out = naNewVector(c);
164     naVec_append(*out, elementPrototype);
165   } else {
166     return 0;
167   }
168   
169   return "";
170 }
171
172 static void elementGhostSetMember(naContext c, void* g, naRef field, naRef value)
173 {
174   const char* fieldName = naStr_data(field);
175   canvas::Element* e = (canvas::Element*) g;
176   SG_UNUSED(fieldName);
177   SG_UNUSED(e);
178 }
179
180
181 static naRef f_canvas_getElement(naContext c, naRef me, int argc, naRef* args)
182 {
183   Canvas* cvs = canvasGhost(me);
184   if (!cvs) {
185     naRuntimeError(c, "canvas.getElement called on non-canvas object");
186   }
187   
188   return naNil();
189 }
190
191 static naRef f_element_addButtonCallback(naContext c, naRef me, int argc, naRef* args)
192 {
193   canvas::Element* e = elementGhost(me);
194   if (!e) {
195     naRuntimeError(c, "element.addButtonCallback called on non-canvas-element object");
196   }
197   
198   return naNil();
199 }
200
201 static naRef f_element_addDragCallback(naContext c, naRef me, int argc, naRef* args)
202 {
203   canvas::Element* e = elementGhost(me);
204   if (!e) {
205     naRuntimeError(c, "element.addDragCallback called on non-canvas-element object");
206   }
207   
208   return naNil();
209 }
210
211 static naRef f_element_addMoveCallback(naContext c, naRef me, int argc, naRef* args)
212 {
213   canvas::Element* e = elementGhost(me);
214   if (!e) {
215     naRuntimeError(c, "element.addMoveCallback called on non-canvas-element object");
216   }
217   
218   return naNil();
219 }
220
221 static naRef f_element_addScrollCallback(naContext c, naRef me, int argc, naRef* args)
222 {
223   canvas::Element* e = elementGhost(me);
224   if (!e) {
225     naRuntimeError(c, "element.addScrollCallback called on non-canvas-element object");
226   }
227   
228   return naNil();
229 }
230
231 static naRef f_canvas(naContext c, naRef me, int argc, naRef* args)
232 {
233   return naNil();
234 }
235
236 // Table of extension functions.  Terminate with zeros.
237 static struct { const char* name; naCFunction func; } funcs[] = {
238   { "getCanvas", f_canvas },
239   { 0, 0 }
240 };
241
242 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
243 {
244     canvasPrototype = naNewHash(c);
245     hashset(c, gcSave, "canvasProto", canvasPrototype);
246   
247     hashset(c, canvasPrototype, "getElement", naNewFunc(c, naNewCCode(c, f_canvas_getElement)));
248     
249     eventPrototype = naNewHash(c);
250     hashset(c, gcSave, "eventProto", eventPrototype);
251     // set any event methods
252   
253     elementPrototype = naNewHash(c);
254     hashset(c, gcSave, "elementProto", elementPrototype);
255     
256     hashset(c, elementPrototype, "addButtonCallback", naNewFunc(c, naNewCCode(c, f_element_addButtonCallback)));
257     hashset(c, elementPrototype, "addDragCallback", naNewFunc(c, naNewCCode(c, f_element_addDragCallback)));
258     hashset(c, elementPrototype, "addMoveCallback", naNewFunc(c, naNewCCode(c, f_element_addMoveCallback)));
259     hashset(c, elementPrototype, "addScrollCallback", naNewFunc(c, naNewCCode(c, f_element_addScrollCallback)));
260       
261     for(int i=0; funcs[i].name; i++) {
262       hashset(c, globals, funcs[i].name,
263       naNewFunc(c, naNewCCode(c, funcs[i].func)));
264     }
265   
266   return naNil();
267 }