Compare commits

..

No commits in common. "137a390b65be102d1b45bb2298f828c702034d59" and "1be48466984f282611adfdd5f74a3b80b62ac00b" have entirely different histories.

View File

@ -143,40 +143,63 @@ kbd={}
add(real_modules,kbd) add(real_modules,kbd)
function kbd:init() function kbd:init()
self.real=btn() self.real=btn()
self.state={btn=0} self.down=0
end end
function kbd:update() function kbd:update()
-- figure out what keys are _really_ pressed -- figure out what keys are _really_ pressed
local now_real,was_real=btn(),self.real local now_real=btn()
local was_real=self.real
self.real=now_real self.real=now_real
-- add keys that are really pressed -- add keys that are really pressed
-- if they weren't really pressed before -- if they weren't really pressed before
-- (they may have been force- -- (they may have been force-
-- released by :release()) -- released by :release())
local real_pressed=~was_real&now_real local real_pressed=(~was_real)&now_real
local state=self.state local now_down=(self.down&now_real)|real_pressed
local now_down=state.btn&now_real|real_pressed local was_down=self.down
local was_down=state.btn self.down=now_down
-- deduce pressed/released by changes in down -- deduce pressed/released by changes in down
state.btn,state.btnp,state.btnr= local pressed=(~was_down)&now_down
now_down, local released=(~now_down)&was_down
~was_down&now_down,
~now_down&was_down self.pressed=pressed
self.released=released
end end
for _kbdi in all(split"btn,btnp,btnr") do function kbd:btn(i)
kbd[_kbdi]=function(self,i,t) return 1<<i&self.state[_kbdi]!=0 end return self.down&(1<<i)!=0
end
function kbd:btnp(i)
return self.pressed&(1<<i)!=0
end
function kbd:btnr(i)
return self.released&(1<<i)!=0
end end
function kbd:release(i) function kbd:release(i)
self.state.btn&=~(1<<i) self.down&=~(1<<i)
end end
function tostring(any) function tostring(any)
if type(any)=="function" then
return "function"
end
if any==nil then
return "nil"
end
if type(any)=="string" then
return any
end
if type(any)=="boolean" then
if any then return "true" end
return "false"
end
if type(any)=="table" then if type(any)=="table" then
local str = "{ " local str = "{ "
for k,v in pairs(any) do for k,v in pairs(any) do
@ -184,7 +207,10 @@ function tostring(any)
end end
return str.."}" return str.."}"
end end
return tostr(any) if type(any)=="number" then
return ""..any
end
return "unknown" -- should never show
end end
-->8 -->8