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
//! Implements the [`Timer`] and [`Metronome`] types.
//!
//! These can be used as part of some control flow to perform an action at a specified point in
//! time.
use crate*;
/// A control structure that can be used to execute an event once after a certain time duration.
///
/// The intended use is to first declare a new [`Timer`] through [`Timer::new`], and then call
/// [`Timer::tick`] for every sample of music. When the elapsed time has just passed, this function
/// will return `true`, which can be used to execute some branching code.
///
/// ## Example
///
/// ```
/// # use pointillism::prelude::*;
/// // The global song time.
/// let mut time = unt::Time::ZERO;
/// // Our timer.
/// let mut timer = ctr::Timer::new(unt::Time::from_samples(10));
///
/// for i in 0..20 {
/// if timer.tick(time) {
/// println!("The timer ticked on frame {i}!");
/// assert_eq!(i, 10);
/// }
///
/// time.advance();
/// }
/// ```
/// A control structure that can be used to execute an event every certain time duration.
///
/// The intended use is to first declare a new [`Metronome`] through [`Metronome::new`], and then
/// call [`Metronome::tick`] for every sample of music. When the elapsed time has just passed, this
/// function will return `true`, which can be used to execute some branching code.
///
/// Note that by design, the metronome can only tick once per frame. If you need something more
/// sophisticated that can run simultaneous events, consider using a [`ctr::Seq`] or [`ctr::Loop`]
/// instead.
///
/// By default, the metronome ticks on the first frame. You can add some condition like
/// `!time.is_zero()` to ignore this tick.
///
/// ## Example
///
/// ```
/// # use pointillism::prelude::*;
/// // The global song time.
/// let mut time = unt::Time::ZERO;
/// // Our metronome.
/// let mut metr = ctr::Metronome::new(unt::Time::from_samples(10));
///
/// for i in 0..20 {
/// if metr.tick(time) {
/// println!("The metronome ticked on frame {i}!");
/// assert_eq!(i % 10, 0);
/// }
///
/// time.advance();
/// }
/// ```