TwitchBlade

0.2.0

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