mariodkzzzz

simpleButton

May 3rd, 2015
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.96 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.                         button:render( colors.green, colors.white )
  27.                         sleep(0.1)
  28.             return button.str
  29.         end
  30.     end
  31. end
  32.  
  33. local tPages = {
  34.     render = function( self )
  35.         term.setBackgroundColor( self.backgroundColor )
  36.         term.clear()
  37.         for k, v in pairs( self[ self.current ] ) do
  38.             v:render( self.bColor, self.bTextColor )
  39.         end
  40.     end,
  41.    
  42.     handleEvents = function( self, ... )
  43.         local event = { ... }
  44.         if event[ 1 ] == "mouse_click" or event[ 1 ] == "monitor_touch" then
  45.             local clicked = getClicked( self[ self.current ], event[ 3 ], event[ 4 ] )
  46.             if clicked then
  47.                 if clicked == ">" then
  48.                     self.current =  self.current + 1
  49.                     term.setBackgroundColor( self.backgroundColor )
  50.                     term.clear()
  51.                     self:render()
  52.                 elseif clicked == "< " then
  53.                     self.current = self.current - 1
  54.                     term.setBackgroundColor( self.backgroundColor )
  55.                     term.clear()
  56.                     self:render()
  57.                 else
  58.                     return "button_click", clicked
  59.                 end
  60.             else
  61.                 return ...
  62.             end
  63.         end
  64.         return ...
  65.     end,
  66. }
  67.  
  68. function makeButtonPages( backgroundColor, bColor, bTextColor, ... )
  69.     local maxx, maxy = term.getSize()
  70.     local tStrings = { ... }
  71.     local maxStringLen = 0
  72.     for k, v in pairs( tStrings ) do
  73.         if #v > maxStringLen then
  74.             maxStringLen = #v
  75.         end
  76.     end
  77.     if #tStrings < 1 then
  78.         error( "Not enough arguments", 2 )
  79.     end
  80.     local tButtonPages = {{}}
  81.     local x = math.ceil( maxx / 2 - maxStringLen / 2 ) - 1
  82.     local nMax = x + maxStringLen + 2
  83.     local y = 1
  84.     for k, v in pairs( tStrings ) do
  85.         if y + 1 >= maxy then
  86.             tButtonPages[ #tButtonPages ][ #tButtonPages[ #tButtonPages ] + 1 ] = newButton( maxx - 1, math.ceil( maxy / 2 ) - 1, maxx + 1,  ">" )
  87.             tButtonPages[ #tButtonPages + 1 ] = {}
  88.             tButtonPages[ #tButtonPages ][ #tButtonPages[ #tButtonPages ] + 1 ] = newButton( 1, math.ceil( maxy / 2 ) - 1, 3, "< " )
  89.             y = 1
  90.         end
  91.         tButtonPages[ #tButtonPages ][ #tButtonPages[ #tButtonPages ] + 1 ] = newButton( x, y, nMax, v )
  92.         y = y + 3
  93.     end
  94.     tButtonPages.backgroundColor = backgroundColor
  95.     tButtonPages.bColor = bColor
  96.     tButtonPages.bTextColor = bTextColor
  97.     tButtonPages.current = 1
  98.     return setmetatable( tButtonPages, { __index = tPages } )
  99. end
Advertisement
Add Comment
Please, Sign In to add comment