Advertisement
Marini21

GUI

May 22nd, 2013
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.00 KB | None | 0 0
  1. -- Piero Marini
  2. -- Started 5/20/13
  3. -- FTB Direwolf20 Pack
  4.  
  5. local rsSide = "back"
  6. local monitorSide = "top"
  7.  
  8. function testForMonitor(_side)
  9.   if peripheral.getType(_side) then
  10.     if peripheral.getType(_side) == "monitor" then
  11.       return true
  12.     else
  13.       return false
  14.     end
  15.   else
  16.     return false
  17.   end
  18. end
  19.  
  20. Args = { ... }
  21.  
  22. if #Args < 1 then
  23.   print("Usage: "..fs.getName(shell.getRunningProgram()).." <monitor side> [cable side]")
  24.   return
  25.  
  26. elseif #Args > 1 then
  27.   monitorSide = Args[1]
  28.   rsSide = Args[2]
  29.   if not testForMonitor(monitorSide) then
  30.     print("There is no valid monitor on that side.")
  31.     return
  32.   end
  33. elseif #Args == 1 then
  34.  
  35.   monitorSide = Args[1]
  36.   if not testForMonitor(monitorSide) then
  37.     print("There is no valid monitor on that side.")
  38.     return
  39.   end
  40.  
  41. else
  42.   print("Error!!!")
  43.   return
  44. end
  45.  
  46. --End of Error Display for arguments missing.
  47.  
  48.  
  49. function toggle(n)
  50.   if not n then
  51.     return
  52.   end
  53.  
  54.   local state = button[n].state
  55.  
  56.   if state then
  57.     state = false
  58.   else
  59.     state = true
  60.   end
  61.  
  62.   button[n].state = state
  63.   return state
  64. end
  65.  
  66. function sayState(n)
  67.   if not n then
  68.     return
  69.   end
  70.   x = button[n].x - 1
  71.   y = button[n].y
  72.   term.setCursorPos(x,y)
  73.   if button[n].state then
  74.     term.setTextColor(colors.lime)
  75.     write("on ")
  76.     term.setTextColor(colors.white)
  77.   else
  78.     term.setTextColor(colors.red)
  79.     write("off")
  80.     term.setTextColor(colors.white)
  81.   end
  82. end
  83. --Function to get hitbox for the right click.
  84. function getButton(xPos,yPos)
  85.   for i=1,12 do
  86.     bxPos = button[i].x
  87.     byPos = button[i].y
  88.     xMax = bxPos + 2
  89.     xMin = bxPos - 2
  90.     yMax = byPos + 1
  91.     yMin = byPos - 1
  92.     if xPos >= xMin and xPos <= xMax and yPos >= yMin and yPos <= yMax then
  93.       return i
  94.     end
  95.   end
  96. end
  97.  
  98. function mPrint(w)
  99.   write(w)
  100.   x,y=term.getCursorPos()
  101.   term.setCursorPos(1, y+1)
  102. end
  103.  
  104. function allTheSame()
  105.   local state = button[1].state
  106.   for i = 2,10 do
  107.     if state == button[i].state then
  108.     else return false
  109.     end
  110.   end
  111.   return true
  112. end
  113.  
  114. function stateWriter()
  115.   mPrint("  _____     _____     _____     _____")
  116.   write("  ")
  117.   for i = 1,4 do
  118.     write("|")
  119.     term.setTextColor(colors.red)
  120.     write("off")
  121.     term.setTextColor(colors.white)
  122.     if i<4 then
  123.       write("|     ")
  124.     else
  125.       mPrint("|")
  126.     end
  127.   end
  128.   mPrint("  ~~~~~     ~~~~~     ~~~~~     ~~~~~")
  129. end
  130.  
  131. function EventTypeWrite(_line,_time)
  132.   term.setCursorPos(1,_line)
  133.   write("  ")
  134.   local additive = (_time - 1) * 4
  135.   local currentx = 3
  136.   for i = 1,4 do
  137.     local buttonNumber = additive + i
  138.     write(button[buttonNumber].textName)
  139.     if i == 4 then
  140.    
  141.     else
  142.       currentx = currentx + 10
  143.       term.setCursorPos(currentx,_line)
  144.     end
  145.   end
  146.   term.setCursorPos(1, _line + 1)
  147. end
  148.    
  149.  
  150. function startText()
  151.   term.setCursorPos(1,1)
  152.   mPrint("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
  153.   mPrint("               Main Menu")
  154.   mPrint(" Right Click to toggle named events")
  155.   mPrint(" ")
  156.   EventTypeWrite(5,1)
  157.   stateWriter()
  158.   mPrint(" ")
  159.   EventTypeWrite(10,2)
  160.   stateWriter()
  161.   mPrint(" ")
  162.   EventTypeWrite(15,3)
  163.   stateWriter()
  164.   mPrint("_______________________________________")
  165. end
  166.  
  167. --Redstone related function, for later use.
  168. function bundleState()
  169.   running = 0
  170.   for light = 1, 10 do
  171.     if button[light].state == true then
  172.       running = running + button[light].color
  173.     end
  174.   end
  175.   return running
  176. end
  177.  
  178. function refresh()
  179.   redstone.setBundledOutput(rsSide, bundleState())
  180. end
  181. --End of Redstone functions.
  182.  
  183. display = peripheral.wrap(monitorSide)
  184. term.redirect(display)
  185. term.clear()
  186. term.setCursorPos(1,1)
  187.  
  188. --Main Control Table.
  189. button = {
  190.  
  191.   [1] = {x = 5; y = 7; state = false; color = colors.white; textName = "House ForceField"},
  192.   [2] = {x = 15; y = 7; state = false; color = colors.orange; textName = "House Power"},
  193.   [3] = {x = 25; y = 7; state = false; color = colors.magenta; textName = "Apiary Power"},
  194.   [4] = {x = 35; y = 7; state = false; color = colors.lightBlue; textName = "Secret Chest"},
  195.  
  196.   [5] = {x = 5; y = 12; state = false; color = colors.yellow; textName = "Quarry"},
  197.   [6] = {x = 15; y = 12; state = false; color = colors.lime; textName = "Oil Refinery"},
  198.   [7] = {x = 25; y = 12; state = false; color = colors.pink; textName = "Mass Fabricator"},
  199.   [8] = {x = 35; y = 12; state = false; color = colors.gray; textName = "Nuclear Reactor"},
  200.  
  201.   [9] = {x = 5; y = 17; state = false; color = colors.lightGray; textName = "Blaze Spawner"},
  202.   [10] = {x = 15; y = 17; state = false; color = colors.cyan; textName = "WitherSkele Spawner"},
  203.   [11] = {x = 25; y = 17; state = false; color = colors.purple; textName = "Toggle"},
  204.   [12] = {x = 35; y = 17; state = false; color = colors.blue; textName = "Master Switch"}
  205.  
  206. }
  207. --End Control Table
  208.  
  209. local resume = true
  210. startText()
  211. for i = 1,10 do
  212.   sayState(i)
  213. end
  214. term.setCursorPos(button[12].x-1,button[12].y)
  215. term.setTextColor(colors.red)
  216. write("off")
  217. term.setTextColor(colors.white)
  218. term.setCursorPos(button[11].x-1,button[11].y)
  219. term.setTextColor(colors.cyan)
  220. write("TGL")
  221. term.setTextColor(colors.white)
  222. refresh()
  223. while resume == true do
  224.   local event, side, xPos, yPos = os.pullEvent("monitor_touch")
  225.   local selectedButton = getButton(xPos,yPos)
  226.   if selectedButton == 11 then
  227.     for i = 1,10 do
  228.       toggle(i)
  229.     end
  230.   elseif selectedButton == 12 then
  231.     toggle(12)
  232.     for i=1,10 do
  233.       button[i].state = button[12].state
  234.     end
  235.     sayState(12)
  236.     allSame=true
  237.   else
  238.     term.setCursorPos(button[12].x - 1, button[12].y)
  239.     term.setTextColor(colors.lightGray)
  240.     write("---")
  241.     term.setTextColor(colors.white)
  242.     toggle(selectedButton)
  243.   end
  244.   for i=1,10 do
  245.     sayState(i)
  246.   end
  247.   if allTheSame() then
  248.     button[12].state = button[1].state
  249.     sayState(12)
  250.   else
  251.     term.setCursorPos(button[12].x - 1, button[12].y)
  252.     term.setTextColor(colors.lightGray)
  253.     write("---")
  254.     term.setTextColor(colors.white)
  255.   end
  256.   refresh()
  257. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement