Advertisement
cragrim

Direwolf20mystcraft-button

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