southpole

Button

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