Finally use parenting in GuiGameList.

Added setCentered(bool) to TextComponent.
Fixed Font::sizeWrappedText to return an accurate width.
This commit is contained in:
Aloshi 2013-07-23 01:27:28 -05:00
parent e2c30a568d
commit 8d78052808
5 changed files with 61 additions and 54 deletions

View file

@ -397,8 +397,7 @@ void Font::drawWrappedText(std::string text, const Eigen::Vector2f& offset, floa
Eigen::Vector2f Font::sizeWrappedText(std::string text, float xLen) const
{
//this is incorrect for text that is so short it doesn't need to wrap
Eigen::Vector2f out(xLen, 0);
Eigen::Vector2f out(0, 0);
float y = 0;
@ -443,6 +442,10 @@ Eigen::Vector2f Font::sizeWrappedText(std::string text, float xLen) const
//move the word we skipped to the next line
line = word;
//update our maximum known line width
if(textSize.x() > out.x())
out[0] = textSize.x();
}else{
//there's still space, continue building the line
line = temp;

View file

@ -31,6 +31,7 @@ GuiGameList::GuiGameList(Window* window) : GuiComponent(window),
mDescription(window),
mDescContainer(window),
mTransitionImage(window, 0.0f, 0.0f, "", (float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight(), true),
mHeaderText(mWindow),
sortStateIndex(Settings::getInstance()->getInt("GameListSortIndex"))
{
//first object initializes the vector
@ -56,22 +57,27 @@ GuiGameList::GuiGameList(Window* window) : GuiComponent(window),
mTransitionImage.setPosition((float)Renderer::getScreenWidth(), 0);
mTransitionImage.setOrigin(0, 0);
mTransitionAnimation.addChild(&mTransitionImage);
//a hack! the GuiGameList doesn't use the children system right now because I haven't redone it to do so yet.
//the list depends on knowing it's final window coordinates (getGlobalOffset), which requires knowing the where the GuiGameList is.
//the GuiGameList now moves during screen transitions, so we have to let it know somehow.
//this should be removed in favor of using real children soon.
mList.setParent(this);
mHeaderText.setColor(0xFF0000FF);
mHeaderText.setFont(Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_LARGE));
mHeaderText.setPosition(0, 1);
mHeaderText.setSize((float)Renderer::getScreenWidth(), 0);
mHeaderText.setCentered(true);
addChild(mTheme);
addChild(&mHeaderText);
addChild(&mScreenshot);
addChild(&mDescContainer);
addChild(&mList);
addChild(&mTransitionImage);
mTransitionAnimation.addChild(this);
setSystemId(0);
}
GuiGameList::~GuiGameList()
{
//undo the parenting hack because otherwise it's not really a child and will try to remove itself on delete
mList.setParent(NULL);
delete mTheme;
}
@ -105,38 +111,8 @@ void GuiGameList::setSystemId(int id)
void GuiGameList::render(const Eigen::Affine3f& parentTrans)
{
if(mTransitionImage.getPosition().x() > 0) //transitioning in from the left
mPosition[0] = mTransitionImage.getPosition().x() - Renderer::getScreenWidth();
else //transitioning in from the right
mPosition[0] = mTransitionImage.getPosition().x() + Renderer::getScreenWidth();
Eigen::Affine3f trans = parentTrans * getTransform();
Renderer::setMatrix(trans);
if(mTheme)
mTheme->render(trans);
//reset modelview matrix if mTheme changed it
Renderer::setMatrix(trans);
std::shared_ptr<Font> headerFont = Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_LARGE);
//header
if(!mTheme->getBool("hideHeader"))
headerFont->drawCenteredText(mSystem->getDescName(), 0, 1, 0xFF0000FF);
if(isDetailed())
{
//divider
if(!mTheme->getBool("hideDividers"))
Renderer::drawRect((int)(Renderer::getScreenWidth() * mTheme->getFloat("listOffsetX")) - 4, headerFont->getHeight() + 2, 8, Renderer::getScreenHeight(), 0x0000FFFF);
mScreenshot.render(trans);
mDescContainer.render(trans);
}
mList.render(trans);
mTransitionImage.render(parentTrans);
renderChildren(trans);
}
bool GuiGameList::input(InputConfig* config, Input input)
@ -330,6 +306,13 @@ void GuiGameList::updateTheme()
mList.setFont(mTheme->getListFont());
mList.setPosition(0.0f, Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_LARGE)->getHeight() + 2.0f);
if(!mTheme->getBool("hideHeader"))
{
mHeaderText.setText(mSystem->getDescName());
}else{
mHeaderText.setText("");
}
if(isDetailed())
{
mList.setCentered(mTheme->getBool("listCentered"));
@ -405,19 +388,21 @@ GuiGameList* GuiGameList::create(Window* window)
void GuiGameList::update(int deltaTime)
{
mImageAnimation.update(deltaTime);
mTransitionAnimation.update(deltaTime);
mList.update(deltaTime);
mDescContainer.update(deltaTime);
mImageAnimation.update(deltaTime);
GuiComponent::update(deltaTime);
}
void GuiGameList::doTransition(int dir)
{
mTransitionImage.copyScreen();
mTransitionImage.setOpacity(255);
mTransitionImage.setPosition(0, 0);
//put the image of what's currently onscreen at what will be (in screen coords) 0, 0
mTransitionImage.setPosition((float)Renderer::getScreenWidth() * dir, 0);
//move the entire thing offscreen so we'll move into place
setPosition((float)Renderer::getScreenWidth() * -dir, mPosition[1]);
mTransitionAnimation.move(Renderer::getScreenWidth() * dir, 0, 50);
}

View file

@ -63,6 +63,7 @@ private:
ScrollableContainer mDescContainer;
AnimationComponent mImageAnimation;
ThemeComponent* mTheme;
TextComponent mHeaderText;
ImageComponent mTransitionImage;
AnimationComponent mTransitionAnimation;

View file

@ -4,12 +4,12 @@
#include "../Window.h"
TextComponent::TextComponent(Window* window) : GuiComponent(window),
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, true)
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, true), mCentered(false)
{
}
TextComponent::TextComponent(Window* window, const std::string& text, std::shared_ptr<Font> font, Eigen::Vector3f pos, Eigen::Vector2f size) : GuiComponent(window),
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, true)
mFont(NULL), mColor(0x000000FF), mAutoCalcExtent(true, true), mCentered(false)
{
setText(text);
setFont(font);
@ -43,6 +43,11 @@ void TextComponent::setText(const std::string& text)
calculateExtent();
}
void TextComponent::setCentered(bool center)
{
mCentered = center;
}
std::shared_ptr<Font> TextComponent::getFont() const
{
if(mFont)
@ -56,9 +61,20 @@ void TextComponent::render(const Eigen::Affine3f& parentTrans)
std::shared_ptr<Font> font = getFont();
Eigen::Affine3f trans = parentTrans * getTransform();
Renderer::setMatrix(trans);
font->drawWrappedText(mText, Eigen::Vector2f(0, 0), getSize().x(), mColor >> 8 << 8 | getOpacity());
if(font && !mText.empty())
{
Renderer::setMatrix(trans);
if(mCentered)
{
Eigen::Vector2f textSize = font->sizeWrappedText(mText, getSize().x());
Eigen::Vector2f pos((getSize().x() - textSize.x()) / 2, 0);
font->drawWrappedText(mText, pos, getSize().x(), (mColor >> 8 << 8) | getOpacity());
}else{
font->drawWrappedText(mText, Eigen::Vector2f(0, 0), getSize().x(), mColor >> 8 << 8 | getOpacity());
}
}
GuiComponent::renderChildren(trans);
}

View file

@ -14,6 +14,7 @@ public:
void onSizeChanged() override;
void setText(const std::string& text);
void setColor(unsigned int color);
void setCentered(bool center); //Default is uncentered.
void render(const Eigen::Affine3f& parentTrans) override;
@ -26,6 +27,7 @@ private:
std::shared_ptr<Font> mFont;
Eigen::Matrix<bool, 1, 2> mAutoCalcExtent;
std::string mText;
bool mCentered;
};
#endif