Added a basic ButtonComponent class.

This commit is contained in:
Aloshi 2013-08-22 15:29:50 -05:00
parent df897c0b5a
commit 77fb840a4b
9 changed files with 136 additions and 35 deletions

View file

@ -137,6 +137,7 @@ set(ES_HEADERS
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.h ${CMAKE_CURRENT_SOURCE_DIR}/src/Window.h
${CMAKE_CURRENT_SOURCE_DIR}/src/XMLReader.h ${CMAKE_CURRENT_SOURCE_DIR}/src/XMLReader.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/AnimationComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/AnimationComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ButtonComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentListComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentListComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.h
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ScrollableContainer.h ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ScrollableContainer.h
@ -183,6 +184,7 @@ set(ES_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}/src/Window.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/Window.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/XMLReader.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/XMLReader.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/AnimationComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/AnimationComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ButtonComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentListComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ComponentListComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ImageComponent.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/components/ScrollableContainer.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ScrollableContainer.cpp

View file

@ -86,7 +86,6 @@ Font::Font(const ResourceManager& rm, const std::string& path, int size) : fontS
Font::~Font() Font::~Font()
{ {
LOG(LogInfo) << "Destroying font \"" << mPath << "\" with size " << mSize << ".";
deinit(); deinit();
} }
@ -256,8 +255,6 @@ void Font::buildAtlas(ResourceData data)
mSize = (int)(mSize * (1.0f / fontScale)); mSize = (int)(mSize * (1.0f / fontScale));
deinit(); deinit();
init(data); init(data);
}else{
LOG(LogInfo) << "Created font \"" << mPath << "\" with size " << mSize << ". textureID: " << textureID;
} }
} }
@ -497,14 +494,15 @@ TextCache* Font::buildTextCache(const std::string& text, float offsetX, float of
x += charData[letter].advX * fontScale; x += charData[letter].advX * fontScale;
} }
TextCache* cache = new TextCache(vertCount, vert, colors); TextCache::CacheMetrics metrics = { sizeText(text) };
TextCache* cache = new TextCache(vertCount, vert, colors, metrics);
if(color != 0x00000000) if(color != 0x00000000)
cache->setColor(color); cache->setColor(color);
return cache; return cache;
} }
TextCache::TextCache(int verts, Vertex* v, GLubyte* c) : vertCount(verts), verts(v), colors(c) TextCache::TextCache(int verts, Vertex* v, GLubyte* c, const CacheMetrics& m) : vertCount(verts), verts(v), colors(c), metrics(m)
{ {
} }

View file

@ -102,9 +102,14 @@ public:
Eigen::Vector2f tex; Eigen::Vector2f tex;
}; };
struct CacheMetrics
{
Eigen::Vector2f size;
} metrics;
void setColor(unsigned int color); void setColor(unsigned int color);
TextCache(int verts, Vertex* v, GLubyte* c); TextCache(int verts, Vertex* v, GLubyte* c, const CacheMetrics& m);
~TextCache(); ~TextCache();
int vertCount; int vertCount;

View file

@ -0,0 +1,57 @@
#include "ButtonComponent.h"
#include "../Renderer.h"
#include "../Window.h"
ButtonComponent::ButtonComponent(Window* window) : GuiComponent(window)
{
setSize(64, 48);
}
void ButtonComponent::setPressedFunc(std::function<void()> f)
{
mPressedFunc = f;
}
bool ButtonComponent::input(InputConfig* config, Input input)
{
if(config->isMappedTo("a", input))
{
mPressedFunc();
return true;
}
return GuiComponent::input(config, input);
}
void ButtonComponent::setText(const std::string& text, unsigned int color)
{
mText = text;
std::shared_ptr<Font> f = getFont();
mTextCache = std::unique_ptr<TextCache>(f->buildTextCache(mText, 0, 0, color));
mOpacity = color & 0x000000FF;
setSize(mTextCache->metrics.size + Eigen::Vector2f(12, 12));
}
void ButtonComponent::render(const Eigen::Affine3f& parentTrans)
{
Eigen::Affine3f trans = parentTrans * getTransform();
if(mTextCache)
{
Eigen::Vector3f centerOffset((mSize.x() - mTextCache->metrics.size.x()) / 2, (mSize.y() - mTextCache->metrics.size.y()) / 2, 0);
trans = trans.translate(centerOffset);
Renderer::setMatrix(trans);
getFont()->renderTextCache(mTextCache.get());
trans = trans.translate(-centerOffset);
}
renderChildren(trans);
}
std::shared_ptr<Font> ButtonComponent::getFont()
{
return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL);
}

