diff --git a/checkbox.lua b/checkbox.lua new file mode 100644 index 0000000..ca2737a --- /dev/null +++ b/checkbox.lua @@ -0,0 +1,46 @@ +local Checkbox = {} +Checkbox.__index = Checkbox + +function Checkbox:new(width, lineWidth, transform, checked) + local box = { + width = width or 20, + lineWidth = lineWidth or 2, + transform = transform or love.math.newTransform(), + checked = checked, + onChanged = nil + } + return setmetatable(box, self) +end + +function Checkbox:draw() + local width = self.width + local lineWidth = self.lineWidth + local halfLine = lineWidth/2 + local boxWidth = width - lineWidth + love.graphics.push("all") + love.graphics.applyTransform(self.transform) + love.graphics.setLineWidth(lineWidth) + love.graphics.rectangle("line", halfLine, halfLine, boxWidth, boxWidth) + if self.checked then + local crossEnd = width - halfLine + love.graphics.line(halfLine, halfLine, crossEnd, crossEnd) + love.graphics.line(halfLine, crossEnd, crossEnd, halfLine) + end + love.graphics.pop() +end + +function Checkbox:getChecked() + return self.checked +end + +function Checkbox:pressmouse(x, y, button) + if button == 1 then + local localX, localY = self.transform:inverseTransformPoint(x, y) + if localX >= 0 and localX < self.width and localY >= 0 and localY < self.width then + self.checked = not self.checked + if self.onChanged then self.onChanged(self.checked) end + end + end +end + +return Checkbox \ No newline at end of file diff --git a/main.lua b/main.lua index 8a8f9de..033994b 100644 --- a/main.lua +++ b/main.lua @@ -1,11 +1,10 @@ local Time = require"time" local Clock = require"clock" +local Checkbox = require"checkbox" function love.load() if arg[#arg] == "-debug" then require("mobdebug").start() end - - local fontsize = love.graphics.getHeight() / 20 local margin = love.graphics.getHeight() / 20 @@ -21,21 +20,49 @@ function love.load() end local exitNote = "Press \"Esc\" to exit." - local exitNoteWidth = love.graphics.getWidth() - column2X - margin - local exitNoteFont = love.graphics.newFont(fontsize) - local _, wraps = exitNoteFont:getWrap(exitNote, exitNoteWidth) - local exitNoteY = love.graphics.getHeight() - table.maxn(wraps) * exitNoteFont:getHeight() - margin + local column2Width = love.graphics.getWidth() - column2X - margin + local font = love.graphics.newFont(love.graphics.getHeight() / 20) + + local _, exitNoteWraps = font:getWrap(exitNote, column2Width) + local exitNoteY = love.graphics.getHeight() - table.maxn(exitNoteWraps) * font:getHeight() - margin + + local checkboxSize = font:getBaseline() + local checkboxLine = love.graphics.getHeight()/180 + local checkbox = Checkbox:new(checkboxSize, checkboxLine, love.math.newTransform(column2X, margin), false) + local showTime = checkbox.checked + checkbox.onChanged = function(checked) + showTime = checked + end + + local textCanvas = love.graphics.newCanvas(love.graphics.getWidth(), love.graphics.getHeight()) + love.graphics.setCanvas(textCanvas) + love.graphics.setColor(1,1,1) + love.graphics.printf(" Show time", font, column2X + checkboxSize + checkboxLine, margin, column2Width) + love.graphics.printf("Press \"Esc\" to exit.", font, column2X, exitNoteY, column2Width) + love.graphics.setCanvas() + + local timeString + local timeIndent + local figureWidth = font:getWidth("0") + clock.onSetHands = function(t) + timeString = tostring(clock:getTime()) + if #timeString < 5 then + timeIndent = figureWidth + else + timeIndent = 0 + end + end + clock.onSetHands(clock:getTime()) love.graphics.setBackgroundColor(0.02,0.53,0.77) - function love.mousemoved(x, y, dx, dy, istouch) clock:movemouse(x, y) end - -- todo mousepressed & mousereleased function love.mousepressed(x, y, button, istouch, presses) clock:pressmouse(x, y, button) + checkbox:pressmouse(x, y, button) end function love.mousereleased(x, y, button, istouch, presses) @@ -44,7 +71,11 @@ function love.load() function love.draw() clock:draw() - love.graphics.printf(exitNote, exitNoteFont, column2X, exitNoteY, exitNoteWidth) + checkbox:draw() + love.graphics.draw(textCanvas, 0, 0) + if showTime then + love.graphics.printf(timeString, font, column2X+timeIndent, margin+font:getHeight(), column2Width) + end end end diff --git a/time.lua b/time.lua index b46aa73..606b7b4 100644 --- a/time.lua +++ b/time.lua @@ -33,7 +33,7 @@ end function Time:__tostring() local h, m = self:get() - return string.format("%2d:%02d", h, m) + return string.format("%d:%02d", h, m) end return Time