@@ -162,8 +162,8 @@ impl Timer {
162
162
/// assert!(timer.repeating());
163
163
/// ```
164
164
#[ inline]
165
- pub fn repeating ( & self ) -> bool {
166
- self . mode . repeating ( )
165
+ pub fn mode ( & self ) -> TimerMode {
166
+ self . mode
167
167
}
168
168
169
169
/// Sets whether the timer is repeating or not.
@@ -176,16 +176,12 @@ impl Timer {
176
176
/// assert!(!timer.repeating());
177
177
/// ```
178
178
#[ inline]
179
- pub fn set_repeating ( & mut self , repeating : bool ) {
180
- if !self . mode . repeating ( ) && repeating && self . finished {
179
+ pub fn set_mode ( & mut self , mode : TimerMode ) {
180
+ if !self . mode . repeating ( ) && mode . repeating ( ) && self . finished {
181
181
self . stopwatch . reset ( ) ;
182
182
self . finished = self . just_finished ( ) ;
183
183
}
184
- self . mode = if repeating {
185
- TimerMode :: Repeating
186
- } else {
187
- TimerMode :: Once
188
- } ;
184
+ self . mode = mode;
189
185
}
190
186
191
187
/// Advance the timer by `delta` seconds.
@@ -208,13 +204,13 @@ impl Timer {
208
204
pub fn tick ( & mut self , delta : Duration ) -> & Self {
209
205
if self . paused ( ) {
210
206
self . times_finished_this_tick = 0 ;
211
- if self . repeating ( ) {
207
+ if self . mode . repeating ( ) {
212
208
self . finished = false ;
213
209
}
214
210
return self ;
215
211
}
216
212
217
- if !self . repeating ( ) && self . finished ( ) {
213
+ if !self . mode . repeating ( ) && self . finished ( ) {
218
214
self . times_finished_this_tick = 0 ;
219
215
return self ;
220
216
}
@@ -223,7 +219,7 @@ impl Timer {
223
219
self . finished = self . elapsed ( ) >= self . duration ( ) ;
224
220
225
221
if self . finished ( ) {
226
- if self . repeating ( ) {
222
+ if self . mode . repeating ( ) {
227
223
self . times_finished_this_tick =
228
224
( self . elapsed ( ) . as_nanos ( ) / self . duration ( ) . as_nanos ( ) ) as u32 ;
229
225
// Duration does not have a modulo
@@ -404,24 +400,19 @@ impl Timer {
404
400
}
405
401
406
402
/// Specifies [`Timer`] behavior.
407
- #[ derive( Debug , Clone , Copy , Reflect ) ]
403
+ #[ derive( Debug , Clone , Copy , Eq , PartialEq , Hash , Default , Reflect ) ]
408
404
#[ reflect( Default ) ]
409
405
pub enum TimerMode {
410
406
/// Run once and stop.
407
+ #[ default]
411
408
Once ,
412
409
/// Reset when finished.
413
410
Repeating ,
414
411
}
415
412
416
413
impl TimerMode {
417
414
pub fn repeating ( self ) -> bool {
418
- matches ! ( self , Self :: Repeating )
419
- }
420
- }
421
-
422
- impl Default for TimerMode {
423
- fn default ( ) -> Self {
424
- Self :: Once
415
+ self == Self :: Repeating
425
416
}
426
417
}
427
418
0 commit comments