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 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(0, 0);
Eigen::Vector2f out(xLen, 0);
float y = 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 //move the word we skipped to the next line
line = word; line = word;
//update our maximum known line width
if(textSize.x() > out.x())
out[0] = textSize.x();
}else{ }else{
//there's still space, continue building the line //there's still space, continue building the line
line = temp; line = temp;

View file

@ -31,6 +31,7 @@ GuiGameList::GuiGameList(Window* window) : GuiComponent(window),
mDescription(window), mDescription(window),
mDescContainer(window), mDescContainer(window),
mTransitionImage(window, 0.0f, 0.0f, "", (float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight(), true), mTransitionImage(window, 0.0f, 0.0f, "", (float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight(), true),
mHeaderText(mWindow),
sortStateIndex(Settings::getInstance()->getInt("GameListSortIndex")) sortStateIndex(Settings::getInstance()->getInt("GameListSortIndex"))
{ {
//first object initializes the vector //first object initializes the vector
@ -56,22 +57,27 @@ GuiGameList::GuiGameList(Window* window) : GuiComponent(window),
mTransitionImage.setPosition((float)Renderer::getScreenWidth(), 0); mTransitionImage.setPosition((float)Renderer::getScreenWidth(), 0);
mTransitionImage.setOrigin(0, 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. mHeaderText.setColor(0xFF0000FF);
//the list depends on knowing it's final window coordinates (getGlobalOffset), which requires knowing the where the GuiGameList is. mHeaderText.setFont(Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_LARGE));
//the GuiGameList now moves during screen transitions, so we have to let it know somehow. mHeaderText.setPosition(0, 1);
//this should be removed in favor of using real children soon. mHeaderText.setSize((float)Renderer::getScreenWidth(), 0);
mList.setParent(this); mHeaderText.setCentered(true);
addChild(mTheme);
addChild(&mHeaderText);
addChild(&mScreenshot);
addChild(&mDescContainer);
addChild(&mList);
addChild(&mTransitionImage);
mTransitionAnimation.addChild(this);
setSystemId(0); setSystemId(0);
} }
GuiGameList::~GuiGameList() 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; delete mTheme;
} }
@ -105,38 +111,8 @@ void GuiGameList::setSystemId(int id)
void GuiGameList::render(const Eigen::Affine3f& parentTrans) 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(); Eigen::Affine3f trans = parentTrans * getTransform();
Renderer::setMatrix(trans); renderChildren(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);
} }
bool GuiGameList::input(InputConfig* config, Input input) bool GuiGameList::input(InputConfig* config, Input input)
@ -330,6 +306,13 @@ void GuiGameList::updateTheme()
mList.setFont(mTheme->getListFont()); mList.setFont(mTheme->getListFont());
mList.setPosition(0.0f, Font::get(*mWindow->getResourceManager(), Font::getDefaultPath(), FONT_SIZE_LARGE)->getHeight() + 2.0f); 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()) if(isDetailed())
{ {
mList.setCentered(mTheme->getBool("listCentered")); mList.setCentered(mTheme->getBool("listCentered"));
@ -405,19 +388,21 @@ GuiGameList* GuiGameList::create(Window* window)
void GuiGameList::update(int deltaTime) void GuiGameList::update(int deltaTime)
{ {
mImageAnimation.update(deltaTime);
mTransitionAnimation.update(deltaTime); mTransitionAnimation.update(deltaTime);
mImageAnimation.update(deltaTime);
mList.update(deltaTime); GuiComponent::update(deltaTime);
mDescContainer.update(deltaTime);
} }
void GuiGameList::doTransition(int dir) void GuiGameList::doTransition(int dir)
{ {
mTransitionImage.copyScreen(); mTransitionImage.copyScreen();
mTransitionImage.setOpacity(255); 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); mTransitionAnimation.move(Renderer::getScreenWidth() * dir, 0, 50);
} }

View file

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

View file

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

View file

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