_Nobody_

stopwatch

Mar 29th, 2021
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.18 KB | None | 0 0
  1. --------
  2. -- Stopwatch Timing Class, v1.2.1
  3. --
  4. -- by Kyle Coburn
  5. -- 10 April 2012
  6. ----
  7. -- For the latest updates, release notes, and support/feedback:
  8. -- http://developer.anscamobile.com/code/stopwatch-timing-class
  9. ----
  10. -- Usage:
  11. -- local stopwatch = require "stopwatch"
  12. --
  13. -- stopwatch.new() --Counts up indefinitely
  14. -- stopwatch.new( 60 ) --Counts down from 60 seconds
  15. --------
  16.  
  17. local stopwatch = {}
  18. local stopwatch_mt = { __index = stopwatch }
  19.  
  20. local floor, getTime = math.floor, system.getTimer
  21.  
  22.  
  23. local function timeFormat(secs)
  24.         if (secs < 1) then
  25.                 return "00:00"
  26.         end
  27.         local mins = "00"
  28.         if (secs > 59) then
  29.                 mins = floor(secs / 60)
  30.                 if (mins < 10) then
  31.                         mins = "0"..mins
  32.                 end
  33.                 secs = secs % 60
  34.         end
  35.         if (secs < 10) then
  36.                 secs = "0"..secs
  37.         end
  38.         return mins..":"..secs
  39. end
  40.  
  41.  
  42. -- Time check
  43. function stopwatch:getElapsed()
  44.         if (self._pauseStart) then
  45.                 return self._pauseStart - self._startTime
  46.         end
  47.         return getTime() - self._startTime
  48. end
  49.  
  50. function stopwatch:getElapsedSeconds()
  51.         return floor(self:getElapsed() / 1000)
  52. end
  53.  
  54. function stopwatch:getRemaining()
  55.         if (self._pauseStart) then
  56.                 return self._endTime - self._pauseStart
  57.         end
  58.         return self._endTime - getTime()
  59. end
  60.  
  61. function stopwatch:getRemainingSeconds()
  62.         return floor(self:getRemaining() / 1000)
  63. end
  64.  
  65. function stopwatch:isElapsed()
  66.         if (self._endTime) then
  67.                 return self:getRemaining() <= 0
  68.         end
  69. end
  70.  
  71.  
  72. -- Modify
  73. function stopwatch:addTime(seconds)
  74.         if (self._endTime) then
  75.                 self._endTime = self._endTime + seconds * 1000
  76.         else
  77.                 self._startTime = self._startTime - seconds * 1000
  78.         end
  79. end
  80.  
  81.  
  82. -- Pause
  83. function stopwatch:isPaused()
  84.         return self._pauseStart ~= nil
  85. end
  86.  
  87. function stopwatch:pause()
  88.         if (not self:isPaused()) then
  89.                 self._pauseStart = getTime()
  90.         end
  91. end
  92.  
  93. function stopwatch:resume()
  94.         if (self:isPaused()) then
  95.                 if (self._endTime) then
  96.                         self._endTime = self._endTime + (getTime() - self._pauseStart)
  97.                 else
  98.                         self._startTime = self._startTime + (getTime() - self._pauseStart)
  99.                 end
  100.                 self._pauseStart = nil
  101.         end
  102. end
  103.  
  104.  
  105. -- Formatting
  106. function stopwatch:toElapsedString()
  107.         return timeFormat(self:getElapsedSeconds())
  108. end
  109.  
  110. function stopwatch:toRemainingString()
  111.         if (not self._endTime) then
  112.                 return "99:99"
  113.         end
  114.         return timeFormat(self:getRemainingSeconds())
  115. end
  116.  
  117.  
  118. -- Object creation
  119. function stopwatch.new(countdown)
  120.         local newTimer = {}
  121.         local startTime = getTime()
  122.  
  123.         if (countdown) then
  124.                 newTimer._endTime = startTime + countdown * 1000
  125.         end
  126.         newTimer._startTime = startTime
  127.  
  128.         return setmetatable(newTimer, stopwatch_mt)
  129. end
  130.  
  131. stopwatch.format = timeFormat
  132.  
  133.  
  134. return stopwatch
Advertisement
Add Comment
Please, Sign In to add comment