From 77fb840a4bbc5152ad1f997c819885453074a321 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Thu, 22 Aug 2013 15:29:50 -0500 Subject: [PATCH] Added a basic ButtonComponent class. --- CMakeLists.txt | 2 + src/Font.cpp | 8 ++-- src/Font.h | 7 ++- src/components/ButtonComponent.cpp | 57 +++++++++++++++++++++++ src/components/ButtonComponent.h | 24 ++++++++++ src/components/ComponentListComponent.cpp | 14 +++--- src/components/GuiGameEd.cpp | 44 ++++++++++------- src/components/GuiGameEd.h | 10 +++- src/components/TextComponent.cpp | 5 +- 9 files changed, 136 insertions(+), 35 deletions(-) create mode 100644 src/components/ButtonComponent.cpp create mode 100644 src/components/ButtonComponent.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 597e3b670..b599f9f67 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -137,6 +137,7 @@ set(ES_HEADERS ${CMAKE_CURRENT_SOURCE_DIR}/src/Window.h ${CMAKE_CURRENT_SOURCE_DIR}/src/XMLReader.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/ImageComponent.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/XMLReader.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/ImageComponent.cpp ${CMAKE_CURRENT_SOURCE_DIR}/src/components/ScrollableContainer.cpp diff --git a/src/Font.cpp b/src/Font.cpp index 2147d7327..ed972e99e 100644 --- a/src/Font.cpp +++ b/src/Font.cpp @@ -86,7 +86,6 @@ Font::Font(const ResourceManager& rm, const std::string& path, int size) : fontS Font::~Font() { - LOG(LogInfo) << "Destroying font \"" << mPath << "\" with size " << mSize << "."; deinit(); } @@ -256,8 +255,6 @@ void Font::buildAtlas(ResourceData data) mSize = (int)(mSize * (1.0f / fontScale)); deinit(); 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; } - TextCache* cache = new TextCache(vertCount, vert, colors); + TextCache::CacheMetrics metrics = { sizeText(text) }; + TextCache* cache = new TextCache(vertCount, vert, colors, metrics); if(color != 0x00000000) cache->setColor(color); 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) { } diff --git a/src/Font.h b/src/Font.h index 6456804d9..90002aa3d 100644 --- a/src/Font.h +++ b/src/Font.h @@ -102,9 +102,14 @@ public: Eigen::Vector2f tex; }; + struct CacheMetrics + { + Eigen::Vector2f size; + } metrics; + void setColor(unsigned int color); - TextCache(int verts, Vertex* v, GLubyte* c); + TextCache(int verts, Vertex* v, GLubyte* c, const CacheMetrics& m); ~TextCache(); int vertCount; diff --git a/src/components/ButtonComponent.cpp b/src/components/ButtonComponent.cpp new file mode 100644 index 000000000..0edb2e552 --- /dev/null +++ b/src/components/ButtonComponent.cpp @@ -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 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 f = getFont(); + mTextCache = std::unique_ptr(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 ButtonComponent::getFont() +{ + return Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_SMALL); +} diff --git a/src/components/ButtonComponent.h b/src/components/ButtonComponent.h new file mode 100644 index 000000000..190332141 --- /dev/null +++ b/src/components/ButtonComponent.h @@ -0,0 +1,24 @@ +#pragma once + +#include "../GuiComponent.h" +#include +#include "../Font.h" + +class ButtonComponent : public GuiComponent +{ +public: + ButtonComponent(Window* window); + + void setPressedFunc(std::function 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 getFont(); + std::function mPressedFunc; + + std::string mText; + std::unique_ptr mTextCache; +}; diff --git a/src/components/ComponentListComponent.cpp b/src/components/ComponentListComponent.cpp index 225525225..6504e7b7f 100644 --- a/src/components/ComponentListComponent.cpp +++ b/src/components/ComponentListComponent.cpp @@ -356,16 +356,16 @@ void ComponentListComponent::update(int deltaTime) { for(auto iter = mEntries.begin(); iter != mEntries.end(); iter++) { - if(iter->updateType == UpdateAlways) + switch(iter->updateType) { + case UpdateAlways: iter->component->update(deltaTime); - continue; - } + break; - if(iter->updateType == UpdateFocused && cursorValid() && getCell(mCursor.x(), mCursor.y())->component == iter->component) - { - iter->component->update(deltaTime); - continue; + case UpdateFocused: + if(cursorValid() && getCell(mCursor.x(), mCursor.y())->component == iter->component) + iter->component->update(deltaTime); + break; } } } diff --git a/src/components/GuiGameEd.cpp b/src/components/GuiGameEd.cpp index f420d741f..3085cab0d 100644 --- a/src/components/GuiGameEd.cpp +++ b/src/components/GuiGameEd.cpp @@ -8,7 +8,8 @@ GuiGameEd::GuiGameEd(Window* window, GameData* game, const std::vectorgetBaseName()); + //initialize buttons + mDeleteButton.setText("DELETE", 0x555555FF); + mFetchButton.setText("FETCH", 0x555555FF); + + mSaveButton.setText("SAVE", 0x0000FFFF); + mSaveButton.setPressedFunc([&] { save(); delete this; }); + //initialize metadata list addChild(&mList); populateList(mdd); @@ -61,17 +69,12 @@ void GuiGameEd::populateList(const std::vector& mdd) int y = 0; - TextComponent* del = new TextComponent(mWindow); - del->setText("DELETE"); - 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); + //delete button + mList.setEntry(Vector2i(0, y), Vector2i(1, 1), &mDeleteButton, true, ComponentListComponent::AlignCenter); + //fetch button + mList.setEntry(Vector2i(1, y), Vector2i(1, 1), &mFetchButton, true, ComponentListComponent::AlignCenter); + y++; for(auto iter = mdd.begin(); iter != mdd.end(); iter++) @@ -79,20 +82,25 @@ void GuiGameEd::populateList(const std::vector& mdd) TextComponent* label = new TextComponent(mWindow); label->setText(iter->key); 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); ed->setSize(mSize.x() / 2, ed->getSize().y()); ed->setValue(mGame->metadata()->get(iter->key)); mList.setEntry(Vector2i(1, y), Vector2i(1, 1), ed, true, ComponentListComponent::AlignRight); - mGeneratedComponents.push_back(ed); + mEditors.push_back(ed); y++; } - TextComponent* save = new TextComponent(mWindow); - save->setText("SAVE"); - save->setColor(0x0000FFFF); - mList.setEntry(Vector2i(0, y), Vector2i(2, 1), save, true, ComponentListComponent::AlignCenter); - mGeneratedComponents.push_back(save); + //save button + mList.setEntry(Vector2i(0, y), Vector2i(2, 1), &mSaveButton, true, ComponentListComponent::AlignCenter); +} + +void GuiGameEd::save() +{ + for(unsigned int i = 0; i < mLabels.size(); i++) + { + mGame->metadata()->set(mLabels.at(i)->getValue(), mEditors.at(i)->getValue()); + } } diff --git a/src/components/GuiGameEd.h b/src/components/GuiGameEd.h index a0e8cccc6..32ed6b4bb 100644 --- a/src/components/GuiGameEd.h +++ b/src/components/GuiGameEd.h @@ -6,6 +6,7 @@ #include "TextComponent.h" #include "../GameData.h" #include "GuiBox.h" +#include "ButtonComponent.h" class GuiGameEd : public GuiComponent { @@ -14,6 +15,8 @@ public: virtual ~GuiGameEd(); private: + void save(); + void populateList(const std::vector& mdd); GuiBox mBox; @@ -22,7 +25,12 @@ private: TextComponent mPathDisp; - std::vector mGeneratedComponents; + std::vector mLabels; + std::vector mEditors; GameData* mGame; + + ButtonComponent mDeleteButton; + ButtonComponent mFetchButton; + ButtonComponent mSaveButton; }; diff --git a/src/components/TextComponent.cpp b/src/components/TextComponent.cpp index f3daa5ae2..188ef5803 100644 --- a/src/components/TextComponent.cpp +++ b/src/components/TextComponent.cpp @@ -73,10 +73,9 @@ void TextComponent::render(const Eigen::Affine3f& parentTrans) Eigen::Affine3f centeredTrans = trans; centeredTrans = centeredTrans.translate(Eigen::Vector3f(pos.x(), pos.y(), 0)); Renderer::setMatrix(centeredTrans); - font->renderTextCache(mTextCache.get()); - }else{ - font->renderTextCache(mTextCache.get()); } + + font->renderTextCache(mTextCache.get()); } GuiComponent::renderChildren(trans);