MT
Size: a a a
MT
MT
MT
MT
S
-- buttons.lua
local buttons = {}
function createBtn(x,y,w,h,txt, handler)
html = [[<div style="position:absolute; top:]]..y..[[; left:]]..x..[[vw; width:]]..w..[[vw; height: ]]..h..[[vh">]]..text..[[</div>]]
screen.setHTML(x,y,html)
buttons[buttons + 1] = {x=x,y=y,x1=w+x,y1=h+y, handler = handler}
end
for _, but in pairs(buttons) do
if x<but.x and x<but.x1 and y > but.y and but.y1 then
but.handler()
end
end
return { createBtn = createBtn }
-- other file
local createBtn = require "buttons".createBtn
createBtn(1,1,2,2, "Hello", function() end)
createBtn(5,4,2,2, "Hello", function() end)-- str = "Hello %item%!"
-- tfill(str, {item = "world"})
function tfill(tmpl, tbl)
local function replace(c)
return tbl[c] or '%' .. c .. '%'
end
return tmpl:gsub('%%(.-)%%', replace)
end
local templates = {}
templates.button = [[
<div style="position:absolute; top:%y%; left:%x%vw; width:%w%vw; height: %h%vh">%text%</div>]]
local buttons = {}
function createButton(info)
info.html = tfill(templates.button, info)
table.insert(buttons, info)
end
createButton{x=10, y=20, w=100, h=200, text="Yo" }ШТ
ipairsЧаще циклом for со счетчиком польдузюсь
S
for i, v in ipairs(t) do ... endT
ipairsЧаще циклом for со счетчиком польдузюсь
S
for i = 1, #tbl do.T
T
for i = 1, #tbl do.T
S
#tbl. Чтобы ходило по дыркам, надо как-то определять максимальное значение массива.S
pairs. Он пропустит дырки и пройдёт по всем не-nil элементам.T
#tbl. Чтобы ходило по дыркам, надо как-то определять максимальное значение массива.