Dragon53535

TardisButtons

Apr 13th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.32 KB | None | 0 0
  1. --Initialize local variables so that the whole script and see and use them.
  2. local modem,mons,slots,coords,oldSlots
  3.  
  4. --Set up my used and not used colors. Each button will toggle colors when clicked
  5. local notUsed = colors.green
  6. local Used = colors.red
  7.  
  8. --Clear the screen, so that we have a blank canvas to use.
  9. term.clear()
  10.  
  11. --Function to find all peripherals of a certain type and return it as a table.
  12. local function findPeriph(types)
  13.  
  14.   --Setup our table that we will return
  15.   local tbl = {}
  16.  
  17.   --Loop through all 6 sides of the computer
  18.   for a, v in pairs(rs.getSides()) do
  19.    
  20.     --If a peripheral is there, and it's type is what we want, then wrap it and add it to our table
  21.     if peripheral.isPresent(v) and peripheral.getType(v) == types then
  22.       table.insert(tbl,peripheral.wrap(v))
  23.     end
  24.   end
  25.  
  26.   --If we have a modem attached to the computer, and we've already found it.
  27.   if modem then
  28.  
  29.     --Loop through all of the peripherals connected to the modem
  30.     for a,v in pairs(modem.getNamesRemote()) do
  31.    
  32.       --Like above, if it's the type we want, wrap and add.
  33.       if peripheral.getType(v) == types then
  34.         table.insert(tbl,peripheral.wrap(v))
  35.       end
  36.     end
  37.   end
  38.  
  39.   --End of function, return our wrapped peripherals.
  40.   return tbl
  41. end
  42.  
  43.  
  44. --Loading from file function
  45. local function findSlots(fileName)
  46.  
  47.   --If the file to load from doesn't exist. Then return an empty table
  48.   if not fs.exists(fileName) then
  49.     return {}
  50.   end
  51.  
  52.   --Else, open the file
  53.   local file = fs.open(fileName,"r")
  54.  
  55.   --Read all of the text and turn it into a table.
  56.   local all = textutils.unserialize(file.readAll())
  57.  
  58.   --Close the file
  59.   file.close()
  60.  
  61.   --Return our read table
  62.   return all
  63. end
  64.  
  65. --drawSlots, only run once. x,y is top left start position. mon is the current monitor (or term) to write to
  66. --buttonSize is how many pixels high and wide should each individual button be. If it's 2 then it's a 2x2 button
  67. --ButtonY and buttonX are how many buttons to create high, and wide.
  68. --If buttonY is 3, then we have 3 rows of buttons that are buttonX wide.
  69. local function drawSlots(x,y,mon,buttonSize,buttonY,buttonX)
  70.  
  71.   --If we're running this the first time, (Originally I had thought to run this every time a button changed)
  72.   --If we are, then fillCoords will be true, otherwise it will be false
  73.   local fillCoords = coords == nil
  74.  
  75.   --Redirect to the monitor (or term) we're using.
  76.   local old = term.redirect(mon)
  77.  
  78.   --Make sure we know what the background color was
  79.   local oldBack = term.getBackgroundColor()
  80.  
  81.   --If fillCoords is true then set coords to an empty table, otherwise set it to itself.
  82.   coords = fillCoords and {} or coords
  83.  
  84.   --Loop down the display. buttonY is the amount of buttons we want
  85.   --It's also one less of the amount of walls AROUND the buttons
  86.   --We start at 0 so that the one less isn't noticed, and so that our math starts on the top line
  87.   --buttonY * buttonSize is the amount of space each row of buttons will take up for all rows together
  88.   for i = 0, buttonY + buttonY* buttonSize do
  89.  
  90.     --Set our cursor just to the start of that line and clear the line with the old background color
  91.     --Then set our background color to gray for the borders. I could of set this up top.
  92.     term.setCursorPos(1,i+y)
  93.     term.setBackgroundColor(oldBack)
  94.     term.clearLine()
  95.     term.setBackgroundColor(colors.gray)
  96.    
  97.     --Loop through our columns of buttons, same sort of math that the above loop uses, just with buttonX.
  98.     for j = 0, buttonX + buttonX * buttonSize do
  99.    
  100.       --Set our cursor at the pixel that we're going to be working on now.
  101.       term.setCursorPos(j+x,i+y)
  102.      
  103.       --If we're at any gray space, then we just need to color it gray.
  104.       --Each gray space will either be at 0, or any whole multiple of buttonSize + 1
  105.       if j % (buttonSize + 1) == 0 or i % (buttonSize + 1) == 0 then
  106.         term.setBackgroundColor(colors.gray)
  107.       else
  108.      
  109.          --MATH. Basically through funky math that I just used to test myself this figures out what button we're drawing.
  110.         --(j-j%(buttonSize+1)) / (buttonSize+1) gets the button we're at in the row starting at 0
  111.         --Second half is the same with i, just multiplied by how many buttons per row.
  112.         --Add them together then then result is the current button number all the way down
  113.         --If we're in the 3rd column, 2nd row. Then we're button 7. Since we start at 0.
  114.         local currSlot = ( (j-j%(buttonSize+1)) / (buttonSize+1) + buttonX * (i-i%(buttonSize+1)) / (buttonSize+1) + 1)
  115.      
  116.         --Boolean value based on if that button should be on or off.
  117.         local corrSlot = slots[currSlot]
  118.        
  119.         --If we need to fill the coordinates table. This is how we know what button needs to be clicked
  120.         if fillCoords then
  121.        
  122.           --If the column hasn't been made yet, make it an empty table
  123.           if not coords[i+y] then
  124.             coords[i+y] = {}
  125.           end
  126.          
  127.           --Set the value at the [y][x] coordinates to be the buttons number
  128.           coords[i+y][j+x] = currSlot
  129.         end
  130.        
  131.         --Set our background color as the color that was saved or not existing
  132.         term.setBackgroundColor(corrSlot and Used or notUsed)
  133.       end
  134.      
  135.       --Write our space so that the background changes color at this location.
  136.       write(" ")
  137.     end
  138.   end
  139.  
  140.   --We're done with the monitor, set it's background color back, and redirect back to where we were.
  141.   term.setBackgroundColor(oldBack)
  142.   term.redirect(old)
  143. end
  144.  
  145. --Since redrawing the ENTIRE table is inefficient, why not redraw an individual button.
  146. --Every parameter is the same as the above function, slot is the buttons number. sizeY is buttonY, sizeX is buttonX
  147. local function redrawSlot(x,y,slot,mon,buttonSize,sizeY,sizeX)
  148.  
  149.   --Find the top left coordinates of the button based on it's slot.
  150.   --More funky math
  151.   --the -1 at the very end is so that we start at 0.
  152.   --If you were to do this, I'd find/make a much better function, as this caused me a load of trouble
  153.   x = x + ( ( ( slot + (sizeX-1) ) % sizeX ) + 1 ) * buttonSize + ( ( slot + (sizeX-1) ) % sizeX ) - 1
  154.  
  155.   --This one is easier though, First half up to the +(
  156.   --Add our starting y coordinate to the current row we're on.
  157.   --math.ceil(slot/sizeX) gets the whole number above our decimal based on the row.
  158.   --Since we started at 0. Multiply that by the size of each button.
  159.   --End part is that math.ceil(slot/sizeX) -2
  160.   --The first part told us which row of button's we were on. However we need the also add in the one space wide gray spots.
  161.   --It technically is a -1) -1 but -2 works just as well.
  162.   y = y + math.ceil(slot/sizeX) * buttonSize + (math.ceil(slot/sizeX) -2)
  163.  
  164.   --Redirect to our monitor or term store old, you know this part
  165.   local old = term.redirect(mon)
  166.   local oldBack = term.getBackgroundColor()
  167.  
  168.   --Set color to the background color of the active button
  169.   term.setBackgroundColor(slots[slot] and Used or notUsed)
  170.  
  171.   --Loop through the full button and just redraw it.
  172.   for i = 0, buttonSize - 1 do
  173.     for j = 0, buttonSize - 1 do
  174.       term.setCursorPos(x+j,y+i)
  175.       write(" ")
  176.     end
  177.   end
  178.  
  179.   --Set color back and redirect back
  180.   term.setBackgroundColor(oldBack)
  181.   term.redirect(old)
  182. end
  183.  
  184. --Our save file function. Just write to file provided with a serialized table
  185. local function saveSlots(fileName,tbl)
  186.   local file = fs.open(fileName,"w")
  187.   file.writeLine(textutils.serialise(tbl))
  188.   file.close()
  189. end
  190.  
  191.  
  192. --Get any modem connected to the computer
  193. --Modem only wants just the first modem provided
  194. modem = unpack(findPeriph("modem"))
  195.  
  196. --Get the monitors connected to the computer, and add in the terminal to the table
  197. mons = findPeriph("monitor")
  198. mons[#mons+1] = term.current()
  199.  
  200. --Attempt to load our saved slots
  201. slots = findSlots("slots")
  202.  
  203. --Original drawing of the slots.
  204. for a,v in ipairs(mons) do
  205.   drawSlots(2,2,v,2,4,5)
  206. end
  207.  
  208. --Main function
  209. local function main()
  210.  
  211.   --saveTable boolean variable. If it's true then we save the table
  212.   local saveTable = false
  213.  
  214.   --timer set to a minute so that every minute it attempts to save
  215.   local timer = os.startTimer(60)
  216.  
  217.   --Infinite loop
  218.   while (true) do
  219.  
  220.     --Pull all events
  221.     local event,_,x,y = os.pullEvent()
  222.    
  223.     --If we have a monitor touch event
  224.     if event == "monitor_touch" then
  225.      
  226.       --If there is a button at those coordinates that they clicked
  227.       if ( coords[y] and coords[y][x] ) then
  228.      
  229.         --Invert the state of the button there
  230.         slots[coords[y][x]] = not slots[coords[y][x]]
  231.        
  232.         --Redraw the button on all monitors/term
  233.         for a,v in pairs(mons) do
  234.           redrawSlot(2,2,coords[y][x],v,2,4,5)
  235.         end
  236.        
  237.         --We made a modification, so we're going to want to save the table.
  238.         saveTable = true
  239.       end
  240.      
  241.     --else if the event is a timer and it's our timer.
  242.     elseif event == "timer" and _ == timer then
  243.       --Restart our minute timer
  244.       timer = os.startTimer(60)
  245.      
  246.       --If we want to save the table
  247.       if saveTable then
  248.      
  249.         --Reset saveTable, and save the table
  250.         saveTable = false
  251.         saveSlots("slots",slots)
  252.       end
  253.     end
  254.   end
  255. end
  256.  
  257. --Call main so it runs.
  258. main()
Advertisement
Add Comment
Please, Sign In to add comment