1 // NasalCanvas.cxx -- expose Canvas classes to Nasal
3 // Written by James Turner, started 2012.
5 // Copyright (C) 2012 James Turner
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.
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.
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.
27 #include "NasalCanvas.hxx"
29 #include <boost/foreach.hpp>
30 #include <boost/algorithm/string/case_conv.hpp>
32 #include <osgGA/GUIEventAdapter>
34 #include <simgear/sg_inlines.h>
36 #include <simgear/canvas/Canvas.hxx>
37 #include <simgear/canvas/elements/CanvasElement.hxx>
39 static naRef canvasPrototype;
40 static naRef elementPrototype;
41 static naRef eventPrototype;
43 static void canvasGhostDestroy(void* g);
44 static void elementGhostDestroy(void* g);
45 static void eventGhostDestroy(void* g);
47 static const char* canvasGhostGetMember(naContext c, void* g, naRef field, naRef* out);
48 naGhostType CanvasGhostType = { canvasGhostDestroy, "canvas", canvasGhostGetMember, 0 };
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 };
55 static const char* eventGhostGetMember(naContext c, void* g, naRef field, naRef* out);
56 naGhostType EventGhostType = { eventGhostDestroy, "ga-event", eventGhostGetMember, 0 };
58 static void hashset(naContext c, naRef hash, const char* key, naRef val)
60 naRef s = naNewString(c);
61 naStr_fromdata(s, (char*)key, strlen(key));
62 naHash_set(hash, s, val);
65 static naRef stringToNasal(naContext c, const std::string& s)
67 return naStr_fromdata(naNewString(c),
68 const_cast<char *>(s.c_str()),
72 static naRef eventTypeToNasal(naContext c, osgGA::GUIEventAdapter::EventType 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");
91 static simgear::canvas::Element* elementGhost(naRef r)
93 if (naGhost_type(r) == &ElementGhostType)
94 return (simgear::canvas::Element*) naGhost_ptr(r);
98 static simgear::canvas::Canvas* canvasGhost(naRef r)
100 if (naGhost_type(r) == &CanvasGhostType)
101 return (simgear::canvas::Canvas*) naGhost_ptr(r);
105 static void elementGhostDestroy(void* g)
109 static void canvasGhostDestroy(void* g)
113 static void eventGhostDestroy(void* g)
115 osgGA::GUIEventAdapter* gea = static_cast<osgGA::GUIEventAdapter*>(g);
119 static const char* eventGhostGetMember(naContext c, void* g, naRef field, naRef* out)
121 const char* fieldName = naStr_data(field);
122 osgGA::GUIEventAdapter* gea = (osgGA::GUIEventAdapter*) g;
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());
139 static const char* canvasGhostGetMember(naContext c, void* g, naRef field, naRef* out)
141 const char* fieldName = naStr_data(field);
142 simgear::canvas::Canvas* cvs = (simgear::canvas::Canvas*) g;
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());
156 static const char* elementGhostGetMember(naContext c, void* g, naRef field, naRef* out)
158 const char* fieldName = naStr_data(field);
159 simgear::canvas::Element* e = (simgear::canvas::Element*) g;
162 if (!strcmp(fieldName, "parents")) {
163 *out = naNewVector(c);
164 naVec_append(*out, elementPrototype);
172 static void elementGhostSetMember(naContext c, void* g, naRef field, naRef value)
174 const char* fieldName = naStr_data(field);
175 simgear::canvas::Element* e = (simgear::canvas::Element*) g;
176 SG_UNUSED(fieldName);
181 static naRef f_canvas_getElement(naContext c, naRef me, int argc, naRef* args)
183 simgear::canvas::Canvas* cvs = canvasGhost(me);
185 naRuntimeError(c, "canvas.getElement called on non-canvas object");
191 static naRef f_element_addButtonCallback(naContext c, naRef me, int argc, naRef* args)
193 simgear::canvas::Element* e = elementGhost(me);
195 naRuntimeError(c, "element.addButtonCallback called on non-canvas-element object");
201 static naRef f_element_addDragCallback(naContext c, naRef me, int argc, naRef* args)
203 simgear::canvas::Element* e = elementGhost(me);
205 naRuntimeError(c, "element.addDragCallback called on non-canvas-element object");
211 static naRef f_element_addMoveCallback(naContext c, naRef me, int argc, naRef* args)
213 simgear::canvas::Element* e = elementGhost(me);
215 naRuntimeError(c, "element.addMoveCallback called on non-canvas-element object");
221 static naRef f_element_addScrollCallback(naContext c, naRef me, int argc, naRef* args)
223 simgear::canvas::Element* e = elementGhost(me);
225 naRuntimeError(c, "element.addScrollCallback called on non-canvas-element object");
231 static naRef f_canvas(naContext c, naRef me, int argc, naRef* args)
236 // Table of extension functions. Terminate with zeros.
237 static struct { const char* name; naCFunction func; } funcs[] = {
238 { "getCanvas", f_canvas },
242 naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
244 canvasPrototype = naNewHash(c);
245 hashset(c, gcSave, "canvasProto", canvasPrototype);
247 hashset(c, canvasPrototype, "getElement", naNewFunc(c, naNewCCode(c, f_canvas_getElement)));
249 eventPrototype = naNewHash(c);
250 hashset(c, gcSave, "eventProto", eventPrototype);
251 // set any event methods
253 elementPrototype = naNewHash(c);
254 hashset(c, gcSave, "elementProto", elementPrototype);
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)));
261 for(int i=0; funcs[i].name; i++) {
262 hashset(c, globals, funcs[i].name,
263 naNewFunc(c, naNewCCode(c, funcs[i].func)));