mistamadd001

buttons test

Jun 3rd, 2014
268
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.74 KB | None | 0 0
  1. local tWidth,tHeight = term.getSize()
  2.  
  3. function title(nameA)
  4.   paintutils.drawLine(1,1,tWidth,1,colors.lime)
  5.   paintutils.drawLine(1,2,tWidth,2,colors.lime)
  6.   paintutils.drawLine(1,3,tWidth,3,colors.lime)
  7.   paintutils.drawLine(1,4,tWidth,4,colors.lime)
  8.   paintutils.drawLine(1,5,tWidth,5,colors.lime)
  9.   paintutils.drawLine(1,6,tWidth,6,colors.lime)
  10.   term.setTextColor(colors.black)
  11.   term.setCursorPos(4,2)
  12.   write(nameA)
  13. end
  14.  
  15. function Button(label,line,alignment,bgColor,tColor)
  16.   local button = {}
  17.   button.label     = label   or " "
  18.   button.bgColor   = bgColor or colors.lightGray
  19.   button.tColor    = tColor  or colors.cyan
  20.   button.line      = line    or 1
  21.   button.alignment = alignment
  22.   button.width     = 12
  23.   if alignment == "left" then
  24.     button.startXpos = math.floor(1+(#label/2))
  25.   elseif alignment == "right" then
  26.     button.startXpos = math.floor(43-(#label/2))
  27.   end
  28.  
  29.   function button.draw()
  30.     --correct for basic terminals
  31.     if not term.isColor() then button.bgColor = colors.white end
  32.     if not term.isColor() then button.tColor  = colors.black end
  33.     --draw button background
  34.     if     button.alignment == "left"  then
  35.       term.setCursorPos(2, button.line)
  36.       term.setBackgroundColor(button.bgColor)
  37.       write(string.rep(" ",button.width))
  38.     elseif button.alignment == "right" then
  39.       term.setCursorPos(37, button.line)
  40.       term.setBackgroundColor(button.bgColor)
  41.       write(string.rep(" ",button.width))
  42.     end
  43.     --draw button label
  44.     term.setCursorPos(button.startXpos,button.line)
  45.     term.setTextColor(button.tColor)
  46.     write(label)
  47.     --reset colours to default
  48.     term.setBackgroundColor(colors.black)
  49.     term.setTextColor(colors.white)
  50.   end
  51.   return button
  52.  
  53.   function button.clicked(column,row)
  54.     return (column >= button.line and column < button.startXpos + button.width and row == button.line)
  55.   end
  56. end
  57.  
  58. buttons = {}
  59. buttons.balance  = Button("Balance",  9,  "left",  colors.orange,colors.purple)
  60. buttons.withdraw = Button("Withdraw", 9,  "right", colors.orange,colors.purple)
  61. buttons.deposit  = Button("Deposit",  11,  "left",  colors.orange,colors.purple)
  62. buttons.transfer = Button("Transfer", 11,  "right", colors.orange,colors.purple)
  63. buttons.quit     = Button("Quit",     15, "right", colors.red,colors.lime)
  64.  
  65.  
  66.  
  67. term.clear()
  68. title(",-.-.         |    ||   |                    \n   | | |,---.,---|,---||---|,---..   .,---.,---.\n   | | |,---||   ||   ||   ||   ||   | ---.|---'\n   | | ||___||___||___||   ||___||___||___||___|")
  69.  
  70. for key,button in pairs(buttons) do
  71.   button.draw()
  72. end
  73.  
  74. while true do
  75.   event ={os.pullEvent("monitor_touch")}
  76.   for key,button in pairs(buttons)
  77.     if button.clicked(event[3],event[4]) then -- column,row
  78.       break
  79. end
Advertisement
Add Comment
Please, Sign In to add comment