Example of allowing multiple components of the same type to be queried.
This commit is contained in:
@ -22,6 +22,9 @@ namespace Dawn {
|
||||
template<class T>
|
||||
T * _sceneForwardGetComponent(SceneItem *item);
|
||||
|
||||
template<class T>
|
||||
std::vector<T*> _sceneForwardGetComponents(SceneItem *item);
|
||||
|
||||
class Scene {
|
||||
private:
|
||||
sceneitemid_t nextId;
|
||||
|
@ -78,6 +78,27 @@ namespace Dawn {
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all components attached to this SceneItem that match the
|
||||
* queried component type. This method will return an array of shared
|
||||
* pointers to the components, or an empty array if the item does not have
|
||||
* any components of the queried type.
|
||||
*
|
||||
* @tparam T Type of component to be fetched.
|
||||
* @return An array of pointers to the components.
|
||||
*/
|
||||
template<class T>
|
||||
std::vector<T*> getComponents() {
|
||||
std::vector<T*> components;
|
||||
auto it = this->components.begin();
|
||||
while(it != this->components.end()) {
|
||||
T *castedAs = dynamic_cast<T*>(*it);
|
||||
if(castedAs != nullptr) components.push_back(castedAs);
|
||||
++it;
|
||||
}
|
||||
return components;
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a (direct) child of this component that has a matching component.
|
||||
|
@ -79,10 +79,13 @@ namespace Dawn {
|
||||
virtual ~SceneItemComponent();
|
||||
};
|
||||
|
||||
|
||||
|
||||
template<class T>
|
||||
T * _sceneForwardGetComponent(SceneItem *item) {
|
||||
return item->getComponent<T>();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T * _sceneForwardGetComponents(SceneItem *item) {
|
||||
return item->getComponents<T>();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user