masterdisasterHD

buttonApi

Apr 18th, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.16 KB | None | 0 0
  1. local buttons = {}
  2.  
  3. local function clear()
  4.  term.clear()
  5.  term.setCursorPos(1,1)
  6. end
  7.  
  8. function reset()
  9.  buttons = {}
  10. end
  11.  
  12. function add(text, startX, startY, endX, endY, tStartX, tStartY, bColor, bcColor, btColor, func)
  13.  --local buttons[#buttons+1] = {}
  14.  local sTemp = {}
  15.  
  16.  sTemp["text"] = text
  17.  sTemp["startX"] = startX
  18.  sTemp["startY"] = startY
  19.  sTemp["endX"] = endX
  20.  sTemp["endY"] = endY
  21.  sTemp["tStartX"] = tStartX
  22.  sTemp["tStartY"] = tStartY
  23.  sTemp["bColor"] = bColor
  24.  sTemp["bcColor"] = bcColor
  25.  sTemp["btColor"] = btColor
  26.  sTemp["func"] = func
  27.  sTemp["isActive"] = false
  28.  
  29.  buttons[#buttons+1] = sTemp
  30. end
  31.  
  32. function addFromTable(tbl)
  33.  if type(tbl) == "table" then
  34.   for i,v in ipairs(tbl) do
  35.    add(unpack(v))
  36.   end
  37.  end
  38. end
  39.  
  40. function draw()
  41.  term.setBackgroundColor(colors.white)
  42.  clear()
  43.  for i=1, #buttons do
  44.   v = buttons[i]
  45.   if v["isActive"] == true then
  46.    term.setBackgroundColor(v["bcColor"])
  47.   else
  48.    term.setBackgroundColor(v["bColor"])
  49.   end
  50.   term.setTextColor(v["btColor"])
  51.   for yPos = v["startY"], v["endY"] do
  52.    for xPos = v["startX"], v["endX"] do
  53.     term.setCursorPos(xPos, yPos)
  54.     write(" ")
  55.    end
  56.   end
  57.   term.setCursorPos(v["tStartX"], v["tStartY"])
  58.   write(v["text"])
  59.  end
  60. end
  61.  
  62. local function pulse(btn)
  63.  term.setBackgroundColor(btn["bcColor"])
  64.  for yPos = btn["startY"], btn["endY"] do
  65.   for xPos = btn["startX"], btn["endX"] do
  66.    term.setCursorPos(xPos,yPos)
  67.    write(" ")
  68.   end
  69.  end
  70.  term.setTextColor(btn["btColor"])
  71.  term.setCursorPos(btn["tStartX"], btn["tStartY"])
  72.  write(btn["text"])
  73.  
  74.  sleep(.1)
  75.  term.setBackgroundColor(btn["bColor"])
  76.  for yPos = btn["startY"], btn["endY"] do
  77.   for xPos = btn["startX"], btn["endX"] do
  78.    term.setCursorPos(xPos,yPos)
  79.    write(" ")
  80.   end
  81.  end
  82.  term.setTextColor(btn["btColor"])
  83.  term.setCursorPos(btn["tStartX"], btn["tStartY"])
  84.  write(btn["text"])
  85.  sleep(.1)
  86. end
  87.  
  88. function check(x,y)
  89.  for i,v in ipairs(buttons) do
  90.   if x >= v["startX"] and x <= v["endX"] and y >= v["startY"] and y <=v["endY"] then
  91.    pulse(v)
  92.    term.setBackgroundColor(colors.black)
  93.    term.setTextColor(colors.white)
  94.    clear()
  95.    v["func"]()
  96.   end
  97.  end
  98. end
Add Comment
Please, Sign In to add comment