#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>
class MouseEvent;
class Canvas:
- public PropertyBasedElement
+ public PropertyBasedElement,
+ public nasal::Object
{
public:
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);
//----------------------------------------------------------------------------
NasalWidget::NasalWidget(naRef impl):
- _nasal_impl(impl)
+ Object(impl)
{
- assert( naIsHash(_nasal_impl.get_naRef()) );
+
}
//----------------------------------------------------------------------------
{
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);
}
//----------------------------------------------------------------------------
_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)
{
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)
{
{
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)
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
{
#include <simgear/nasal/cppbind/from_nasal.hxx>
#include <simgear/nasal/cppbind/NasalHash.hxx>
+#include <simgear/nasal/cppbind/NasalObject.hxx>
namespace simgear
{
* Baseclass/ghost to create widgets with Nasal.
*/
class NasalWidget:
- public LayoutItem
+ public LayoutItem,
+ public nasal::Object
{
public:
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
*/
protected:
SetGeometryFunc _set_geometry;
- nasal::ObjectHolder<> _nasal_impl;
-
- static naRef getParents(NasalWidget&, naContext);
virtual SGVec2i sizeHintImpl() const;
virtual SGVec2i minimumSizeImpl() const;
Ghost.hxx
NasalCallContext.hxx
NasalHash.hxx
+ NasalObject.hxx
NasalObjectHolder.hxx
NasalString.hxx
from_nasal.hxx
set(SOURCES
NasalHash.cxx
NasalString.cxx
+ NasalObject.cxx
detail/from_nasal_helper.cxx
detail/to_nasal_helper.cxx
)
--- /dev/null
+// 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
--- /dev/null
+///@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_ */