Canvas/Layout: tweak the way elements are exposed to Nasal.

This commit is contained in:
Thomas Geymayer 2014-06-01 12:00:33 +02:00
parent 820a315cbe
commit 150039f9ba
7 changed files with 160 additions and 47 deletions

View File

@ -25,8 +25,10 @@
#include <simgear/canvas/elements/CanvasGroup.hxx>
#include <simgear/canvas/layout/Layout.hxx>
#include <simgear/math/SGRect.hxx>
#include <simgear/nasal/cppbind/NasalObject.hxx>
#include <simgear/props/PropertyBasedElement.hxx>
#include <simgear/props/propertyObject.hxx>
#include <osg/NodeCallback>
#include <osg/observer_ptr>
@ -42,7 +44,8 @@ namespace canvas
class MouseEvent;
class Canvas:
public PropertyBasedElement
public PropertyBasedElement,
public nasal::Object
{
public:

View File

@ -93,7 +93,7 @@ namespace canvas
CanvasPtr getCanvas() const;
/**
* Set the parent layout item (usally this is a layout).
* Set the parent layout item (usually this is a layout).
*/
void setParent(const LayoutItemWeakRef& parent);

View File

@ -28,9 +28,9 @@ namespace canvas
//----------------------------------------------------------------------------
NasalWidget::NasalWidget(naRef impl):
_nasal_impl(impl)
Object(impl)
{
assert( naIsHash(_nasal_impl.get_naRef()) );
}
//----------------------------------------------------------------------------
@ -38,11 +38,25 @@ namespace canvas
{
if( _geometry == geom )
return;
_geometry = geom;
if( _set_geometry )
_set_geometry(_nasal_impl.get_naRef(), geom);
if( !_set_geometry )
return;
naContext c = naNewContext();
try
{
_set_geometry(nasal::to_nasal(c, this), geom);
}
catch( std::exception const& ex )
{
SG_LOG(
SG_GUI,
SG_WARN,
"NasalWidget::setGeometry: callback error: '" << ex.what() << "'"
);
}
naFreeContext(c);
}
//----------------------------------------------------------------------------
@ -51,18 +65,6 @@ namespace canvas
_set_geometry = func;
}
//----------------------------------------------------------------------------
void NasalWidget::setImpl(naRef obj)
{
_nasal_impl.reset(obj);
}
//----------------------------------------------------------------------------
naRef NasalWidget::getImpl() const
{
return _nasal_impl.get_naRef();
}
//----------------------------------------------------------------------------
void NasalWidget::setSizeHint(const SGVec2i& s)
{
@ -95,16 +97,6 @@ namespace canvas
invalidateParent();
}
//----------------------------------------------------------------------------
bool NasalWidget::_set(naContext c, const std::string& key, naRef val)
{
if( !_nasal_impl.valid() )
return false;
nasal::Hash(_nasal_impl.get_naRef(), c).set(key, val);
return true;
}
//----------------------------------------------------------------------------
static naRef f_makeNasalWidget(const nasal::CallContext& ctx)
{
@ -118,8 +110,7 @@ namespace canvas
{
nasal::Ghost<NasalWidgetRef>::init("canvas.Widget")
.bases<LayoutItemRef>()
._set(&NasalWidget::_set)
.member("parents", &NasalWidget::getParents)
.bases<nasal::ObjectRef>()
.method("setSetGeometryFunc", &NasalWidget::setSetGeometryFunc)
.method("setSizeHint", &NasalWidget::setSizeHint)
.method("setMinimumSize", &NasalWidget::setMinimumSize)
@ -129,13 +120,6 @@ namespace canvas
widget_hash.set("new", &f_makeNasalWidget);
}
//----------------------------------------------------------------------------
naRef NasalWidget::getParents(NasalWidget& w, naContext c)
{
naRef parents[] = { w._nasal_impl.get_naRef() };
return nasal::to_nasal(c, parents);
}
//----------------------------------------------------------------------------
SGVec2i NasalWidget::sizeHintImpl() const
{

View File

@ -23,6 +23,7 @@
#include <simgear/nasal/cppbind/from_nasal.hxx>
#include <simgear/nasal/cppbind/NasalHash.hxx>
#include <simgear/nasal/cppbind/NasalObject.hxx>
namespace simgear
{
@ -33,7 +34,8 @@ namespace canvas
* Baseclass/ghost to create widgets with Nasal.
*/
class NasalWidget:
public LayoutItem
public LayoutItem,
public nasal::Object
{
public:
@ -50,15 +52,10 @@ namespace canvas
void setSetGeometryFunc(const SetGeometryFunc& func);
void setImpl(naRef obj);
naRef getImpl() const;
void setSizeHint(const SGVec2i& s);
void setMinimumSize(const SGVec2i& s);
void setMaximumSize(const SGVec2i& s);
bool _set(naContext c, const std::string& key, naRef val);
/**
* @param ns Namespace to register the class interface
*/
@ -66,9 +63,6 @@ namespace canvas
protected:
SetGeometryFunc _set_geometry;
nasal::ObjectHolder<> _nasal_impl;
static naRef getParents(NasalWidget&, naContext);
virtual SGVec2i sizeHintImpl() const;
virtual SGVec2i minimumSizeImpl() const;

View File

@ -4,6 +4,7 @@ set(HEADERS
Ghost.hxx
NasalCallContext.hxx
NasalHash.hxx
NasalObject.hxx
NasalObjectHolder.hxx
NasalString.hxx
from_nasal.hxx
@ -21,6 +22,7 @@ set(DETAIL_HEADERS
set(SOURCES
NasalHash.cxx
NasalString.cxx
NasalObject.cxx
detail/from_nasal_helper.cxx
detail/to_nasal_helper.cxx
)

View File

@ -0,0 +1,72 @@
// Object exposed to Nasal including a Nasal hash for data storage.
//
// Copyright (C) 2014 Thomas Geymayer <tomgey@gmail.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#include "NasalObject.hxx"
#include <simgear/nasal/cppbind/NasalHash.hxx>
namespace nasal
{
//----------------------------------------------------------------------------
Object::Object(naRef impl):
_nasal_impl(impl)
{
}
//----------------------------------------------------------------------------
void Object::setImpl(naRef obj)
{
_nasal_impl.reset(obj);
}
//----------------------------------------------------------------------------
naRef Object::getImpl() const
{
return _nasal_impl.get_naRef();
}
//----------------------------------------------------------------------------
bool Object::_set(naContext c, const std::string& key, naRef val)
{
if( !_nasal_impl.valid() )
_nasal_impl.reset(naNewHash(c));
nasal::Hash(_nasal_impl.get_naRef(), c).set(key, val);
return true;
}
//----------------------------------------------------------------------------
bool Object::_get(naContext c, const std::string& key, naRef& out)
{
if( !_nasal_impl.valid() )
return false;
return naHash_get(_nasal_impl.get_naRef(), to_nasal(c, key), &out);
}
//----------------------------------------------------------------------------
void Object::setupGhost()
{
NasalObject::init("Object")
._set(&Object::_set)
._get(&Object::_get)
.member("_impl", &Object::getImpl, &Object::setImpl);
}
} // namespace nasal

View File

@ -0,0 +1,58 @@
///@file Object exposed to Nasal including a Nasal hash for data storage.
//
// Copyright (C) 2014 Thomas Geymayer <tomgey@gmail.com>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library 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
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
#ifndef SG_NASAL_OBJECT_HXX_
#define SG_NASAL_OBJECT_HXX_
#include "NasalObjectHolder.hxx"
#include "Ghost.hxx"
namespace nasal
{
class Object:
public virtual SGVirtualWeakReferenced
{
public:
/**
*
* @param impl Initial implementation hash (nasal part of
* implementation)
*/
Object(naRef impl = naNil());
void setImpl(naRef obj);
naRef getImpl() const;
bool _set(naContext c, const std::string& key, naRef val);
bool _get(naContext c, const std::string& key, naRef& out);
static void setupGhost();
protected:
ObjectHolder<> _nasal_impl;
};
typedef SGSharedPtr<Object> ObjectRef;
typedef SGWeakPtr<Object> ObjectWeakRef;
typedef Ghost<ObjectRef> NasalObject;
} // namespace nasal
#endif /* SG_NASAL_OBJECT_HXX_ */