Composite.cpp

Go to the documentation of this file.
00001 #include "Composite.hpp"
00002 #include "Component.hpp"
00003 #include "CompositeFactory.hpp"
00004 #include "comp/list/ComponentList.hpp"
00005 #include "comp/list/CompositeList.hpp"
00006 
00007 
00008 namespace se_core {
00009     Composite
00010     ::Composite(const CompositeFactory* factory)
00011             : factory_(factory), tag_(0), isActive_(false), isDead_(false), parent_(0), ptr_(this) {
00012         name_ = factory_->name();
00013         for(int i = 0; i < FAST_COMPONENT_COUNT; ++i) {
00014             fastComponents_[i] = 0;
00015         }
00016         components_.clear();
00017         children_.clear();
00018     }
00019 
00020 
00021     Composite
00022     ::Composite(const CompositeFactory* factory, const char* name)
00023             : name_(name), factory_(factory), tag_(0), isActive_(false), isDead_(false), parent_(0), ptr_(this) {
00024         for(int i = 0; i < FAST_COMPONENT_COUNT; ++i) {
00025             fastComponents_[i] = 0;
00026         }
00027         components_.clear();
00028         children_.clear();
00029     }
00030 
00031 
00032     Composite
00033     ::~Composite() {
00034         releaseComponents();
00035     }
00036 
00037 
00038     const char* Composite
00039     ::name() const {
00040         return name_;
00041     }
00042 
00043 
00044     int Composite
00045     ::type() const {
00046         return factory_->type();
00047     }
00048 
00049 
00050     void Composite
00051     ::setActive(bool state, bool doTraverseChildren) {
00052         if(state == isActive_) return;
00053 
00054         isActive_ = state;
00055 
00056         // Children is inactive before *this
00057         if(!state && doTraverseChildren) {
00058             CompositeList::Iterator composites(children_);
00059             while(composites.hasNext()) {
00060                 Composite& c = composites.next();
00061                 c.setActive(state);
00062             }
00063         }
00064 
00065         ComponentList::Iterator it(components_);
00066         while(it.hasNext()) {
00067             it.next().setActive(state);
00068         }
00069 
00070         // Children goes active after *this
00071         if(state && doTraverseChildren) {
00072             CompositeList::Iterator composites(children_);
00073             while(composites.hasNext()) {
00074                 Composite& c = composites.next();
00075                 c.setActive(state);
00076             }
00077         }
00078 
00079     }
00080 
00081 
00082     Component*  Composite
00083     ::component(int type) {
00084         if(type < FAST_COMPONENT_COUNT) {
00085             return fastComponents_[ type ];
00086         }
00087         ComponentList::Iterator it(components_);
00088         while(it.hasNext()) {
00089             Component& c = it.next();
00090             if(c.type() == type) {
00091                 return &c;
00092             }
00093         }
00094         //return composites_.lookup(type);
00095         return 0;
00096     }
00097 
00098 
00099     const Component*  Composite
00100     ::component(int type) const {
00101         ComponentList::Iterator it(components_);
00102         while(it.hasNext()) {
00103             const Component& c = it.next();
00104             if(c.type() == type) {
00105                 return &c;
00106             }
00107         }
00108         //return composites_.lookup(type);
00109         return 0;
00110     }
00111 
00112 
00113     void Composite
00114     ::releaseComponents() {
00115         ComponentList::Iterator it(components_);
00116         while(it.hasNext()) {
00117             Component& c = it.next();
00118             if(c.factory()) {
00119                 c.factory()->release(&c);
00120             }
00121         }
00122     }
00123 
00124 
00125     void Composite
00126     ::addComponent(Component& c) {
00127         //LogDetail("Added component of type " << c.type() << " to " << c.owner()->name());
00128         AssertFatal(component(c.type()) == 0, name() << ": " << c.type());
00129         components_.add(c);
00130         if(c.type() < FAST_COMPONENT_COUNT) {
00131             fastComponents_[ c.type() ] = &c;
00132         }
00133     }
00134 
00135 
00136     void Composite
00137     ::removeComponent(Component& c) {
00138         if(c.type() < FAST_COMPONENT_COUNT) {
00139             fastComponents_[ c.type() ] = 0;
00140         }
00141         components_.remove(c);
00142     }
00143 
00144 
00145     void Composite
00146     ::addChild(Composite& node) {
00147         children_.add(node);
00148     }
00149 
00150 
00151     void Composite
00152     ::removeChild(Composite& node) {
00153         children_.remove(node);
00154     }
00155 
00156 
00157     void Composite
00158     ::resetParent() {
00159         if(parent_)
00160             parent_->removeChild(*this);
00161 
00162         ComponentList::Iterator it(components_);
00163         while(it.hasNext()) {
00164             Component& c = it.next();
00165             c.parentChanged(0, parent_);
00166         }
00167 
00168         parent_ = 0;
00169 
00170         setActive(false, true);
00171     }
00172 
00173 
00174     void Composite
00175     ::setParent(Composite& p) {
00176         if(parent_ == &p)
00177             return;
00178 
00179         if(parent_)
00180             parent_->removeChild(*this);
00181 
00182         ComponentList::Iterator it(components_);
00183         while(it.hasNext()) {
00184             Component& c = it.next();
00185             c.parentChanged(&p, parent_);
00186         }
00187 
00188         parent_ = &p;
00189         parent_->addChild(*this);
00190 
00191         setActive(parent_->isActive(), true);
00192     }
00193 
00194 
00195     void Composite
00196     ::zoneChanged(int type, Composite* newZone, Composite* oldZone) {
00197         ComponentList::Iterator it(components_);
00198         while(it.hasNext()) {
00199             Component& c = it.next();
00200             c.zoneChanged(type, newZone, oldZone);
00201         }
00202     }
00203 
00204 
00205     void Composite
00206 	::init(bool doTraverseChildren) {
00207         // Init self first...
00208         ComponentList::Iterator it(components_);
00209         while(it.hasNext()) {
00210             Component& c = it.next();
00211             c.init();
00212         }
00213 
00214         // ... then children
00215         if(doTraverseChildren) {
00216             CompositeList::Iterator composites(children_);
00217             while(composites.hasNext()) {
00218                 Composite& c = composites.next();
00219                 c.init();
00220             }
00221         }
00222     }
00223 
00224 
00225     void Composite
00226 	::cleanup(bool doTraverseChildren) {
00227 
00228         // Cleanup children first...
00229         if(doTraverseChildren) {
00230             CompositeList::Iterator composites(children_);
00231             while(composites.hasNext()) {
00232                 Composite& c = composites.next();
00233                 c.cleanup(true);
00234             }
00235         }
00236 
00237         // ... then self
00238         setActive(false, true);
00239         resetParent();
00240 
00241         ComponentList::Iterator it(components_);
00242         while(it.hasNext()) {
00243             Component& c = it.next();
00244             c.cleanup();
00245         }
00246     }
00247 
00248 
00249     void Composite
00250     ::scheduleForDestruction() {
00251         if(isDead_) return;
00252 
00253         ComponentList::Iterator it(components_);
00254         while(it.hasNext()) {
00255             Component& c = it.next();
00256             c.setDead();
00257         }
00258 
00259         isDead_ = true;
00260     }
00261 }

Home Page | SagaEngine trunk (updated nightly) reference generated Sun Dec 2 20:06:10 2007 by Doxygen version 1.3.9.1.

SourceForge.net Logo