]> git.mxchange.org Git - flightgear.git/commitdiff
Expose canvas::Window ghost to Nasal.
authorThomas Geymayer <tomgey@gmail.com>
Mon, 3 Jun 2013 21:43:46 +0000 (23:43 +0200)
committerThomas Geymayer <tomgey@gmail.com>
Mon, 3 Jun 2013 21:46:34 +0000 (23:46 +0200)
src/Canvas/gui_mgr.cxx
src/Canvas/gui_mgr.hxx
src/Scripting/NasalCanvas.cxx

index 006dd2d689ee601f1e5ca9db2400aa916f774d3b..3bb86bc6960e6b989fbfeac8f385f71ce81979d3 100644 (file)
@@ -157,6 +157,12 @@ GUIMgr::GUIMgr():
   stateSet->setMode(GL_DEPTH_TEST, osg::StateAttribute::OFF);
 }
 
+//------------------------------------------------------------------------------
+canvas::WindowPtr GUIMgr::createWindow(const std::string& name)
+{
+  return boost::static_pointer_cast<canvas::Window>( createElement(name) );
+}
+
 //------------------------------------------------------------------------------
 void GUIMgr::init()
 {
@@ -187,6 +193,30 @@ void GUIMgr::shutdown()
          ->removeEventHandler( _event_handler );
 }
 
+//------------------------------------------------------------------------------
+bool GUIMgr::handleEvent(const osgGA::GUIEventAdapter& ea)
+{
+  switch( ea.getEventType() )
+  {
+    case osgGA::GUIEventAdapter::PUSH:
+    case osgGA::GUIEventAdapter::RELEASE:
+//    case osgGA::GUIEventAdapter::DOUBLECLICK:
+//    // DOUBLECLICK doesn't seem to be triggered...
+    case osgGA::GUIEventAdapter::DRAG:
+    case osgGA::GUIEventAdapter::MOVE:
+    case osgGA::GUIEventAdapter::SCROLL:
+      return handleMouse(ea);
+    case osgGA::GUIEventAdapter::RESIZE:
+      handleResize( ea.getWindowX(),
+                    ea.getWindowY(),
+                    ea.getWindowWidth(),
+                    ea.getWindowHeight() );
+      return false; // Let other event handlers also consume resize events
+    default:
+      return false;
+  }
+}
+
 //------------------------------------------------------------------------------
 void GUIMgr::elementCreated(simgear::PropertyBasedElementPtr element)
 {
@@ -213,30 +243,6 @@ void GUIMgr::elementCreated(simgear::PropertyBasedElementPtr element)
   layer->addChild(window->getGroup());
 }
 
