Changed makeButtonGrid() to return a pre-padded ComponentGrid so every GUI doesn't have to do its own thing for vertical padding.

This commit is contained in:
Aloshi 2014-03-21 11:54:48 -05:00
parent 91546ac2bc
commit 9fe7ceeb72
8 changed files with 59 additions and 49 deletions

View file

@ -1,7 +1,8 @@
#include "MenuComponent.h"
#include "ButtonComponent.h"
#define BUTTON_GRID_HEIGHT ((float)Font::get(FONT_SIZE_MEDIUM)->getHeight() + 32)
#define BUTTON_GRID_VERT_PADDING 20
#define BUTTON_GRID_HORIZ_PADDING 16
using namespace Eigen;
@ -27,9 +28,14 @@ MenuComponent::MenuComponent(Window* window, const char* title) : GuiComponent(w
mGrid.resetCursor();
}
float MenuComponent::getButtonGridHeight() const
{
return (mButtonGrid ? mButtonGrid->getSize().y() : Font::get(FONT_SIZE_MEDIUM)->getHeight() + BUTTON_GRID_VERT_PADDING);
}
void MenuComponent::updateSize()
{
float height = mTitle->getSize().y() + mList->getTotalRowHeight() + BUTTON_GRID_HEIGHT + 2;
float height = mTitle->getSize().y() + mList->getTotalRowHeight() + getButtonGridHeight() + 2;
if(height > Renderer::getScreenHeight() * 0.7f)
height = Renderer::getScreenHeight() * 0.7f;
@ -42,7 +48,7 @@ void MenuComponent::onSizeChanged()
// update grid row/col sizes
mGrid.setRowHeightPerc(0, mTitle->getSize().y() / mSize.y());
mGrid.setRowHeightPerc(2, (BUTTON_GRID_HEIGHT) / mSize.y());
mGrid.setRowHeightPerc(2, getButtonGridHeight() / mSize.y());
mGrid.setSize(mSize);
}
@ -77,7 +83,7 @@ std::shared_ptr<ComponentGrid> makeButtonGrid(Window* window, const std::vector<
{
std::shared_ptr<ComponentGrid> buttonGrid = std::make_shared<ComponentGrid>(window, Vector2i(buttons.size(), 1));
float buttonGridWidth = 16.0f * buttons.size(); // initialize to padding
float buttonGridWidth = (float)BUTTON_GRID_HORIZ_PADDING * buttons.size(); // initialize to padding
for(int i = 0; i < (int)buttons.size(); i++)
{
buttonGrid->setEntry(buttons.at(i), Vector2i(i, 0), true, false);
@ -85,10 +91,10 @@ std::shared_ptr<ComponentGrid> makeButtonGrid(Window* window, const std::vector<
}
for(unsigned int i = 0; i < buttons.size(); i++)
{
buttonGrid->setColWidthPerc(i, (buttons.at(i)->getSize().x() + 16) / buttonGridWidth);
buttonGrid->setColWidthPerc(i, (buttons.at(i)->getSize().x() + BUTTON_GRID_HORIZ_PADDING) / buttonGridWidth);
}
buttonGrid->setSize(buttonGridWidth, buttons.at(0)->getSize().y());
buttonGrid->setSize(buttonGridWidth, buttons.at(0)->getSize().y() + BUTTON_GRID_VERT_PADDING);
return buttonGrid;
}

View file

@ -37,6 +37,7 @@ public:
private:
void updateSize();
void updateGrid();
float getButtonGridHeight() const;
NinePatchComponent mBackground;
ComponentGrid mGrid;

View file

@ -98,28 +98,31 @@ void ScraperSearchComponent::onSizeChanged()
// metadata
// (mMD_Grid has already been resized by mGrid)
const int fontHeight = (int)(mMD_Grid->getSize().y() / mMD_Pairs.size() * 0.8f);
auto fontLbl = Font::get(fontHeight, FONT_PATH_REGULAR);
auto fontComp = Font::get(fontHeight, FONT_PATH_LIGHT);
// update label fonts
float maxLblWidth = 0;
for(auto it = mMD_Pairs.begin(); it != mMD_Pairs.end(); it++)
if(mMD_Grid->getSize().y() > mMD_Pairs.size())
{
it->first->setFont(fontLbl);
it->first->setSize(0, 0);
if(it->first->getSize().x() > maxLblWidth)
maxLblWidth = it->first->getSize().x() + 6;
const int fontHeight = (int)(mMD_Grid->getSize().y() / mMD_Pairs.size() * 0.8f);
auto fontLbl = Font::get(fontHeight, FONT_PATH_REGULAR);
auto fontComp = Font::get(fontHeight, FONT_PATH_LIGHT);
// update label fonts
float maxLblWidth = 0;
for(auto it = mMD_Pairs.begin(); it != mMD_Pairs.end(); it++)
{
it->first->setFont(fontLbl);
it->first->setSize(0, 0);
if(it->first->getSize().x() > maxLblWidth)
maxLblWidth = it->first->getSize().x() + 6;
}
// update component fonts
mMD_ReleaseDate->setFont(fontComp);
mMD_Developer->setFont(fontComp);
mMD_Publisher->setFont(fontComp);
mMD_Genre->setFont(fontComp);
mMD_Players->setFont(fontComp);
mMD_Grid->setColWidthPerc(0, maxLblWidth / mMD_Grid->getSize().x());
}
// update component fonts
mMD_ReleaseDate->setFont(fontComp);
mMD_Developer->setFont(fontComp);
mMD_Publisher->setFont(fontComp);
mMD_Genre->setFont(fontComp);
mMD_Players->setFont(fontComp);
mMD_Grid->setColWidthPerc(0, maxLblWidth / mMD_Grid->getSize().x());
}
void ScraperSearchComponent::updateViewStyle()

View file

@ -17,18 +17,9 @@ GuiGameScraper::GuiGameScraper(Window* window, ScraperSearchParams params, std::
addChild(&mBox);
addChild(&mGrid);
setSize((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
mGrid.setSize(mSize.x() * 0.7f, mSize.y() * 0.65f);
auto headerFont = Font::get(FONT_SIZE_LARGE);
mGrid.setRowHeightPerc(0, headerFont->getHeight() / mGrid.getSize().y()); // header
mGrid.setRowHeightPerc(2, 0.19f); // buttons
// header
mGrid.setEntry(std::make_shared<TextComponent>(mWindow, getCleanFileName(mSearchParams.game->getName()),
headerFont, 0x777777FF, true), Eigen::Vector2i(0, 0), false, true);
mHeader = std::make_shared<TextComponent>(mWindow, getCleanFileName(mSearchParams.game->getName()), Font::get(FONT_SIZE_LARGE), 0x777777FF, true);
mGrid.setEntry(mHeader, Eigen::Vector2i(0, 0), false, true);
// ScraperSearchComponent
mSearch = std::make_shared<ScraperSearchComponent>(window, ScraperSearchComponent::NEVER_AUTO_ACCEPT);
@ -38,13 +29,9 @@ GuiGameScraper::GuiGameScraper(Window* window, ScraperSearchParams params, std::
std::vector< std::shared_ptr<ButtonComponent> > buttons;
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, "INPUT", "manually search"));
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, "CANCEL", "cancel", [&] { delete this; }));
auto buttonGrid = makeButtonGrid(mWindow, buttons);
mButtonGrid = makeButtonGrid(mWindow, buttons);
mGrid.setEntry(buttonGrid, Eigen::Vector2i(0, 2), true, false);
// center everything
mGrid.setPosition((mSize.x() - mGrid.getSize().x()) / 2, (mSize.y() - mGrid.getSize().y()) / 2);
mBox.fitTo(mGrid.getSize(), mGrid.getPosition(), Eigen::Vector2f(-32, -32));
mGrid.setEntry(mButtonGrid, Eigen::Vector2i(0, 2), true, false);
// we call this->close() instead of just delete this; in the accept callback:
// this is because of how GuiComponent::update works. if it was just delete this, this would happen when the metadata resolver is done:
@ -71,10 +58,22 @@ GuiGameScraper::GuiGameScraper(Window* window, ScraperSearchParams params, std::
mSearch->setAcceptCallback([this, doneFunc](const ScraperSearchResult& result) { doneFunc(result); close(); });
mSearch->setCancelCallback([&] { delete this; });
setSize(Renderer::getScreenWidth() * 0.7f, Renderer::getScreenHeight() * 0.65f);
setPosition((Renderer::getScreenWidth() - mSize.x()) / 2, (Renderer::getScreenHeight() - mSize.y()) / 2);
mGrid.resetCursor();
mSearch->search(params); // start the search
}
void GuiGameScraper::onSizeChanged()
{
mBox.fitTo(mSize, Eigen::Vector3f::Zero(), Eigen::Vector2f(-32, -32));
mGrid.setSize(mSize);
mGrid.setRowHeightPerc(0, mHeader->getFont()->getHeight() / mSize.y()); // header
mGrid.setRowHeightPerc(2, mButtonGrid->getSize().y() / mSize.y()); // buttons
}
bool GuiGameScraper::input(InputConfig* config, Input input)
{
if(config->isMappedTo("b", input) && input.value)

View file

@ -9,6 +9,8 @@ class GuiGameScraper : public GuiComponent
public:
GuiGameScraper(Window* window, ScraperSearchParams params, std::function<void(const ScraperSearchResult&)> doneFunc);
void onSizeChanged() override;
bool input(InputConfig* config, Input input) override;
void update(int deltaTime);
virtual std::vector<HelpPrompt> getHelpPrompts() override;
@ -20,7 +22,9 @@ private:
ComponentGrid mGrid;
NinePatchComponent mBox;
std::shared_ptr<TextComponent> mHeader;
std::shared_ptr<ScraperSearchComponent> mSearch;
std::shared_ptr<ComponentGrid> mButtonGrid;
ScraperSearchParams mSearchParams;

View file

@ -5,8 +5,6 @@
#include "../components/MenuComponent.h" // for makeButtonGrid
#include "../Util.h"
#define BUTTON_VERT_PADDING 32.0f
GuiMsgBox::GuiMsgBox(Window* window, const std::string& text,
const std::string& name1, const std::function<void()>& func1,
const std::string& name2, const std::function<void()>& func2,
@ -60,7 +58,7 @@ GuiMsgBox::GuiMsgBox(Window* window, const std::string& text,
}
const float msgHeight = std::max(Font::get(FONT_SIZE_LARGE)->getHeight(), mMsg->getSize().y());
setSize(width, msgHeight + mButtonGrid->getSize().y() + BUTTON_VERT_PADDING);
setSize(width, msgHeight + mButtonGrid->getSize().y());
// center for good measure
setPosition((Renderer::getScreenWidth() - mSize.x()) / 2.0f, (Renderer::getScreenHeight() - mSize.y()) / 2.0f);
@ -85,7 +83,7 @@ void GuiMsgBox::onSizeChanged()
mGrid.setSize(mSize);
mBackground.fitTo(mSize, Eigen::Vector3f::Zero(), Eigen::Vector2f(-32, -32));
mGrid.setRowHeightPerc(1, (mButtonGrid->getSize().y() + BUTTON_VERT_PADDING) / mSize.y());
mGrid.setRowHeightPerc(1, mButtonGrid->getSize().y() / mSize.y());
}
void GuiMsgBox::deleteMeAndCall(const std::function<void()>& func)

View file

@ -37,7 +37,6 @@ GuiScraperMulti::GuiScraperMulti(Window* window, const std::queue<ScraperSearchP
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, "SKIP", "skip this game", std::bind(&GuiScraperMulti::skip, this)));
buttons.push_back(std::make_shared<ButtonComponent>(mWindow, "STOP", "cancel scraping", std::bind(&GuiScraperMulti::finish, this)));
mButtonGrid = makeButtonGrid(mWindow, buttons);
mButtonGrid->setSize(mButtonGrid->getSize().x(), mButtonGrid->getSize().y() + 32);
mGrid.setEntry(mButtonGrid, Vector2i(0, 3), true, false);
setSize(Renderer::getScreenWidth() * 0.7f, Renderer::getScreenHeight() * 0.65f);

View file

@ -27,7 +27,7 @@ GuiTextEditPopup::GuiTextEditPopup(Window* window, const std::string& title, con
float textHeight = mText->getFont()->getHeight();
if(multiLine)
textHeight *= 3;
textHeight *= 6;
setSize(Renderer::getScreenWidth() * 0.5f, mTitle->getFont()->getHeight() + textHeight + mButtonGrid->getSize().y());
setPosition((Renderer::getScreenWidth() - mSize.x()) / 2, (Renderer::getScreenHeight() - mSize.y()) / 2);