local tButton = {
isClicked = function( self, x, y )
return true and self.x <= x and self.maxx > x and self.y <= y and self.y + 3 > y or false
end,
render = function( self, bColor, tColor )
term.setBackgroundColor( bColor )
term.setTextColor( tColor )
for i = 0, 2 do
term.setCursorPos( self.x, self.y + i )
term.write( string.rep( " ", self.maxx - self.x ) )
end
term.setCursorPos( math.ceil( (self.maxx + self.x - #self.str)/ 2 ), self.y + 1 )
term.write( self.str )
end,
}
local function newButton( x, y, maxx, str )
local button = { x = x, y = y, maxx = maxx, str = str }
setmetatable( button, { __index = tButton } )
return button
end
local function getClicked( tButtons, x, y )
for _, button in ipairs( tButtons ) do
if button:isClicked( x, y ) then
return button.str
end
end
end
local tPages = {
render = function( self )
term.setBackgroundColor( self.backgroundColor )
term.clear()
for k, v in pairs( self[ self.current ] ) do
v:render( self.bColor, self.bTextColor )
end
end,
handleEvents = function( self, ... )
local event = { ... }
if event[ 1 ] == "mouse_click" then
local clicked = getClicked( self[ self.current ], event[ 3 ], event[ 4 ] )
if clicked then
if clicked == ">" then
self.current = self.current + 1
term.setBackgroundColor( self.backgroundColor )
term.clear()
self:render()
elseif clicked == "<" then
self.current = self.current - 1
term.setBackgroundColor( self.backgroundColor )
term.clear()
self:render()
else
return "button_click", clicked
end
else
return ...
end
end
return ...
end,
}
function makeButtonPages( backgroundColor, bColor, bTextColor, ... )
local maxx, maxy = term.getSize()
local tStrings = { ... }
local maxStringLen = 0
for k, v in pairs( tStrings ) do
if #v > maxStringLen then
maxStringLen = #v
end
end
if #tStrings < 1 then
error( "Not enough arguments", 2 )
end
local tButtonPages = {{}}
local x = math.ceil( maxx / 2 - maxStringLen / 2 ) - 1
local nMax = x + maxStringLen + 2
local y = 3
for k, v in pairs( tStrings ) do
if y + 3 >= maxy then
tButtonPages[ #tButtonPages + 1 ] = {}
end
tButtonPages[ #tButtonPages ][ #tButtonPages[ #tButtonPages ] + 1 ] = newButton( x, y, nMax, v )
y = y + 4
end
for k, v in ipairs( tButtonPages ) do
if k ~= 1 then
v[ #v + 1 ] = newButton( 1, 2, math.ceil( maxy / 2 + 1 ), "<" )
end
if k ~= #tButtonPages then
v[ #v + 1 ] = newButton( maxx - 2, maxx, math.ceil( maxy / 2 ), ">" )
end
end
tButtonPages.backgroundColor = backgroundColor
tButtonPages.bColor = bColor
tButtonPages.bTextColor = bTextColor
tButtonPages.current = 1
return setmetatable( tButtonPages, { __index = tPages } )
end
local menu = makeButtonPages( colors.lightBlue, colors.blue, colors.green, "Click Me!", "Test", "Hello", "This one is really long", "Page_2" )
menu:render()
while true do
local event = { menu:handleEvents( os.pullEvent() ) }
if event[ 1 ] == "button_click" then
term.clear()
term.setCursorPos( 1, 1 )
print( event[ 2 ] )
error()
end
end
[string "term"]:7: bad argument #1 to '?' (number expected, got nil)
AFAIK, this doesn't give me a reference I should be looking at, other than this is me passing a nil to term.setCursorPos. I wish I knew which line, since I could trace from there, but there's not much I can do. (Btw, is there any particular reason it doesn't give me the line number? If there isn't, could I suggest this be changed?)
Edit: Updated code, but now pages aren't working. More details in last post.