]> git.mxchange.org Git - simgear.git/blobdiff - simgear/canvas/elements/CanvasGroup.cxx
First working version of DOM like Canvas event handling
[simgear.git] / simgear / canvas / elements / CanvasGroup.cxx
index aa8c45c5da27efd781695a4d7b779b368b10a9ec..0740026828c721d9146eeba92e56ea6a3e793514 100644 (file)
@@ -21,6 +21,8 @@
 #include "CanvasMap.hxx"
 #include "CanvasPath.hxx"
 #include "CanvasText.hxx"
+#include <simgear/canvas/CanvasEventVisitor.hxx>
+#include <simgear/canvas/MouseEvent.hxx>
 
 #include <boost/bind.hpp>
 #include <boost/foreach.hpp>
@@ -36,16 +38,18 @@ namespace canvas
   template<typename T>
   ElementPtr createElement( const CanvasWeakPtr& canvas,
                             const SGPropertyNode_ptr& node,
-                            const Style& style )
+                            const Style& style,
+                            Element* parent )
   {
-    return ElementPtr( new T(canvas, node, style) );
+    return ElementPtr( new T(canvas, node, style, parent) );
   }
 
   //----------------------------------------------------------------------------
   Group::Group( const CanvasWeakPtr& canvas,
                 const SGPropertyNode_ptr& node,
-                const Style& parent_style ):
-    Element(canvas, node, parent_style)
+                const Style& parent_style,
+                Element* parent ):
+    Element(canvas, node, parent_style, parent)
   {
     _child_factories["group"] = &createElement<Group>;
     _child_factories["image"] = &createElement<Image>;
@@ -62,11 +66,11 @@ namespace canvas
 
   //----------------------------------------------------------------------------
   ElementPtr Group::createChild( const std::string& type,
-                                 const std::string& name )
+                                 const std::string& id )
   {
     SGPropertyNode* node = _node->addChild(type, 0, false);
-    if( !name.empty() )
-      node->setStringValue("name", name);
+    if( !id.empty() )
+      node->setStringValue("id", id);
 
     return getChild(node);
   }
@@ -81,6 +85,32 @@ namespace canvas
     return child->second;
   }
 
+  //----------------------------------------------------------------------------
+  ElementPtr Group::getElementById(const std::string& id)
+  {
+    std::vector<GroupPtr> groups;
+
+    BOOST_FOREACH( ChildList::value_type child, _children )
+    {
+      const ElementPtr& el = child.second;
+      if( el->getProps()->getStringValue("id") == id )
+        return el;
+
+      GroupPtr group = boost::dynamic_pointer_cast<Group>(el);
+      if( group )
+        groups.push_back(group);
+    }
+
+    BOOST_FOREACH( GroupPtr group, groups )
+    {
+      ElementPtr el = group->getElementById(id);
+      if( el )
+        return el;
+    }
+
+    return ElementPtr();
+  }
+
   //----------------------------------------------------------------------------
   void Group::update(double dt)
   {
@@ -91,12 +121,12 @@ namespace canvas
   }
 
   //----------------------------------------------------------------------------
-  bool Group::handleLocalMouseEvent(const canvas::MouseEvent& event)
+  bool Group::traverse(EventVisitor& visitor)
   {
     // Iterate in reverse order as last child is displayed on top
     BOOST_REVERSE_FOREACH( ChildList::value_type child, _children )
     {
-      if( child.second->handleMouseEvent(event) )
+      if( child.second->accept(visitor) )
         return true;
     }
     return false;
@@ -112,7 +142,7 @@ namespace canvas
       _child_factories.find( child->getNameString() );
     if( child_factory != _child_factories.end() )
     {
-      ElementPtr element = child_factory->second(_canvas, child, _style);
+      ElementPtr element = child_factory->second(_canvas, child, _style, this);
 
       // Add to osg scene graph...
       _transform->addChild( element->getMatrixTransform() );
@@ -222,7 +252,7 @@ namespace canvas
     (
       _children.begin(),
       _children.end(),
-      boost::bind(&Group::ChildList::value_type::first, _1) == node
+      boost::bind(&ChildList::value_type::first, _1) == node
     );
   }