Need a unique gift idea?
A Pastebin account makes a great Christmas gift
SHARE
TWEET

Button API Pocket

DerMarten Jul 28th, 2015 194 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
 
  1. term.setTextScale(1)
  2. term.setTextColor(colors.white)
  3. local button={}
  4. term.setBackgroundColor(colors.black)
  5.  
  6. function clearTable()
  7.    button = {}
  8.    term.clear()
  9. end
  10.                
  11. function setTable(name, func, xmin, xmax, ymin, ymax)
  12.    button[name] = {}
  13.    button[name]["func"] = func
  14.    button[name]["active"] = false
  15.    button[name]["xmin"] = xmin
  16.    button[name]["ymin"] = ymin
  17.    button[name]["xmax"] = xmax
  18.    button[name]["ymax"] = ymax
  19. end
  20.  
  21. function funcName()
  22.    print("You clicked buttonText")
  23. end
  24.        
  25. function fillTable()
  26.    setTable("ButtonText", funcName, 5, 25, 4, 8)
  27. end    
  28.  
  29. function fill(text, color, bData)
  30.    term.setBackgroundColor(color)
  31.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  32.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  33.    for j = bData["ymin"], bData["ymax"] do
  34.       term.setCursorPos(bData["xmin"], j)
  35.       if j == yspot then
  36.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  37.             if k == xspot then
  38.                term.write(text)
  39.             else
  40.                term.write(" ")
  41.             end
  42.          end
  43.       else
  44.          for i = bData["xmin"], bData["xmax"] do
  45.             term.write(" ")
  46.          end
  47.       end
  48.    end
  49.    term.setBackgroundColor(colors.black)
  50. end
  51.      
  52. function screen()
  53.    local currColor
  54.    for name,data in pairs(button) do
  55.       local on = data["active"]
  56.       if on == true then currColor = colors.lime else currColor = colors.red end
  57.       fill(name, currColor, data)
  58.    end
  59. end
  60.  
  61. function toggleButton(name)
  62.    button[name]["active"] = not button[name]["active"]
  63.    screen()
  64. end    
  65.  
  66. function flash(name)
  67.    toggleButton(name)
  68.    screen()
  69.    sleep(0.15)
  70.    toggleButton(name)
  71.    screen()
  72. end
  73.                                              
  74. function checkxy(x, y)
  75.    for name, data in pairs(button) do
  76.       if y>=data["ymin"] and  y <= data["ymax"] then
  77.          if x>=data["xmin"] and x<= data["xmax"] then
  78.             data["func"]()
  79.             return true
  80.             --data["active"] = not data["active"]
  81.             --print(name)
  82.          end
  83.       end
  84.    end
  85.    return false
  86. end
  87.      
  88. function heading(text)
  89.    w, h = term.getSize()
  90.    term.setCursorPos((w-string.len(text))/2+1, 1)
  91.    term.write(text)
  92. end
  93.      
  94. function label(w, h, text)
  95.    term.setCursorPos(w, h)
  96.    term.write(text)
  97. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top