Compare commits

..

2 Commits

Author SHA1 Message Date
137a390b65 Golf tostring() 2022-12-22 18:51:05 -08:00
58bf1d70bf Golf keyboard 2022-12-22 18:49:03 -08:00

View File

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