127 lines
2.7 KiB
Lua
127 lines
2.7 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(m, t)
|
|
|
|
local ks = self.key_size
|
|
local kg = self.key_gap
|
|
|
|
local kd = ks + kg
|
|
local kw = kd*3 - kg
|
|
local kh = kd*5 - kg
|
|
local sx = (320 - kw)/2
|
|
local sy = (240 - kh)/2
|
|
|
|
kp.number = 0
|
|
kp.keys = {}
|
|
kp.padx = sx
|
|
kp.pady = sy
|
|
kp.indicator_width = kw
|
|
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(m, "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:accept()
|
|
if self.number == self.code then
|
|
log:log("correct!")
|
|
else
|
|
self.state = 'wrong'
|
|
log:log("it seeems that the number does not match.")
|
|
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)
|
|
|
|
-- draw indicator box
|
|
love.graphics.rectangle('line', self.padx, self.pady, self.indicator_width, self.key_size)
|
|
|
|
if self.state == 'normal' then
|
|
love.graphics.printf(tostring(self.number), self.padx + 5, self.pady + 5, self.indicator_width - 8, 'right')
|
|
elseif self.state == 'wrong' then
|
|
love.graphics.printf("wrong", self.padx + 5, self.pady + 5, self.indicator_width - 8, 'right')
|
|
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
|
|
|
|
for i, v in pairs(self.keys) do
|
|
if v:mousepressed(x, y, button) then
|
|
return
|
|
end
|
|
end
|
|
|
|
return self.edges[1]:mousepressed(x, y, button)
|
|
end
|
|
|
|
return m
|