Put ComponentContainer directly in GuiComponent.

Necessary for parenting to allow getOffset(), etc.
This commit is contained in:
Aloshi 2013-06-02 11:11:29 -05:00
parent 424fcb0329
commit 628b0b6958
4 changed files with 53 additions and 61 deletions

View file

@ -1,34 +0,0 @@
#include "ComponentContainer.h"
#include "GuiComponent.h"
void ComponentContainer::addChild(GuiComponent* cmp)
{
mChildren.push_back(cmp);
}
void ComponentContainer::removeChild(GuiComponent* cmp)
{
for(auto i = mChildren.begin(); i != mChildren.end(); i++)
{
if(*i == cmp)
{
mChildren.erase(i);
return;
}
}
}
void ComponentContainer::clearChildren()
{
mChildren.clear();
}
unsigned int ComponentContainer::getChildCount()
{
return mChildren.size();
}
GuiComponent* ComponentContainer::getChild(unsigned int i)
{
return mChildren.at(i);
}

View file

@ -1,25 +0,0 @@
#ifndef _COMPONENTCONTAINER_H_
#define _COMPONENTCONTAINER_H_
#include <vector>
class GuiComponent;
/**
Generic container for Components.
**/
class ComponentContainer
{
public:
void addChild(GuiComponent* cmp);
void removeChild(GuiComponent* cmp);
void clearChildren();
unsigned int getChildCount();
GuiComponent* getChild(unsigned int i);
private:
std::vector<GuiComponent*> mChildren;
};
#endif

View file

@ -1,5 +1,6 @@
#include "GuiComponent.h"
#include "Window.h"
#include "Log.h"
GuiComponent::GuiComponent(Window* window) : mWindow(window), mParent(NULL)
{
@ -89,3 +90,47 @@ void GuiComponent::setOffset(int x, int y)
mOffset.x = x;
mOffset.y = y;
}
void GuiComponent::addChild(GuiComponent* cmp)
{
mChildren.push_back(cmp);
if(cmp->getParent())
cmp->getParent()->removeChild(cmp);
cmp->setParent(this);
}
void GuiComponent::removeChild(GuiComponent* cmp)
{
if(cmp->getParent() != this)
{
LOG(LogError) << "Tried to remove child from incorrect parent!";
}
cmp->setParent(NULL);
for(auto i = mChildren.begin(); i != mChildren.end(); i++)
{
if(*i == cmp)
{
mChildren.erase(i);
return;
}
}
}
void GuiComponent::clearChildren()
{
mChildren.clear();
}
unsigned int GuiComponent::getChildCount()
{
return mChildren.size();
}
GuiComponent* GuiComponent::getChild(unsigned int i)
{
return mChildren.at(i);
}

View file

@ -3,11 +3,10 @@
#include "InputConfig.h"
#include "Vector2.h"
#include "ComponentContainer.h"
class Window;
class GuiComponent : public ComponentContainer
class GuiComponent
{
public:
GuiComponent(Window* window);
@ -28,10 +27,17 @@ public:
void setParent(GuiComponent* parent);
GuiComponent* getParent();
void addChild(GuiComponent* cmp);
void removeChild(GuiComponent* cmp);
void clearChildren();
unsigned int getChildCount();
GuiComponent* getChild(unsigned int i);
protected:
Window* mWindow;
GuiComponent* mParent;
Vector2i mOffset;
std::vector<GuiComponent*> mChildren;
};
#endif