]> git.mxchange.org Git - simgear.git/blobdiff - simgear/canvas/Canvas.cxx
canvas::Layout: clear parent/canvas after calling onRemove.
[simgear.git] / simgear / canvas / Canvas.cxx
index f7ba3228cef1e89c9af370a54d3ef6a5a90d2fa7..c7898b4535df604b07de4b0ccda9fa545af708af 100644 (file)
@@ -19,8 +19,8 @@
 #include "Canvas.hxx"
 #include "CanvasEventManager.hxx"
 #include "CanvasEventVisitor.hxx"
-#include <simgear/canvas/MouseEvent.hxx>
-#include <simgear/canvas/CanvasPlacement.hxx>
+#include "CanvasPlacement.hxx"
+#include <simgear/canvas/events/MouseEvent.hxx>
 #include <simgear/scene/util/parse_color.hxx>
 #include <simgear/scene/util/RenderConstants.hxx>
 
@@ -191,6 +191,14 @@ namespace canvas
     return _root_group;
   }
 
+  //----------------------------------------------------------------------------
+  void Canvas::setLayout(const LayoutRef& layout)
+  {
+    _layout = layout;
+    _layout->setCanvas(this);
+    _status |= LAYOUT_DIRTY;
+  }
+
   //----------------------------------------------------------------------------
   void Canvas::enableRendering(bool force)
   {
@@ -202,11 +210,10 @@ namespace canvas
   //----------------------------------------------------------------------------
   void Canvas::update(double delta_time_sec)
   {
-    if(    (!_texture.serviceable() && _status != STATUS_DIRTY)
-        || (_status & CREATE_FAILED) )
+    if( _status & (CREATE_FAILED | MISSING_SIZE) )
       return;
 
-    if( _status == STATUS_DIRTY )
+    if( _status & STATUS_DIRTY )
     {
       _texture.setSize(_size_x, _size_y);
 
@@ -248,6 +255,17 @@ namespace canvas
       }
     }
 
+    if( _layout )
+    {
+      if( (_status & LAYOUT_DIRTY) )
+      {
+        _layout->setGeometry(SGRecti(0, 0, _view_width, _view_height));
+        _status &= ~LAYOUT_DIRTY;
+      }
+      else
+        _layout->update();
+    }
+
     if( _visible || _render_always )
     {
       BOOST_FOREACH(CanvasWeakPtr canvas_weak, _child_canvases)
@@ -326,11 +344,20 @@ namespace canvas
                                  const EventListener& cb )
   {
     if( !_root_group.get() )
-      throw std::runtime_error("Canvas::AddEventListener: no root group!");
+      throw std::runtime_error("Canvas::addEventListener: no root group!");
 
     return _root_group->addEventListener(type, cb);
   }
 
+  //----------------------------------------------------------------------------
+  bool Canvas::dispatchEvent(const EventPtr& event)
+  {
+    if( !_root_group.get() )
+      throw std::runtime_error("Canvas::dispatchEvent: no root group!");
+
+    return _root_group->dispatchEvent(event);
+  }
+
   //----------------------------------------------------------------------------
   void Canvas::setSizeX(int sx)
   {
@@ -383,6 +410,7 @@ namespace canvas
     if( _view_width == w )
       return;
     _view_width = w;
+    _status |= LAYOUT_DIRTY;
 
     _texture.setViewSize(_view_width, _view_height);
   }
@@ -393,6 +421,7 @@ namespace canvas
     if( _view_height == h )
       return;
     _view_height = h;
+    _status |= LAYOUT_DIRTY;
 
     _texture.setViewSize(_view_width, _view_height);
   }
@@ -418,7 +447,7 @@ namespace canvas
   //----------------------------------------------------------------------------
   bool Canvas::handleMouseEvent(const MouseEventPtr& event)
   {
-    if( !_root_group.get() )
+    if( !_root_group )
       return false;
 
     EventVisitor visitor( EventVisitor::TRAVERSE_DOWN,
@@ -430,6 +459,13 @@ namespace canvas
     return _event_manager->handleEvent(event, visitor.getPropagationPath());
   }
 
+  //----------------------------------------------------------------------------
+  bool Canvas::propagateEvent( EventPtr const& event,
+                               EventPropagationPath const& path )
+  {
+    return _event_manager->propagateEvent(event, path);
+  }
+
   //----------------------------------------------------------------------------
   void Canvas::childAdded( SGPropertyNode * parent,
                            SGPropertyNode * child )
@@ -461,7 +497,10 @@ namespace canvas
   //----------------------------------------------------------------------------
   void Canvas::valueChanged(SGPropertyNode* node)
   {
-    if( boost::starts_with(node->getNameString(), "status") )
+    const std::string& name = node->getNameString();
+
+    if(    boost::starts_with(name, "status")
+        || boost::starts_with(name, "data-") )
       return;
     _render_dirty = true;
 
@@ -500,7 +539,7 @@ namespace canvas
     }
     else if( node->getParent() == _node )
     {
-      if( node->getNameString() == "background" )
+      if( name == "background" )
       {
         osg::Vec4 color;
         if( _texture.getCamera() && parseColor(node->getStringValue(), color) )
@@ -509,35 +548,41 @@ namespace canvas
           _render_dirty = true;
         }
       }
-      else if(    node->getNameString() == "mipmapping"
-              || node->getNameString() == "coverage-samples"
-              || node->getNameString() == "color-samples" )
+      else if(   name == "mipmapping"
+              || name == "coverage-samples"
+              || name == "color-samples" )
       {
         _sampling_dirty = true;
       }
-      else if( node->getNameString() == "additive-blend" )
+      else if( name == "additive-blend" )
       {
         _texture.useAdditiveBlend( node->getBoolValue() );
       }
-      else if( node->getNameString() == "render-always" )
+      else if( name == "render-always" )
       {
         _render_always = node->getBoolValue();
       }
-      else if( node->getNameString() == "size" )
+      else if( name == "size" )
       {
         if( node->getIndex() == 0 )
           setSizeX( node->getIntValue() );
         else if( node->getIndex() == 1 )
           setSizeY( node->getIntValue() );
       }
-      else if( node->getNameString() == "view" )
+      else if( name == "update" )
+      {
+        if( _root_group )
+          _root_group->update(0);
+        return update(0);
+      }
+      else if( name == "view" )
       {
         if( node->getIndex() == 0 )
           setViewWidth( node->getIntValue() );
         else if( node->getIndex() == 1 )
           setViewHeight( node->getIntValue() );
       }
-      else if( node->getNameString() == "freeze" )
+      else if( name == "freeze" )
         _texture.setRender( node->getBoolValue() );
       else
         handled = false;
@@ -638,7 +683,7 @@ namespace canvas
       _status_msg = "Missing size-y";
     else if( _status & CREATE_FAILED )
       _status_msg = "Creating render target failed";
-    else if( _status == STATUS_DIRTY )
+    else if( _status & STATUS_DIRTY )
       _status_msg = "Creation pending...";
     else
       _status_msg = "Ok";