QmlMaterial 0.1.0
Loading...
Searching...
No Matches
motion_spec.hpp
1#pragma once
2
3#include "qml_material/token/motion.hpp"
4#include <QEasingCurve>
5#include <QList>
6#include <optional>
7
8namespace qml_material
9{
10struct QML_MATERIAL_API AnimationSpec {
11 Q_GADGET
12 QML_VALUE_TYPE(animationSpec)
13 QML_STRUCTURED_VALUE
14 Q_PROPERTY(Kind kind MEMBER kind FINAL)
15 Q_PROPERTY(int duration MEMBER duration FINAL)
16 Q_PROPERTY(int delay MEMBER delay FINAL)
17 Q_PROPERTY(QEasingCurve easing MEMBER easing FINAL)
18 Q_PROPERTY(qreal stiffness MEMBER stiffness FINAL)
19 Q_PROPERTY(qreal dampingRatio MEMBER dampingRatio FINAL)
20 Q_PROPERTY(qreal threshold MEMBER threshold FINAL)
21 Q_PROPERTY(qreal velocityThreshold MEMBER velocityThreshold FINAL)
22public:
23 enum Kind
24 {
25 Tween,
26 Spring,
27 Snap
28 };
29 Q_ENUM(Kind)
30 Kind kind = Spring;
31 int duration = 300, delay = 0;
32 QEasingCurve easing { QEasingCurve::Linear };
33 qreal stiffness = 700, dampingRatio = .9, threshold = .01, velocityThreshold = .1;
34 bool valid() const;
35 bool operator==(const AnimationSpec&) const = default;
36};
37struct QML_MATERIAL_API DecaySpec {
38 Q_GADGET
39 QML_VALUE_TYPE(decaySpec)
40 QML_STRUCTURED_VALUE
41 Q_PROPERTY(qreal friction MEMBER friction FINAL)
42 Q_PROPERTY(qreal velocityThreshold MEMBER velocityThreshold FINAL)
43public:
44 qreal friction = 5, velocityThreshold = .1;
45 bool valid() const;
46 bool operator==(const DecaySpec&) const = default;
47};
48class QML_MATERIAL_API Motion : public QObject {
49 Q_OBJECT
50 QML_ELEMENT
51 QML_SINGLETON
52public:
53 using QObject::QObject;
54 Q_INVOKABLE static AnimationSpec tween(int duration, QEasingCurve easing = QEasingCurve(),
55 int delay = 0);
56 Q_INVOKABLE static AnimationSpec spring(qreal stiffness = 700, qreal dampingRatio = .9);
57 Q_INVOKABLE static AnimationSpec snap();
58 Q_INVOKABLE static DecaySpec decay(qreal friction = 5, qreal velocityThreshold = .1);
59};
60namespace anim
61{
62using Vector = QList<qreal>;
63enum class SpringCompletion
64{
65 Displacement,
66 DisplacementAndVelocity
67};
68struct Sample {
69 Vector value, velocity;
70};
71struct SpringSample {
72 qreal value = 0, velocity = 0;
73};
74QML_MATERIAL_API SpringSample sampleSpring(token::SpringSpec, qreal, qreal, qreal, qreal);
75QML_MATERIAL_API int springDuration(token::SpringSpec, qreal, qreal, qreal, qreal);
76QML_MATERIAL_API bool finite(const Vector&);
77class QML_MATERIAL_API PreparedMotion {
78public:
79 static std::optional<PreparedMotion>
80 prepare(const Vector& from, const Vector& to, const Vector& velocity, AnimationSpec,
81 const Vector& thresholds = {},
82 SpringCompletion = SpringCompletion::DisplacementAndVelocity);
83 static std::optional<PreparedMotion> decay(const Vector& from, const Vector& velocity,
84 DecaySpec);
85 Sample sample(qreal milliseconds) const;
86 const Vector& target() const { return m_to; }
87 int duration() const { return m_duration; }
88
89private:
90 Vector m_from, m_to, m_velocity;
91 QList<int> m_durations;
92 AnimationSpec m_spec;
93 std::optional<DecaySpec> m_decay;
94 int m_duration = 0;
95};
96} // namespace anim
97} // namespace qml_material