-//------------------------------------------------------------------------------
-bool GUIMgr::handleEvent(const osgGA::GUIEventAdapter& ea)
-{
-  switch( ea.getEventType() )
-  {
-    case osgGA::GUIEventAdapter::PUSH:
-    case osgGA::GUIEventAdapter::RELEASE:
-//    case osgGA::GUIEventAdapter::DOUBLECLICK:
-//    // DOUBLECLICK doesn't seem to be triggered...
-    case osgGA::GUIEventAdapter::DRAG:
-    case osgGA::GUIEventAdapter::MOVE:
-    case osgGA::GUIEventAdapter::SCROLL:
-      return handleMouse(ea);
-    case osgGA::GUIEventAdapter::RESIZE:
-      handleResize( ea.getWindowX(),
-                    ea.getWindowY(),
-                    ea.getWindowWidth(),
-                    ea.getWindowHeight() );
-      return false; // Let other event handlers also consume resize events
-    default:
-      return false;
-  }
-}
-
 //------------------------------------------------------------------------------
 canvas::WindowPtr GUIMgr::getWindow(size_t i)
 {
index 00319dd47bdcd3c77aed4dfe4e0bec0cdd35720f..408dd62c56ef377804e4bf58dd2ea496ffe81c4c 100644 (file)
@@ -41,11 +41,11 @@ class GUIMgr:
   public:
     GUIMgr();
 
+    canvas::WindowPtr createWindow(const std::string& name = "");
+
     virtual void init();
     virtual void shutdown();
 
-    virtual void elementCreated(simgear::PropertyBasedElementPtr element);
-
     bool handleEvent(const osgGA::GUIEventAdapter& ea);
 
   protected:
@@ -68,6 +68,8 @@ class GUIMgr:
           _last_y;
     double _last_scroll_time;
 
+    virtual void elementCreated(simgear::PropertyBasedElementPtr element);
+
     canvas::WindowPtr getWindow(size_t i);
     simgear::canvas::Placements
     addPlacement(SGPropertyNode*, simgear::canvas::CanvasPtr canvas);
index 8f6671d2875d6ee2aeb7d5aa92b3f58cae0f9b9f..f6ab47ac529a9e098defc67e6ca399dd7a0ba3df 100644 (file)
@@ -24,6 +24,8 @@
 
 #include "NasalCanvas.hxx"
 #include <Canvas/canvas_mgr.hxx>
+#include <Canvas/gui_mgr.hxx>
+#include <Canvas/window.hxx>
 #include <Main/globals.hxx>
 #include <Scripting/NasalSys.hxx>
 
@@ -57,6 +59,7 @@ typedef nasal::Ghost<sc::CanvasPtr> NasalCanvas;
 typedef nasal::Ghost<sc::ElementPtr> NasalElement;
 typedef nasal::Ghost<sc::GroupPtr> NasalGroup;
 typedef nasal::Ghost<sc::TextPtr> NasalText;
+typedef nasal::Ghost<canvas::WindowWeakPtr> NasalWindow;
 
 SGPropertyNode* from_nasal_helper(naContext c, naRef ref, SGPropertyNode**)
 {
@@ -77,12 +80,34 @@ CanvasMgr& requireCanvasMgr(naContext c)
   return *canvas_mgr;
 }
 
+GUIMgr& requireGUIMgr(naContext c)
+{
+  GUIMgr* mgr =
+    static_cast<GUIMgr*>(globals->get_subsystem("CanvasGUI"));
+  if( !mgr )
+    naRuntimeError(c, "Failed to get CanvasGUI subsystem");
+
+  return *mgr;
+}
+
 /**
  * Create new Canvas and get ghost for it.
  */
-static naRef f_createCanvas(naContext c, naRef me, int argc, naRef* args)
+static naRef f_createCanvas(const nasal::CallContext& ctx)
 {
-  return NasalCanvas::create(c, requireCanvasMgr(c).createCanvas());
+  return NasalCanvas::create(ctx.c, requireCanvasMgr(ctx.c).createCanvas());
+}
+
+/**
+ * Create new Window and get ghost for it.
+ */
+static naRef f_createWindow(const nasal::CallContext& ctx)
+{
+  return NasalWindow::create
+  (
+    ctx.c,
+    requireGUIMgr(ctx.c).createWindow( ctx.getArg<std::string>(0) )
+  );
 }
 
 /**
@@ -191,6 +216,7 @@ naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
     .member("size_x", &sc::Canvas::getSizeX)
     .member("size_y", &sc::Canvas::getSizeY)
     .method("_createGroup", &f_canvasCreateGroup)
+    .method("_getGroup", &sc::Canvas::getGroup)
     .method("addEventListener", &sc::Canvas::addEventListener);
   NasalElement::init("canvas.Element")
     .member("_node_ghost", &elementGetNode<sc::Element>)
@@ -205,10 +231,14 @@ naRef initNasalCanvas(naRef globals, naContext c, naRef gcSave)
     .bases<NasalElement>()
     .method("getNearestCursor", &sc::Text::getNearestCursor);
 
+  NasalWindow::init("canvas.Window")
+    .member("_node_ghost", &elementGetNode<canvas::Window>);
+
   nasal::Hash globals_module(globals, c),
               canvas_module = globals_module.createHash("canvas");
 
   canvas_module.set("_newCanvasGhost", f_createCanvas);
+  canvas_module.set("_newWindowGhost", f_createWindow);
   canvas_module.set("_getCanvasGhost", f_getCanvas);
 
   return naNil();