From fa8e60b7b5935bf1fc64db42e061d1fe00003cc1 Mon Sep 17 00:00:00 2001 From: Aloshi <allofquist@yahoo.com> Date: Tue, 21 Jan 2014 20:43:33 -0600 Subject: [PATCH] Changed "center" property of textlist to "alignment". TextListComponent can now be aligned either left, right, or center. --- THEMES.md | 4 +- src/ThemeData.cpp | 2 +- src/ThemeData.h | 2 +- src/components/TextComponent.cpp | 2 +- src/components/TextListComponent.h | 51 ++++++++++++++++----- src/views/gamelist/DetailedGameListView.cpp | 2 +- 6 files changed, 46 insertions(+), 17 deletions(-) diff --git a/THEMES.md b/THEMES.md index 878e01574..3dc3953f7 100644 --- a/THEMES.md +++ b/THEMES.md @@ -331,8 +331,8 @@ Can be created as an extra. - Path to a truetype font (.ttf). * `fontSize` - type: FLOAT. - Size of the font as a percentage of screen height (e.g. for a value of `0.1`, the text's height would be 10% of the screen height). -* `center` - type: BOOLEAN. - - If true, center the text on the x-axis. +* `alignment` - type: STRING. + - Valid values are "left", "center", or "right". Controls alignment on the X axis. #### textlist diff --git a/src/ThemeData.cpp b/src/ThemeData.cpp index e59937180..5ed8b82f8 100644 --- a/src/ThemeData.cpp +++ b/src/ThemeData.cpp @@ -36,7 +36,7 @@ std::map< std::string, std::map<std::string, ThemeData::ElementPropertyType> > T ("fontPath", PATH) ("fontSize", FLOAT) ("scrollSound", PATH) - ("center", BOOLEAN)) + ("alignment", STRING)) ("container", boost::assign::map_list_of ("pos", NORMALIZED_PAIR) ("size", NORMALIZED_PAIR)) diff --git a/src/ThemeData.h b/src/ThemeData.h index 899926d8b..e459ec6a4 100644 --- a/src/ThemeData.h +++ b/src/ThemeData.h @@ -33,7 +33,7 @@ namespace ThemeFlags FONT_PATH = 32, FONT_SIZE = 64, SOUND = 128, - CENTER = 256, + ALIGNMENT = 256, TEXT = 512, ALL = 0xFFFFFFFF diff --git a/src/components/TextComponent.cpp b/src/components/TextComponent.cpp index 4b0b00073..d89b9990a 100644 --- a/src/components/TextComponent.cpp +++ b/src/components/TextComponent.cpp @@ -172,7 +172,7 @@ void TextComponent::applyTheme(const std::shared_ptr<ThemeData>& theme, const st if(properties & COLOR && elem->has("color")) setColor(elem->get<unsigned int>("color")); - if(properties & CENTER && elem->has("center")) + if(properties & ALIGNMENT && elem->has("center")) setCentered(elem->get<bool>("center")); if(properties & TEXT && elem->has("text")) diff --git a/src/components/TextListComponent.h b/src/components/TextListComponent.h index 4c4279a38..a9da7893b 100644 --- a/src/components/TextListComponent.h +++ b/src/components/TextListComponent.h @@ -48,14 +48,21 @@ public: void stopScrolling(); inline bool isScrolling() const { return mScrollDir != 0; } - inline void setCentered(bool centered) { mCentered = centered; } - enum CursorState { CURSOR_STOPPED, CURSOR_SCROLLING }; + enum Alignment + { + ALIGN_LEFT, + ALIGN_CENTER, + ALIGN_RIGHT + }; + + inline void setAlignment(Alignment align) { mAlignment = align; } + inline void setCursorChangedCallback(const std::function<void(CursorState state)>& func) { mCursorChangedCallback = func; } inline void setFont(const std::shared_ptr<Font>& font) @@ -88,7 +95,7 @@ private: int mMarqueeOffset; int mMarqueeTime; - bool mCentered; + Alignment mAlignment; std::vector<ListRow> mRowVector; int mCursor; @@ -114,7 +121,7 @@ TextListComponent<T>::TextListComponent(Window* window) : mMarqueeOffset = 0; mMarqueeTime = -MARQUEE_DELAY; - mCentered = true; + mAlignment = ALIGN_CENTER; mFont = Font::get(FONT_SIZE_MEDIUM); mSelectorColor = 0x000000FF; @@ -179,8 +186,6 @@ void TextListComponent<T>::render(const Eigen::Affine3f& parentTrans) ListRow& row = mRowVector.at((unsigned int)i); - float x = (float)(mCursor == i ? -mMarqueeOffset : 0); - unsigned int color; if(mCursor == i && mSelectedColor) color = mSelectedColor; @@ -192,10 +197,24 @@ void TextListComponent<T>::render(const Eigen::Affine3f& parentTrans) row.textCache->setColor(color); - Eigen::Vector3f offset(x, y, 0); + Eigen::Vector3f offset(0, y, 0); - if(mCentered) - offset[0] += (mSize.x() - row.textCache->metrics.size.x()) / 2; + switch(mAlignment) + { + case ALIGN_LEFT: + offset[0] = 0; + break; + case ALIGN_CENTER: + offset[0] = (mSize.x() - row.textCache->metrics.size.x()) / 2; + break; + case ALIGN_RIGHT: + offset[0] = (mSize.x() - row.textCache->metrics.size.x()); + break; + } + + if(offset[0] < 0) + offset[0] = 0; + offset[0] += (float)(mCursor == i ? -mMarqueeOffset : 0); Eigen::Affine3f drawTrans = trans; drawTrans.translate(offset); @@ -434,8 +453,18 @@ void TextListComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme, c if(properties & SOUND && elem->has("scrollSound")) setSound(Sound::get(elem->get<std::string>("scrollSound"))); - if(properties & CENTER && elem->has("center")) - mCentered = elem->get<bool>("center"); + if(properties & ALIGNMENT && elem->has("alignment")) + { + const std::string& str = elem->get<std::string>("alignment"); + if(str == "left") + setAlignment(ALIGN_LEFT); + else if(str == "center") + setAlignment(ALIGN_CENTER); + else if(str == "right") + setAlignment(ALIGN_RIGHT); + else + LOG(LogError) << "Unknown TextListComponent alignment \"" << str << "\"!"; + } } #endif diff --git a/src/views/gamelist/DetailedGameListView.cpp b/src/views/gamelist/DetailedGameListView.cpp index d79ba5e6d..522e67564 100644 --- a/src/views/gamelist/DetailedGameListView.cpp +++ b/src/views/gamelist/DetailedGameListView.cpp @@ -19,7 +19,7 @@ DetailedGameListView::DetailedGameListView(Window* window, FileData* root) : mList.setPosition(mSize.x() * (0.50f + padding), mList.getPosition().y()); mList.setSize(mSize.x() * (0.50f - 2*padding), mList.getSize().y()); - mList.setCentered(false); + mList.setAlignment(TextListComponent<FileData*>::ALIGN_LEFT); mList.setCursorChangedCallback([&](TextListComponent<FileData*>::CursorState state) { updateInfoPanel(); }); // image