From ea009315e9076a8216b40d093e13c91fe48660d5 Mon Sep 17 00:00:00 2001 From: Aloshi Date: Tue, 21 Jan 2014 21:10:49 -0600 Subject: [PATCH] Added horizontal margins to textlist. Updated and corrected documentation (center -> alignment for textlist, not text!). --- THEMES.md | 10 ++++--- src/ThemeData.cpp | 3 +- src/components/TextListComponent.h | 46 +++++++++++++++++++----------- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/THEMES.md b/THEMES.md index 3dc3953f7..903a385e8 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). -* `alignment` - type: STRING. - - Valid values are "left", "center", or "right". Controls alignment on the X axis. +* `center` - type: BOOLEAN. + - True to center, false to left-align. #### textlist @@ -350,8 +350,10 @@ Can be created as an extra. * `fontSize` - type: FLOAT. * `scrollSound` - type: PATH. - Sound that is played when the list is scrolled. -* `center` - type: BOOLEAN. - - True to center, false to left-align. +* `alignment` - type: STRING. + - Valid values are "left", "center", or "right". Controls alignment on the X axis. +* `horizontalMargin` - type: FLOAT. + - Horizontal offset for text from the alignment point. If `alignment` is "left", offsets the text to the right. If `alignment` is "right", offsets text to the left. No effect if `alignment` is "center". Given as a percentage of the element's parent's width (same unit as `size`'s X value). #### ninepatch diff --git a/src/ThemeData.cpp b/src/ThemeData.cpp index 5ed8b82f8..6481b1918 100644 --- a/src/ThemeData.cpp +++ b/src/ThemeData.cpp @@ -36,7 +36,8 @@ std::map< std::string, std::map > T ("fontPath", PATH) ("fontSize", FLOAT) ("scrollSound", PATH) - ("alignment", STRING)) + ("alignment", STRING) + ("horizontalMargin", FLOAT)) ("container", boost::assign::map_list_of ("pos", NORMALIZED_PAIR) ("size", NORMALIZED_PAIR)) diff --git a/src/components/TextListComponent.h b/src/components/TextListComponent.h index a9da7893b..fd12f1f40 100644 --- a/src/components/TextListComponent.h +++ b/src/components/TextListComponent.h @@ -96,6 +96,7 @@ private: int mMarqueeTime; Alignment mAlignment; + float mHorizontalMargin; std::vector mRowVector; int mCursor; @@ -121,6 +122,7 @@ TextListComponent::TextListComponent(Window* window) : mMarqueeOffset = 0; mMarqueeTime = -MARQUEE_DELAY; + mHorizontalMargin = 0; mAlignment = ALIGN_CENTER; mFont = Font::get(FONT_SIZE_MEDIUM); @@ -202,20 +204,25 @@ void TextListComponent::render(const Eigen::Affine3f& parentTrans) switch(mAlignment) { case ALIGN_LEFT: - offset[0] = 0; + offset[0] = mHorizontalMargin; break; case ALIGN_CENTER: offset[0] = (mSize.x() - row.textCache->metrics.size.x()) / 2; + if(offset[0] < 0) + offset[0] = 0; break; case ALIGN_RIGHT: offset[0] = (mSize.x() - row.textCache->metrics.size.x()); + offset[0] -= mHorizontalMargin; + if(offset[0] < 0) + offset[0] = 0; + break; } - if(offset[0] < 0) - offset[0] = 0; - offset[0] += (float)(mCursor == i ? -mMarqueeOffset : 0); - + if(mCursor == i) + offset[0] -= mMarqueeOffset; + Eigen::Affine3f drawTrans = trans; drawTrans.translate(offset); Renderer::setMatrix(drawTrans); @@ -312,7 +319,7 @@ void TextListComponent::update(int deltaTime) Eigen::Vector2f textSize = mFont->sizeText(text); //it's long enough to marquee - if(textSize.x() - mMarqueeOffset > getSize().x() - 12) + if(textSize.x() - mMarqueeOffset > getSize().x() - 12 - (mAlignment != ALIGN_CENTER ? mHorizontalMargin : 0)) { mMarqueeTime += deltaTime; while(mMarqueeTime > MARQUEE_SPEED) @@ -453,17 +460,24 @@ void TextListComponent::applyTheme(const std::shared_ptr& theme, c if(properties & SOUND && elem->has("scrollSound")) setSound(Sound::get(elem->get("scrollSound"))); - if(properties & ALIGNMENT && elem->has("alignment")) + if(properties & ALIGNMENT) { - const std::string& str = elem->get("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 << "\"!"; + if(elem->has("alignment")) + { + const std::string& str = elem->get("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 << "\"!"; + } + if(elem->has("horizontalMargin")) + { + mHorizontalMargin = elem->get("horizontalMargin") * (mParent ? mParent->getSize().x() : (float)Renderer::getScreenWidth()); + } } }