View file

@ -0,0 +1,24 @@
#pragma once
#include "../GuiComponent.h"
#include <functional>
#include "../Font.h"
class ButtonComponent : public GuiComponent
{
public:
ButtonComponent(Window* window);
void setPressedFunc(std::function<void()> f);
bool input(InputConfig* config, Input input) override;
void render(const Eigen::Affine3f& parentTrans) override;
void setText(const std::string& text, unsigned int color);
private:
std::shared_ptr<Font> getFont();
std::function<void()> mPressedFunc;
std::string mText;
std::unique_ptr<TextCache> mTextCache;
};

View file

@ -356,16 +356,16 @@ void ComponentListComponent::update(int deltaTime)
{ {
for(auto iter = mEntries.begin(); iter != mEntries.end(); iter++) for(auto iter = mEntries.begin(); iter != mEntries.end(); iter++)
{ {
if(iter->updateType == UpdateAlways) switch(iter->updateType)
{ {
case UpdateAlways:
iter->component->update(deltaTime); iter->component->update(deltaTime);
continue; break;
}
if(iter->updateType == UpdateFocused && cursorValid() && getCell(mCursor.x(), mCursor.y())->component == iter->component) case UpdateFocused:
{ if(cursorValid() && getCell(mCursor.x(), mCursor.y())->component == iter->component)
iter->component->update(deltaTime); iter->component->update(deltaTime);
continue; break;
} }
} }
} }

View file

@ -8,7 +8,8 @@ GuiGameEd::GuiGameEd(Window* window, GameData* game, const std::vector<MetaDataD
mBox(mWindow, 0, 0, 0, 0), mBox(mWindow, 0, 0, 0, 0),
mList(window, Eigen::Vector2i(3, mdd.size() + MDED_RESERVED_ROWS)), mList(window, Eigen::Vector2i(3, mdd.size() + MDED_RESERVED_ROWS)),
mPathDisp(window), mPathDisp(window),
mGame(game) mGame(game),
mDeleteButton(window), mFetchButton(window), mSaveButton(window)
{ {
LOG(LogInfo) << "Creating GuiGameEd"; LOG(LogInfo) << "Creating GuiGameEd";
@ -35,6 +36,13 @@ GuiGameEd::GuiGameEd(Window* window, GameData* game, const std::vector<MetaDataD
mPathDisp.setCentered(true); mPathDisp.setCentered(true);
mPathDisp.setText(mGame->getBaseName()); mPathDisp.setText(mGame->getBaseName());
//initialize buttons
mDeleteButton.setText("DELETE", 0x555555FF);
mFetchButton.setText("FETCH", 0x555555FF);
mSaveButton.setText("SAVE", 0x0000FFFF);
mSaveButton.setPressedFunc([&] { save(); delete this; });
//initialize metadata list //initialize metadata list
addChild(&mList); addChild(&mList);
populateList(mdd); populateList(mdd);
@ -61,17 +69,12 @@ void GuiGameEd::populateList(const std::vector<MetaDataDecl>& mdd)
int y = 0; int y = 0;
TextComponent* del = new TextComponent(mWindow); //delete button
del->setText("DELETE"); mList.setEntry(Vector2i(0, y), Vector2i(1, 1), &mDeleteButton, true, ComponentListComponent::AlignCenter);
del->setColor(0xFF0000FF);
mList.setEntry(Vector2i(0, y), Vector2i(1, 1), del, true, ComponentListComponent::AlignCenter);
mGeneratedComponents.push_back(del);
TextComponent* fetch = new TextComponent(mWindow);
fetch->setText("FETCH");
mList.setEntry(Vector2i(1, y), Vector2i(1, 1), fetch, true, ComponentListComponent::AlignCenter);
mGeneratedComponents.push_back(fetch);
//fetch button
mList.setEntry(Vector2i(1, y), Vector2i(1, 1), &mFetchButton, true, ComponentListComponent::AlignCenter);
y++; y++;
for(auto iter = mdd.begin(); iter != mdd.end(); iter++) for(auto iter = mdd.begin(); iter != mdd.end(); iter++)
@ -79,20 +82,25 @@ void GuiGameEd::populateList(const std::vector<MetaDataDecl>& mdd)
TextComponent* label = new TextComponent(mWindow); TextComponent* label = new TextComponent(mWindow);
label->setText(iter->key); label->setText(iter->key);
mList.setEntry(Vector2i(0, y), Vector2i(1, 1), label, false, ComponentListComponent::AlignLeft); mList.setEntry(Vector2i(0, y), Vector2i(1, 1), label, false, ComponentListComponent::AlignLeft);
mGeneratedComponents.push_back(label); mLabels.push_back(label);
GuiComponent* ed = MetaDataList::makeEditor(mWindow, iter->type); GuiComponent* ed = MetaDataList::makeEditor(mWindow, iter->type);
ed->setSize(mSize.x() / 2, ed->getSize().y()); ed->setSize(mSize.x() / 2, ed->getSize().y());
ed->setValue(mGame->metadata()->get(iter->key)); ed->setValue(mGame->metadata()->get(iter->key));
mList.setEntry(Vector2i(1, y), Vector2i(1, 1), ed, true, ComponentListComponent::AlignRight); mList.setEntry(Vector2i(1, y), Vector2i(1, 1), ed, true, ComponentListComponent::AlignRight);
mGeneratedComponents.push_back(ed); mEditors.push_back(ed);
y++; y++;
} }
TextComponent* save = new TextComponent(mWindow); //save button
save->setText("SAVE"); mList.setEntry(Vector2i(0, y), Vector2i(2, 1), &mSaveButton, true, ComponentListComponent::AlignCenter);
save->setColor(0x0000FFFF); }
mList.setEntry(Vector2i(0, y), Vector2i(2, 1), save, true, ComponentListComponent::AlignCenter);
mGeneratedComponents.push_back(save); void GuiGameEd::save()
{
for(unsigned int i = 0; i < mLabels.size(); i++)
{
mGame->metadata()->set(mLabels.at(i)->getValue(), mEditors.at(i)->getValue());
}
} }

