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),
|
2016-09-12 11:31:44 +00:00
|
|
|
mPosition(Eigen::Vector3f::Zero()), mSize(Eigen::Vector2f::Zero()), mTransform(Eigen::Affine3f::Identity()),
|
|
|
|
mIsProcessing(false)
|
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);
|
|
|
|
|
2014-04-15 02:03:11 +00:00
|
|
|
cancelAllAnimations();
|
2013-12-13 03:17:59 +00:00
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:29:07 +00:00
|
|
|
void GuiComponent::updateSelf(int deltaTime)
|
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++)
|
2014-05-15 01:58:16 +00:00
|
|
|
advanceAnimation(i, deltaTime);
|
2014-06-05 19:29:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GuiComponent::updateChildren(int deltaTime)
|
|
|
|
{
|
2013-06-02 15:08:32 +00:00
|
|
|
for(unsigned int i = 0; i < getChildCount(); i++)
|
|
|
|
{
|
|
|
|
getChild(i)->update(deltaTime);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-05 19:29:07 +00:00
|
|
|
void GuiComponent::update(int deltaTime)
|
|
|
|
{
|
|
|
|
updateSelf(deltaTime);
|
|
|
|
updateChildren(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
|
|
|
|
2014-05-29 20:41:47 +00:00
|
|
|
const Eigen::Affine3f& GuiComponent::getTransform()
|
2013-07-10 11:29:43 +00:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2014-04-18 17:19:46 +00:00
|
|
|
void GuiComponent::setAnimation(Animation* anim, int delay, 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];
|
2014-04-18 17:19:46 +00:00
|
|
|
mAnimationMap[slot] = new AnimationController(anim, delay, finishedCallback, reverse);
|
2013-12-13 03:17:59 +00:00
|
|
|
|
|
|
|
if(oldAnim)
|
|
|
|
delete oldAnim;
|
2013-12-08 17:35:43 +00:00
|
|
|
}
|
|
|
|
|
2014-04-18 17:19:46 +00:00
|
|
|
bool 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;
|
2014-04-18 17:19:46 +00:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
2013-12-13 03:17:59 +00:00
|
|
|
}
|
2013-12-08 17:35:43 +00:00
|
|
|
}
|
2014-01-01 05:39:22 +00:00
|
|
|
|
2014-04-18 17:19:46 +00:00
|
|
|
bool GuiComponent::cancelAnimation(unsigned char slot)
|
2014-04-15 02:03:11 +00:00
|
|
|
{
|
|
|
|
assert(slot < MAX_ANIMATIONS);
|
|
|
|
if(mAnimationMap[slot])
|
|
|
|
{
|
|
|
|
mAnimationMap[slot]->removeFinishedCallback();
|
|
|
|
delete mAnimationMap[slot];
|
|
|
|
mAnimationMap[slot] = NULL;
|
2014-04-18 17:19:46 +00:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GuiComponent::finishAnimation(unsigned char slot)
|
|
|
|
{
|
|
|
|
assert(slot < MAX_ANIMATIONS);
|
|
|
|
if(mAnimationMap[slot])
|
|
|
|
{
|
|
|
|
// skip to animation's end
|
|
|
|
const bool done = mAnimationMap[slot]->update(mAnimationMap[slot]->getAnimation()->getDuration() - mAnimationMap[slot]->getTime());
|
|
|
|
assert(done);
|
|
|
|
|
|
|
|
delete mAnimationMap[slot]; // will also call finishedCallback
|
|
|
|
mAnimationMap[slot] = NULL;
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
2014-04-15 02:03:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-15 01:58:16 +00:00
|
|
|
bool GuiComponent::advanceAnimation(unsigned char slot, unsigned int time)
|
|
|
|
{
|
|
|
|
assert(slot < MAX_ANIMATIONS);
|
|
|
|
AnimationController* anim = mAnimationMap[slot];
|
|
|
|
if(anim)
|
|
|
|
{
|
|
|
|
bool done = anim->update(time);
|
|
|
|
if(done)
|
|
|
|
{
|
|
|
|
mAnimationMap[slot] = NULL;
|
|
|
|
delete anim;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-15 02:03:11 +00:00
|
|
|
void GuiComponent::stopAllAnimations()
|
|
|
|
{
|
|
|
|
for(unsigned char i = 0; i < MAX_ANIMATIONS; i++)
|
|
|
|
stopAnimation(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GuiComponent::cancelAllAnimations()
|
|
|
|
{
|
|
|
|
for(unsigned char i = 0; i < MAX_ANIMATIONS; i++)
|
|
|
|
cancelAnimation(i);
|
|
|
|
}
|
|
|
|
|
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)
|
2014-05-29 20:41:47 +00:00
|
|
|
mWindow->setHelpPrompts(prompts, getHelpStyle());
|
|
|
|
}
|
|
|
|
|
|
|
|
HelpStyle GuiComponent::getHelpStyle()
|
|
|
|
{
|
|
|
|
return HelpStyle();
|
2014-01-25 23:34:29 +00:00
|
|
|
}
|
2016-09-12 11:31:44 +00:00
|
|
|
|
|
|
|
bool GuiComponent::isProcessing() const
|
|
|
|
{
|
|
|
|
return mIsProcessing;
|
|
|
|
}
|