Guest User

Untitled

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