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

This commit is contained in:
Thomas Geymayer
2014-06-01 12:12:28 +02:00
parent 820a315cbe
commit 150039f9ba
7 changed files with 160 additions and 47 deletions
+4 -1
View File
@@ -25,8 +25,10 @@
#include <simgear/canvas/elements/CanvasGroup.hxx> #include <simgear/canvas/elements/CanvasGroup.hxx>
#include <simgear/canvas/layout/Layout.hxx> #include <simgear/canvas/layout/Layout.hxx>
#include <simgear/math/SGRect.hxx> #include <simgear/math/SGRect.hxx>
#include <simgear/nasal/cppbind/NasalObject.hxx>
#include <simgear/props/PropertyBasedElement.hxx> #include <simgear/props/PropertyBasedElement.hxx>
#include <simgear/props/propertyObject.hxx> #include <simgear/props/propertyObject.hxx>
#include <osg/NodeCallback> #include <osg/NodeCallback>
#include <osg/observer_ptr> #include <osg/observer_ptr>
@@ -42,7 +44,8 @@ namespace canvas
class MouseEvent; class MouseEvent;
class Canvas: class Canvas:
public PropertyBasedElement public PropertyBasedElement,
public nasal::Object
{ {
public: public:
+1 -1
View File
@@ -93,7 +93,7 @@ namespace canvas
CanvasPtr getCanvas() const; 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); void setParent(const LayoutItemWeakRef& parent);
+20 -36
View File
@@ -28,9 +28,9 @@ namespace canvas
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
NasalWidget::NasalWidget(naRef impl): NasalWidget::NasalWidget(naRef impl):
_nasal_impl(impl) Object(impl)
{ {
assert( naIsHash(_nasal_impl.get_naRef()) );
} }
//---------------------------------------------------------------------------- //----------------------------------------------------------------------------
@@ -38,11 +38,25 @@ namespace canvas
{ {
if( _geometry == geom ) if( _geometry == geom )
return; return;
_geometry = geom; _geometry = geom;
if( _set_geometry ) if( !_set_geometry )
_set_geometry(_nasal_impl.get_naRef(), geom); 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; _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) void NasalWidget::setSizeHint(const SGVec2i& s)
{ {
@@ -95,16 +97,6 @@ namespace canvas
invalidateParent(); 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) static naRef f_makeNasalWidget(const nasal::CallContext& ctx)
{ {
@@ -118,8 +110,7 @@ namespace canvas
{ {
nasal::Ghost<NasalWidgetRef>::init("canvas.Widget") nasal::Ghost<NasalWidgetRef>::init("canvas.Widget")
.bases<LayoutItemRef>() .bases<LayoutItemRef>()
._set(&NasalWidget::_set) .bases<nasal::ObjectRef>()
.member("parents", &NasalWidget::getParents)
.method("setSetGeometryFunc", &NasalWidget::setSetGeometryFunc) .method("setSetGeometryFunc", &NasalWidget::setSetGeometryFunc)
.method("setSizeHint", &NasalWidget::setSizeHint) .method("setSizeHint", &NasalWidget::setSizeHint)
.method("setMinimumSize", &NasalWidget::setMinimumSize) .method("setMinimumSize", &NasalWidget::setMinimumSize)
@@ -129,13 +120,6 @@ namespace canvas
widget_hash.set("new", &f_makeNasalWidget); 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 SGVec2i NasalWidget::sizeHintImpl() const
{ {
+3 -9
View File
@@ -23,6 +23,7 @@
#include <simgear/nasal/cppbind/from_nasal.hxx> #include <simgear/nasal/cppbind/from_nasal.hxx>
#include <simgear/nasal/cppbind/NasalHash.hxx> #include <simgear/nasal/cppbind/NasalHash.hxx>
#include <simgear/nasal/cppbind/NasalObject.hxx>
namespace simgear namespace simgear
{ {
@@ -33,7 +34,8 @@ namespace canvas
* Baseclass/ghost to create widgets with Nasal. * Baseclass/ghost to create widgets with Nasal.
*/ */
class NasalWidget: class NasalWidget:
public LayoutItem public LayoutItem,
public nasal::Object
{ {
public: public:
@@ -50,15 +52,10 @@ namespace canvas
void setSetGeometryFunc(const SetGeometryFunc& func); void setSetGeometryFunc(const SetGeometryFunc& func);
void setImpl(naRef obj);
naRef getImpl() const;
void setSizeHint(const SGVec2i& s); void setSizeHint(const SGVec2i& s);
void setMinimumSize(const SGVec2i& s); void setMinimumSize(const SGVec2i& s);
void setMaximumSize(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 * @param ns Namespace to register the class interface
*/ */
@@ -66,9 +63,6 @@ namespace canvas
protected: protected:
SetGeometryFunc _set_geometry; SetGeometryFunc _set_geometry;
nasal::ObjectHolder<> _nasal_impl;
static naRef getParents(NasalWidget&, naContext);
virtual SGVec2i sizeHintImpl() const; virtual SGVec2i sizeHintImpl() const;
virtual SGVec2i minimumSizeImpl() const; virtual SGVec2i minimumSizeImpl() const;
+2
View File
@@ -4,6 +4,7 @@ set(HEADERS
Ghost.hxx Ghost.hxx
NasalCallContext.hxx NasalCallContext.hxx
NasalHash.hxx NasalHash.hxx
NasalObject.hxx
NasalObjectHolder.hxx NasalObjectHolder.hxx
NasalString.hxx NasalString.hxx
from_nasal.hxx from_nasal.hxx
@@ -21,6 +22,7 @@ set(DETAIL_HEADERS
set(SOURCES set(SOURCES
NasalHash.cxx NasalHash.cxx
NasalString.cxx NasalString.cxx
NasalObject.cxx
detail/from_nasal_helper.cxx detail/from_nasal_helper.cxx
detail/to_nasal_helper.cxx detail/to_nasal_helper.cxx
) )
+72
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
+58
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_ */