|
| 1 | +#include "animation_helper.h" |
| 2 | +#include <cmath> |
| 3 | +#include <vector> |
| 4 | +#include <sstream> |
| 5 | + |
| 6 | + |
| 7 | +// references for cubic bezier: |
| 8 | +// https://matthewlein.com/tools/ceaser |
| 9 | +// https://cubic-bezier.com/ |
| 10 | +double Animation_Helper::CubicBezier(float t, const double& p0, const double& p1, const double& p2, const double& p3) { |
| 11 | + |
| 12 | + float u = 1 - t; |
| 13 | + float tt = t * t; |
| 14 | + float uu = u * u; |
| 15 | + float uuu = uu * u; |
| 16 | + float ttt = tt * t; |
| 17 | + |
| 18 | + //Point2d p = {0,0}; |
| 19 | + //p.x = uuu * 0 + 3 * uu * t * p0 + 3 * u * tt * p2 + ttt * 1; |
| 20 | + return uuu * 0 + 3 * uu * t * p1 + 3 * u * tt * p3 + ttt * 1; |
| 21 | + |
| 22 | + //return p.y; |
| 23 | +} |
| 24 | + |
| 25 | +double Animation_Helper::GetEasedTime(const std::string& easing_type, double t, double b, double c, double d) { |
| 26 | + if (easing_type == "linear") return Animation_Helper::CubicBezier(t, 0.250, 0.250, 0.750, 0.750); |
| 27 | + |
| 28 | + else if (easing_type == "ease") return Animation_Helper::CubicBezier(t, 0.250, 0.100, 0.250, 1.000); |
| 29 | + else if (easing_type == "easeIn") return Animation_Helper::CubicBezier(t, 0.420, 0.000, 1.000, 1.000); |
| 30 | + else if (easing_type == "easeOut") return Animation_Helper::CubicBezier(t, 0.000, 0.000, 0.580, 1.000); |
| 31 | + else if (easing_type == "easeInOut") return Animation_Helper::CubicBezier(t, 0.420, 0.000, 0.580, 1.000); |
| 32 | + |
| 33 | + else if (easing_type == "quadIn") return Animation_Helper::CubicBezier(t, 0.550, 0.085, 0.680, 0.530); |
| 34 | + else if (easing_type == "quadOut") return Animation_Helper::CubicBezier(t, 0.250, 0.460, 0.450, 0.940); |
| 35 | + else if (easing_type == "quadInOut") return Animation_Helper::CubicBezier(t, 0.455, 0.030, 0.515, 0.955); |
| 36 | + |
| 37 | + else if (easing_type == "cubicIn") return Animation_Helper::CubicBezier(t, 0.550, 0.055, 0.675, 0.190); |
| 38 | + else if (easing_type == "cubicOut") return Animation_Helper::CubicBezier(t, 0.215, 0.610, 0.355, 1.000); |
| 39 | + else if (easing_type == "cubicInOut") return Animation_Helper::CubicBezier(t, 0.645, 0.045, 0.355, 1.000); |
| 40 | + |
| 41 | + else if (easing_type == "quartIn") return Animation_Helper::CubicBezier(t, 0.895, 0.030, 0.685, 0.220); |
| 42 | + else if (easing_type == "quartOut") return Animation_Helper::CubicBezier(t, 0.165, 0.840, 0.440, 1.000); |
| 43 | + else if (easing_type == "quartInOut") return Animation_Helper::CubicBezier(t, 0.770, 0.000, 0.175, 1.000); |
| 44 | + |
| 45 | + else if (easing_type == "quintIn") return Animation_Helper::CubicBezier(t, 0.755, 0.050, 0.855, 0.060); |
| 46 | + else if (easing_type == "quintOut") return Animation_Helper::CubicBezier(t, 0.230, 1.000, 0.320, 1.000); |
| 47 | + else if (easing_type == "quintInOut") return Animation_Helper::CubicBezier(t, 0.860, 0.000, 0.070, 1.000); |
| 48 | + |
| 49 | + else if (easing_type == "sineIn") return Animation_Helper::CubicBezier(t, 0.470, 0.000, 0.745, 0.715); |
| 50 | + else if (easing_type == "sineOut") return Animation_Helper::CubicBezier(t, 0.390, 0.575, 0.565, 1.000); |
| 51 | + else if (easing_type == "sineInOut") return Animation_Helper::CubicBezier(t, 0.445, 0.050, 0.550, 0.950); |
| 52 | + |
| 53 | + else if (easing_type == "ExpoIn") return Animation_Helper::CubicBezier(t, 0.950, 0.050, 0.795, 0.035); |
| 54 | + else if (easing_type == "expoOut") return Animation_Helper::CubicBezier(t, 0.190, 1.000, 0.220, 1.000); |
| 55 | + else if (easing_type == "expoInOut") return Animation_Helper::CubicBezier(t, 1.000, 0.000, 0.000, 1.000); |
| 56 | + |
| 57 | + else if (easing_type == "circIn") return Animation_Helper::CubicBezier(t, 0.600, 0.040, 0.980, 0.335); |
| 58 | + else if (easing_type == "circOut") return Animation_Helper::CubicBezier(t, 0.075, 0.820, 0.165, 1.000); |
| 59 | + else if (easing_type == "circInOut") return Animation_Helper::CubicBezier(t, 0.785, 0.135, 0.150, 0.860); |
| 60 | + |
| 61 | + else if (easing_type == "backIn") return Animation_Helper::CubicBezier(t, 0.600, -0.280, 0.735, 0.045); |
| 62 | + else if (easing_type == "backOut") return Animation_Helper::CubicBezier(t, 0.175, 0.885, 0.320, 1.275); |
| 63 | + else if (easing_type == "backInOut") return Animation_Helper::CubicBezier(t, 0.680, -0.550, 0.265, 1.550); |
| 64 | + |
| 65 | + else if (easing_type == "elasticIn") { |
| 66 | + if (t == 0) { |
| 67 | + return b; |
| 68 | + } |
| 69 | + if ((t /= d) == 1) { |
| 70 | + return b + c; |
| 71 | + } |
| 72 | + |
| 73 | + double p = d * 0.3; |
| 74 | + double a = c; |
| 75 | + double s = p / 4; |
| 76 | + |
| 77 | + double post_increment_fix = a * pow(2, 10 * (t -= 1)); |
| 78 | + return -(post_increment_fix * sin((t * d - s) * (2 * M_PI) / p)) + b; |
| 79 | + } |
| 80 | + else if (easing_type == "elasticOut") { |
| 81 | + if (t == 0) { |
| 82 | + return b; |
| 83 | + } |
| 84 | + if ((t /= d) == 1) { |
| 85 | + return b + c; |
| 86 | + } |
| 87 | + |
| 88 | + double p = d * 0.3; |
| 89 | + double a = c; |
| 90 | + double s = p / 4; |
| 91 | + |
| 92 | + return (a * pow(2, -10 * t) * sin((t * d - s) * (2 * M_PI) / p) + c + b); |
| 93 | + } |
| 94 | + else if (easing_type == "elasticInOut") { |
| 95 | + if (t == 0) { |
| 96 | + return b; |
| 97 | + } |
| 98 | + if ((t /= d / 2) == 2) { |
| 99 | + return b + c; |
| 100 | + } |
| 101 | + |
| 102 | + double p = d * (0.3 * 1.5); |
| 103 | + double a = c; |
| 104 | + double s = p / 4; |
| 105 | + |
| 106 | + if (t < 1) { |
| 107 | + double post_increment_fix = a * pow(2, 10 * (t -= 1)); |
| 108 | + return -0.5 * (post_increment_fix * sin((t * d - s) * (2 * M_PI) / p)) + b; |
| 109 | + } |
| 110 | + |
| 111 | + double post_increment_fix = a * pow(2, -10 * (t -= 1)); |
| 112 | + return post_increment_fix * sin((t * d - s) * (2 * M_PI) / p) * 0.5 + c + b; |
| 113 | + } |
| 114 | + |
| 115 | + else if (easing_type == "bounceIn") { |
| 116 | + return c - Animation_Helper::GetEasedTime("bounceOut", d - t, 0, c, d) + b; |
| 117 | + } |
| 118 | + else if (easing_type == "bounceOut") { |
| 119 | + if ((t /= d) < (1 / 2.75)) { |
| 120 | + return c * (7.5625 * t * t) + b; |
| 121 | + } |
| 122 | + else if (t < (2 / 2.75)) { |
| 123 | + t -= (1.5 / 2.75); |
| 124 | + return c * (7.5625 * t * t + 0.75) + b; |
| 125 | + } |
| 126 | + else if (t < (2.5 / 2.75)) { |
| 127 | + t -= (2.25 / 2.75); |
| 128 | + return c * (7.5625 * t * t + 0.9375) + b; |
| 129 | + } |
| 130 | + else { |
| 131 | + t -= (2.625 / 2.75); |
| 132 | + return c * (7.5625 * t * t + 0.984375) + b; |
| 133 | + } |
| 134 | + } |
| 135 | + else if (easing_type == "bounceInOut") { |
| 136 | + if (t < d / 2) { |
| 137 | + return Animation_Helper::GetEasedTime("bounceIn", t * 2, 0, c, d) * 0.5 + b; |
| 138 | + } |
| 139 | + else { |
| 140 | + return Animation_Helper::GetEasedTime("bounceOut", t * 2 - d, 0, c, d) * 0.5 + c * 0.5 + b; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + if (easing_type.substr(0, 6) == "bezier") { |
| 145 | + std::vector<double> bezier_params; |
| 146 | + |
| 147 | + size_t start_pos = easing_type.find("(") + 1; |
| 148 | + size_t end_pos = easing_type.find(")"); |
| 149 | + std::string value_string = easing_type.substr(start_pos, end_pos - start_pos); |
| 150 | + |
| 151 | + std::istringstream iss(value_string); |
| 152 | + double value; |
| 153 | + |
| 154 | + while (iss >> value) bezier_params.push_back(value), iss.ignore(); |
| 155 | + |
| 156 | + if (bezier_params.size() == 4) |
| 157 | + return Animation_Helper::CubicBezier(t, bezier_params[0], bezier_params[1], bezier_params[2], bezier_params[3]); |
| 158 | + } |
| 159 | + |
| 160 | + return c * t / d + b; // Default to linear easing if the easing type is not recognized |
| 161 | +} |
0 commit comments