-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatch.lua
134 lines (108 loc) · 2.5 KB
/
stopwatch.lua
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
--------
-- Stopwatch Timing Class, v1.2.1
--
-- by Kyle Coburn
-- 10 April 2012
----
-- For the latest updates, release notes, and support/feedback:
-- http://developer.anscamobile.com/code/stopwatch-timing-class
----
-- Usage:
-- local stopwatch = require "stopwatch"
--
-- stopwatch.new() --Counts up indefinitely
-- stopwatch.new( 60 ) --Counts down from 60 seconds
--------
local stopwatch = {}
local stopwatch_mt = { __index = stopwatch }
local floor, getTime = math.floor, system.getTimer
local function timeFormat(secs)
if (secs < 1) then
return "00:00"
end
local mins = "00"
if (secs > 59) then
mins = floor(secs / 60)
if (mins < 10) then
mins = "0"..mins
end
secs = secs % 60
end
if (secs < 10) then
secs = "0"..secs
end
return mins..":"..secs
end
-- Time check
function stopwatch:getElapsed()
if (self._pauseStart) then
return self._pauseStart - self._startTime
end
return getTime() - self._startTime
end
function stopwatch:getElapsedSeconds()
return floor(self:getElapsed() / 1000)
end
function stopwatch:getRemaining()
if (self._pauseStart) then
return self._endTime - self._pauseStart
end
return self._endTime - getTime()
end
function stopwatch:getRemainingSeconds()
return floor(self:getRemaining() / 1000)
end
function stopwatch:isElapsed()
if (self._endTime) then
return self:getRemaining() <= 0
end
end
-- Modify
function stopwatch:addTime(seconds)
if (self._endTime) then
self._endTime = self._endTime + seconds * 1000
else
self._startTime = self._startTime - seconds * 1000
end
end
-- Pause
function stopwatch:isPaused()
return self._pauseStart ~= nil
end
function stopwatch:pause()
if (not self:isPaused()) then
self._pauseStart = getTime()
end
end
function stopwatch:resume()
if (self:isPaused()) then
if (self._endTime) then
self._endTime = self._endTime + (getTime() - self._pauseStart)
else
self._startTime = self._startTime + (getTime() - self._pauseStart)
end
self._pauseStart = nil
end
end
-- Formatting
function stopwatch:toElapsedString()
return timeFormat(self:getElapsedSeconds())
end
function stopwatch:toRemainingString()
if (not self._endTime) then
return "99:99"
end
return timeFormat(self:getRemainingSeconds())
end
-- Object creation
function stopwatch.new(countdown)
local newTimer = {}
local startTime = getTime()
if (countdown) then
newTimer._endTime = startTime + countdown * 1000
end
newTimer._startTime = startTime
return setmetatable(newTimer, stopwatch_mt)
end
stopwatch.format = timeFormat
return stopwatch