]> git.mxchange.org Git - simgear.git/commitdiff
Canvas/Layout: tweak the way elements are exposed to Nasal.
authorThomas Geymayer <tomgey@gmail.com>
Sun, 1 Jun 2014 10:00:33 +0000 (12:00 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Sun, 1 Jun 2014 10:12:28 +0000 (12:12 +0200)
simgear/canvas/Canvas.hxx
simgear/canvas/layout/LayoutItem.hxx
simgear/canvas/layout/NasalWidget.cxx
simgear/canvas/layout/NasalWidget.hxx
simgear/nasal/cppbind/CMakeLists.txt
simgear/nasal/cppbind/NasalObject.cxx [new file with mode: 0644]
simgear/nasal/cppbind/NasalObject.hxx [new file with mode: 0644]

index c2de8d483f7ec496ad78a4eb1088636e5b820a54..2c817f61bcbf98fe17805b505847204df1d41f7f 100644 (file)
 #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:
 
index 18e56b9284aa5394a7645d0b1390a0690997eec7..740ccd1a97034d29bf9661ccfbf87353698a4255 100644 (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);
 
index a8532c9e62c92bce3f524d856ffb3b2457184b07..62e0b78f6fe137aa7b65814d3cca32000803f7b3 100644 (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
   {
index d9e80450ba323d930980ff28db3ff3cfc2f0f416..e07ffb5389d034a6099e1151b771ef0f970695de 100644 (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;
index 946d09d4c966bbbef5d62e2d97054e81facb44f2..4dba88f4440132c11d686b67d13c794822e38e72 100644 (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
 )
diff --git a/simgear/nasal/cppbind/NasalObject.cxx b/simgear/nasal/cppbind/NasalObject.cxx
new file mode 100644 (file)
index 0000000..ac1ead9
--- /dev/null
@@ -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
diff --git a/simgear/nasal/cppbind/NasalObject.hxx b/simgear/nasal/cppbind/NasalObject.hxx
new file mode 100644 (file)
index 0000000..bcdd990
--- /dev/null
@@ -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_ */