Trait that adds time-based alpha animation to any component.
More...
#include <Malena/Traits/Visual/Fadeable.h>
|
| | 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.
|
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:
});
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.
Adding Fadeable to a component
class MyComp :
public ml::Component<ml::Fadeable, ml::Messenger> { ... };
Primary base class for all user-facing Malena components.
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:
{
_body.setFillColor(c);
}
uint8_t getAlpha() const
Return the current alpha as a uint8_t (0–255).
void draw(const Drawable &drawable, const RenderStates &states=RenderStates::Default)
- See also
- Draggable, Positionable, Tween
Definition at line 56 of file Fadeable.h.
◆ Fadeable()
| ml::Fadeable::Fadeable |
( |
| ) |
|
◆ ~Fadeable()
| virtual ml::Fadeable::~Fadeable |
( |
| ) |
|
|
virtual |
◆ 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
-
| duration | Animation duration in seconds. Defaults to 0.5s. |
| tween | Easing curve. Defaults to LINEAR. |
| onComplete | Optional 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
-
| duration | Animation duration in seconds. Defaults to 0.5s. |
| tween | Easing curve. Defaults to LINEAR. |
| onComplete | Optional 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
-
| targetAlpha | Target alpha (0–255). |
| duration | Animation duration in seconds. Defaults to 0.5s. |
| tween | Easing curve. Defaults to LINEAR. |
| onComplete | Optional 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.
- Parameters
-
| duration | Hold duration in seconds. |
| onComplete | Optional 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
-
| alpha | The new alpha value (0–255). |
The documentation for this class was generated from the following file: