Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require "component"
- local event = require "event"
- local term = require "term"
- local computer = require "computer"
- local gpu = component.gpu
- local key_space = require("keyboard").keys.space
- local running = true
- local x, y, width, height
- local outputWidth = 9 -- 00:00.000 == 9 chars
- local startTime, timerEvent
- local events = setmetatable({}, {__index = function() return function() end end})
- function events.key_up(keyboard, char, code, player)
- if (code == key_space) then
- running = false
- end
- end
- function events.screen_resized(screenAddress, newWidth, newHeight)
- width, height = newWidth, newHeight
- x = math.max(1, math.floor((width - outputWidth) / 2))
- y = math.max(1, math.floor(height / 2))
- term.clear()
- clockTimer(true)
- end
- function events.touch(adr, x, y, button, player)
- if (timerEvent) then
- event.cancel(timerEvent)
- timerEvent = nil
- else
- startTime = computer.uptime()
- timerEvent = event.timer(1/20, clockTimer, math.huge)
- end
- end
- function clockTimer(reset)
- if (reset) then
- gpu.set(x, y, "00:00.000")
- else
- local m, s, ms
- local t = math.floor((computer.uptime() - startTime) * 1000)
- ms = t % 1000
- t = math.floor(t / 1000)
- s = t % 60
- t = math.floor(t / 60)
- m = t % 60
- gpu.set(x, y, string.format("%02d:%02d.%03d", m, s, ms))
- end
- end
- function handleEvent(event, ...)
- if (event) then
- events[event](...)
- end
- end
- local prevWidth, prevHeight = gpu.getResolution()
- if (not gpu.setResolution(outputWidth, 1)) then
- events.screen_resized(false, prevWidth, prevHeight)
- end
- while running do
- handleEvent(event.pull())
- end
- if (timerEvent) then event.cancel(timerEvent) end
- gpu.setResolution(prevWidth, prevHeight)
Add Comment
Please, Sign In to add comment