Advertisement
1ng0

SDRC - Button - Monitor

Dec 9th, 2015
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.26 KB | None | 0 0
  1. -- Small Draconic Reactor Control - --
  2. -- !!! Save this file as SDRC !!! ---
  3. -- ######################################## --
  4.  
  5. --[[I have added a 2nd parameter to the setTable function be sure to follow the example]]--
  6. --[[If no 2nd parameter is needed leave blank quotation marks, ie ""]]--
  7. --[[edit]]--
  8. local mon = peripheral.find("monitor")
  9. --local mon = term]]-- If you want to use this for pocket computers
  10. mon.setTextColor(colors.white)
  11. mon.setBackgroundColor(colors.black)
  12. mon.setTextScale(1)
  13.  
  14. --[[Examples]]--
  15. --function funcName()
  16. --  print("You clicked buttonText")
  17. --end
  18. --function fillTable()
  19. -- setTable("ButtonText", funcName, "param1", "param2", 5, 25, 4, 8)
  20. --end
  21.  
  22. --[[Don't edit anything below this]]--
  23. local button={}
  24.  
  25. function clearTable()
  26.    button = {}
  27. end
  28.      
  29. function setTable(name, func, param1, param2, active, xmin, xmax, ymin, ymax)
  30.    button[name] = {}
  31.    button[name]["func"] = func
  32.    button[name]["active"] = active
  33.    if active == nil then
  34.      active = false
  35.    end
  36.    button[name]["param1"] = param1
  37.    button[name]["param2"] = param2
  38.    button[name]["xmin"] = xmin
  39.    button[name]["ymin"] = ymin
  40.    button[name]["xmax"] = xmax
  41.    button[name]["ymax"] = ymax
  42. end
  43.  
  44. function remTable(name)
  45.   button[name] = {}
  46.   button[name]["xmin"] = 0
  47.   button[name]["ymin"] = 0
  48.   button[name]["xmax"] = 0
  49.   button[name]["ymax"] = 0
  50. end
  51.    
  52.  
  53. function fill(text, color, bData)
  54.    mon.setBackgroundColor(color)
  55.    local yspot = math.floor((bData["ymin"] + bData["ymax"]) /2)
  56.    local xspot = math.floor((bData["xmax"] - bData["xmin"] - string.len(text)) /2) +1
  57.    for j = bData["ymin"], bData["ymax"] do
  58.       mon.setCursorPos(bData["xmin"], j)
  59.       if j == yspot then
  60.          for k = 0, bData["xmax"] - bData["xmin"] - string.len(text) +1 do
  61.             if k == xspot then
  62.                mon.write(text)
  63.             else
  64.                mon.write(" ")
  65.             end
  66.          end
  67.       else
  68.          for i = bData["xmin"], bData["xmax"] do
  69.             mon.write(" ")
  70.          end
  71.       end
  72.    end
  73.    mon.setBackgroundColor(colors.black)
  74. end
  75.      
  76. function screen()
  77.    local currColor
  78.    for name,data in pairs(button) do
  79.       local on = data["active"]
  80.       if on == true then currColor = colors.lime else currColor = colors.red end
  81.       fill(name, currColor, data)
  82.    end
  83. end
  84.  
  85. function toggleButton(name)
  86.    button[name]["active"] = not button[name]["active"]
  87.    screen()
  88. end    
  89.  
  90. function flash(name)
  91.    toggleButton(name)
  92.    screen()
  93.    sleep(0.15)
  94.    toggleButton(name)
  95.    screen()
  96. end
  97.                                              
  98. function checkxy(x, y)
  99.    for name, data in pairs(button) do
  100.       if y>=data["ymin"] and  y <= data["ymax"] then
  101.          if x>=data["xmin"] and x<= data["xmax"] then
  102.             if data["param1"] == "" then
  103.               data["func"]()
  104.             else
  105.               data["func"](data["param1"], data["param2"])
  106.             end
  107.         return true
  108.          end
  109.       end
  110.    end
  111.    return false
  112. end
  113.      
  114. function heading(text)
  115.    w, h = mon.getSize()
  116.    mon.setCursorPos((w-string.len(text))/2+1, 1)
  117.    mon.write(text)
  118. end
  119.      
  120. function label(w, h, text)
  121.    mon.setCursorPos(w, h)
  122.    mon.write(text)
  123. end
  124.  
  125. -- ######################################## --
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement