2013-06-02 15:08:32 +00:00
|
|
|
#include "GuiComponent.h"
|
|
|
|
#include "Window.h"
|
2013-06-02 16:11:29 +00:00
|
|
|
#include "Log.h"
|
2013-06-02 19:34:50 +00:00
|
|
|
#include "Renderer.h"
|
2013-12-08 17:35:43 +00:00
|
|
|
#include "animations/AnimationController.h"
|
2014-01-01 05:39:22 +00:00
|
|
|
#include "ThemeData.h"
|
2013-06-02 15:08:32 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
GuiComponent::GuiComponent(Window* window) : mWindow(window), mParent(NULL), mOpacity(255),
|
|
|
|
mPosition(Eigen::Vector3f::Zero()), mSize(Eigen::Vector2f::Zero()), mTransform(Eigen::Affine3f::Identity())
|
2013-06-02 15:08:32 +00:00
|
|
|
{
|
2013-12-13 03:17:59 +00:00
|
|
|
for(unsigned char i = 0; i < MAX_ANIMATIONS; i++)
|
|
|
|
mAnimationMap[i] = NULL;
|
2013-06-02 15:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GuiComponent::~GuiComponent()
|
|
|
|
{
|
|
|
|
mWindow->removeGui(this);
|
|
|
|
|
2013-12-13 03:17:59 +00:00
|
|
|
for(unsigned char i = 0; i < MAX_ANIMATIONS; i++)
|
|
|
|
{
|
|
|
|
if(mAnimationMap[i])
|
|
|
|
delete mAnimationMap[i];
|
|
|
|
}
|
|
|
|
|
2013-06-02 15:08:32 +00:00
|
|
|
if(mParent)
|
|
|
|
mParent->removeChild(this);
|
2013-06-19 01:12:30 +00:00
|
|
|
|
|
|
|
for(unsigned int i = 0; i < getChildCount(); i++)
|
|
|
|
getChild(i)->setParent(NULL);
|
2013-06-02 15:08:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiComponent::input(InputConfig* config, Input input)
|
|
|
|
{
|
|
|
|
for(unsigned int i = 0; i < getChildCount(); i++)
|
|
|
|
{
|
|
|
|
if(getChild(i)->input(config, input))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiComponent::update(int deltaTime)
|
|
|
|
{
|
2013-12-13 03:17:59 +00:00
|
|
|
for(unsigned char i = 0; i < MAX_ANIMATIONS; i++)
|
|
|
|
{
|
|
|
|
AnimationController* anim = mAnimationMap[i];
|
|
|
|
if(anim)
|
|
|
|
{
|
|
|
|
bool done = anim->update(deltaTime);
|
|
|
|
if(done)
|
|
|
|
{
|
|
|
|
mAnimationMap[i] = NULL;
|
|
|
|
delete anim;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-08 17:35:43 +00:00
|
|
|
|
2013-06-02 15:08:32 +00:00
|
|
|
for(unsigned int i = 0; i < getChildCount(); i++)
|
|
|
|
{
|
|
|
|
getChild(i)->update(deltaTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void GuiComponent::render(const Eigen::Affine3f& parentTrans)
|
2013-06-02 19:34:50 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Affine3f trans = parentTrans * getTransform();
|
|
|
|
renderChildren(trans);
|
2013-06-02 19:34:50 +00:00
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void GuiComponent::renderChildren(const Eigen::Affine3f& transform) const
|
2013-06-02 15:08:32 +00:00
|
|
|
{
|
|
|
|
for(unsigned int i = 0; i < getChildCount(); i++)
|
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
getChild(i)->render(transform);
|
2013-06-02 15:08:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Vector3f GuiComponent::getPosition() const
|
2013-06-02 15:08:32 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
return mPosition;
|
2013-06-02 15:08:32 +00:00
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void GuiComponent::setPosition(const Eigen::Vector3f& offset)
|
2013-06-02 15:08:32 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
mPosition = offset;
|
|
|
|
onPositionChanged();
|
2013-06-02 15:08:32 +00:00
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void GuiComponent::setPosition(float x, float y, float z)
|
2013-06-02 15:08:32 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
mPosition << x, y, z;
|
|
|
|
onPositionChanged();
|
2013-06-02 15:08:32 +00:00
|
|
|
}
|
2013-06-02 16:11:29 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
Eigen::Vector2f GuiComponent::getSize() const
|
2013-06-02 22:33:49 +00:00
|
|
|
{
|
|
|
|
return mSize;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void GuiComponent::setSize(const Eigen::Vector2f& size)
|
2013-07-02 23:48:39 +00:00
|
|
|
{
|
|
|
|
mSize = size;
|
|
|
|
onSizeChanged();
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
void GuiComponent::setSize(float w, float h)
|
2013-07-02 23:48:39 +00:00
|
|
|
{
|
2013-07-10 11:29:43 +00:00
|
|
|
mSize << w, h;
|
2013-07-02 23:48:39 +00:00
|
|
|
onSizeChanged();
|
|
|
|
}
|
|
|
|
|
2013-06-02 19:34:50 +00:00
|
|
|
//Children stuff.
|
2013-06-02 16:11:29 +00:00
|
|
|
void GuiComponent::addChild(GuiComponent* cmp)
|
|
|
|
{
|
|
|
|
mChildren.push_back(cmp);
|
|
|
|
|
|
|
|
if(cmp->getParent())
|
|
|
|
cmp->getParent()->removeChild(cmp);
|
|
|
|
|
|
|
|
cmp->setParent(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiComponent::removeChild(GuiComponent* cmp)
|
|
|
|
{
|
2013-11-12 23:28:15 +00:00
|
|
|
if(!cmp->getParent())
|
|
|
|
return;
|
|
|
|
|
2013-06-02 16:11:29 +00:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
unsigned int GuiComponent::getChildCount() const
|
2013-06-02 16:11:29 +00:00
|
|
|
{
|
|
|
|
return mChildren.size();
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
GuiComponent* GuiComponent::getChild(unsigned int i) const
|
2013-06-02 16:11:29 +00:00
|
|
|
{
|
|
|
|
return mChildren.at(i);
|
|
|
|
}
|
2013-06-02 19:34:50 +00:00
|
|
|
|
|
|
|
void GuiComponent::setParent(GuiComponent* parent)
|
|
|
|
{
|
|
|
|
mParent = parent;
|
|
|
|
}
|
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
GuiComponent* GuiComponent::getParent() const
|
2013-06-02 19:34:50 +00:00
|
|
|
{
|
|
|
|
return mParent;
|
|
|
|
}
|
2013-07-02 14:51:33 +00:00
|
|
|
|
2013-07-10 11:29:43 +00:00
|
|
|
unsigned char GuiComponent::getOpacity() const
|
2013-07-02 14:51:33 +00:00
|
|
|
{
|
|
|
|
return mOpacity;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiComponent::setOpacity(unsigned char opacity)
|
|
|
|
{
|
|
|
|
mOpacity = opacity;
|
2013-10-10 21:14:33 +00:00
|
|
|
for(auto it = mChildren.begin(); it != mChildren.end(); it++)
|
|
|
|
{
|
|
|
|
(*it)->setOpacity(opacity);
|
|
|
|
}
|
2013-07-03 00:12:49 +00:00
|
|
|
}
|
2013-07-10 11:29:43 +00:00
|
|
|
|
|
|
|
const Eigen::Affine3f GuiComponent::getTransform()
|
|
|
|
{
|
|
|
|
mTransform.setIdentity();
|
|
|
|
mTransform.translate(mPosition);
|
|
|
|
return mTransform;
|
|
|
|
}
|
2013-08-14 12:16:49 +00:00
|
|
|
|
|
|
|
void GuiComponent::setValue(const std::string& value)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string GuiComponent::getValue() const
|
|
|
|
{
|
|
|
|
return "";
|
|
|
|
}
|
2013-08-19 15:36:48 +00:00
|
|
|
|
|
|
|
void GuiComponent::textInput(const char* text)
|
|
|
|
{
|
|
|
|
for(auto iter = mChildren.begin(); iter != mChildren.end(); iter++)
|
|
|
|
{
|
|
|
|
(*iter)->textInput(text);
|
|
|
|
}
|
|
|
|
}
|
2013-12-08 17:35:43 +00:00
|
|
|
|
2013-12-13 03:17:59 +00:00
|
|
|
void GuiComponent::setAnimation(Animation* anim, std::function<void()> finishedCallback, bool reverse, unsigned char slot)
|
2013-12-08 17:35:43 +00:00
|
|
|
{
|
2013-12-13 03:17:59 +00:00
|
|
|
assert(slot < MAX_ANIMATIONS);
|
|
|
|
|
|
|
|
AnimationController* oldAnim = mAnimationMap[slot];
|
|
|
|
mAnimationMap[slot] = new AnimationController(anim, finishedCallback, reverse);
|
|
|
|
|
|
|
|
if(oldAnim)
|
|
|
|
delete oldAnim;
|
2013-12-08 17:35:43 +00:00
|
|
|
}
|
|
|
|
|
2013-12-13 03:17:59 +00:00
|
|
|
void GuiComponent::stopAnimation(unsigned char slot)
|
2013-12-08 17:35:43 +00:00
|
|
|
{
|
2013-12-13 03:17:59 +00:00
|
|
|
assert(slot < MAX_ANIMATIONS);
|
|
|
|
if(mAnimationMap[slot])
|
|
|
|
{
|
|
|
|
delete mAnimationMap[slot];
|
|
|
|
mAnimationMap[slot] = NULL;
|
|
|
|
}
|
2013-12-08 17:35:43 +00:00
|
|
|
}
|
2014-01-01 05:39:22 +00:00
|
|
|
|
2014-01-25 00:10:13 +00:00
|
|
|
bool GuiComponent::isAnimationPlaying(unsigned char slot) const
|
|
|
|
{
|
|
|
|
return mAnimationMap[slot] != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiComponent::isAnimationReversed(unsigned char slot) const
|
|
|
|
{
|
|
|
|
assert(mAnimationMap[slot] != NULL);
|
|
|
|
return mAnimationMap[slot]->isReversed();
|
|
|
|
}
|
|
|
|
|
|
|
|
int GuiComponent::getAnimationTime(unsigned char slot) const
|
|
|
|
{
|
|
|
|
assert(mAnimationMap[slot] != NULL);
|
|
|
|
return mAnimationMap[slot]->getTime();
|
|
|
|
}
|
|
|
|
|
2014-01-01 05:39:22 +00:00
|
|
|
void GuiComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const std::string& view, const std::string& element, unsigned int properties)
|
|
|
|
{
|
|
|
|
Eigen::Vector2f scale = getParent() ? getParent()->getSize() : Eigen::Vector2f((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
|
|
|
|
|
|
|
|
const ThemeData::ThemeElement* elem = theme->getElement(view, element, "");
|
2014-01-03 14:26:39 +00:00
|
|
|
if(!elem)
|
|
|
|
return;
|
2014-01-01 05:39:22 +00:00
|
|
|
|
|
|
|
using namespace ThemeFlags;
|
|
|
|
if(properties & POSITION && elem->has("pos"))
|
|
|
|
{
|
|
|
|
Eigen::Vector2f denormalized = elem->get<Eigen::Vector2f>("pos").cwiseProduct(scale);
|
|
|
|
setPosition(Eigen::Vector3f(denormalized.x(), denormalized.y(), 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(properties & ThemeFlags::SIZE && elem->has("size"))
|
|
|
|
setSize(elem->get<Eigen::Vector2f>("size").cwiseProduct(scale));
|
|
|
|
}
|
2014-01-25 23:34:29 +00:00
|
|
|
|
|
|
|
void GuiComponent::updateHelpPrompts()
|
|
|
|
{
|
|
|
|
if(getParent())
|
|
|
|
{
|
|
|
|
getParent()->updateHelpPrompts();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<HelpPrompt> prompts = getHelpPrompts();
|
|
|
|
|
|
|
|
if(mWindow->peekGui() == this)
|
|
|
|
mWindow->setHelpPrompts(prompts);
|
|
|
|
}
|