inlib
1.2.0
|
00001 // Copyright (C) 2010, Guy Barrand. All rights reserved. 00002 // See the file inlib.license for terms. 00003 00004 #ifndef inlib_sg_group 00005 #define inlib_sg_group 00006 00007 #include <vector> 00008 00009 #include "node" 00010 00011 #include "pick_action" 00012 #include "bbox_action" 00013 #include "event_action" 00014 00015 namespace inlib { 00016 namespace sg { 00017 00018 class group : public node { 00019 public: 00020 static const std::string& s_class() { 00021 static const std::string s_v("inlib::sg::group"); 00022 return s_v; 00023 } 00024 public: 00025 virtual void* cast(const std::string& a_class) const { 00026 if(void* p = inlib::cmp_cast<group>(this,a_class)) {return p;} 00027 return node::cast(a_class); 00028 } 00029 public: //node 00030 virtual node* copy() const {return new group(*this);} 00031 virtual const std::string& s_cls() const {return s_class();} 00032 public: 00033 virtual void render(render_action& a_action) { 00034 std::vector<node*>::iterator it; 00035 for(it=m_children.begin();it!=m_children.end();++it) { 00036 (*it)->render(a_action); 00037 } 00038 } 00039 virtual void pick(pick_action& a_action) { 00040 std::vector<node*>::iterator it; 00041 for(it=m_children.begin();it!=m_children.end();++it) { 00042 (*it)->pick(a_action); 00043 if(a_action.done()) break; 00044 } 00045 } 00046 virtual void bbox(bbox_action& a_action) { 00047 std::vector<node*>::iterator it; 00048 for(it=m_children.begin();it!=m_children.end();++it) { 00049 (*it)->bbox(a_action); 00050 } 00051 } 00052 virtual void event(event_action& a_action) { 00053 std::vector<node*>::iterator it; 00054 for(it=m_children.begin();it!=m_children.end();++it) { 00055 (*it)->event(a_action); 00056 //if(a_action.handled()) break; //do not stop if handled. 00057 } 00058 } 00059 virtual void search(search_action& a_action) { 00060 std::vector<node*>::iterator it; 00061 for(it=m_children.begin();it!=m_children.end();++it) { 00062 (*it)->search(a_action); 00063 if(a_action.done()) break; 00064 } 00065 } 00066 virtual bool write(write_action& a_action) { 00067 if(!a_action.beg_node(*this)) return false; 00068 if(!write_children(a_action)) return false; 00069 if(!a_action.end_node(*this)) return false; 00070 return true; 00071 } 00072 public: 00073 group(){} 00074 virtual ~group(){clear();} 00075 public: 00076 group(const group& a_from) 00077 :node(a_from) 00078 { 00079 std::vector<node*>::const_iterator it; 00080 for(it=a_from.m_children.begin();it!=a_from.m_children.end();++it) { 00081 m_children.push_back((*it)->copy()); 00082 } 00083 } 00084 group& operator=(const group& a_from){ 00085 node::operator=(a_from); 00086 clear(); 00087 std::vector<node*>::const_iterator it; 00088 for(it=a_from.m_children.begin();it!=a_from.m_children.end();++it) { 00089 m_children.push_back((*it)->copy()); 00090 } 00091 return *this; 00092 } 00093 public: 00094 void add(node* a_node) { 00095 //WARNING : take ownership of a_node. 00096 m_children.push_back(a_node); 00097 } 00098 void set(unsigned a_index,node* a_node) { 00099 //WARNING : take ownership of a_node. 00100 //WARNING : no check is done on a_index. 00101 m_children[a_index] = a_node; 00102 } 00103 /* 00104 void remove(node* a_node){ 00105 //NOTE : no delete on a_node is performed. 00106 std::vector<node*>::iterator it; 00107 for(it=m_children.begin();it!=m_children.end();) { 00108 if(a_node==(*it)) { 00109 it = m_children.erase(it); 00110 } else { 00111 ++it; 00112 } 00113 } 00114 } 00115 bool is_in(node* a_node){ 00116 std::vector<node*>::iterator it; 00117 for(it=m_children.begin();it!=m_children.end();++it) { 00118 if(a_node==(*it)) return true; 00119 } 00120 return false; 00121 } 00122 */ 00123 void clear(){ 00124 std::vector<node*>::iterator it; 00125 for(it=m_children.begin();it!=m_children.end();) { 00126 node* child = *it; 00127 it = m_children.erase(it); 00128 delete child; 00129 } 00130 m_children.clear(); 00131 } 00132 unsigned int size() const {return m_children.size();} 00133 bool empty() const {return m_children.size()?false:true;} 00134 node* operator[](unsigned int a_index) const{ 00135 //WARNING : no check is done on a_index. 00136 return m_children[a_index]; 00137 } 00138 const std::vector<node*>& children() const {return m_children;} 00139 protected: 00140 bool write_children(write_action& a_action) { 00141 std::vector<node*>::iterator it; 00142 for(it=m_children.begin();it!=m_children.end();++it) { 00143 if(!(*it)->write(a_action)) return false; 00144 } 00145 return true; 00146 } 00147 protected: 00148 std::vector<node*> m_children; 00149 }; 00150 00151 }} 00152 00153 #endif