Advertisement
Guest User

button

a guest
Feb 18th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.84 KB | None | 0 0
  1. local mon = peripheral.wrap("right")
  2. mon.setTextScale(1)
  3. local button={}
  4. mon.setBackgroundColor(colors.black)
  5.  
  6. function clearTable()
  7.    button = {}
  8.    mon.clear()
  9. end
  10.                
  11. function setTable(name, func, xmin, xmax, ymin, ymax, onBackgroundColor, offBackgroundColor)
  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.  
  20.    button[name]["onBackgroundColor"]  = onBackgroundColor
  21.    button[name]["offBackgroundColor"] = offBackgroundColor
  22. end
  23.  
  24. function funcName()
  25.    print("You clicked buttonText")
  26. end
  27.        
  28. function fillTable()
  29.    setTable("ButtonText", funcName, 5, 25, 4, 8, onBackgroundColor, offBackgroundColor)
  30. end    
  31.  
  32. function fill(text, color, bData)
  33.    mon.setBackgroundColor(color)
  34.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  35.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  36.    for j = bData["ymin"], bData["ymax"] do
  37.       mon.setCursorPos(bData["xmin"], j)
  38.       if j == yspot then
  39.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  40.             if k == xspot then
  41.                mon.write(text)
  42.             else
  43.                mon.write(" ")
  44.             end
  45.          end
  46.       else
  47.          for i = bData["xmin"], bData["xmax"] do
  48.             mon.write(" ")
  49.          end
  50.       end
  51.    end
  52.    mon.setBackgroundColor(colors.black)
  53. end
  54.  
  55. function screen()
  56.         local currColor
  57.         for name,data in pairs(button) do
  58.                 if data.active then
  59.                         currColor = data.onBackgroundColor or colors.lime
  60.                 else
  61.                         currColor = data.offBackgroundColor or colors.red
  62.                 end
  63.                 fill (name, currColor, data)
  64.         end
  65. end
  66.  
  67. function toggleButton(name)
  68.    button[name]["active"] = not button[name]["active"]
  69.    screen()
  70. end  
  71.  
  72. function isActive(bName)
  73.  local on = button[bName]["active"]
  74.  if on == true then
  75.  return true
  76.  else
  77.  return false
  78.  end
  79. end
  80.  
  81. function flash(name)
  82.    toggleButton(name)
  83.    screen()
  84.    sleep(0.15)
  85.    toggleButton(name)
  86.    screen()
  87. end
  88.                                              
  89. function checkxy(x, y)
  90.    for name, data in pairs(button) do
  91.       if y>=data["ymin"] and  y <= data["ymax"] then
  92.          if x>=data["xmin"] and x<= data["xmax"] then
  93.             data["func"]()
  94.             return true
  95.             --data["active"] = not data["active"]
  96.             --print(name)
  97.          end
  98.       end
  99.    end
  100.    return false
  101. end
  102.      
  103. function heading(text)
  104.    w, h = mon.getSize()
  105.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  106.    mon.write(text)
  107. end
  108.      
  109. function label(w, h, text)
  110.    mon.setCursorPos(w, h)
  111.    mon.write(text)
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement