]> git.mxchange.org Git - simgear.git/blobdiff - simgear/canvas/Canvas.cxx
Fix #1783: repeated error message on console
[simgear.git] / simgear / canvas / Canvas.cxx
index 8b02f49c7b8b63cbbb7cccc279e6910561249b4a..025b6f7f4b2508cfe07431748b77f1a1ed55721c 100644 (file)
@@ -20,6 +20,7 @@
 #include "CanvasEventManager.hxx"
 #include "CanvasEventVisitor.hxx"
 #include "CanvasPlacement.hxx"
+#include <simgear/canvas/events/KeyboardEvent.hxx>
 #include <simgear/canvas/events/MouseEvent.hxx>
 #include <simgear/scene/util/parse_color.hxx>
 #include <simgear/scene/util/RenderConstants.hxx>
@@ -191,6 +192,32 @@ namespace canvas
     return _root_group;
   }
 
+  //----------------------------------------------------------------------------
+  void Canvas::setLayout(const LayoutRef& layout)
+  {
+    _layout = layout;
+    _layout->setCanvas(this);
+  }
+
+  //----------------------------------------------------------------------------
+  void Canvas::setFocusElement(const ElementPtr& el)
+  {
+    if( el && el->getCanvas().lock() != this )
+    {
+      SG_LOG(SG_GUI, SG_WARN, "setFocusElement: element not from this canvas");
+      return;
+    }
+
+    // TODO focus out/in events
+    _focus_element = el;
+  }
+
+  //----------------------------------------------------------------------------
+  void Canvas::clearFocusElement()
+  {
+    _focus_element.reset();
+  }
+
   //----------------------------------------------------------------------------
   void Canvas::enableRendering(bool force)
   {
@@ -202,11 +229,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 +274,9 @@ namespace canvas
       }
     }
 
+    if( _layout )
+      _layout->setGeometry(SGRecti(0, 0, _view_width, _view_height));
+
     if( _visible || _render_always )
     {
       BOOST_FOREACH(CanvasWeakPtr canvas_weak, _child_canvases)
@@ -326,11 +355,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)
   {
@@ -430,6 +468,18 @@ namespace canvas
     return _event_manager->handleEvent(event, visitor.getPropagationPath());
   }
 
+  //----------------------------------------------------------------------------
+  bool Canvas::handleKeyboardEvent(const KeyboardEventPtr& event)
+  {
+    ElementPtr target = _focus_element.lock();
+    if( !target )
+      target = _root_group;
+    if( !target )
+      return false;
+
+    return target->dispatchEvent(event);
+  }
+
   //----------------------------------------------------------------------------
   bool Canvas::propagateEvent( EventPtr const& event,
                                EventPropagationPath const& path )
@@ -540,6 +590,12 @@ namespace canvas
         else if( node->getIndex() == 1 )
           setSizeY( node->getIntValue() );
       }
+      else if( name == "update" )
+      {
+        if( _root_group )
+          _root_group->update(0);
+        return update(0);
+      }
       else if( name == "view" )
       {
         if( node->getIndex() == 0 )
@@ -648,7 +704,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";