Advertisement
caramba2654

newButtonAPI v2.1 by arshia111

Apr 14th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.02 KB | None | 0 0
  1. -- Original by direwolf20
  2. -- Edited by arshia111
  3. -- Thanks to GufNZ for helping me out
  4.  
  5. -- VERSIONS
  6. -- v1.0, July 16, 2013 - Initial Release
  7. -- v1.1, April 13, 2014 - A few improvements
  8. -- v2.0, April 14, 2014 - Button rendering using windows
  9. -- v2.1, April 15, 2014 - Fix in the text color rendering
  10.  
  11. local monSide = "back"
  12. local backgroundColor = colors.blue
  13. local textColor = colors.white
  14. local textScale = 1
  15.    
  16. local mon = peripheral.wrap(monSide)
  17. mon.setTextScale(textScale)
  18. mon.setTextColor(textColor)
  19. mon.setBackgroundColor(backgroundColor)
  20.    
  21. local button={}
  22.      
  23. -- If you want the default
  24. -- "green when on, red when off, white text" button
  25. -- just set tcolor, bcolorOn and bcolorOff to nil
  26. function setTable(name, func, arg, parent, x, y, width, height, tcolor, bcolorOn, bcolorOff)
  27.   button[name] = {}
  28.   button[name]["window"] = window.create(parent, x, y, width, height)
  29.   button[name]["func"] = func
  30.   button[name]["arg"] = arg
  31.   button[name]["active"] = false
  32.   button[name]["x"] = x
  33.   button[name]["y"] = y
  34.   button[name]["width"] = width
  35.   button[name]["height"] = height
  36.   button[name]["tcolor"] = tcolor
  37.   button[name]["bcolorOn"] = bcolorOn
  38.   button[name]["bcolorOff"] = bcolorOff
  39. end
  40.  
  41. function clearTable()
  42.   button = {}
  43.   mon.clear()
  44. end
  45.  
  46. function getActive(name)
  47.   return button[name]["active"]
  48. end
  49.  
  50. function screen()
  51.   for name,data in pairs(button) do  
  52.          
  53.     local bgColor
  54.     if data["active"] then
  55.       bgColor = data["bcolorOn"] or colors.lime
  56.     else
  57.       bgColor = data["bcolorOff"] or colors.red
  58.     end
  59.    
  60.     local tColor = data["tcolor"] or colors.white
  61.    
  62.     local xspot = math.floor((data["width"] - string.len(name)) / 2) + 1
  63.     local yspot = math.ceil(data["height"] / 2)
  64.    
  65.     data["window"].setBackgroundColor(bgColor)
  66.     data["window"].clear()
  67.     data["window"].setCursorPos(xspot, yspot)
  68.     data["window"].setTextColor(tColor)
  69.     data["window"].write(name)    
  70.   end
  71. end
  72.                                              
  73. function checkxy(cx, cy)
  74.   for name, data in pairs(button) do
  75.     if cy >= data["y"] then
  76.     if cy <= (data["height"] + data["y"] - 1) then
  77.     if cx >= data["x"] then
  78.     if cx <= (data["width"] + data["x"] - 1) then
  79.       if data["arg"] == nil then
  80.         data["func"]()
  81.       else
  82.         data["func"](data["arg"])
  83.       end
  84.       return true
  85.     end
  86.     end
  87.     end
  88.     end
  89.   end
  90.   return false
  91. end
  92.  
  93. function toggleButton(name)
  94.   button[name]["active"] = not button[name]["active"]
  95.   screen()
  96. end    
  97.  
  98. function flashButton(name, time)
  99.   toggleButton(name)
  100.   sleep(time)
  101.   toggleButton(name)
  102. end
  103.      
  104. function centerText(text, y)
  105.   w, h = mon.getSize()
  106.   if (w-string.len(text)) > w then
  107.      print("The text to be centered at line "..y.." is too long!")
  108.   end
  109.   if y > h then
  110.     print("The text on line "..y.." is too low!")
  111.   end
  112.   mon.setCursorPos((w-string.len(text))/2+1, y)
  113.   mon.write(text)
  114. end
  115.      
  116. function label(w, h, text)
  117.   mon.setCursorPos(w, h)
  118.   mon.write(text)
  119. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement