Optimized the hell out of TextListComponent (and TextComponent a bit)

This commit is contained in:
Aloshi 2013-11-21 16:39:02 -06:00
parent 87137df51e
commit 4b4fff39ef
5 changed files with 48 additions and 13 deletions

View file

@ -83,7 +83,7 @@ void TextComponent::render(const Eigen::Affine3f& parentTrans)
if(mCentered)
{
Eigen::Vector2f textSize = font->sizeWrappedText(mText, getSize().x());
const Eigen::Vector2f& textSize = mTextCache->metrics.size;
Eigen::Vector2f pos((getSize().x() - textSize.x()) / 2, 0);
Eigen::Affine3f centeredTrans = trans;

View file

@ -37,6 +37,7 @@ public:
std::string name;
T object;
unsigned int colorId;
std::shared_ptr<TextCache> textCache;
};
void add(const std::string& name, const T& obj, unsigned int colorId);
@ -52,7 +53,7 @@ public:
void stopScrolling();
inline bool isScrolling() const { return mScrollDir != 0; }
inline void setTheme(const std::shared_ptr<ThemeData>& theme) { mTheme = theme; }
void setTheme(const std::shared_ptr<ThemeData>& theme);
inline void setCentered(bool centered) { mCentered = centered; }
enum CursorState {
@ -113,8 +114,7 @@ template <typename T>
void TextListComponent<T>::render(const Eigen::Affine3f& parentTrans)
{
Eigen::Affine3f trans = parentTrans * getTransform();
Renderer::setMatrix(trans);
std::shared_ptr<Font> font = mTheme->getFont(THEME_FONT);
const int cutoff = 0;
@ -155,10 +155,11 @@ void TextListComponent<T>::render(const Eigen::Affine3f& parentTrans)
//draw selector bar
if(mCursor == i)
{
Renderer::setMatrix(trans);
Renderer::drawRect(0, (int)y, (int)getSize().x(), font->getHeight(), mTheme->getColor(THEME_SELECTOR_COLOR));
}
ListRow row = mRowVector.at((unsigned int)i);
ListRow& row = mRowVector.at((unsigned int)i);
float x = (float)(mCursor == i ? -mMarqueeOffset : 0);
@ -168,11 +169,22 @@ void TextListComponent<T>::render(const Eigen::Affine3f& parentTrans)
else
color = mTheme->getColor(THEME_ENTRY_COLOR[row.colorId]);
if(mCentered)
font->drawCenteredText(row.name, x, y, color);
else
font->drawText(row.name, Eigen::Vector2f(x, y), color);
if(!row.textCache)
row.textCache = std::unique_ptr<TextCache>(font->buildTextCache(row.name, 0, 0, 0x000000FF));
row.textCache->setColor(color);
Eigen::Vector3f offset(x, y, 0);
if(mCentered)
offset[0] += (mSize.x() - row.textCache->metrics.size.x()) / 2;
Eigen::Affine3f drawTrans = trans;
drawTrans.translate(offset);
Renderer::setMatrix(drawTrans);
font->renderTextCache(row.textCache.get());
y += entrySize;
}
@ -370,4 +382,14 @@ void TextListComponent<T>::onCursorChanged(CursorState state)
mCursorChangedCallback(state);
}
template <typename T>
void TextListComponent<T>::setTheme(const std::shared_ptr<ThemeData>& theme)
{
mTheme = theme;
// invalidate text caches in case font changed
for(auto it = mRowVector.begin(); it != mRowVector.end(); it++)
it->textCache.reset();
}
#endif

View file

@ -6,7 +6,6 @@ DetailedGameListView::DetailedGameListView(Window* window, FileData* root) :
mImage(window), mInfoBackground(window)
{
mHeaderImage.setPosition(mSize.x() * 0.25f, 0);
mHeaderImage.setResize(mSize.x() * 0.5f, 0, true);
mInfoBackground.setPosition(0, mSize.y() * 0.5f, 0);
mInfoBackground.setOrigin(0, 0.5f);
@ -38,11 +37,16 @@ DetailedGameListView::DetailedGameListView(Window* window, FileData* root) :
void DetailedGameListView::setTheme(const std::shared_ptr<ThemeData>& theme)
{
mHeaderImage.setResize(mSize.x() * 0.5f, 0, true);
BasicGameListView::setTheme(theme);
if(mHeaderImage.getPosition().y() + mHeaderImage.getSize().y() > mImage.getPosition().y())
mHeaderImage.setResize(0, mSize.y() * 0.185f, true);
mDescription.setFont(theme->getFont("descriptionFont"));
mDescription.setColor(theme->getColor("descriptionColor"));
mInfoBackground.setImage(theme->getImage("infoBackgroundImage").getTexture());
mInfoBackground.setTiling(theme->getImage("infoBackgroundImage").tile);
}
void DetailedGameListView::updateInfoPanel()

View file

@ -180,5 +180,15 @@ void ViewController::render(const Eigen::Affine3f& parentTrans)
//should really do some clipping here
for(auto it = mSystemViews.begin(); it != mSystemViews.end(); it++)
it->second->render(trans);
{
Eigen::Vector3f pos = it->second->getPosition();
Eigen::Vector2f size = it->second->getSize();
Eigen::Vector3f camPos = -trans.translation();
Eigen::Vector2f camSize((float)Renderer::getScreenWidth(), (float)Renderer::getScreenHeight());
if(pos.x() + size.x() >= camPos.x() && pos.y() + size.y() >= camPos.y() &&
pos.x() <= camPos.x() + camSize.x() && pos.y() <= camPos.y() + camSize.y())
it->second->render(trans);
}
}

View file

@ -14,8 +14,7 @@ public:
void goToPrevSystem();
void goToSystem(SystemData* system);
void goToSystemSelect();
void showQuickSystemSelect();
void onFileChanged(FileData* file, FileChangeType change);
// Plays a nice launch effect and launches the game at the end of it.