# include "config.h"
#endif
-#include <memory>
-#include <string.h>
-
#include "NasalCanvas.hxx"
#include <Canvas/canvas_mgr.hxx>
+#include <Canvas/gui_mgr.hxx>
+#include <Canvas/window.hxx>
#include <Main/globals.hxx>
+#include <Scripting/NasalSys.hxx>
-//#include <boost/python.hpp>
-#include <boost/foreach.hpp>
-#include <boost/algorithm/string/case_conv.hpp>
-#include <boost/make_shared.hpp>
#include <osgGA/GUIEventAdapter>
#include <simgear/sg_inlines.h>
#include <simgear/canvas/Canvas.hxx>
#include <simgear/canvas/elements/CanvasElement.hxx>
+#include <simgear/canvas/elements/CanvasText.hxx>
+#include <simgear/canvas/MouseEvent.hxx>
#include <simgear/nasal/cppbind/from_nasal.hxx>
#include <simgear/nasal/cppbind/to_nasal.hxx>
extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
-//void initCanvasPython()
-//{
-// using namespace boost::python;
-// class_<simgear::canvas::Canvas>("Canvas");
-//}
-
namespace sc = simgear::canvas;
-naRef canvasGetNode(naContext c, sc::Canvas* canvas)
+template<class Element>
+naRef elementGetNode(naContext c, Element& element)
{
- return propNodeGhostCreate(c, canvas->getProps());
+ return propNodeGhostCreate(c, element.getProps());
}
-struct Base
+typedef nasal::Ghost<sc::EventPtr> NasalEvent;
+typedef nasal::Ghost<sc::MouseEventPtr> NasalMouseEvent;
+typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
+typedef nasal::Ghost<sc::ElementPtr> NasalElement;
+typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
+typedef nasal::Ghost<sc::TextPtr> NasalText;
+typedef nasal::Ghost<canvas::WindowWeakPtr> NasalWindow;
+
+SGPropertyNode* from_nasal_helper(naContext c, naRef ref, SGPropertyNode**)
{
- int getInt() const
- {
- return 8;
- }
-};
+ SGPropertyNode* props = ghostToPropNode(ref);
+ if( !props )
+ naRuntimeError(c, "Not a SGPropertyNode ghost.");
+
+ return props;
+}
-struct Test:
- public Base
+CanvasMgr& requireCanvasMgr(naContext c)
{
- Test(): x(1) {}
- int x;
- void setX(int x_) { x = x_; }
- naRef test(int argc, naRef* args)
- {
- return naNil();
- }
-};
+ CanvasMgr* canvas_mgr =
+ static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
+ if( !canvas_mgr )
+ naRuntimeError(c, "Failed to get Canvas subsystem");
-typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
+ return *canvas_mgr;
+}
-void initCanvas(naContext c)
+GUIMgr& requireGUIMgr(naContext c)
{
+ GUIMgr* mgr =
+ static_cast<GUIMgr*>(globals->get_subsystem("CanvasGUI"));
+ if( !mgr )
+ naRuntimeError(c, "Failed to get CanvasGUI subsystem");
- NasalCanvas::init("Canvas")
- .member("_node_ghost", &canvasGetNode)
- .member("size_x", &sc::Canvas::getSizeX)
- .member("size_y", &sc::Canvas::getSizeY);
- nasal::Ghost<sc::ElementPtr>::init("canvas.Element");
- nasal::Ghost<sc::GroupPtr>::init("canvas.Group")
- .bases<sc::ElementPtr>();
-
- nasal::Ghost<Base>::init("BaseClass")
- .member("int", &Base::getInt);
- nasal::Ghost<Test>::init("TestClass")
- .bases<Base>()
- .member("x", &Test::setX)
- .method<&Test::test>("test");
+ return *mgr;
}
-#if 0
/**
- * Class for exposing C++ objects to Nasal
+ * Create new Canvas and get ghost for it.
*/
-template<class T, class Derived>
-class NasalObject
+static naRef f_createCanvas(const nasal::CallContext& ctx)
{
- public:
- // TODO use variadic template when supporting C++11
- template<class A1>
- static naRef create( naContext c, const A1& a1 )
- {
- return makeGhost(c, new T(a1));
- }
-
- template<class A1, class A2>
- static naRef create( naContext c, const A1& a1,
- const A2& a2 )
- {
- return makeGhost(c, new T(a1, a2));
- }
-
- template<class A1, class A2, class A3>
- static naRef create( naContext c, const A1& a1,
- const A2& a2,
- const A3& a3 )
- {
- return makeGhost(c, new T(a1, a2, a3));
- }
-
- template<class A1, class A2, class A3, class A4>
- static naRef create( naContext c, const A1& a1,
- const A2& a2,
- const A3& a3,
- const A4& a4 )
- {
- return makeGhost(c, new T(a1, a2, a3, a4));
- }
-
- template<class A1, class A2, class A3, class A4, class A5>
- static naRef create( naContext c, const A1& a1,
- const A2& a2,
- const A3& a3,
- const A4& a4,
- const A5& a5 )
- {
- return makeGhost(c, new T(a1, a2, a3, a4, a5));
- }
-
- // TODO If you need more arguments just do some copy&paste :)
-
- static Derived& getInstance()
- {
- static Derived instance;
- return instance;
- }
-
- void setParent(const naRef& parent)
- {
- // TODO check if we need to take care of reference counting/gc
- _parents.resize(1);
- _parents[0] = parent;
- }
-
- protected:
-
- // TODO switch to boost::/std::function (with C++11 lambdas this can make
- // adding setters easier and shorter)
- typedef naRef (Derived::*getter_t)(naContext, const T&);
- typedef std::map<std::string, getter_t> MemberMap;
-
- const std::string _ghost_name;
- std::vector<naRef> _parents;
- MemberMap _members;
-
- NasalObject(const std::string& ghost_name):
- _ghost_name( ghost_name )
- {
- _ghost_type.destroy = &destroyGhost;
- _ghost_type.name = _ghost_name.c_str();
- _ghost_type.get_member = &Derived::getMember;
- _ghost_type.set_member = 0;
-
- _members["parents"] = &NasalObject::getParents;
- }
-
- naRef getParents(naContext c, const T&)
- {
- naRef parents = naNewVector(c);
- for(size_t i = 0; i < _parents.size(); ++i)
- naVec_append(parents, _parents[i]);
- return parents;
- }
-
- static naRef makeGhost(naContext c, void *ptr)
- {
- std::cout << "create " << ptr << std::endl;
- return naNewGhost2(c, &(getInstance()._ghost_type), ptr);
- }
-
- static void destroyGhost(void *ptr)
- {
- std::cout << "destroy " << ptr << std::endl;
- delete (T*)ptr;
- }
-
- static const char* getMember(naContext c, void* g, naRef field, naRef* out)
- {
- typename MemberMap::iterator getter =
- getInstance()._members.find(naStr_data(field));
-
- if( getter == getInstance()._members.end() )
- return 0;
-
- *out = (getInstance().*getter->second)(c, *static_cast<T*>(g));
- return "";
- }
-
- private:
-
- naGhostType _ghost_type;
-
-};
-
-typedef osg::ref_ptr<osgGA::GUIEventAdapter> GUIEventPtr;
-
-class NasalCanvasEvent:
- public NasalObject<GUIEventPtr, NasalCanvasEvent>
+ return NasalCanvas::create(ctx.c, requireCanvasMgr(ctx.c).createCanvas());
+}
+
+/**
+ * Create new Window and get ghost for it.
+ */
+static naRef f_createWindow(const nasal::CallContext& ctx)
{
- public:
-
- NasalCanvasEvent():
- NasalObject("CanvasEvent")
- {
- _members["type"] = &NasalCanvasEvent::getEventType;
- }
-
- naRef getEventType(naContext c, const GUIEventPtr& event)
- {
-#define RET_EVENT_STR(type, str)\
- case osgGA::GUIEventAdapter::type:\
- return nasal::to_nasal(c, str);
-
- switch( event->getEventType() )
- {
- RET_EVENT_STR(PUSH, "push");
- RET_EVENT_STR(RELEASE, "release");
- RET_EVENT_STR(DOUBLECLICK, "double-click");
- RET_EVENT_STR(DRAG, "drag");
- RET_EVENT_STR(MOVE, "move");
- RET_EVENT_STR(SCROLL, "scroll");
- RET_EVENT_STR(KEYUP, "key-up");
- RET_EVENT_STR(KEYDOWN, "key-down");
-
-#undef RET_EVENT_STR
-
- default:
- return naNil();
- }
- }
-};
-#endif
-#if 0
-static const char* eventGhostGetMember(naContext c, void* g, naRef field, naRef* out)
+ return NasalWindow::create
+ (
+ ctx.c,
+ requireGUIMgr(ctx.c).createWindow( ctx.getArg<std::string>(0) )
+ );
+}
+
+/**
+ * Get ghost for existing Canvas.
+ */
+static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
{
- const char* fieldName = naStr_data(field);
- osgGA::GUIEventAdapter* gea = (osgGA::GUIEventAdapter*) g;
-
- if (!strcmp(fieldName, "windowX")) *out = naNum(gea->getWindowX());
- else if (!strcmp(fieldName, "windowY")) *out = naNum(gea->getWindowY());
- else if (!strcmp(fieldName, "time")) *out = naNum(gea->getTime());
- else if (!strcmp(fieldName, "button")) *out = naNum(gea->getButton());
- else {
- return 0;
+ nasal::CallContext ctx(c, argc, args);
+ SGPropertyNode& props = *ctx.requireArg<SGPropertyNode*>(0);
+ CanvasMgr& canvas_mgr = requireCanvasMgr(c);
+
+ sc::CanvasPtr canvas;
+ if( canvas_mgr.getPropertyRoot() == props.getParent() )
+ {
+ // get a canvas specified by its root node
+ canvas = canvas_mgr.getCanvas( props.getIndex() );
+ if( !canvas || canvas->getProps() != &props )
+ return naNil();
+ }
+ else
+ {
+ // get a canvas by name
+ if( props.hasValue("name") )
+ canvas = canvas_mgr.getCanvas( props.getStringValue("name") );
+ else if( props.hasValue("index") )
+ canvas = canvas_mgr.getCanvas( props.getIntValue("index") );
}
- return "";
+ return NasalCanvas::create(c, canvas);
}
-static naRef f_element_addButtonCallback(naContext c, naRef me, int argc, naRef* args)
+naRef f_canvasCreateGroup(sc::Canvas& canvas, const nasal::CallContext& ctx)
{
- simgear::canvas::Element* e = elementGhost(me);
- if (!e) {
- naRuntimeError(c, "element.addButtonCallback called on non-canvas-element object");
- }
-
- return naNil();
+ return NasalGroup::create
+ (
+ ctx.c,
+ canvas.createGroup( ctx.getArg<std::string>(0) )
+ );
}
-static naRef f_element_addDragCallback(naContext c, naRef me, int argc, naRef* args)
+/**
+ * Get group containing all gui windows
+ */
+naRef f_getDesktop(naContext c, naRef me, int argc, naRef* args)
{
- simgear::canvas::Element* e = elementGhost(me);
- if (!e) {
- naRuntimeError(c, "element.addDragCallback called on non-canvas-element object");
- }
-
- return naNil();
+ return NasalGroup::create(c, requireGUIMgr(c).getDesktop());
}
-static naRef f_element_addMoveCallback(naContext c, naRef me, int argc, naRef* args)
+naRef f_elementGetTransformedBounds(sc::Element& el, const nasal::CallContext& ctx)
{
- simgear::canvas::Element* e = elementGhost(me);
- if (!e) {
- naRuntimeError(c, "element.addMoveCallback called on non-canvas-element object");
- }
-
- return naNil();
+ osg::BoundingBox bb = el.getTransformedBounds( osg::Matrix::identity() );
+
+ std::vector<float> bb_vec(4);
+ bb_vec[0] = bb._min.x();
+ bb_vec[1] = bb._min.y();
+ bb_vec[2] = bb._max.x();
+ bb_vec[3] = bb._max.y();
+
+ return nasal::to_nasal(ctx.c, bb_vec);
}
-static naRef f_element_addScrollCallback(naContext c, naRef me, int argc, naRef* args)
+naRef f_groupCreateChild(sc::Group& group, const nasal::CallContext& ctx)
{
- simgear::canvas::Element* e = elementGhost(me);
- if (!e) {
- naRuntimeError(c, "element.addScrollCallback called on non-canvas-element object");
- }
-
- return naNil();
+ return NasalElement::create
+ (
+ ctx.c,
+ group.createChild( ctx.requireArg<std::string>(0),
+ ctx.getArg<std::string>(1) )
+ );
}
-#endif
-static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
+naRef f_groupGetChild(sc::Group& group, const nasal::CallContext& ctx)
{
- std::cout << "f_createCanvas" << std::endl;
+ return NasalElement::create
+ (
+ ctx.c,
+ group.getChild( ctx.requireArg<SGPropertyNode*>(0) )
+ );
+}
- CanvasMgr* canvas_mgr =
- static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
- if( !canvas_mgr )
- return naNil();
+naRef f_groupGetElementById(sc::Group& group, const nasal::CallContext& ctx)
+{
+ return NasalElement::create
+ (
+ ctx.c,
+ group.getElementById( ctx.requireArg<std::string>(0) )
+ );
+}
- return NasalCanvas::create(c, canvas_mgr->createCanvas());
+naRef to_nasal_helper(naContext c, const sc::ElementWeakPtr& el)
+{
+ return NasalElement::create(c, el.lock());
}
-naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
+naRef initNasalCanvas(naRef globals, naContext c)
{
- /*naNewHash(c);
- hashset(c, gcSave, "canvasProto", canvasPrototype);
-
- hashset(c, canvasPrototype, "getElement", naNewFunc(c, naNewCCode(c, f_canvas_getElement)));*/
- // set any event methods
-
-#if 0
- elementPrototype = naNewHash(c);
- hashset(c, gcSave, "elementProto", elementPrototype);
-
- hashset(c, elementPrototype, "addButtonCallback", naNewFunc(c, naNewCCode(c, f_element_addButtonCallback)));
- hashset(c, elementPrototype, "addDragCallback", naNewFunc(c, naNewCCode(c, f_element_addDragCallback)));
- hashset(c, elementPrototype, "addMoveCallback", naNewFunc(c, naNewCCode(c, f_element_addMoveCallback)));
- hashset(c, elementPrototype, "addScrollCallback", naNewFunc(c, naNewCCode(c, f_element_addScrollCallback)));
-#endif
+ NasalEvent::init("canvas.Event")
+ .member("type", &sc::Event::getTypeString)
+ .member("target", &sc::Event::getTarget)
+ .member("currentTarget", &sc::Event::getCurrentTarget)
+ .method("stopPropagation", &sc::Event::stopPropagation);
+ NasalMouseEvent::init("canvas.MouseEvent")
+ .bases<NasalEvent>()
+ .member("screenX", &sc::MouseEvent::getScreenX)
+ .member("screenY", &sc::MouseEvent::getScreenY)
+ .member("clientX", &sc::MouseEvent::getClientX)
+ .member("clientY", &sc::MouseEvent::getClientY)
+ .member("localX", &sc::MouseEvent::getLocalX)
+ .member("localY", &sc::MouseEvent::getLocalY)
+ .member("deltaX", &sc::MouseEvent::getDeltaX)
+ .member("deltaY", &sc::MouseEvent::getDeltaY)
+ .member("click_count", &sc::MouseEvent::getCurrentClickCount);
+ NasalCanvas::init("Canvas")
+ .member("_node_ghost", &elementGetNode<sc::Canvas>)
+ .member("size_x", &sc::Canvas::getSizeX)
+ .member("size_y", &sc::Canvas::getSizeY)
+ .method("_createGroup", &f_canvasCreateGroup)
+ .method("_getGroup", &sc::Canvas::getGroup)
+ .method("addEventListener", &sc::Canvas::addEventListener);
+ NasalElement::init("canvas.Element")
+ .member("_node_ghost", &elementGetNode<sc::Element>)
+ .method("addEventListener", &sc::Element::addEventListener)
+ .method("getTransformedBounds", &f_elementGetTransformedBounds);
+ NasalGroup::init("canvas.Group")
+ .bases<NasalElement>()
+ .method("_createChild", &f_groupCreateChild)
+ .method("_getChild", &f_groupGetChild)
+ .method("_getElementById", &f_groupGetElementById);
+ NasalText::init("canvas.Text")
+ .bases<NasalElement>()
+ .method("getNearestCursor", &sc::Text::getNearestCursor);
+
+ NasalWindow::init("canvas.Window")
+ .bases<NasalElement>()
+ .member("_node_ghost", &elementGetNode<canvas::Window>)
+ .method("_getCanvasDecoration", &canvas::Window::getCanvasDecoration);
+
nasal::Hash globals_module(globals, c),
canvas_module = globals_module.createHash("canvas");
- canvas_module.set("_new", f_createCanvas);
- canvas_module.set("testClass", nasal::Ghost<Test>::f_create);
-
- initCanvas(c);
+ canvas_module.set("_newCanvasGhost", f_createCanvas);
+ canvas_module.set("_newWindowGhost", f_createWindow);
+ canvas_module.set("_getCanvasGhost", f_getCanvas);
+ canvas_module.set("_getDesktopGhost", f_getDesktop);
return naNil();
}