Loading...
Searching...
No Matches
Animate.h
Go to the documentation of this file.
1// Copyright (c) 2025 Dave R. Smith.
2// Malena Framework — Licensed under PolyForm Noncommercial 1.0.0; commercial use requires a paid license. See LICENSE.
3
4//
5// Animate.h — per-object animation facade.
6//
7// Obtained via Positionable::animate(). Drives the owner's position (and any
8// app-supplied property) through the shared AnimationManager, using delta-time
9// Tweeners and Easing curves. Replaces the old moveTo/moveDistance code.
10//
11
12#ifndef MALENA_ANIMATE_H
13#define MALENA_ANIMATE_H
14
15#pragma once
16
21#include <functional>
22#include <memory>
23
24namespace ml
25{
26 class Positionable;
27
44 {
45 public:
46 explicit Animate(Positionable* owner) : _owner(owner) {}
48
49 Animate(const Animate&) = delete;
50 Animate& operator=(const Animate&) = delete;
51
53 Animate& move(sf::Vector2f to, float seconds,
55 std::function<void()> onComplete = {});
56
58 Animate& moveBy(sf::Vector2f delta, float seconds,
60 std::function<void()> onComplete = {});
61
69 template<class T>
70 Animate& value(T from, T to, float seconds, Easing::Fn ease,
71 std::function<void(const T&)> apply,
72 std::function<void()> onComplete = {})
73 {
74 auto tw = std::make_shared<Tweener<T>>(from, to, seconds, ease);
75 tw->onUpdate(std::move(apply));
76 if (onComplete) tw->onComplete(std::move(onComplete));
77 AnimationManager::add(_owner, [tw](float dt) { tw->update(dt); return tw->done(); });
78 return *this;
79 }
80
82 void cancel();
83
85 [[nodiscard]] bool active() const;
86
87 private:
88 Positionable* _owner;
89 };
90
91} // namespace ml
92
93#endif // MALENA_ANIMATE_H
Animate & moveBy(sf::Vector2f delta, float seconds, Easing::Fn ease=Easing::EaseOutCubic, std::function< void()> onComplete={})
Animate the owner's position by a relative delta over seconds.
Animate & value(T from, T to, float seconds, Easing::Fn ease, std::function< void(const T &)> apply, std::function< void()> onComplete={})
Animate an arbitrary value and deliver it through apply.
Definition Animate.h:70
Animate & move(sf::Vector2f to, float seconds, Easing::Fn ease=Easing::EaseOutCubic, std::function< void()> onComplete={})
Animate the owner's position to to over seconds.
Animate & operator=(const Animate &)=delete
Animate(const Animate &)=delete
Animate(Positionable *owner)
Definition Animate.h:46
bool active() const
True while any animation on this object is running.
void cancel()
Cancel every animation on this object (no completion callbacks fire).
static void add(const void *owner, Step step)
Register an animation step owned by owner (used for cancellation).
Trait that provides position, bounds, and animated movement.
#define MALENA_API
Definition Animate.h:25
Vector2< float > Vector2f
static float EaseOutCubic(float t)
Definition Easing.h:50
float(*)(float) Fn
Definition Easing.h:34