1313
1414STOPWATCH_STOPPED , STOPWATCH_PLAYING , STOPWATCH_PAUSED = 0 , 1 , 2
1515
16- local StopWatch = {}
17- StopWatch .__index = StopWatch
16+ --- @class Stopwatch
17+ --- @field playback_rate number
18+ --- @field playback_now number
19+ --- @field playback_elapsed number
20+ --- @field playback_duration number
21+ --- @field state number # STOPWATCH_STOPPED, STOPWATCH_PLAYING, STOPWATCH_PAUSED
22+ --- @field delay number
23+ --- @field looping boolean
24+ local Stopwatch = {}
25+ Stopwatch .__index = Stopwatch
1826
1927local timer_now = RealTime
2028
2129--- Creates a StopWatch.
22- -- @param number duration How long the stopwatch will last
23- -- @param function callback What to run when the stopwatch finishes.
24- local function Initialize (_ , duration , fn )
25- local self = setmetatable ({}, StopWatch )
30+ --- @param duration number # How long the stopwatch will last
31+ --- @param callback fun ( self : Stopwatch ) # What to run when the stopwatch finishes.
32+ --- @return Stopwatch
33+ local function Initialize (_ , duration , callback )
34+ local self = setmetatable ({}, Stopwatch )
2635 self .playback_rate = 1
2736 self .playback_now = timer_now ()
2837 self .playback_elapsed = 0
@@ -45,7 +54,7 @@ local function Initialize(_, duration, fn)
4554 self .playback_elapsed = self .playback_duration
4655 self .state = STOPWATCH_STOPPED
4756 end
48- fn (self )
57+ callback (self )
4958 end )
5059 timer .Stop (mangled )
5160
@@ -54,20 +63,20 @@ local function Initialize(_, duration, fn)
5463 return self
5564end
5665
57- setmetatable (StopWatch , {
66+ setmetatable (Stopwatch , {
5867 __call = Initialize
5968})
6069
6170--- Pauses a stopwatch at the current time to be resumed with :Play
62- function StopWatch :Pause ()
71+ function Stopwatch :Pause ()
6372 if self .state == STOPWATCH_PLAYING then
6473 self .state = STOPWATCH_PAUSED
6574 timer .Pause (self .timerid )
6675 end
6776end
6877
6978--- Resumes the stopwatch after it was paused. You can't :Play a :Stop(ped) timer, use :Start for that.
70- function StopWatch :Play ()
79+ function Stopwatch :Play ()
7180 if self .state == STOPWATCH_PAUSED then
7281 self .playback_now = timer_now ()
7382 self .state = STOPWATCH_PLAYING
@@ -76,7 +85,7 @@ function StopWatch:Play()
7685end
7786
7887--- Used internally by GetTime, don't use.
79- function StopWatch :UpdateTime ()
88+ function Stopwatch :UpdateTime ()
8089 if self .state == STOPWATCH_PLAYING then
8190 local now = timer_now ()
8291 local elapsed = (now - self .playback_now ) * self .playback_rate
8897
8998--- Stops the timer with the stored elapsed time.
9099-- Continue from here with :Start()
91- function StopWatch :Stop ()
100+ function Stopwatch :Stop ()
92101 if self .state ~= STOPWATCH_STOPPED then
93102 self :UpdateTime ()
94103 self .state = STOPWATCH_STOPPED
@@ -97,7 +106,7 @@ function StopWatch:Stop()
97106end
98107
99108--- (Re)starts the stopwatch.
100- function StopWatch :Start ()
109+ function Stopwatch :Start ()
101110 if self .state == STOPWATCH_STOPPED then
102111 self .playback_now = timer_now ()
103112 self .state = STOPWATCH_PLAYING
@@ -107,15 +116,15 @@ function StopWatch:Start()
107116end
108117
109118--- Returns the playback duration of the stopwatch.
110- -- @return number Length
111- function StopWatch :GetDuration ()
119+ --- @return number length
120+ function Stopwatch :GetDuration ()
112121 return self .playback_duration
113122end
114123
115124--- Returns the playback duration of the stopwatch.
116- -- @param number duration
117- -- @return StopWatch self
118- function StopWatch :SetDuration (duration )
125+ --- @param duration number
126+ --- @return Stopwatch self
127+ function Stopwatch :SetDuration (duration )
119128 self .playback_duration = duration
120129 self .delay = duration
121130 timer .Adjust ( self .timerid , duration )
@@ -126,27 +135,27 @@ function StopWatch:SetDuration(duration)
126135end
127136
128137--- Sets the playback rate / speed of the stopwatch. 2 is twice as fast, etc.
129- -- @param number n Speed
130- -- @return StopWatch self
131- function StopWatch :SetRate (n )
138+ --- @param speed number
139+ --- @return Stopwatch self
140+ function Stopwatch :SetRate (speed )
132141 self :UpdateTime ()
133- self .playback_rate = n
142+ self .playback_rate = speed
134143 -- New Duration - Elapsed
135- self .delay = (self .playback_duration / n ) - self .playback_elapsed
144+ self .delay = (self .playback_duration / speed ) - self .playback_elapsed
136145 timer .Adjust ( self .timerid , self .delay )
137146 return self
138147end
139148
140149--- Returns the playback rate of the stopwatch. Default 1
141- -- @return number Playback rate
142- function StopWatch :GetRate ()
150+ --- @return number rate # Playback rate
151+ function Stopwatch :GetRate ()
143152 return self .playback_rate
144153end
145154
146155--- Sets the playback time of the stopwatch.
147- -- @param number n Time
148- -- @return StopWatch self
149- function StopWatch :SetTime (n )
156+ --- @param n number # Time
157+ --- @return Stopwatch self
158+ function Stopwatch :SetTime (n )
150159 self .playback_now = timer_now ()
151160 self .playback_elapsed = n
152161 self .delay = (self .playback_duration - n ) / self .playback_rate
@@ -156,22 +165,22 @@ function StopWatch:SetTime(n)
156165end
157166
158167--- Returns the current playback time in seconds of the stopwatch
159- -- @return number Playback time
160- function StopWatch :GetTime ()
168+ --- @return number time # Playback time
169+ function Stopwatch :GetTime ()
161170 self :UpdateTime ()
162171 return self .playback_elapsed
163172end
164173
165174--- Returns the current playback state of the stopwatch. 0 for STOPWATCH_STOPPED, 1 for STOPWATCH_PLAYING, 2 for STOPWATCH_PAUSED
166- -- @return number Playback state
167- function StopWatch :GetState ()
175+ --- @return number state Playback state
176+ function Stopwatch :GetState ()
168177 return self .state
169178end
170179
171180--- Sets the stopwatch to loop. Won't call the callback if it is looping.
172- -- @param boolean loop Whether it's looping
173- -- @return StopWatch self
174- function StopWatch :SetLooping (loop )
181+ --- @param loop boolean Whether it' s looping
182+ --- @return Stopwatch self
183+ function Stopwatch :SetLooping (loop )
175184 if self .looping ~= loop then
176185 self .looping = loop
177186 timer .Adjust ( self .timerid , self .delay , 0 , nil )
@@ -180,11 +189,11 @@ function StopWatch:SetLooping(loop)
180189end
181190
182191--- Returns if the stopwatch is looping
183- -- @return boolean Looping
184- function StopWatch :GetLooping ()
192+ --- @return boolean looping
193+ function Stopwatch :GetLooping ()
185194 return self .looping
186195end
187196
188- _G .StopWatch = StopWatch
197+ _G .StopWatch = Stopwatch
189198
190- return StopWatch
199+ return Stopwatch
0 commit comments