Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
Easing.h
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 fuddlesworth
2// SPDX-License-Identifier: LGPL-2.1-or-later
3
4#pragma once
5
7#include <PhosphorAnimation/phosphoranimation_export.h>
8
9#include <QString>
10#include <QtGlobal>
11
12#include <optional>
13
14namespace PhosphorAnimation {
15
19class PHOSPHORANIMATION_EXPORT Easing final : public Curve
20{
21public:
22 enum class Type {
23 CubicBezier,
24 ElasticIn,
25 ElasticOut,
26 ElasticInOut,
27 BounceIn,
28 BounceOut,
29 BounceInOut,
30 };
31
32 Easing() = default;
33
34 Easing(const Easing&) = default;
35 Easing& operator=(const Easing&) = default;
36 Easing(Easing&&) = default;
37 Easing& operator=(Easing&&) = default;
38
39 QString typeId() const override;
40 qreal evaluate(qreal x) const override;
41 QString toString() const override;
42 std::unique_ptr<Curve> clone() const override;
43 bool overshoots() const override;
44 bool equals(const Curve& other) const override;
45
49 static std::optional<Easing> tryParse(const QString& str);
51 static Easing fromString(const QString& str);
52
59 static constexpr qreal MaxElasticPeak = 2.0;
60
61 static constexpr qreal MinElasticPeriod = 0.1;
62 static constexpr qreal MaxElasticPeriod = 1.0;
63
67 static constexpr int ElasticSolveIterations = 24;
68
79 static qreal minElasticPeak(qreal period);
80
92 static qreal clampAmplitude(Type t, qreal amp, qreal period);
93
95
97 qreal x1 = 0.33;
98 qreal y1 = 1.0;
99 qreal x2 = 0.68;
100 qreal y2 = 1.0;
101
115 qreal amplitude = 1.0;
118 qreal period = 0.3;
120 int bounces = 3;
121
122 bool operator==(const Easing& other) const;
123 bool operator!=(const Easing& other) const
124 {
125 return !(*this == other);
126 }
127
128private:
132 static qreal solveElasticGain(qreal peak, qreal period);
133
134 static qreal evaluateElasticOut(qreal t, qreal peak, qreal per);
135 static qreal evaluateBounceOut(qreal t, qreal amp, int n);
136};
137
138} // namespace PhosphorAnimation
Easing(Easing &&)=default
qreal y1
Definition Easing.h:98
bool equals(const Curve &other) const override
Same typeId + same parameters.
static constexpr qreal MinElasticPeriod
Definition Easing.h:61
static constexpr qreal MaxElasticPeak
Highest peak an elastic curve may reach.
Definition Easing.h:59
static std::optional< Easing > tryParse(const QString &str)
Parse from config string.
static Easing fromString(const QString &str)
tryParse, or the default OutCubic when str is malformed.
qreal evaluate(qreal x) const override
Evaluate at normalized time t in [0,1]. May overshoot by design.
static constexpr qreal MaxElasticPeriod
Definition Easing.h:62
bool overshoots() const override
True if this curve may evaluate outside [0,1] during progression.
std::unique_ptr< Curve > clone() const override
Deep copy with identical parameters.
static qreal minElasticPeak(qreal period)
The gentlest bounce elastic can make at period — the low end of the overshoot range,...
int bounces
Bounce count. [1, 8].
Definition Easing.h:120
qreal x1
Bezier control points (P1, P2). x clamped [0,1]; y clamped [-1,2].
Definition Easing.h:97
qreal amplitude
Elastic: the PEAK VALUE the curve reaches, so 1.5 means it travels 50% past its target before settlin...
Definition Easing.h:115
QString toString() const override
Serialize to "typeId:params" or bare "x1,y1,x2,y2" for cubic-bezier.
QString typeId() const override
Stable identifier for this curve subclass (e.g. "bezier", "spring").
bool operator==(const Easing &other) const
qreal period
Elastic oscillation period: how FAST it wiggles.
Definition Easing.h:118
qreal y2
Definition Easing.h:100
Type
Definition Easing.h:22
@ CubicBezier
Definition Easing.h:23
Type type
Definition Easing.h:94
static constexpr int ElasticSolveIterations
Bisection steps used to invert the peak into an internal gain.
Definition Easing.h:67
bool operator!=(const Easing &other) const
Definition Easing.h:123
Easing & operator=(Easing &&)=default
Easing & operator=(const Easing &)=default
Easing(const Easing &)=default
qreal x2
Definition Easing.h:99
static qreal clampAmplitude(Type t, qreal amp, qreal period)
Clamp amp to the range the given curve type actually honours.
Definition AnimatedValue.h:32