Advertisement
Etsukazu_Aoi

api button beta

Jul 1st, 2015
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.79 KB | None | 0 0
  1. --1.0 added centerTextXY and centerTextX for api by overwol
  2. --2.0 added button colors background custom for click This button or not clicked
  3. --2.1 recod api for optimizing api
  4.  
  5.  function centerTextXY(text,htext)
  6.   local w,h = term.getSize()
  7.   if htext == nil then
  8.     htext = 0
  9.   end
  10.   local htext = tonumber(htext)
  11.   term.setCursorPos(math.floor(w/2-text:len()/2+.5), math.floor(h/2+0.5+htext))
  12.   io.write(text)
  13. end
  14.  
  15. function centerTextX(text,htext)
  16.   local w,h = term.getSize()
  17.   term.setCursorPos(math.floor(w/2-text:len()/2+.5), htext)
  18.   io.write(text)
  19. end
  20.  
  21. function heading(text)
  22.    w, h = term.getSize()
  23.    term.setCursorPos((w-string.len(text))/2+1, 1)
  24.    term.write(text)
  25. end
  26.  
  27. function setTable(name, textl1, textl2, func, funcv, xmin, ymin, xmax, ymax, textColor, backgroundColorOn,  backgroundColorOff, active)
  28.    button[name] = {}
  29.    button[name]["textl1"] = textl1
  30.    button[name]["textl2"] = textl2
  31.    button[name]["func"] = func
  32.    button[name]["funcv"] = funcv
  33.    if active == nil then
  34.       button[name]["active"] = false
  35.    else
  36.       button[name]["active"] = active
  37.    end
  38.    button[name]["xmin"] = xmin
  39.    button[name]["ymin"] = ymin
  40.    button[name]["xmax"] = xmax
  41.    button[name]["ymax"] = ymax
  42.    button[name]["textColor"] = textColor
  43.    button[name]["backgroundColorOn"] = backgroundColorOn
  44.    button[name]["backgroundColorOff"] = backgroundColorOff
  45. end
  46.  
  47. function screen()
  48.    local currColor
  49.    for name,data in pairs(button) do
  50.       term.setTextColor(data["textColor"] or colors.white)
  51.       local on = data["active"]
  52.       if on == false then
  53.          currColor = data["backgroundColorOff"] or colors.red
  54.       elseif on == true then
  55.          currColor = data["backgroundColorOn"] or colors.lime
  56.       end
  57.       fill(name, currColor, data)
  58.       end
  59. end
  60.  
  61. function fill(name, color, bData)
  62.    term.setBackgroundColor(color)
  63.    local text1 = bData["textl1"]
  64.    local text2 = bData["textl2"]
  65.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  66.    local yspot2 = math.floor((bData["ymin"] + bData["ymax"]) /2+1)
  67.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text1)) /2) +1
  68.    local xspot2 = math.floor((bData["xmax"] - bData["xmin"] - string.len(text2)) /2) +1
  69.    for j = bData["ymin"], bData["ymax"] do
  70.       term.setCursorPos(bData["xmin"], j)
  71.       if j == yspot then
  72.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text1) +1 do
  73.             if k == xspot then
  74.                term.write(text1)
  75.             else
  76.                term.write(" ")
  77.             end
  78.          end
  79.       elseif j == yspot2 then
  80.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text2) +1 do
  81.             if k == xspot then
  82.                term.write(text2)
  83.             else
  84.                term.write(" ")
  85.             end
  86.          end
  87.        else
  88.          for i = bData["xmin"], bData["xmax"] do
  89.             term.write(" ")
  90.          end
  91.       end
  92.    end
  93.    term.setBackgroundColor(colors.black)
  94. end
  95.  
  96. function toggleButton(name)
  97.    button[name]["active"] = not button[name]["active"]
  98.    screen()
  99. end
  100.  
  101. function flash(name)
  102.    toggleButton(name)
  103.    screen()
  104.    sleep(0.15)
  105.    toggleButton(name)
  106.    screen()
  107. end
  108.  
  109. function checkxy(x, y)
  110.    for name, data in pairs(button) do
  111.       if y>=data["ymin"] and  y <= data["ymax"] then
  112.          if x>=data["xmin"] and x<= data["xmax"] then
  113.             funcvar = data["funcv"]
  114.             if funcvar == "" then
  115.                 data["func"]()
  116.             else
  117.                 data["func"](data["funcv"])
  118.             end
  119.             return true
  120.             --data["active"] = not data["active"]
  121.             --print(name)
  122.          end
  123.       end
  124.    end
  125.    return false
  126. end
  127.  
  128. function getClick()
  129.         local event,side,x,y = os.pullEvent("monitor_touch")
  130.         checkxy(x,y)
  131. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement