RandomShovel

[CC] Button API ( Credit to Direwolf20! )

Mar 2nd, 2014
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.45 KB | None | 0 0
  1. --[[
  2. Created by DireWolf20
  3. Cleaned up by RandomShovel
  4. There may be bugs!
  5. ]]--
  6.  
  7. --[[  Config  ]]--
  8.  
  9. monLocat = "top"
  10.  
  11.  
  12. --[[  Tables  ]]--
  13.  
  14. local button={}
  15. local buttonHist={}
  16.  
  17.  
  18. --[[  Variables  ]]--
  19.  
  20. local mon = peripheral.wrap(monLocat)
  21.  
  22.  
  23. --[[  Fix Monitor  ]]--
  24.  
  25. mon.setTextScale(1)
  26. mon.setTextColor(colors.white)
  27. mon.setBackgroundColor(colors.black)
  28.  
  29.  
  30. --[[  Functions  ]]--
  31.  
  32. function clearTable()
  33.  button = {}
  34.  mon.clear()
  35. end
  36.  
  37. function setTable(name, func, xmin, xmax, ymin, ymax)
  38.  button[name] = {}
  39.  button[name]["func"] = func
  40.  button[name]["active"] = false
  41.  button[name]["xmin"] = xmin
  42.  button[name]["ymin"] = ymin
  43.  button[name]["xmax"] = xmax
  44.  button[name]["ymax"] = ymax
  45. end
  46.  
  47. --  Debugging purposes
  48.  
  49. function funcName()
  50.  print("You clicked buttonText")
  51. end
  52.  
  53. function fillTable()
  54.  setTable("ButtonText", funcName, 5, 25, 4, 8)
  55. end    
  56.  
  57.  
  58. function fill(text, color, bData)
  59.  mon.setBackgroundColor(color)
  60.  local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2) -- Averages y positions for text
  61.  local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1  -- Averages x positions for text
  62.  for j = bData["ymin"], bData["ymax"] do  -- Fills the button
  63.   mon.setCursorPos(bData["xmin"], j)
  64.   if j == yspot then  -- If current position is where text's Y level should be
  65.    for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  66.     if k == xspot then  -- If current position is where text's starting point should be
  67.      mon.write(text)
  68.     else -- If not at text's positions, write space, or "Button"
  69.      mon.write(" ")
  70.     end
  71.    end
  72.   else
  73.    for i = bData["xmin"], bData["xmax"] do
  74.     mon.write(" ")
  75.    end
  76.   end
  77.  end
  78.  mon.setBackgroundColor(colors.black)
  79. end
  80.  
  81. function screen()
  82.  local currColor
  83.  for name,data in pairs(button) do  -- Gathers data from button in pairs
  84.   local on = data["active"]
  85.   if on == true then
  86.    currColor = colors.lime  -- If button is active, change color to lime
  87.   else
  88.    currColor = colors.red  -- If button is inactive, change color to red
  89.   end
  90.   fill(name, currColor, data)  -- Populate screen
  91.  end
  92. end
  93.  
  94. function toggleButton(name)
  95.  button[name]["active"] = not button[name]["active"]  -- Inverts current button
  96.  screen()
  97. end
  98.  
  99. function isActive(bName)
  100.  local on = button[bName]["active"]
  101.  if on == true then
  102.   return true
  103.  else
  104.   return false
  105.  end
  106. end
  107.  
  108. function toggleFlash(name)
  109.  if #buttonHist > 0 then
  110.   if name == buttonHist[1] then
  111.    table.remove(buttonHist, 1)
  112.   else
  113.    toggleButton(buttonHist[1])
  114.    table.remove(buttonHist, 1)
  115.    table.insert(buttonHist, name)
  116.   end
  117.  else
  118.   table.insert(buttonHist, name)
  119.  end
  120.  toggleButton(name)
  121. end
  122.  
  123. function flash(name)
  124.  toggleButton(name)
  125.  screen()
  126.  sleep(0.15)
  127.  toggleButton(name)
  128.  screen()
  129. end
  130.  
  131. function checkxy(x, y)
  132.  for name, data in pairs(button) do  -- Grabing data from button in pairs
  133.   if y>=data["ymin"] and  y <= data["ymax"] then  -- If y is in button's y range
  134.    if x>=data["xmin"] and x<= data["xmax"] then  -- If x is in button's x range
  135.     data["func"]()  -- Run function
  136.     return true
  137.     -- Debugging
  138.     --data["active"] = not data["active"]
  139.     --print(name)
  140.    end
  141.   end
  142.  end
  143.  return false
  144. end
  145.  
  146. function heading(text)
  147.  w, h = mon.getSize()  -- Get monitor size
  148.  mon.setCursorPos((w-string.len(text))/2+1, 1)  -- Positions header
  149.  mon.write(text)
  150. end
  151.  
  152. function label(w, h, text)
  153.  mon.setCursorPos(w, h)
  154.  mon.write(text)
  155. end
Advertisement
Add Comment
Please, Sign In to add comment