Advertisement
King0fGamesYami

simpleButton

Jan 8th, 2015
642
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. local tButton = {
  2.     isClicked = function( self, x, y )
  3.         return true and self.x <= x and self.maxx > x and self.y <= y and self.y + 3 > y or false
  4.     end,
  5.     render = function( self, bColor, tColor )
  6.         term.setBackgroundColor( bColor )
  7.         term.setTextColor( tColor )
  8.         for i = 0, 2 do
  9.             term.setCursorPos( self.x, self.y + i )
  10.             term.write( string.rep( " ", self.maxx - self.x ) )
  11.         end
  12.         term.setCursorPos( math.ceil( (self.maxx + self.x - #self.str)/ 2 ), self.y + 1 )
  13.         term.write( self.str )
  14.     end,
  15. }
  16.  
  17. local function newButton( x, y, maxx, str )
  18.     local button = { x = x, y = y, maxx = maxx, str = str }
  19.     setmetatable( button, { __index = tButton } )
  20.     return button
  21. end
  22.  
  23. local function getClicked( tButtons, x, y )
  24.     for _, button in ipairs( tButtons ) do
  25.         if button:isClicked( x, y ) then
  26.             return button.str
  27.         end
  28.     end
  29. end
  30.  
  31. local tPages = {
  32.     render = function( self )
  33.         term.setBackgroundColor( self.backgroundColor )
  34.         term.clear()
  35.         for k, v in pairs( self[ self.current ] ) do
  36.             v:render( self.bColor, self.bTextColor )
  37.         end
  38.     end,
  39.    
  40.     handleEvents = function( self, ... )
  41.         local event = { ... }
  42.         if event[ 1 ] == "mouse_click" or event[ 1 ] == "monitor_touch" then
  43.             local clicked = getClicked( self[ self.current ], event[ 3 ], event[ 4 ] )
  44.             if clicked then
  45.                 if clicked == ">" then
  46.                     self.current =  self.current + 1
  47.                     term.setBackgroundColor( self.backgroundColor )
  48.                     term.clear()
  49.                     self:render()
  50.                 elseif clicked == "< " then
  51.                     self.current = self.current - 1
  52.                     term.setBackgroundColor( self.backgroundColor )
  53.                     term.clear()
  54.                     self:render()
  55.                 else
  56.                     return "button_click", clicked
  57.                 end
  58.             else
  59.                 return ...
  60.             end
  61.         end
  62.         return ...
  63.     end,
  64. }
  65.  
  66. function makeButtonPages( backgroundColor, bColor, bTextColor, ... )
  67.     local maxx, maxy = term.getSize()
  68.     local tStrings = { ... }
  69.     local maxStringLen = 0
  70.     for k, v in pairs( tStrings ) do
  71.         if #v > maxStringLen then
  72.             maxStringLen = #v
  73.         end
  74.     end
  75.     if #tStrings < 1 then
  76.         error( "Not enough arguments", 2 )
  77.     end
  78.     local tButtonPages = {{}}
  79.     local x = math.ceil( maxx / 2 - maxStringLen / 2 ) - 1
  80.     local nMax = x + maxStringLen + 2
  81.     local y = 3
  82.     for k, v in pairs( tStrings ) do
  83.         if y + 3 >= maxy then
  84.             tButtonPages[ #tButtonPages ][ #tButtonPages[ #tButtonPages ] + 1 ] = newButton( maxx - 1, math.ceil( maxy / 2 ) - 1, maxx + 1,  ">" )
  85.             tButtonPages[ #tButtonPages + 1 ] = {}
  86.             tButtonPages[ #tButtonPages ][ #tButtonPages[ #tButtonPages ] + 1 ] = newButton( 1, math.ceil( maxy / 2 ) - 1, 3, "< " )
  87.             y = 3
  88.         end
  89.         tButtonPages[ #tButtonPages ][ #tButtonPages[ #tButtonPages ] + 1 ] = newButton( x, y, nMax, v )
  90.         y = y + 4
  91.     end
  92.     tButtonPages.backgroundColor = backgroundColor
  93.     tButtonPages.bColor = bColor
  94.     tButtonPages.bTextColor = bTextColor
  95.     tButtonPages.current = 1
  96.     return setmetatable( tButtonPages, { __index = tPages } )
  97. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement