Skip to content

Commit

Permalink
libappfw|LabelWidget: Optionally animate label size when ready for dr…
Browse files Browse the repository at this point in the history
…awing
  • Loading branch information
skyjake committed Jan 26, 2014
1 parent c86633b commit 9bd6382
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
22 changes: 22 additions & 0 deletions doomsday/libappfw/include/de/widgets/labelwidget.h
Expand Up @@ -170,6 +170,28 @@ class LIBAPPFW_PUBLIC LabelWidget : public GuiWidget
void setWidthPolicy(ui::SizePolicy policy);
void setHeightPolicy(ui::SizePolicy policy);

enum AppearanceAnimation {
AppearInstantly,
AppearGrowHorizontally,
AppearGrowVertically
};

/**
* Sets the way the label's content affects its size.
*
* @param method Method of appearance:
* - AppearInstantly: The size is unaffected by the content's state.
* This is the default.
* - AppearGrowHorizontally: The widget's width is initially zero, but when the
* content is ready for drawing, the width will animate
* to the appropriate width in the specified time span.
* - AppearGrowVertically: The widget's height is initially zero, but when the
* content is ready for drawing, the height will animate
* to the appropriate height in the specified time span.
* @param span Animation time span for the appearance.
*/
void setAppearanceAnimation(AppearanceAnimation method, TimeDelta const &span = 0.0);

// Events.
void viewResized();
void update();
Expand Down
44 changes: 44 additions & 0 deletions doomsday/libappfw/src/widgets/labelwidget.cpp
Expand Up @@ -49,6 +49,9 @@ public Font::RichFormat::IStyle

ConstantRule *width;
ConstantRule *height;
ScalarRule *appearSize;
LabelWidget::AppearanceAnimation appearType;
TimeDelta appearSpan;

// Style.
Vector4i margin;
Expand Down Expand Up @@ -83,6 +86,9 @@ public Font::RichFormat::IStyle
, imageColor (1, 1, 1, 1)
, textGLColor (1, 1, 1, 1)
, maxTextWidth(0)
, appearSize (new ScalarRule(0))
, appearType (AppearInstantly)
, appearSpan (0.0)
, gapId ("label.gap")
, richStyle (0)
, uMvpMatrix ("uMvpMatrix", GLUniform::Mat4)
Expand All @@ -99,6 +105,7 @@ public Font::RichFormat::IStyle
{
releaseRef(width);
releaseRef(height);
releaseRef(appearSize);
}

void updateStyle()
Expand Down Expand Up @@ -432,6 +439,18 @@ public Font::RichFormat::IStyle
height->set(combined.height() + margin.y + margin.w);
}

void updateAppearanceAnimation()
{
if(appearType != AppearInstantly)
{
float const target = (appearType == AppearGrowHorizontally? width->value() : height->value());
if(!fequal(appearSize->animation().target(), target))
{
appearSize->set(target, appearSpan);
}
}
}

void updateGeometry()
{
// Update the image on the atlas.
Expand Down Expand Up @@ -591,6 +610,7 @@ void LabelWidget::update()
GuiWidget::update();

d->updateSize();
d->updateAppearanceAnimation();
}

void LabelWidget::drawContent()
Expand Down Expand Up @@ -678,6 +698,30 @@ void LabelWidget::setHeightPolicy(SizePolicy policy)
}
}

void LabelWidget::setAppearanceAnimation(AppearanceAnimation method, TimeDelta const &span)
{
d->appearType = method;
d->appearSpan = span;

switch(d->appearType)
{
case AppearInstantly:
if(d->horizPolicy == Expand) rule().setInput(Rule::Width, *d->width);
if(d->vertPolicy == Expand) rule().setInput(Rule::Height, *d->height);
break;

case AppearGrowHorizontally:
if(d->horizPolicy == Expand) rule().setInput(Rule::Width, *d->appearSize);
if(d->vertPolicy == Expand) rule().setInput(Rule::Height, *d->height);
break;

case AppearGrowVertically:
if(d->horizPolicy == Expand) rule().setInput(Rule::Width, *d->width);
if(d->vertPolicy == Expand) rule().setInput(Rule::Height, *d->appearSize);
break;
}
}

LabelWidget *LabelWidget::newWithText(String const &label, GuiWidget *parent)
{
LabelWidget *w = new LabelWidget;
Expand Down

0 comments on commit 9bd6382

Please sign in to comment.