Skip to content

Commit b4e3dd4

Browse files
committed
Simplify DynTweenable implementation
Signed-off-by: Alex Saveau <[email protected]>
1 parent 874c351 commit b4e3dd4

File tree

1 file changed

+28
-48
lines changed

1 file changed

+28
-48
lines changed

src/tweenable.rs

Lines changed: 28 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -248,42 +248,40 @@ pub enum DynTweenable<T> {
248248
Tween(Tween<T>),
249249
}
250250

251-
impl<T> Tweenable<T> for DynTweenable<T> {
252-
fn duration(&self) -> Duration {
251+
impl<T> DynTweenable<T> {
252+
fn as_dyn(&self) -> &dyn Tweenable<T> {
253253
match self {
254-
Self::Boxed(b) => b.duration(),
255-
Self::Delay(d) => Tweenable::<T>::duration(d),
256-
Self::Sequence(s) => s.duration(),
257-
Self::Tracks(t) => t.duration(),
258-
Self::Tween(t) => t.duration(),
254+
Self::Boxed(b) => b,
255+
Self::Delay(d) => d,
256+
Self::Sequence(s) => s,
257+
Self::Tracks(t) => t,
258+
Self::Tween(t) => t,
259259
}
260260
}
261-
fn is_looping(&self) -> bool {
261+
262+
fn as_mut_dyn(&mut self) -> &mut dyn Tweenable<T> {
262263
match self {
263-
Self::Boxed(b) => b.is_looping(),
264-
Self::Delay(d) => Tweenable::<T>::is_looping(d),
265-
Self::Sequence(s) => s.is_looping(),
266-
Self::Tracks(t) => t.is_looping(),
267-
Self::Tween(t) => t.is_looping(),
264+
Self::Boxed(b) => b,
265+
Self::Delay(d) => d,
266+
Self::Sequence(s) => s,
267+
Self::Tracks(t) => t,
268+
Self::Tween(t) => t,
268269
}
269270
}
271+
}
272+
273+
impl<T> Tweenable<T> for DynTweenable<T> {
274+
fn duration(&self) -> Duration {
275+
self.as_dyn().duration()
276+
}
277+
fn is_looping(&self) -> bool {
278+
self.as_dyn().is_looping()
279+
}
270280
fn set_progress(&mut self, progress: f32) {
271-
match self {
272-
Self::Boxed(b) => b.set_progress(progress),
273-
Self::Delay(d) => Tweenable::<T>::set_progress(d, progress),
274-
Self::Sequence(s) => s.set_progress(progress),
275-
Self::Tracks(t) => t.set_progress(progress),
276-
Self::Tween(t) => t.set_progress(progress),
277-
}
281+
self.as_mut_dyn().set_progress(progress);
278282
}
279283
fn progress(&self) -> f32 {
280-
match self {
281-
Self::Boxed(b) => b.progress(),
282-
Self::Delay(d) => Tweenable::<T>::progress(d),
283-
Self::Sequence(s) => s.progress(),
284-
Self::Tracks(t) => t.progress(),
285-
Self::Tween(t) => t.progress(),
286-
}
284+
self.as_dyn().progress()
287285
}
288286
fn tick(
289287
&mut self,
@@ -292,31 +290,13 @@ impl<T> Tweenable<T> for DynTweenable<T> {
292290
entity: Entity,
293291
event_writer: &mut EventWriter<TweenCompleted>,
294292
) -> TweenState {
295-
match self {
296-
Self::Boxed(b) => b.tick(delta, target, entity, event_writer),
297-
Self::Delay(d) => d.tick(delta, target, entity, event_writer),
298-
Self::Sequence(s) => s.tick(delta, target, entity, event_writer),
299-
Self::Tracks(t) => t.tick(delta, target, entity, event_writer),
300-
Self::Tween(t) => t.tick(delta, target, entity, event_writer),
301-
}
293+
self.as_mut_dyn().tick(delta, target, entity, event_writer)
302294
}
303295
fn times_completed(&self) -> u32 {
304-
match self {
305-
Self::Boxed(b) => b.times_completed(),
306-
Self::Delay(d) => Tweenable::<T>::times_completed(d),
307-
Self::Sequence(s) => s.times_completed(),
308-
Self::Tracks(t) => t.times_completed(),
309-
Self::Tween(t) => t.times_completed(),
310-
}
296+
self.as_dyn().times_completed()
311297
}
312298
fn rewind(&mut self) {
313-
match self {
314-
Self::Boxed(b) => b.rewind(),
315-
Self::Delay(d) => Tweenable::<T>::rewind(d),
316-
Self::Sequence(s) => s.rewind(),
317-
Self::Tracks(t) => t.rewind(),
318-
Self::Tween(t) => t.rewind(),
319-
}
299+
self.as_mut_dyn().rewind();
320300
}
321301
}
322302

0 commit comments

Comments
 (0)