Loading...
Searching...
No Matches
ml::Fadeable Class Reference

Trait that adds time-based alpha animation to any component. More...

#include <Fadeable.h>

Inheritance diagram for ml::Fadeable:

Public Member Functions

 Fadeable ()
virtual ~Fadeable ()
void fadeIn (float duration=0.5f, Tween tween=LINEAR, std::function< void()> onComplete=nullptr)
 Animate alpha from 0 to 255 (fully opaque).
void fadeOut (float duration=0.5f, Tween tween=LINEAR, std::function< void()> onComplete=nullptr)
 Animate alpha from its current value to 0 (fully transparent).
void fadeTo (uint8_t targetAlpha, float duration=0.5f, Tween tween=LINEAR, std::function< void()> onComplete=nullptr)
 Animate alpha from its current value to targetAlpha.
uint8_t getAlpha () const
 Return the current alpha as a uint8_t (0–255).
void holdFor (float duration, std::function< void()> onComplete=nullptr)
 Hold at the current alpha for duration seconds, then fire onComplete.
bool isFading () const
 Return true while a fade or hold animation is running.
void setAlpha (uint8_t alpha)
 Set alpha immediately with no animation.

Detailed Description

Trait that adds time-based alpha animation to any component.

Fadeable manages a float alpha value (0.0–255.0) and animates it toward a target over a specified duration using linear or exponential easing. The animation tick is driven automatically by the framework's update event — no manual onUpdate call is needed in the component.

An optional completion callback fires exactly once when the animation reaches its target, enabling chained sequences:

fadeIn(0.3f, ml::LINEAR, [this]{
holdFor(2.f, [this]{ fadeOut(0.5f); });
});
void fadeOut(float duration=0.5f, Tween tween=LINEAR, std::function< void()> onComplete=nullptr)
Animate alpha from its current value to 0 (fully transparent).
void fadeIn(float duration=0.5f, Tween tween=LINEAR, std::function< void()> onComplete=nullptr)
Animate alpha from 0 to 255 (fully opaque).
void holdFor(float duration, std::function< void()> onComplete=nullptr)
Hold at the current alpha for duration seconds, then fire onComplete.
@ LINEAR
Constant-speed interpolation.
Definition Tween.h:41

Adding Fadeable to a component

class MyComp : public ml::Component<ml::Fadeable> { ... };
// Or combined with other traits:
class MyComp : public ml::Component<ml::Fadeable, ml::Messenger> { ... };
Primary base class for all user-facing Malena components.
Definition Component.h:131

Applying the alpha in draw()

Fadeable does not modify any drawable automatically — call getAlpha() in your draw() override and apply the value to whatever you are drawing:

void draw(sf::RenderTarget& t, const sf::RenderStates& s) const override
{
sf::Color c = _body.getFillColor();
c.a = getAlpha();
_body.setFillColor(c);
t.draw(_body, s);
}
uint8_t getAlpha() const
Return the current alpha as a uint8_t (0–255).
std::uint8_t a
void draw(const Drawable &drawable, const RenderStates &states=RenderStates::Default)
See also
Draggable, Positionable, Tween

Definition at line 51 of file Fadeable.h.

Constructor & Destructor Documentation

◆ Fadeable()

ml::Fadeable::Fadeable ( )

◆ ~Fadeable()

virtual ml::Fadeable::~Fadeable ( )
virtual

Member Function Documentation

◆ fadeIn()

void ml::Fadeable::fadeIn ( float duration = 0.5f,
Tween tween = LINEAR,
std::function< void()> onComplete = nullptr )

Animate alpha from 0 to 255 (fully opaque).

Shorthand for fadeTo(255, duration, tween, onComplete).

Parameters
durationAnimation duration in seconds. Defaults to 0.5s.
tweenEasing curve. Defaults to LINEAR.
onCompleteOptional callback fired once when the animation ends.

◆ fadeOut()

void ml::Fadeable::fadeOut ( float duration = 0.5f,
Tween tween = LINEAR,
std::function< void()> onComplete = nullptr )

Animate alpha from its current value to 0 (fully transparent).

Shorthand for fadeTo(0, duration, tween, onComplete).

Parameters
durationAnimation duration in seconds. Defaults to 0.5s.
tweenEasing curve. Defaults to LINEAR.
onCompleteOptional callback fired once when the animation ends.

◆ fadeTo()

void ml::Fadeable::fadeTo ( uint8_t targetAlpha,
float duration = 0.5f,
Tween tween = LINEAR,
std::function< void()> onComplete = nullptr )

Animate alpha from its current value to targetAlpha.

Parameters
targetAlphaTarget alpha (0–255).
durationAnimation duration in seconds. Defaults to 0.5s.
tweenEasing curve. Defaults to LINEAR.
onCompleteOptional callback fired once when the animation ends.

◆ getAlpha()

uint8_t ml::Fadeable::getAlpha ( ) const

Return the current alpha as a uint8_t (0–255).

Call this in your draw() override to apply the animated value to your drawable's color.

Returns
Current alpha value.

◆ holdFor()

void ml::Fadeable::holdFor ( float duration,
std::function< void()> onComplete = nullptr )

Hold at the current alpha for duration seconds, then fire onComplete.

Useful for sequencing: fade in → hold → fade out.

fadeIn(0.3f, LINEAR, [this]{
holdFor(2.f, [this]{ fadeOut(0.5f); });
});
Parameters
durationHold duration in seconds.
onCompleteOptional callback fired once the hold period ends.

◆ isFading()

bool ml::Fadeable::isFading ( ) const

Return true while a fade or hold animation is running.

Returns
true if an animation or hold is currently in progress.

◆ setAlpha()

void ml::Fadeable::setAlpha ( uint8_t alpha)

Set alpha immediately with no animation.

Cancels any animation in progress.

Parameters
alphaThe new alpha value (0–255).

The documentation for this class was generated from the following file: