Added horizontal margins to textlist.

Updated and corrected documentation (center -> alignment for textlist, not text!).
This commit is contained in:
Aloshi 2014-01-21 21:10:49 -06:00
parent fa8e60b7b5
commit ea009315e9
3 changed files with 38 additions and 21 deletions

View file

@ -331,8 +331,8 @@ Can be created as an extra.
- Path to a truetype font (.ttf). - Path to a truetype font (.ttf).
* `fontSize` - type: FLOAT. * `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). - 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. * `center` - type: BOOLEAN.
- Valid values are "left", "center", or "right". Controls alignment on the X axis. - True to center, false to left-align.
#### textlist #### textlist
@ -350,8 +350,10 @@ Can be created as an extra.
* `fontSize` - type: FLOAT. * `fontSize` - type: FLOAT.
* `scrollSound` - type: PATH. * `scrollSound` - type: PATH.
- Sound that is played when the list is scrolled. - Sound that is played when the list is scrolled.
* `center` - type: BOOLEAN. * `alignment` - type: STRING.
- True to center, false to left-align. - 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 #### ninepatch

View file

@ -36,7 +36,8 @@ std::map< std::string, std::map<std::string, ThemeData::ElementPropertyType> > T
("fontPath", PATH) ("fontPath", PATH)
("fontSize", FLOAT) ("fontSize", FLOAT)
("scrollSound", PATH) ("scrollSound", PATH)
("alignment", STRING)) ("alignment", STRING)
("horizontalMargin", FLOAT))
("container", boost::assign::map_list_of ("container", boost::assign::map_list_of
("pos", NORMALIZED_PAIR) ("pos", NORMALIZED_PAIR)
("size", NORMALIZED_PAIR)) ("size", NORMALIZED_PAIR))

View file

@ -96,6 +96,7 @@ private:
int mMarqueeTime; int mMarqueeTime;
Alignment mAlignment; Alignment mAlignment;
float mHorizontalMargin;
std::vector<ListRow> mRowVector; std::vector<ListRow> mRowVector;
int mCursor; int mCursor;
@ -121,6 +122,7 @@ TextListComponent<T>::TextListComponent(Window* window) :
mMarqueeOffset = 0; mMarqueeOffset = 0;
mMarqueeTime = -MARQUEE_DELAY; mMarqueeTime = -MARQUEE_DELAY;
mHorizontalMargin = 0;
mAlignment = ALIGN_CENTER; mAlignment = ALIGN_CENTER;
mFont = Font::get(FONT_SIZE_MEDIUM); mFont = Font::get(FONT_SIZE_MEDIUM);
@ -202,20 +204,25 @@ void TextListComponent<T>::render(const Eigen::Affine3f& parentTrans)
switch(mAlignment) switch(mAlignment)
{ {
case ALIGN_LEFT: case ALIGN_LEFT:
offset[0] = 0; offset[0] = mHorizontalMargin;
break; break;
case ALIGN_CENTER: case ALIGN_CENTER:
offset[0] = (mSize.x() - row.textCache->metrics.size.x()) / 2; offset[0] = (mSize.x() - row.textCache->metrics.size.x()) / 2;
if(offset[0] < 0)
offset[0] = 0;
break; break;
case ALIGN_RIGHT: case ALIGN_RIGHT:
offset[0] = (mSize.x() - row.textCache->metrics.size.x()); offset[0] = (mSize.x() - row.textCache->metrics.size.x());
offset[0] -= mHorizontalMargin;
if(offset[0] < 0)
offset[0] = 0;
break; break;
} }
if(offset[0] < 0) if(mCursor == i)
offset[0] = 0; offset[0] -= mMarqueeOffset;
offset[0] += (float)(mCursor == i ? -mMarqueeOffset : 0);
Eigen::Affine3f drawTrans = trans; Eigen::Affine3f drawTrans = trans;
drawTrans.translate(offset); drawTrans.translate(offset);
Renderer::setMatrix(drawTrans); Renderer::setMatrix(drawTrans);
@ -312,7 +319,7 @@ void TextListComponent<T>::update(int deltaTime)
Eigen::Vector2f textSize = mFont->sizeText(text); Eigen::Vector2f textSize = mFont->sizeText(text);
//it's long enough to marquee //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; mMarqueeTime += deltaTime;
while(mMarqueeTime > MARQUEE_SPEED) while(mMarqueeTime > MARQUEE_SPEED)
@ -453,17 +460,24 @@ void TextListComponent<T>::applyTheme(const std::shared_ptr<ThemeData>& theme, c
if(properties & SOUND && elem->has("scrollSound")) if(properties & SOUND && elem->has("scrollSound"))
setSound(Sound::get(elem->get<std::string>("scrollSound"))); setSound(Sound::get(elem->get<std::string>("scrollSound")));
if(properties & ALIGNMENT && elem->has("alignment")) if(properties & ALIGNMENT)
{ {
const std::string& str = elem->get<std::string>("alignment"); if(elem->has("alignment"))
if(str == "left") {
setAlignment(ALIGN_LEFT); const std::string& str = elem->get<std::string>("alignment");
else if(str == "center") if(str == "left")
setAlignment(ALIGN_CENTER); setAlignment(ALIGN_LEFT);
else if(str == "right") else if(str == "center")
setAlignment(ALIGN_RIGHT); setAlignment(ALIGN_CENTER);
else else if(str == "right")
LOG(LogError) << "Unknown TextListComponent alignment \"" << str << "\"!"; setAlignment(ALIGN_RIGHT);
else
LOG(LogError) << "Unknown TextListComponent alignment \"" << str << "\"!";
}
if(elem->has("horizontalMargin"))
{
mHorizontalMargin = elem->get<float>("horizontalMargin") * (mParent ? mParent->getSize().x() : (float)Renderer::getScreenWidth());
}
} }
} }