theguywhojoojes

Button API

Apr 4th, 2020
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. --Button API with pixels instead of squares
  2. --because my version of computercraft doesn't have them
  3.  
  4. --table with all the buttons
  5. buttons={}
  6.  
  7. --makes a table with all the button information
  8. --and insert it into main buttons table
  9. function makeNewButton(name,text,IX,IY,FX,FY)
  10.  --(Initial x,y and final x,y as button coordinates)
  11.  local z={IX=IX,IY=IY,FX=FX,FY=FY,text=text,state=false}
  12.  buttons[name]=z
  13. end
  14.  
  15. --actually draws the button
  16. function renderButton(button,color,monitor_name)
  17.  
  18.  --sets color (i'm going to add the other colors lateer)
  19.  
  20.  if color=="white" then
  21.   color=1
  22.  elseif color=="green" then
  23.   color=32
  24.  elseif color=="gray" then
  25.   color=128
  26.  elseif color=="blue" then
  27.   color=2048
  28.  elseif color=="red" then
  29.   color=16384
  30.  end
  31.  
  32.  --checks if there any monitors are called
  33.  if monitor_name~=nil then
  34.   term.redirect(monitor_name)
  35.  end
  36.  
  37.  --draws button based on its coordinates
  38.  local b = buttons[button]
  39.  for x=b["IX"],b["FX"] do
  40.   for y=b["IY"],b["FY"] do
  41.    paintutils.drawPixel(x,y,color)
  42.   end
  43.  end
  44.  
  45.  --draws the text in the middle of the button with black to add contrast
  46.  term.setCursorPos(((b["FX"]+b["IX"])/2)-(#b["text"]/2),(b["FY"]+b["IY"])/2)
  47.  term.setTextColor(colors.black)
  48.  term.write(b["text"])
  49.  
  50.  --restore the cursor position and set cursor in the computer again
  51.  term.setCursorPos(1,1)
  52.  term.restore()
  53.  
  54. end
  55.  
  56. --checks if the button is colliding with set coordinates
  57. function isButtonColliding(button,target_x,target_y)
  58.  
  59.  local b = buttons[button]
  60.  
  61.  --collision states x and y
  62.  colx = (target_x>=b["IX"] and target_x<=b["FX"])
  63.  coly = (target_y>=b["IY"] and target_y<=b["FY"])
  64.  
  65.  return (colx and coly)
  66.  
  67. end
Add Comment
Please, Sign In to add comment