Phosphor
Qt6 / Wayland library suite for window-management tools
Loading...
Searching...
No Matches
Spring.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
20class PHOSPHORANIMATION_EXPORT Spring final : public Curve
21{
22public:
31 static constexpr qreal MaxSettleSeconds = 30.0;
32
33 Spring() = default;
34 Spring(qreal omega, qreal zeta);
35
36 Spring(const Spring&) = default;
37 Spring& operator=(const Spring&) = default;
38 Spring(Spring&&) = default;
39 Spring& operator=(Spring&&) = default;
40
41 QString typeId() const override;
42 qreal evaluate(qreal t) const override;
43 void step(qreal dt, CurveState& state, qreal target) const override;
44 bool isStateful() const override
45 {
46 return true;
47 }
48 qreal settleTime() const override;
49 QString toString() const override;
50 std::unique_ptr<Curve> clone() const override;
52 bool overshoots() const override;
53 bool equals(const Curve& other) const override;
54
58 static std::optional<Spring> tryParse(const QString& str);
60 static Spring fromString(const QString& str);
61
62 static Spring snappy();
63 static Spring smooth();
64 static Spring bouncy();
65
66 qreal omega = 12.0;
67 qreal zeta = 0.8;
68
69 bool operator==(const Spring& other) const;
70 bool operator!=(const Spring& other) const
71 {
72 return !(*this == other);
73 }
74};
75
76} // namespace PhosphorAnimation
bool equals(const Curve &other) const override
Same typeId + same parameters.
std::unique_ptr< Curve > clone() const override
Deep copy with identical parameters.
bool operator==(const Spring &other) const
Spring & operator=(const Spring &)=default
Spring(qreal omega, qreal zeta)
static std::optional< Spring > tryParse(const QString &str)
Parse from "spring:omega,zeta" or "omega,zeta".
QString typeId() const override
Stable identifier for this curve subclass (e.g. "bezier", "spring").
static constexpr qreal MaxSettleSeconds
Hard ceiling on settleTime(), in seconds.
Definition Spring.h:31
qreal zeta
Damping ratio. [0.0, 10.0].
Definition Spring.h:67
static Spring snappy()
Responsive, slight overshoot.
static Spring smooth()
Critically damped, no overshoot.
Spring(Spring &&)=default
Spring & operator=(Spring &&)=default
qreal settleTime() const override
Approximate settle time in seconds.
bool operator!=(const Spring &other) const
Definition Spring.h:70
static Spring fromString(const QString &str)
tryParse, or the default spring when str is malformed.
Spring(const Spring &)=default
static Spring bouncy()
Visible bounce.
qreal evaluate(qreal t) const override
Evaluate at normalized time t in [0,1]. May overshoot by design.
qreal omega
Angular natural frequency (rad/s). [0.1, 200].
Definition Spring.h:66
void step(qreal dt, CurveState &state, qreal target) const override
Advance state by dt real seconds toward target.
bool isStateful() const override
True if this curve requires persistent CurveState across frames.
Definition Spring.h:44
bool overshoots() const override
True for underdamped (zeta < 1). Critical/overdamped never overshoot.
QString toString() const override
Serialize to "typeId:params" or bare "x1,y1,x2,y2" for cubic-bezier.
Definition AnimatedValue.h:32
Mutable state for stateful curve progression (springs carry position+velocity across frames; stateles...
Definition Curve.h:20