2012-08-02 12:18:38 +01:00
|
|
|
// NasalCanvas.cxx -- expose Canvas classes to Nasal
|
|
|
|
//
|
|
|
|
// Written by James Turner, started 2012.
|
|
|
|
//
|
|
|
|
// Copyright (C) 2012 James Turner
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as
|
|
|
|
// published by the Free Software Foundation; either version 2 of the
|
|
|
|
// License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "NasalCanvas.hxx"
|
2012-11-09 00:06:17 +01:00
|
|
|
#include <Canvas/canvas_mgr.hxx>
|
|
|
|
#include <Main/globals.hxx>
|
2012-11-18 16:38:35 +01:00
|
|
|
#include <Scripting/NasalSys.hxx>
|
2012-08-02 12:18:38 +01:00
|
|
|
|
|
|
|
#include <osgGA/GUIEventAdapter>
|
|
|
|
|
|
|
|
#include <simgear/sg_inlines.h>
|
|
|
|
|
2012-11-04 14:18:31 +01:00
|
|
|
#include <simgear/canvas/Canvas.hxx>
|
2012-11-04 23:03:06 +01:00
|
|
|
#include <simgear/canvas/elements/CanvasElement.hxx>
|
2012-08-02 12:18:38 +01:00
|
|
|
|
2012-11-12 12:13:06 +01:00
|
|
|
#include <simgear/nasal/cppbind/from_nasal.hxx>
|
2012-11-09 19:33:03 +01:00
|
|
|
#include <simgear/nasal/cppbind/to_nasal.hxx>
|
|
|
|
#include <simgear/nasal/cppbind/NasalHash.hxx>
|
2012-11-12 12:13:06 +01:00
|
|
|
#include <simgear/nasal/cppbind/Ghost.hxx>
|
2012-08-02 12:18:38 +01:00
|
|
|
|
2012-11-09 00:06:17 +01:00
|
|
|
extern naRef propNodeGhostCreate(naContext c, SGPropertyNode* n);
|
2012-08-02 12:18:38 +01:00
|
|
|
|
2012-11-09 00:06:17 +01:00
|
|
|
namespace sc = simgear::canvas;
|
|
|
|
|
2012-11-16 12:34:38 +01:00
|
|
|
template<class Element>
|
|
|
|
naRef elementGetNode(naContext c, Element& element)
|
2012-08-02 12:18:38 +01:00
|
|
|
{
|
2012-11-16 12:34:38 +01:00
|
|
|
return propNodeGhostCreate(c, element.getProps());
|
2012-08-02 12:18:38 +01:00
|
|
|
}
|
|
|
|
|
2012-11-12 12:13:06 +01:00
|
|
|
typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
|
2012-11-16 12:34:38 +01:00
|
|
|
typedef nasal::Ghost<sc::ElementPtr> NasalElement;
|
2012-11-12 23:28:53 +01:00
|
|
|
typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
|
2012-08-02 12:18:38 +01:00
|
|
|
|
2012-11-09 19:33:03 +01:00
|
|
|
#if 0
|
2012-11-09 00:06:17 +01:00
|
|
|
typedef osg::ref_ptr<osgGA::GUIEventAdapter> GUIEventPtr;
|
|
|
|
|
|
|
|
class NasalCanvasEvent:
|
|
|
|
public NasalObject<GUIEventPtr, NasalCanvasEvent>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
naRef getEventType(naContext c, const GUIEventPtr& event)
|
|
|
|
{
|
|
|
|
#define RET_EVENT_STR(type, str)\
|
|
|
|
case osgGA::GUIEventAdapter::type:\
|
2012-11-09 19:33:03 +01:00
|
|
|
return nasal::to_nasal(c, str);
|
2012-11-09 00:06:17 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2012-11-09 19:33:03 +01:00
|
|
|
#endif
|
2012-08-02 12:18:38 +01:00
|
|
|
|
2012-11-18 16:38:35 +01:00
|
|
|
SGPropertyNode& requireArg(naContext c, int argc, naRef* args, int index = 0)
|
|
|
|
{
|
|
|
|
if( argc <= index )
|
|
|
|
naRuntimeError(c, "missing argument #%d", index);
|
|
|
|
|
|
|
|
SGPropertyNode* props = ghostToPropNode(args[index]);
|
|
|
|
if( !props )
|
|
|
|
naRuntimeError(c, "arg #%d: not a SGPropertyNode ghost");
|
|
|
|
|
|
|
|
return *props;
|
|
|
|
}
|
|
|
|
|
|
|
|
CanvasMgr& requireCanvasMgr(naContext c)
|
2012-08-02 12:18:38 +01:00
|
|
|
{
|
2012-11-09 00:06:17 +01:00
|
|
|
CanvasMgr* canvas_mgr =
|
|
|
|
static_cast<CanvasMgr*>(globals->get_subsystem("Canvas"));
|
|
|
|
if( !canvas_mgr )
|
2012-11-18 16:38:35 +01:00
|
|
|
naRuntimeError(c, "Failed to get Canvas subsystem");
|
2012-11-09 00:06:17 +01:00
|
|
|
|
2012-11-18 16:38:35 +01:00
|
|
|
return *canvas_mgr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new Canvas and get ghost for it.
|
|
|
|
*/
|
|
|
|
static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
|
|
|
|
{
|
|
|
|
return NasalCanvas::create(c, requireCanvasMgr(c).createCanvas());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get ghost for existing Canvas.
|
|
|
|
*/
|
|
|
|
static naRef f_getCanvas(naContext c, naRef me, int argc, naRef* args)
|
|
|
|
{
|
|
|
|
SGPropertyNode& props = requireArg(c, argc, args);
|
|
|
|
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 NasalCanvas::create(c, canvas);
|
2012-08-02 12:18:38 +01:00
|
|
|
}
|
|
|
|
|
2012-11-12 23:28:53 +01:00
|
|
|
naRef f_canvasCreateGroup( sc::Canvas& canvas,
|
|
|
|
naContext c,
|
|
|
|
int argc,
|
|
|
|
naRef* args )
|
|
|
|
{
|
|
|
|
std::string name;
|
|
|
|
if( argc > 0 )
|
|
|
|
name = nasal::from_nasal<std::string>(c, args[0]);
|
|
|
|
|
|
|
|
return NasalGroup::create(c, canvas.createGroup(name));
|
|
|
|
}
|
|
|
|
|
2012-08-02 12:18:38 +01:00
|
|
|
naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
|
|
|
|
{
|
2012-11-12 23:28:53 +01:00
|
|
|
NasalCanvas::init("Canvas")
|
2012-11-16 12:34:38 +01:00
|
|
|
.member("_node_ghost", &elementGetNode<sc::Canvas>)
|
2012-11-12 23:28:53 +01:00
|
|
|
.member("size_x", &sc::Canvas::getSizeX)
|
|
|
|
.member("size_y", &sc::Canvas::getSizeY)
|
2012-11-13 12:43:51 +01:00
|
|
|
.method_func<&f_canvasCreateGroup>("createGroup");
|
2012-11-16 12:34:38 +01:00
|
|
|
NasalElement::init("canvas.Element")
|
|
|
|
.member("_node_ghost", &elementGetNode<sc::Element>);
|
|
|
|
NasalGroup::init("canvas.Group")
|
|
|
|
.bases<NasalElement>();
|
2012-11-12 23:28:53 +01:00
|
|
|
|
2012-11-09 19:33:03 +01:00
|
|
|
nasal::Hash globals_module(globals, c),
|
|
|
|
canvas_module = globals_module.createHash("canvas");
|
2012-11-09 00:06:17 +01:00
|
|
|
|
2012-11-12 23:28:53 +01:00
|
|
|
canvas_module.set("_newCanvasGhost", f_createCanvas);
|
2012-11-18 16:38:35 +01:00
|
|
|
canvas_module.set("_getCanvasGhost", f_getCanvas);
|
2012-11-09 00:06:17 +01:00
|
|
|
|
2012-08-02 12:18:38 +01:00
|
|
|
return naNil();
|
|
|
|
}
|