View file

@ -6,6 +6,7 @@
#include "TextComponent.h" #include "TextComponent.h"
#include "../GameData.h" #include "../GameData.h"
#include "GuiBox.h" #include "GuiBox.h"
#include "ButtonComponent.h"
class GuiGameEd : public GuiComponent class GuiGameEd : public GuiComponent
{ {
@ -14,6 +15,8 @@ public:
virtual ~GuiGameEd(); virtual ~GuiGameEd();
private: private:
void save();
void populateList(const std::vector<MetaDataDecl>& mdd); void populateList(const std::vector<MetaDataDecl>& mdd);
GuiBox mBox; GuiBox mBox;
@ -22,7 +25,12 @@ private:
TextComponent mPathDisp; TextComponent mPathDisp;
std::vector<GuiComponent*> mGeneratedComponents; std::vector<TextComponent*> mLabels;
std::vector<GuiComponent*> mEditors;
GameData* mGame; GameData* mGame;
ButtonComponent mDeleteButton;
ButtonComponent mFetchButton;
ButtonComponent mSaveButton;
}; };

View file

@ -73,10 +73,9 @@ void TextComponent::render(const Eigen::Affine3f& parentTrans)
Eigen::Affine3f centeredTrans = trans; Eigen::Affine3f centeredTrans = trans;
centeredTrans = centeredTrans.translate(Eigen::Vector3f(pos.x(), pos.y(), 0)); centeredTrans = centeredTrans.translate(Eigen::Vector3f(pos.x(), pos.y(), 0));
Renderer::setMatrix(centeredTrans); Renderer::setMatrix(centeredTrans);
font->renderTextCache(mTextCache.get());
}else{
font->renderTextCache(mTextCache.get());
} }
font->renderTextCache(mTextCache.get());
} }
GuiComponent::renderChildren(trans); GuiComponent::renderChildren(trans);