]> git.mxchange.org Git - flightgear.git/blob - src/Scripting/NasalCanvas.cxx
bbafebd0a4be62150e6facdd354795c2f0973732
[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       ; // fall through
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   
161   if (!strcmp(fieldName, "parents")) {
162     *out = naNewVector(c);
163     naVec_append(*out, elementPrototype);
164   } else {
165     return 0;
166   }
167   
168   return "";
169 }
170
171 static void elementGhostSetMember(naContext c, void* g, naRef field, naRef value)
172 {
173   const char* fieldName = naStr_data(field);
174   canvas::Element* e = (canvas::Element*) g;
175 }
176
177
178 static naRef f_canvas_getElement(naContext c, naRef me, int argc, naRef* args)
179 {
180   Canvas* cvs = canvasGhost(me);
181   if (!cvs) {
182     naRuntimeError(c, "canvas.getElement called on non-canvas object");
183   }
184   
185   return naNil();
186 }
187
188 static naRef f_element_addButtonCallback(naContext c, naRef me, int argc, naRef* args)
189 {
190   canvas::Element* e = elementGhost(me);
191   if (!e) {
192     naRuntimeError(c, "element.addButtonCallback called on non-canvas-element object");
193   }
194   
195   return naNil();
196 }
197
198 static naRef f_element_addDragCallback(naContext c, naRef me, int argc, naRef* args)
199 {
200   canvas::Element* e = elementGhost(me);
201   if (!e) {
202     naRuntimeError(c, "element.addDragCallback called on non-canvas-element object");
203   }
204   
205   return naNil();
206 }
207
208 static naRef f_element_addMoveCallback(naContext c, naRef me, int argc, naRef* args)
209 {
210   canvas::Element* e = elementGhost(me);
211   if (!e) {
212     naRuntimeError(c, "element.addMoveCallback called on non-canvas-element object");
213   }
214   
215   return naNil();
216 }
217
218 static naRef f_element_addScrollCallback(naContext c, naRef me, int argc, naRef* args)
219 {
220   canvas::Element* e = elementGhost(me);
221   if (!e) {
222     naRuntimeError(c, "element.addScrollCallback called on non-canvas-element object");
223   }
224   
225   return naNil();
226 }
227
228 static naRef f_canvas(naContext c, naRef me, int argc, naRef* args)
229 {
230   return naNil();
231 }
232
233 // Table of extension functions.  Terminate with zeros.
234 static struct { const char* name; naCFunction func; } funcs[] = {
235   { "canvas", f_canvas },
236   { 0, 0 }
237 };
238
239 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
240 {
241     canvasPrototype = naNewHash(c);
242     hashset(c, gcSave, "canvasProto", canvasPrototype);
243   
244     hashset(c, canvasPrototype, "getElement", naNewFunc(c, naNewCCode(c, f_canvas_getElement)));
245     
246     eventPrototype = naNewHash(c);
247     hashset(c, gcSave, "eventProto", eventPrototype);
248     // set any event methods
249   
250     elementPrototype = naNewHash(c);
251     hashset(c, gcSave, "elementProto", elementPrototype);
252     
253     hashset(c, elementPrototype, "addButtonCallback", naNewFunc(c, naNewCCode(c, f_element_addButtonCallback)));
254     hashset(c, elementPrototype, "addDragCallback", naNewFunc(c, naNewCCode(c, f_element_addDragCallback)));
255     hashset(c, elementPrototype, "addMoveCallback", naNewFunc(c, naNewCCode(c, f_element_addMoveCallback)));
256     hashset(c, elementPrototype, "addScrollCallback", naNewFunc(c, naNewCCode(c, f_element_addScrollCallback)));
257       
258     for(int i=0; funcs[i].name; i++) {
259       hashset(c, globals, funcs[i].name,
260       naNewFunc(c, naNewCCode(c, funcs[i].func)));
261     }
262   
263   return naNil();
264 }