TwitchBlade

0.1.0

Sep 25th, 2018
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.31 KB | None | 0 0
  1. --Required Librarys
  2. text = require("text")
  3. term = require("term")
  4. component = require("component")
  5. event = require("event")
  6. gpu = component.gpu
  7.  
  8. --Variables
  9. uiB = {}
  10. red = 0xFF0000
  11. green = 0x008000
  12. black = 0x000000
  13. white = 0xFFFFFF
  14. lime = 0x00FF00
  15. blue = 0x0000FF
  16. yellow = 0xFFFF00
  17. cyan = 0x00FFFF
  18. magenta = 0xFF00FF
  19. silver = 0xC0C0C0
  20. gray = 0x808080
  21. maroon = 0x800000
  22. olive = 0x808000
  23. purple = 0x800080
  24. teal = 0x 008080
  25. navy = 0x000080
  26.  
  27. --Functions
  28. function addButton(nm, text, func, buttonType, colorOn, colorOff, x, y, width, height, displayed, onState)
  29.   uiB[nm] = {}
  30.   uiB[nm]["x"] = x
  31.   uiB[nm]["y"] = y
  32.   uiB[nm]["width"] = width
  33.   uiB[nm]["height"] = height
  34.   uiB[nm]["buttonType"] = buttonType
  35.   uiB[nm]["text"] = text
  36.   uiB[nm]["func"] = func
  37.   uiB[nm]["colorOn"] = colorOn
  38.   uiB[nm]["colorOff"] = colorOff
  39.   uiB[nm]["currentColor"] = colorOff
  40.   uiB[nm]["displayed"] = displayed
  41.   uiB[nm]["onState"] = onState
  42. end
  43.  
  44. function displayUI()
  45.   gpu.setBackground(black)
  46.   term.clear()
  47.   for k, v in pairs(uiB) do
  48.     if uiB[k]["displayed"] then
  49.       gpu.setBackground(uiB[k]["currentColor"])
  50.       gpu.fill(uiB[k]["x"], uiB[k]["y"], uiB[k]["width"], uiB[k]["height"], "X")
  51.       TPosX = uiB[k]["width"] / 2 + uiB[k]["x"] - math.floor(string.len(uiB[k]["text"]) / 2)
  52.       TPosY = uiB[k]["height"] / 2 + uiB[k]["y"]
  53.       print(TPosX .. " " .. TPosY)
  54.       gpu.set(TPosX, TPosY, uiB[k]["text"])
  55.     end
  56.   end
  57. end
  58.  
  59. function checkButton(x, y)
  60.   for k, v in pairs(uiB) do
  61.     if uiB[k]["displayed"] then
  62.       if x >= uiB[k]["x"] and x <= uiB[k]["x"] + uiB[k]["width"] then
  63.         if y >= uiB[k]["y"] and y <= uiB[k]["y"] + uiB[k]["height"] then
  64.           if uiB[k]["buttonType"] == "button" then
  65.             uiB[k]["currentColor"] = uiB[k]["colorOn"]
  66.             os.sleep(0.1)
  67.             displayUI()
  68.             uiB[k]["func"]()
  69.             uiB[k]["currentColor"] = uiB[k]["colorOff"]
  70.             displayUI()
  71.           elseif uiB[k]["buttonType"] == "toggle" then
  72.             if uiB[k]["currentColor"] == uiB[k]["colorOff"] then
  73.               uiB[k]["currentColor"] = uiB[k]["colorOn"]
  74.               uiB[k]["onState"] = true
  75.             elseif uiB[k]["currentColor"] == uiB[k]["colorOn"] then
  76.               uiB[k]["currentColor"] = uiB[k]["colorOff"]
  77.               uiB[k]["onState"] = false
  78.             end
  79.             uiB[k]["func"]()
  80.           end
  81.         end
  82.       end
  83.     end
  84.   end
  85. end
  86.  
  87. function exit()
  88.   print("Exiting")
  89.   os.sleep(1)
  90.   gpu.setBackground(black)
  91.   os.exit()
  92. end
  93.  
  94. function testToggle()
  95.   print("Being Toggled")
  96.   os.sleep(1)
  97. end
  98.  
  99. function doNothing()
  100.   print("Doing Nothing")
  101.   os.sleep(0.2)
  102. end
  103.  
  104. function menuSwitch()
  105.   if uiB["menuSwitch"][onState] then
  106.     uiB["exit"]["displayed"] = true
  107.   elseif ~uiB["menuSwitch"][onState] then
  108.     uiB["exit"]["displayed"] = false
  109.   end
  110. end
  111.  
  112. --Add buttons
  113. addButton("exit", "Exit Button", exit, "button", green, red, 5, 15, 15, 5, true, false)
  114. addButton("testToggle", "Test Toggle", testToggle, "toggle", green, red, 25, 15, 15, 5, true, false)
  115. addButton("example", "Example Button", doNothing, "button", green, red, 5, 15, 15, 5, true, false)
  116. addButton("menuSwitch", "Menu Switcher", menuSwitch, "toggle", green, red, 5, 15, 15, 5, true, false)
  117.  
  118. --Main Code Block
  119. while true do
  120.   e = {event.pull(1)}
  121.   if e[4] == 207 then
  122.     os.exit()
  123.   elseif e[1] == "touch" then
  124.     checkButton(e[3], e[4])
  125.   end
  126.   displayUI()
  127. end
Advertisement
Add Comment
Please, Sign In to add comment