Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --Initialize local variables so that the whole script and see and use them.
- local modem,mons,slots,coords,oldSlots
- --Set up my used and not used colors. Each button will toggle colors when clicked
- local notUsed = colors.green
- local Used = colors.red
- --Clear the screen, so that we have a blank canvas to use.
- term.clear()
- --Function to find all peripherals of a certain type and return it as a table.
- local function findPeriph(types)
- --Setup our table that we will return
- local tbl = {}
- --Loop through all 6 sides of the computer
- for a, v in pairs(rs.getSides()) do
- --If a peripheral is there, and it's type is what we want, then wrap it and add it to our table
- if peripheral.isPresent(v) and peripheral.getType(v) == types then
- table.insert(tbl,peripheral.wrap(v))
- end
- end
- --If we have a modem attached to the computer, and we've already found it.
- if modem then
- --Loop through all of the peripherals connected to the modem
- for a,v in pairs(modem.getNamesRemote()) do
- --Like above, if it's the type we want, wrap and add.
- if peripheral.getType(v) == types then
- table.insert(tbl,peripheral.wrap(v))
- end
- end
- end
- --End of function, return our wrapped peripherals.
- return tbl
- end
- --Loading from file function
- local function findSlots(fileName)
- --If the file to load from doesn't exist. Then return an empty table
- if not fs.exists(fileName) then
- return {}
- end
- --Else, open the file
- local file = fs.open(fileName,"r")
- --Read all of the text and turn it into a table.
- local all = textutils.unserialize(file.readAll())
- --Close the file
- file.close()
- --Return our read table
- return all
- end
- --drawSlots, only run once. x,y is top left start position. mon is the current monitor (or term) to write to
- --buttonSize is how many pixels high and wide should each individual button be. If it's 2 then it's a 2x2 button
- --ButtonY and buttonX are how many buttons to create high, and wide.
- --If buttonY is 3, then we have 3 rows of buttons that are buttonX wide.
- local function drawSlots(x,y,mon,buttonSize,buttonY,buttonX)
- --If we're running this the first time, (Originally I had thought to run this every time a button changed)
- --If we are, then fillCoords will be true, otherwise it will be false
- local fillCoords = coords == nil
- --Redirect to the monitor (or term) we're using.
- local old = term.redirect(mon)
- --Make sure we know what the background color was
- local oldBack = term.getBackgroundColor()
- --If fillCoords is true then set coords to an empty table, otherwise set it to itself.
- coords = fillCoords and {} or coords
- --Loop down the display. buttonY is the amount of buttons we want
- --It's also one less of the amount of walls AROUND the buttons
- --We start at 0 so that the one less isn't noticed, and so that our math starts on the top line
- --buttonY * buttonSize is the amount of space each row of buttons will take up for all rows together
- for i = 0, buttonY + buttonY* buttonSize do
- --Set our cursor just to the start of that line and clear the line with the old background color
- --Then set our background color to gray for the borders. I could of set this up top.
- term.setCursorPos(1,i+y)
- term.setBackgroundColor(oldBack)
- term.clearLine()
- term.setBackgroundColor(colors.gray)
- --Loop through our columns of buttons, same sort of math that the above loop uses, just with buttonX.
- for j = 0, buttonX + buttonX * buttonSize do
- --Set our cursor at the pixel that we're going to be working on now.
- term.setCursorPos(j+x,i+y)
- --If we're at any gray space, then we just need to color it gray.
- --Each gray space will either be at 0, or any whole multiple of buttonSize + 1
- if j % (buttonSize + 1) == 0 or i % (buttonSize + 1) == 0 then
- term.setBackgroundColor(colors.gray)
- else
- --MATH. Basically through funky math that I just used to test myself this figures out what button we're drawing.
- --(j-j%(buttonSize+1)) / (buttonSize+1) gets the button we're at in the row starting at 0
- --Second half is the same with i, just multiplied by how many buttons per row.
- --Add them together then then result is the current button number all the way down
- --If we're in the 3rd column, 2nd row. Then we're button 7. Since we start at 0.
- local currSlot = ( (j-j%(buttonSize+1)) / (buttonSize+1) + buttonX * (i-i%(buttonSize+1)) / (buttonSize+1) + 1)
- --Boolean value based on if that button should be on or off.
- local corrSlot = slots[currSlot]
- --If we need to fill the coordinates table. This is how we know what button needs to be clicked
- if fillCoords then
- --If the column hasn't been made yet, make it an empty table
- if not coords[i+y] then
- coords[i+y] = {}
- end
- --Set the value at the [y][x] coordinates to be the buttons number
- coords[i+y][j+x] = currSlot
- end
- --Set our background color as the color that was saved or not existing
- term.setBackgroundColor(corrSlot and Used or notUsed)
- end
- --Write our space so that the background changes color at this location.
- write(" ")
- end
- end
- --We're done with the monitor, set it's background color back, and redirect back to where we were.
- term.setBackgroundColor(oldBack)
- term.redirect(old)
- end
- --Since redrawing the ENTIRE table is inefficient, why not redraw an individual button.
- --Every parameter is the same as the above function, slot is the buttons number. sizeY is buttonY, sizeX is buttonX
- local function redrawSlot(x,y,slot,mon,buttonSize,sizeY,sizeX)
- --Find the top left coordinates of the button based on it's slot.
- --More funky math
- --the -1 at the very end is so that we start at 0.
- --If you were to do this, I'd find/make a much better function, as this caused me a load of trouble
- x = x + ( ( ( slot + (sizeX-1) ) % sizeX ) + 1 ) * buttonSize + ( ( slot + (sizeX-1) ) % sizeX ) - 1
- --This one is easier though, First half up to the +(
- --Add our starting y coordinate to the current row we're on.
- --math.ceil(slot/sizeX) gets the whole number above our decimal based on the row.
- --Since we started at 0. Multiply that by the size of each button.
- --End part is that math.ceil(slot/sizeX) -2
- --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.
- --It technically is a -1) -1 but -2 works just as well.
- y = y + math.ceil(slot/sizeX) * buttonSize + (math.ceil(slot/sizeX) -2)
- --Redirect to our monitor or term store old, you know this part
- local old = term.redirect(mon)
- local oldBack = term.getBackgroundColor()
- --Set color to the background color of the active button
- term.setBackgroundColor(slots[slot] and Used or notUsed)
- --Loop through the full button and just redraw it.
- for i = 0, buttonSize - 1 do
- for j = 0, buttonSize - 1 do
- term.setCursorPos(x+j,y+i)
- write(" ")
- end
- end
- --Set color back and redirect back
- term.setBackgroundColor(oldBack)
- term.redirect(old)
- end
- --Our save file function. Just write to file provided with a serialized table
- local function saveSlots(fileName,tbl)
- local file = fs.open(fileName,"w")
- file.writeLine(textutils.serialise(tbl))
- file.close()
- end
- --Get any modem connected to the computer
- --Modem only wants just the first modem provided
- modem = unpack(findPeriph("modem"))
- --Get the monitors connected to the computer, and add in the terminal to the table
- mons = findPeriph("monitor")
- mons[#mons+1] = term.current()
- --Attempt to load our saved slots
- slots = findSlots("slots")
- --Original drawing of the slots.
- for a,v in ipairs(mons) do
- drawSlots(2,2,v,2,4,5)
- end
- --Main function
- local function main()
- --saveTable boolean variable. If it's true then we save the table
- local saveTable = false
- --timer set to a minute so that every minute it attempts to save
- local timer = os.startTimer(60)
- --Infinite loop
- while (true) do
- --Pull all events
- local event,_,x,y = os.pullEvent()
- --If we have a monitor touch event
- if event == "monitor_touch" then
- --If there is a button at those coordinates that they clicked
- if ( coords[y] and coords[y][x] ) then
- --Invert the state of the button there
- slots[coords[y][x]] = not slots[coords[y][x]]
- --Redraw the button on all monitors/term
- for a,v in pairs(mons) do
- redrawSlot(2,2,coords[y][x],v,2,4,5)
- end
- --We made a modification, so we're going to want to save the table.
- saveTable = true
- end
- --else if the event is a timer and it's our timer.
- elseif event == "timer" and _ == timer then
- --Restart our minute timer
- timer = os.startTimer(60)
- --If we want to save the table
- if saveTable then
- --Reset saveTable, and save the table
- saveTable = false
- saveSlots("slots",slots)
- end
- end
- end
- end
- --Call main so it runs.
- main()
Advertisement
Add Comment
Please, Sign In to add comment