146 lines
3 KiB
Lua
146 lines
3 KiB
Lua
local window = require("window")
|
|
local room = require("room")
|
|
local edge = require("edge")
|
|
local button = require("button")
|
|
|
|
local m = room:from{
|
|
code = 45100,
|
|
key_size = 25,
|
|
key_gap = 10,
|
|
}
|
|
|
|
local oldfrom = m.from
|
|
function m:from(t)
|
|
local kp = oldfrom(self, t)
|
|
|
|
local ks = kp.key_size
|
|
local kg = kp.key_gap
|
|
|
|
local kd = ks + kg
|
|
local kw = kd*3 - kg
|
|
local kh = kd*5 - kg
|
|
local sx, sy = room.calc_align(kw, kh)
|
|
|
|
kp.number = 0
|
|
kp.keys = {}
|
|
kp.pad_width = kw
|
|
kp.pad_height = kh
|
|
kp.pad_x = sx
|
|
kp.pad_y = sy
|
|
kp.state = 'normal'
|
|
kp.timer = 0
|
|
|
|
for i = 1, 9 do
|
|
local x = sx + kd * ((i-1)%3)
|
|
local y = sy + kd * math.floor(1 + (i-1) / 3)
|
|
|
|
kp.keys[i] = button.new(tostring(i),
|
|
function () kp:input(i) end,
|
|
x, y, ks, ks)
|
|
end
|
|
|
|
local br = sy + kd*4
|
|
|
|
kp.keys['C'] = button.new('C',
|
|
function () kp:clear() end,
|
|
sx, br, ks, ks)
|
|
|
|
kp.keys[0] = button.new('0',
|
|
function () kp:input(0) end,
|
|
sx + kd, br, ks, ks)
|
|
|
|
kp.keys['A'] = button.new('A',
|
|
function () kp:accept() end,
|
|
sx + kd*2, br, ks, ks)
|
|
|
|
edge:set(kp, "down", kp.room)
|
|
|
|
register(kp)
|
|
|
|
return kp
|
|
end
|
|
|
|
function m:clear()
|
|
self.number = 0
|
|
end
|
|
|
|
function m:input(n)
|
|
if self.number < 100000 then
|
|
self.number = self.number * 10 + n
|
|
end
|
|
end
|
|
|
|
function m:oncorrect()
|
|
log:log("Correct!")
|
|
end
|
|
|
|
function m:onwrong()
|
|
log:log("it seeems that the number does not match.")
|
|
end
|
|
|
|
function m:accept()
|
|
if self.number == self.code then
|
|
self.state = 'correct'
|
|
self:oncorrect()
|
|
else
|
|
self.state = 'wrong'
|
|
self:onwrong()
|
|
end
|
|
end
|
|
|
|
m.update = function(self, dt)
|
|
if self.state == 'wrong' then
|
|
self.timer = self.timer + dt
|
|
|
|
if self.timer > 1 then
|
|
self.state = 'normal'
|
|
self.timer = 0
|
|
self:clear()
|
|
end
|
|
end
|
|
end
|
|
|
|
m.draw = window.draw(function (self)
|
|
love.graphics.setLineWidth(3)
|
|
|
|
-- start drawing keypad
|
|
|
|
-- draw indicator box
|
|
love.graphics.rectangle('line', self.pad_x, self.pad_y, self.pad_width, self.key_size)
|
|
|
|
local function display(str)
|
|
love.graphics.printf(str, self.pad_x + 5, self.pad_y + 5, self.pad_width - 8, 'right')
|
|
end
|
|
|
|
if self.state == 'normal' then
|
|
display(tostring(self.number))
|
|
elseif self.state == 'correct' then
|
|
display("correct")
|
|
elseif self.state == 'wrong' then
|
|
display("wrong")
|
|
else
|
|
error("keypad is not in a defined state")
|
|
end
|
|
|
|
for i, v in pairs(self.keys) do
|
|
v:draw()
|
|
end
|
|
|
|
self.edges[1]:draw()
|
|
end)
|
|
|
|
m.mousepressed = function (self, x, y, button)
|
|
local x, y = x - self.x, y - self.y
|
|
|
|
if self.state == 'normal' then
|
|
for i, v in pairs(self.keys) do
|
|
if v:mousepressed(x, y, button) then
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
|
|
return self.edges[1]:mousepressed(x, y, button)
|
|
end
|
|
|
|
return m
|