1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
use crate::{ curve::{Curved, CurvedDistance, CurvedOffset, CurvedTangent}, phase::Phase, spline::Spline, }; use core::Scalar; use serde::{de::DeserializeOwned, Deserialize, Serialize}; #[derive(Debug, Default, Clone, Serialize, Deserialize)] #[serde(bound = "T: Serialize + DeserializeOwned")] pub struct Animation<T> where T: Default + Clone + Curved + CurvedOffset + CurvedDistance + CurvedTangent, { pub value_spline: Spline<T>, #[serde(default)] pub time_phase: Phase, #[serde(default)] pub playing: bool, #[serde(default)] pub looped: bool, #[serde(default)] time: Scalar, } impl<T> Animation<T> where T: Default + Clone + Curved + CurvedOffset + CurvedDistance + CurvedTangent, { pub fn new(value_spline: Spline<T>, time_phase: Phase) -> Self { Self { value_spline, time_phase, playing: false, looped: false, time: 0.0, } } pub fn instant(value: T) -> Self { Self::new( Spline::point(value) .expect("Could not create point value spline for instant animation"), Phase::point(1.0).expect("Could not create point time phase for instant animation"), ) } pub fn time(&self) -> Scalar { self.time } pub fn start(&mut self) { self.time = 0.0; } pub fn end(&mut self) { self.time = self.duration(); } pub fn set_time(&mut self, time: Scalar) { self.time = time.max(0.0).min(self.duration()); } pub fn in_progress(&self) -> bool { self.playing } pub fn is_complete(&self) -> bool { !self.in_progress() } pub fn duration(&self) -> Scalar { self.time_phase.duration() } pub fn value(&self) -> T { self.sample(self.time) } pub fn sample(&self, time: Scalar) -> T { self.value_spline.sample(self.time_phase.sample(time)) } pub fn process(&mut self, delta_time: Scalar) { if self.playing { let duration = self.duration(); let mut time = self.time + delta_time; if time >= duration { if self.looped { time = 0.0; } else { self.playing = false; } } self.time = time.max(0.0).min(duration); } } } #[derive(Debug, Default, Clone, Serialize, Deserialize)] pub struct PhaseAnimation { pub value_phase: Phase, #[serde(default)] pub playing: bool, #[serde(default)] pub looped: bool, #[serde(default)] time: Scalar, } impl PhaseAnimation { pub fn new(value_phase: Phase) -> Self { Self { value_phase, playing: false, looped: false, time: 0.0, } } pub fn instant(value: Scalar) -> Self { Self::new( Phase::point(value).expect("Could not create point phase for instant phase animation"), ) } pub fn time(&self) -> Scalar { self.time } pub fn start(&mut self) { self.time = 0.0; } pub fn end(&mut self) { self.time = self.duration(); } pub fn set_time(&mut self, time: Scalar) { self.time = time.max(0.0).min(self.duration()); } pub fn in_progress(&self) -> bool { self.playing } pub fn is_complete(&self) -> bool { !self.in_progress() } pub fn duration(&self) -> Scalar { self.value_phase.duration() } pub fn value(&self) -> Scalar { self.sample(self.time) } pub fn sample(&self, time: Scalar) -> Scalar { self.value_phase.sample(time) } pub fn process(&mut self, delta_time: Scalar) { if self.playing { let duration = self.duration(); let mut time = self.time + delta_time; if time >= duration { if self.looped { time = 0.0; } else { self.playing = false; } } self.time = time.max(0.0).min(duration); } } } #[derive(Debug, Default, Clone, Serialize, Deserialize)] #[serde(bound = "T: Serialize + DeserializeOwned")] pub struct Interpolation<T> where T: Default + Clone + Curved, { #[serde(default)] pub from: T, #[serde(default)] pub to: T, #[serde(default)] pub time_phase: Phase, #[serde(default)] pub playing: bool, #[serde(default)] pub looped: bool, #[serde(default)] time: Scalar, } impl<T> Interpolation<T> where T: Default + Clone + Curved, { pub fn new(from: T, to: T, time_phase: Phase) -> Self { Self { from, to, time_phase, playing: false, looped: false, time: 0.0, } } pub fn instant(value: T) -> Self { Self::new( value.clone(), value, Phase::point(1.0).expect("Could not create point time phase for instant animation"), ) } pub fn time(&self) -> Scalar { self.time } pub fn start(&mut self) { self.time = 0.0; } pub fn end(&mut self) { self.time = self.duration(); } pub fn set_time(&mut self, time: Scalar) { self.time = time.max(0.0).min(self.duration()); } pub fn in_progress(&self) -> bool { self.playing } pub fn is_complete(&self) -> bool { !self.in_progress() } pub fn duration(&self) -> Scalar { self.time_phase.duration() } pub fn value(&self) -> T { self.sample(self.time) } pub fn sample(&self, time: Scalar) -> T { self.from .interpolate(&self.to, self.time_phase.sample(time)) } pub fn set(&mut self, value: T) { self.from = self.value(); self.to = value; self.time = 0.0; } pub fn process(&mut self, delta_time: Scalar) { if self.playing { let duration = self.duration(); let mut time = self.time + delta_time; if time >= duration { if self.looped { time = 0.0; } else { self.playing = false; } } self.time = time.max(0.0).min(duration); } } }