From: Thomas Geymayer Date: Sun, 1 Jun 2014 10:00:33 +0000 (+0200) Subject: Canvas/Layout: tweak the way elements are exposed to Nasal. X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=150039f9ba6cd8fcc2b7ea5b00c0cb2511baeaf3;p=simgear.git Canvas/Layout: tweak the way elements are exposed to Nasal. --- diff --git a/simgear/canvas/Canvas.hxx b/simgear/canvas/Canvas.hxx index c2de8d48..2c817f61 100644 --- a/simgear/canvas/Canvas.hxx +++ b/simgear/canvas/Canvas.hxx @@ -25,8 +25,10 @@ #include #include #include +#include #include #include + #include #include @@ -42,7 +44,8 @@ namespace canvas class MouseEvent; class Canvas: - public PropertyBasedElement + public PropertyBasedElement, + public nasal::Object { public: diff --git a/simgear/canvas/layout/LayoutItem.hxx b/simgear/canvas/layout/LayoutItem.hxx index 18e56b92..740ccd1a 100644 --- a/simgear/canvas/layout/LayoutItem.hxx +++ b/simgear/canvas/layout/LayoutItem.hxx @@ -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); diff --git a/simgear/canvas/layout/NasalWidget.cxx b/simgear/canvas/layout/NasalWidget.cxx index a8532c9e..62e0b78f 100644 --- a/simgear/canvas/layout/NasalWidget.cxx +++ b/simgear/canvas/layout/NasalWidget.cxx @@ -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::init("canvas.Widget") .bases() - ._set(&NasalWidget::_set) - .member("parents", &NasalWidget::getParents) + .bases() .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 { diff --git a/simgear/canvas/layout/NasalWidget.hxx b/simgear/canvas/layout/NasalWidget.hxx index d9e80450..e07ffb53 100644 --- a/simgear/canvas/layout/NasalWidget.hxx +++ b/simgear/canvas/layout/NasalWidget.hxx @@ -23,6 +23,7 @@ #include #include +#include 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; diff --git a/simgear/nasal/cppbind/CMakeLists.txt b/simgear/nasal/cppbind/CMakeLists.txt index 946d09d4..4dba88f4 100644 --- a/simgear/nasal/cppbind/CMakeLists.txt +++ b/simgear/nasal/cppbind/CMakeLists.txt @@ -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 ) diff --git a/simgear/nasal/cppbind/NasalObject.cxx b/simgear/nasal/cppbind/NasalObject.cxx new file mode 100644 index 00000000..ac1ead9d --- /dev/null +++ b/simgear/nasal/cppbind/NasalObject.cxx @@ -0,0 +1,72 @@ +// Object exposed to Nasal including a Nasal hash for data storage. +// +// Copyright (C) 2014 Thomas Geymayer +// +// 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 + +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 diff --git a/simgear/nasal/cppbind/NasalObject.hxx b/simgear/nasal/cppbind/NasalObject.hxx new file mode 100644 index 00000000..bcdd9908 --- /dev/null +++ b/simgear/nasal/cppbind/NasalObject.hxx @@ -0,0 +1,58 @@ +///@file Object exposed to Nasal including a Nasal hash for data storage. +// +// Copyright (C) 2014 Thomas Geymayer +// +// 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 ObjectRef; + typedef SGWeakPtr ObjectWeakRef; + typedef Ghost NasalObject; + +} // namespace nasal + +#endif /* SG_NASAL_OBJECT_HXX_ */