|
| 1 | +package me.stringfromjava.funkin.tween.settings; |
| 2 | + |
| 3 | +import me.stringfromjava.funkin.tween.FunkinTween; |
| 4 | + |
| 5 | +/** |
| 6 | + * Class where all easer functions are stored, mostly used for tweening. |
| 7 | + */ |
| 8 | +public final class FunkinTweenEase { |
| 9 | + |
| 10 | + // Easing constants for specific functions. |
| 11 | + private static final float PI2 = (float) Math.PI / 2; |
| 12 | + private static final float EL = (float) ((float) 2 * Math.PI / .45); |
| 13 | + private static final float B1 = (float) ((float) 1 / 2.75); |
| 14 | + private static final float B2 = (float) ((float) 2 / 2.75); |
| 15 | + private static final float B3 = (float) ((float) 1.5 / 2.75); |
| 16 | + private static final float B4 = (float) ((float) 2.5 / 2.75); |
| 17 | + private static final float B5 = (float) ((float) 2.25 / 2.75); |
| 18 | + private static final float B6 = (float) ((float) 2.625 / 2.75); |
| 19 | + private static final float ELASTIC_AMPLITUDE = 1; |
| 20 | + private static final float ELASTIC_PERIOD = 0.4f; |
| 21 | + |
| 22 | + private FunkinTweenEase() { |
| 23 | + } |
| 24 | + |
| 25 | + public static float linear(float t) { |
| 26 | + return t; |
| 27 | + } |
| 28 | + |
| 29 | + public static float quadIn(float t) { |
| 30 | + return t * t; |
| 31 | + } |
| 32 | + |
| 33 | + public static float quadOut(float t) { |
| 34 | + return -t * (t - 2); |
| 35 | + } |
| 36 | + |
| 37 | + public static float quadInOut(float t) { |
| 38 | + return t <= .5 ? t * t * 2 : 1 - (--t) * t * 2; |
| 39 | + } |
| 40 | + |
| 41 | + public static float cubeIn(float t) { |
| 42 | + return t * t * t; |
| 43 | + } |
| 44 | + |
| 45 | + public static float cubeOut(float t) { |
| 46 | + return 1 + (--t) * t * t; |
| 47 | + } |
| 48 | + |
| 49 | + public static float cubeInOut(float t) { |
| 50 | + return t <= .5 ? t * t * t * 4 : 1 + (--t) * t * t * 4; |
| 51 | + } |
| 52 | + |
| 53 | + public static float quartIn(float t) { |
| 54 | + return t * t * t * t; |
| 55 | + } |
| 56 | + |
| 57 | + public static float quartOut(float t) { |
| 58 | + return 1 - (t -= 1) * t * t * t; |
| 59 | + } |
| 60 | + |
| 61 | + public static float quartInOut(float t) { |
| 62 | + return t <= .5 ? t * t * t * t * 8 : (float) ((1 - (t = t * 2 - 2) * t * t * t) / 2 + .5); |
| 63 | + } |
| 64 | + |
| 65 | + public static float quintIn(float t) { |
| 66 | + return t * t * t * t * t; |
| 67 | + } |
| 68 | + |
| 69 | + public static float quintOut(float t) { |
| 70 | + return (t = t - 1) * t * t * t * t + 1; |
| 71 | + } |
| 72 | + |
| 73 | + public static float quintInOut(float t) { |
| 74 | + return ((t *= 2) < 1) ? (t * t * t * t * t) / 2 : ((t -= 2) * t * t * t * t + 2) / 2; |
| 75 | + } |
| 76 | + |
| 77 | + public static float smoothStepIn(float t) { |
| 78 | + return 2 * smoothStepInOut(t / 2); |
| 79 | + } |
| 80 | + |
| 81 | + public static float smoothStepOut(float t) { |
| 82 | + return 2 * smoothStepInOut((float) (t / 2 + 0.5)) - 1; |
| 83 | + } |
| 84 | + |
| 85 | + public static float smoothStepInOut(float t) { |
| 86 | + return t * t * (t * -2 + 3); |
| 87 | + } |
| 88 | + |
| 89 | + public static float smootherStepIn(float t) { |
| 90 | + return 2 * smootherStepInOut(t / 2); |
| 91 | + } |
| 92 | + |
| 93 | + public static float smootherStepOut(float t) { |
| 94 | + return 2 * smootherStepInOut((float) (t / 2 + 0.5)) - 1; |
| 95 | + } |
| 96 | + |
| 97 | + public static float smootherStepInOut(float t) { |
| 98 | + return t * t * t * (t * (t * 6 - 15) + 10); |
| 99 | + } |
| 100 | + |
| 101 | + public static float sineIn(float t) { |
| 102 | + return (float) (-Math.cos(PI2 * t) + 1); |
| 103 | + } |
| 104 | + |
| 105 | + public static float sineOut(float t) { |
| 106 | + return (float) Math.sin(PI2 * t); |
| 107 | + } |
| 108 | + |
| 109 | + public static float sineInOut(float t) { |
| 110 | + return (float) (-Math.cos(Math.PI * t) / 2 + .5); |
| 111 | + } |
| 112 | + |
| 113 | + public static float bounceIn(float t) { |
| 114 | + return 1 - bounceOut(1 - t); |
| 115 | + } |
| 116 | + |
| 117 | + public static float bounceOut(float t) { |
| 118 | + if (t < B1) |
| 119 | + return (float) (7.5625 * t * t); |
| 120 | + if (t < B2) |
| 121 | + return (float) (7.5625 * (t - B3) * (t - B3) + .75); |
| 122 | + if (t < B4) |
| 123 | + return (float) (7.5625 * (t - B5) * (t - B5) + .9375); |
| 124 | + return (float) (7.5625 * (t - B6) * (t - B6) + .984375); |
| 125 | + } |
| 126 | + |
| 127 | + public static float bounceInOut(float t) { |
| 128 | + return t < 0.5 |
| 129 | + ? (1 - bounceOut(1 - 2 * t)) / 2 |
| 130 | + : (1 + bounceOut(2 * t - 1)) / 2; |
| 131 | + } |
| 132 | + |
| 133 | + public static float circIn(float t) { |
| 134 | + return (float) -(Math.sqrt(1 - t * t) - 1); |
| 135 | + } |
| 136 | + |
| 137 | + public static float circOut(float t) { |
| 138 | + return (float) Math.sqrt(1 - (t - 1) * (t - 1)); |
| 139 | + } |
| 140 | + |
| 141 | + public static float circInOut(float t) { |
| 142 | + return (float) (t <= .5 ? (Math.sqrt(1 - t * t * 4) - 1) / -2 : (Math.sqrt(1 - (t * 2 - 2) * (t * 2 - 2)) + 1) / 2); |
| 143 | + } |
| 144 | + |
| 145 | + public static float expoIn(float t) { |
| 146 | + return (float) Math.pow(2, 10 * (t - 1)); |
| 147 | + } |
| 148 | + |
| 149 | + public static float expoOut(float t) { |
| 150 | + return (float) (-Math.pow(2, -10 * t) + 1); |
| 151 | + } |
| 152 | + |
| 153 | + public static float expoInOut(float t) { |
| 154 | + return (float) (t < .5 ? Math.pow(2, 10 * (t * 2 - 1)) / 2 : (-Math.pow(2, -10 * (t * 2 - 1)) + 2) / 2); |
| 155 | + } |
| 156 | + |
| 157 | + public static float backIn(float t) { |
| 158 | + return (float) (t * t * (2.70158 * t - 1.70158)); |
| 159 | + } |
| 160 | + |
| 161 | + public static float backOut(float t) { |
| 162 | + return (float) (1 - (--t) * (t) * (-2.70158 * t - 1.70158)); |
| 163 | + } |
| 164 | + |
| 165 | + public static float backInOut(float t) { |
| 166 | + t *= 2; |
| 167 | + if (t < 1) |
| 168 | + return (float) (t * t * (2.70158 * t - 1.70158) / 2); |
| 169 | + t--; |
| 170 | + return (float) ((1 - (--t) * (t) * (-2.70158 * t - 1.70158)) / 2 + .5); |
| 171 | + } |
| 172 | + |
| 173 | + public static float elasticIn(float t) { |
| 174 | + return (float) -(ELASTIC_AMPLITUDE * Math.pow(2, |
| 175 | + 10 * (t -= 1)) * Math.sin((t - (ELASTIC_PERIOD / (2 * Math.PI) * Math.asin(1 / ELASTIC_AMPLITUDE))) * (2 * Math.PI) / ELASTIC_PERIOD)); |
| 176 | + } |
| 177 | + |
| 178 | + public static float elasticOut(float t) { |
| 179 | + return (float) (ELASTIC_AMPLITUDE * Math.pow(2, |
| 180 | + -10 * t) * Math.sin((t - (ELASTIC_PERIOD / (2 * Math.PI) * Math.asin(1 / ELASTIC_AMPLITUDE))) * (2 * Math.PI) / ELASTIC_PERIOD) |
| 181 | + + 1); |
| 182 | + } |
| 183 | + |
| 184 | + public static float elasticInOut(float t) { |
| 185 | + if (t < 0.5) { |
| 186 | + return (float) (-0.5 * (Math.pow(2, 10 * (t -= 0.5f)) * Math.sin((t - (ELASTIC_PERIOD / 4)) * (2 * Math.PI) / ELASTIC_PERIOD))); |
| 187 | + } |
| 188 | + return (float) (Math.pow(2, -10 * (t -= 0.5f)) * Math.sin((t - (ELASTIC_PERIOD / 4)) * (2 * Math.PI) / ELASTIC_PERIOD) * 0.5 + 1); |
| 189 | + } |
| 190 | + |
| 191 | + @FunctionalInterface |
| 192 | + public interface FunkinTweenEaseFunction { |
| 193 | + float compute(float t); |
| 194 | + } |
| 195 | + |
| 196 | + @FunctionalInterface |
| 197 | + public interface FunkinTweenEaseStartCallback { |
| 198 | + void run(FunkinTween tween); |
| 199 | + } |
| 200 | + |
| 201 | + @FunctionalInterface |
| 202 | + public interface FunkinTweenEaseUpdateCallback { |
| 203 | + void run(FunkinTween tween); |
| 204 | + } |
| 205 | + |
| 206 | + @FunctionalInterface |
| 207 | + public interface FunkinTweenEaseCompleteCallback { |
| 208 | + void run(FunkinTween tween); |
| 209 | + } |
| 210 | +} |
0 commit comments