Advertisement
Xenogami

button

Feb 3rd, 2015
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.26 KB | None | 0 0
  1. function findDev (dType)
  2. local d
  3. for _,d in pairs(peripheral.getNames()) do
  4. if (peripheral.getType(d) == dType) then
  5. return peripheral.wrap(d)
  6. end
  7. end
  8. return nil, dType..": not found"
  9. end
  10.  
  11. m=findDev("monitor")
  12.  
  13. local tButton = {
  14. isClicked = function( self, x, y )
  15. return true and self.x <= x and self.maxx > x and self.y <= y and self.y + 3 > y or false
  16. end,
  17. render = function( self, bColor, tColor )
  18. m.setBackgroundColor( bColor )
  19. m.setTextColor( tColor )
  20. for i = 0, 2 do
  21. m.setCursorPos( self.x, self.y + i )
  22. m.write( string.rep( " ", self.maxx - self.x ) )
  23. end
  24. m.setCursorPos( math.ceil( (self.maxx + self.x - #self.str)/ 2 ), self.y + 1 )
  25. m.write( self.str )
  26. end,
  27. }
  28.  
  29. local function newButton( x, y, maxx, str )
  30. local button = { x = x, y = y, maxx = maxx, str = str }
  31. setmetatable( button, { __index = tButton } )
  32. return button
  33. end
  34.  
  35. local tPages = {
  36. render = function( self )
  37. m.setBackgroundColor( self.backgroundColor )
  38. m.clear()
  39. for k, v in pairs( self[ self.current ] ) do
  40. v:render( self.bColor, self.bTextColor )
  41. end
  42. end,
  43.  
  44. }
  45.  
  46. function makeButtonPages( backgroundColor, bColor, bTextColor, ... )
  47. local maxx, maxy = m.getSize()
  48. local tStrings = { ... }
  49. local maxStringLen = 0
  50. for k, v in pairs( tStrings ) do
  51. if #v > maxStringLen then
  52. maxStringLen = #v
  53. end
  54. end
  55. if #tStrings < 1 then
  56. error( "Not enough arguments", 2 )
  57. end
  58. local tButtonPages = {{}}
  59. local x = math.ceil( maxx / 2 - maxStringLen / 2 ) - 1
  60. local nMax = x + maxStringLen + 2
  61. local y = 3
  62. for k, v in pairs( tStrings ) do
  63. if y + 3 >= maxy then
  64. tButtonPages[ #tButtonPages ][ #tButtonPages[ #tButtonPages ] + 1 ] = newButton( maxx - 1, math.ceil( maxy / 2 ) - 1, maxx + 1, ">" )
  65. tButtonPages[ #tButtonPages + 1 ] = {}
  66. tButtonPages[ #tButtonPages ][ #tButtonPages[ #tButtonPages ] + 1 ] = newButton( 1, math.ceil( maxy / 2 ) - 1, 3, "< " )
  67. y = 3
  68. end
  69. tButtonPages[ #tButtonPages ][ #tButtonPages[ #tButtonPages ] + 1 ] = newButton( x, y, nMax, v )
  70. y = y + 4
  71. end
  72. tButtonPages.backgroundColor = backgroundColor
  73. tButtonPages.bColor = bColor
  74. tButtonPages.bTextColor = bTextColor
  75. tButtonPages.current = 1
  76. return setmetatable( tButtonPages, { __index = tPages } )
  77. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement