TachyonUI 0.1.0
High-performance UI for building tools.
Loading...
Searching...
No Matches
gui_easing.h
Go to the documentation of this file.
1#pragma once
2
3namespace tachyon
4{
7
11 {
12 float x1 = 0.0f, y1 = 0.0f, x2 = 1.0f, y2 = 1.0f;
13
15 float operator()(float t) const
16 {
17 if (t <= 0.0f) return 0.0f;
18 if (t >= 1.0f) return 1.0f;
19 return Y(ParamForX(t));
20 }
21
24 float TimeAt(float value) const
25 {
26 if (value <= 0.0f) return 0.0f;
27 if (value >= 1.0f) return 1.0f;
28 float lo = 0.0f, hi = 1.0f;
29 for (int i = 0; i < 32; ++i)
30 {
31 const float mid = 0.5f * (lo + hi);
32 (Y(mid) < value ? lo : hi) = mid;
33 }
34 return X(0.5f * (lo + hi));
35 }
36
37 bool operator==(const CubicBezier&) const = default;
38
39 private:
40 // The curve runs from (0, 0) to (1, 1) through the two control points.
41 static float Bezier(float a, float b, float s)
42 {
43 const float r = 1.0f - s;
44 return 3.0f * r * r * s * a + 3.0f * r * s * s * b + s * s * s;
45 }
46 float X(float s) const { return Bezier(x1, x2, s); }
47 float Y(float s) const { return Bezier(y1, y2, s); }
48
49 // x grows with s (x1 and x2 are in [0, 1]), so halving finds it.
50 float ParamForX(float x) const
51 {
52 float lo = 0.0f, hi = 1.0f;
53 for (int i = 0; i < 32; ++i)
54 {
55 const float mid = 0.5f * (lo + hi);
56 (X(mid) < x ? lo : hi) = mid;
57 }
58 return 0.5f * (lo + hi);
59 }
60 };
61
62 namespace Easing
63 {
64 inline constexpr CubicBezier Linear { 0.0f, 0.0f, 1.0f, 1.0f };
65 inline constexpr CubicBezier EaseOut { 0.2f, 0.0f, 0.0f, 1.0f };
66 inline constexpr CubicBezier EaseInOut { 0.4f, 0.0f, 0.2f, 1.0f };
67 }
68
70} // namespace tachyon
Definition gui_easing.h:63
constexpr CubicBezier Linear
Definition gui_easing.h:64
constexpr CubicBezier EaseInOut
slow at both ends
Definition gui_easing.h:66
constexpr CubicBezier EaseOut
fast start, long settle
Definition gui_easing.h:65
Definition gui_logging.h:24
A CSS cubic-bezier(x1, y1, x2, y2) timing curve: maps the share of a transition's time that has passe...
Definition gui_easing.h:11
float operator()(float t) const
How far the value has gone at time t.
Definition gui_easing.h:15
float y1
Definition gui_easing.h:12
float x1
Definition gui_easing.h:12
float x2
Definition gui_easing.h:12
bool operator==(const CubicBezier &) const =default
float TimeAt(float value) const
The time at which the value has gone value of the way: the inverse of operator(), for curves whose y ...
Definition gui_easing.h:24
float y2
Definition gui_easing.h:12