GNOOR1S

Updated Direwolf20 Api

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