Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Dots shell by PaymentOption
- 3 November 2012
- This is a simple clickable desktop interface that allows
- for a windows-like icon desktop system.
- Change Log:
- - 0.1: Initial Release
- Planned Features:
- - Ability to manipulate dots as you can in CraftOS.
- - Possibly a basic security system for logins and such.
- --]]
- --[[
- NOTES:
- - Read only files are red dots!
- - Dot properties are stored as: <dotName>_<dotName>
- * The color of the dot will be the first line. An example being: 'red'. NOT colors.red.
- --]]
- --=VARIABLES=====================--
- local screenWidth, screenHeight = term.getSize() -- The x and y dimensions of the screen.
- local scrollOffset = 0 -- The amount of offsetting needed to be done in the screen buffer every update.
- local BACKGROUND_COLOR = colors.cyan -- The color of the background used for the desktop.
- local DEFAULT_FILE_COLOR = colors.orange -- The color of a dot if it is a file and is using default settings.
- local DEFAULT_DIR_COLOR = colors.lime -- The color of a dot if it is a directory and is using default settings.
- local propertiesDirectory = ".dotProperties"
- local tScreenBuffer = {}
- local tDots = {} -- The dots that are currently loaded.
- local tempDots = tDots -- A temporary buffer for scrolling the dots on screen.
- local tScrollBarPositions = {} -- The positions in which the scroll bar exists.
- local currentDirectory = "" -- The current directory that we are in.
- --=END VARIABLES=================--
- --=FILE HANDLING FUNCTIONS============--
- -- Loads the property file for the given dot. Takes
- -- the path of the object represented by the dot.
- function loadDotProperty(dotPath, dotLabel)
- -- Get the specific identifier for this dot's property file.
- local dotIdentifier = string.gsub(dotPath, '/', '_')
- local dotIsFile = not fs.isDir(dotPath)
- -- Check if the object represented by the dot actually exists on this computer.
- if fs.exists(dotPath) and fs.exists(propertiesDirectory .. '/' .. dotLabel .. '_' .. dotIdentifier) then
- -- Make sure that the file is not read only. If it is, however, then return red.
- if fs.isReadOnly(dotPath) then
- return colors.red
- end
- -- Open the properties file related to this dot.
- local dotPropertyFileHandle = fs.open(propertiesDirectory .. '/' .. dotLabel .. '_' .. dotIdentifier, 'r')
- -- Make sure the file handle is valid.
- if dotPropertyFileHandle then
- -- Get the color stored in the file and close the file handle.
- local dotColor = getColorFromString(dotPropertyFileHandle.readAll()) -- This will be colors.someColor.
- dotPropertyFileHandle.close()
- -- Make sure the color stored is valid.
- if dotColor then
- return dotColor
- else
- return getDefaultColor(dotIsFile, dotPath)
- end
- else
- return getDefaultColor(dotIsFile, dotPath)
- end
- return getDefaultColor(dotIsFile, dotPath)
- end
- end
- -- Returns the appropriate default color for the type given. Takes a boolean value
- -- representing whether or not the dot is a file.
- function getDefaultColor(dotIsFile, dotPath)
- -- If the dot is a read only file, then return red.
- if fs.isReadOnly(dotPath) then
- return colors.red
- end
- -- If the dot is a file, then return the default file color for a dot.
- if dotIsFile then
- return DEFAULT_FILE_COLOR
- end
- -- If the dot is a directory, then return the default directory color for a dot.
- return DEFAULT_DIR_COLOR
- end
- --=END FILE HANDLING FUNCTIONS=======--
- --=SCREEN BUFFER FUNCTIONS============--
- -- Empties the screen buffer.
- function dumpBuffer()
- tScreenBuffer = {}
- end
- -- Writes text to the screen using the screen buffer.
- function writeToScreen(text, tempTextColor, tempBackgroundColor, tempPath)
- -- Make sure the text color and backgrond color are valid.
- tempTextColor = isColorValid(tempTextColor) or colors.white
- tempBackgroundColor = isColorValid(tempBackgroundColor) or colors.black
- -- Get the x and y position of the text that will be written.
- local xCursorPos, yCursorPos = term.getCursorPos()
- -- Add the entry to the screen buffer.
- local entry = {
- label = text,
- xPos = xCursorPos,
- yPos = yCursorPos,
- textColor = tempTextColor,
- backgroundColor = tempBackgroundColor,
- path = tempPath
- }
- table.insert(tScreenBuffer, entry)
- -- Write the text to the screen.
- term.setTextColor(tempTextColor)
- term.setBackgroundColor(tempBackgroundColor)
- term.write(text)
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- -- Draws the screen buffer to the screen.
- function redrawBuffer()
- for index, item in pairs(tScreenBuffer) do
- term.setTextColor(item.textColor)
- term.setBackgroundColor(item.backgroundColor)
- term.setCursorPos(item.xPos, item.yPos)
- term.write(item.label)
- end
- -- Reset the text and background colors to white on black.
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- end
- --=END SCREEN BUFFER FUNCTIONS========--
- --=COLOR FUNCTIONS====================--
- -- Returns the proper color value from the colors table when given a string.
- function getColorFromString(colorString)
- colorString = colorString
- -- Use an 'if-else' ladder to retrieve the proper color from the colors table.
- -- Leave red out of it since that is the custom color of read only files.
- if colorString == "white" then
- return colors.white
- elseif colorString == "orange" then
- return colors.orange
- elseif colorString == "magenta" then
- return colors.magenta
- elseif colorString == "lightBlue" then
- return colors.lightBlue
- elseif colorString == "yellow" then
- return colors.yellow
- elseif colorString == "lime" then
- return colors.lime
- elseif colorString == "pink" then
- return colors.pink
- elseif colorString == "gray" then
- return colors.gray
- elseif colorString == "lightGray" then
- return colors.lightGray
- elseif colorString == "purple" then
- return colors.purple
- elseif colorString == "blue" then
- return colors.blue
- elseif colorString == "brown" then
- return colors.brown
- elseif colorString == "green" then
- return colors.green
- elseif colorString == "black" then
- return colors.black
- end
- end
- -- Returns the color string value of the color given.
- function getColorFromValue(colorValue)
- -- Use an 'if-else ladder' to retrieve the proper color string from the colors table.
- -- Leave red out of it since that is the curstom color of read only files.
- if colorValue == colors.white then
- return "white"
- elseif colorValue == colors.orange then
- return "orange"
- elseif colorValue == colors.magenta then
- return "magenta"
- elseif colorValue == colors.lightBlue then
- return "lightBlue"
- elseif colorValue == colors.yellow then
- return "yellow"
- elseif colorValue == colors.lime then
- return "lime"
- elseif colorValue == colors.pink then
- return "pink"
- elseif colorValue == colors.gray then
- return "gray"
- elseif colorValue == colors.lightGray then
- return "lightGray"
- elseif colorValue == colors.purple then
- return "purple"
- elseif colorValue == colors.blue then
- return "blue"
- elseif colorValue == colors.brown then
- return "brown"
- elseif colorValue == colors.green then
- return "green"
- elseif colorValue == colors.black then
- return "black"
- end
- end
- -- Checks if the given color is valid. Returns the given color
- -- if it is valid; returns nil if it is invalid.
- function isColorValid(givenColor)
- for index, color in pairs(colors) do
- if color == givenColor then
- return givenColor
- end
- end
- -- If the loop completed without returning, then the color given is invalid;
- -- return nil.
- return nil
- end
- -- Sets the background color for all screens.
- function setBackgroundColor(color)
- if isColorValid(color) then
- BACKGROUND_COLOR = color
- end
- end
- --=END COLOR FUNCTIONS================--
- --=SCREEN UTILITES====================--
- -- Clears the screen of all text without dumping the screen buffer.
- function clearScreen()
- term.setBackgroundColor(BACKGROUND_COLOR)
- term.clear()
- term.setCursorPos(1, 1)
- term.setBackgroundColor(colors.black)
- end
- -- Resets the screen in the case of a directory change.
- function resetScreen(path)
- -- Reinitialize the dots and reset the scroll offset.
- tDots = {}
- initializeDots(path)
- tempDots = tDots
- scrollOffset = 0
- end
- --=END SCREEN UTILITES================--
- --=DOT FUNCTIONS======================--
- -- Initializes the dot table with the directory given..
- function initializeDots(directory)
- local contents = fs.list(directory)
- for index, item in ipairs(contents) do
- local dotColor = loadDotProperty(directory .. '/' .. item, item)
- if fs.isReadOnly(item) then
- addDot(colors.red, directory .. '/' .. item, "Read only item.")
- else
- addDot(dotColor or DEFAULT_DOT_COLOR, directory .. '/' .. item, "Root content.")
- end
- end
- end
- -- Adds a dot to the list of current dots.
- -- NOTE: Generates a valid x position and y position for this new dot.
- function addDot(tempColor, path, tempDescription)
- -- Make sure there is a description for this dot.
- tempDescription = tempDescription or "No description provided"
- -- Make sure that the color given is valid.
- if not isColorValid(tempColor) then
- -- If the file/directory is read only, then set its default color to red.
- if fs.isReadOnly(path) then
- tempColor = colors.red
- else
- -- If the color isn't valid then use the default color for either a directory or a file.
- if fs.isDir(path) then
- tempColor = DEFAULT_DIR_COLOR
- else
- tempColor = DEFAULT_FILE_COLOR
- end
- end
- end
- -- Make sure that the path given is valid. If it is not valid, then
- -- make the color a light gray.
- if not fs.exists(path) then
- tempColor = colors.lightGray
- end
- -- Add the dot to the dot table.
- local dotEntry = {
- label = fs.getName(path),
- fullPath = path,
- color = tempColor,
- description = tempDescription
- }
- table.insert(tDots, dotEntry)
- end
- -- Removes a dot from the list of current dots.
- -- NOTE: This function will use the next option provided to locate the dot in the table!
- -- Example: If you provide only the index, the loop will use that information to remove the dot.
- -- If you provide the index and the label, the loop will use the label.
- -- Etc.
- function removeDot(dotIndex, givenLabel, path)
- for index, dot in pairs(tDots) do
- if path then
- if dot.fullPath == path then
- table.remove(tDots, index)
- end
- elseif label then
- if dot.label == givenLabel then
- table.remove(tDots, index)
- end
- elseif dotIndex then
- table.remove(tDots, dotIndex)
- break
- end
- end
- end
- -- Scrolls the dot table so that when it is drawn we see the scrolled values.
- -- Returns the modified table that only contains the values that should appear
- -- on screen after scrolling.
- function scrollDots(up)
- -- If the screen should be scrolled up.
- if up then
- -- Check if the screen can be scrolled up.
- if #tDots / 25 + scrollOffset > #tDots / 25 then
- scrollOffset = scrollOffset - 1
- end
- -- If the screen should be scrolled down.
- else
- -- Check if the screen can be scrolled down.
- if #tDots / 25 - scrollOffset - 1 >= 0 then
- scrollOffset = scrollOffset + 1
- end
- end
- local scrolledTable = tDots
- if scrollOffset > 0 then
- scrolledTable = {}
- for index = scrollOffset * 25, #tDots do
- table.insert(scrolledTable, tDots[index])
- end
- end
- return scrolledTable
- end
- -- Draws the dots from the dot table to the screen.
- function drawDots(scrolledDots)
- scrolledDots = scrolledDots or tDots
- -- Draw the dots in a linear fashion. 25 in one line, 9 lines per screen.
- local pos = 2
- local line = 2
- -- Use a loop to go through the dot table and draw the dots by index.
- for index, dot in pairs(scrolledDots) do
- if line > 18 then
- break
- end
- -- Set the proper position and color for the current dot.
- term.setBackgroundColor(dot.color)
- term.setCursorPos(pos, line)
- term.write(' ')
- -- writeToScreen(' ', colors.white, dot.color, dot.path)
- -- Add the position drawn to the temp-dots table.
- tempDots[index] = {label = dot.label, fullPath = dot.fullPath, color = dot.color, descirption = dot.descirption, xPos = pos, yPos = line}
- pos = pos + 2 -- Add 2 to the position for next dot so there will be a space between each dot.
- -- If the position is greater than 50 then go ahead
- -- and jump to the next line.
- if pos > 50 then
- pos = 2
- line = line + 2 -- Move the line down 2 so that there is a space between each line.
- end
- end
- local scrollBarSize = calculateScrollBar() -- Get the size of the scroll bar.
- -- If the scroll bar is greater than 18, meaning it can be scrolled, then draw the scroll bar.
- if scrollBarSize then drawScrollBar(scrollBarSize) end
- end
- -- Brings up a screen that provides information about the clicked dot.
- function drawDotScreen(dotIndex)
- -- Draw a larger representation of the dot clicked.
- clearScreen()
- drawLargeDot(tempDots[dotIndex].color)
- -- List all information about the dot given.
- local informationStartPos_X = 20
- local informationStartPos_Y = 7
- local dotDescription = fs.isReadOnly(tempDots[dotIndex].fullPath) and "Read only content." or nil
- term.setCursorPos(informationStartPos_X, informationStartPos_Y)
- writeToScreen("Name: " .. tempDots[dotIndex].label, colors.white, colors.cyan, nil)
- term.setCursorPos(informationStartPos_X, informationStartPos_Y + 1)
- writeToScreen("Path: " .. tempDots[dotIndex].fullPath, colors.white, colors.cyan, nil)
- term.setCursorPos(informationStartPos_X, informationStartPos_Y + 3)
- writeToScreen(dotDescription, colors.white, colors.cyan, nil)
- -- Draw the back button on the right side of the screen.
- term.setCursorPos(screenWidth - 1, screenHeight)
- writeToScreen("->", colors.white, colors.lightBlue, nil)
- -- If this dot is a file, then offer to execute the file.
- -- However, if this dot is a directory, then offer to enter the directory.
- local clickableOption = (not fs.isDir(tempDots[dotIndex].fullPath) and "Execute" or "Enter")
- local editOption = "Edit Description"
- while true do
- term.setBackgroundColor(colors.lightBlue)
- term.setTextColor(colors.white)
- -- Draw the execute/enter option.
- term.setCursorPos(2, screenHeight)
- term.write(clickableOption)
- -- Draw the change color option underneath the larger dot.
- term.setCursorPos(5, 14)
- term.write("Change")
- -- Check for a click on the 'execute' or 'enter' option.
- local event, p1, p2, p3 = os.pullEvent()
- -- If the event was click, then check if it was on the option provided.
- if event == "mouse_click" then
- -- If the click was a left click then execute the command.
- if p1 == 1 then
- if (p2 >= 2 and p2 <= 2 + clickableOption:len()) and p3 == screenHeight then
- -- If the option was to execute the given file, then run it with the shell.
- if clickableOption == "Execute" then
- clearScreen()
- term.setTextColor(colors.white)
- term.setBackgroundColor(colors.black)
- shell.run(tempDots[dotIndex].fullPath)
- -- Dump the screen buffer and return from the screen.
- dumpBuffer()
- return
- -- If the option was to enter the given directory, then enter the directory.
- else
- -- Set the current directory to the appropriate path.
- currentDirectory = tempDots[dotIndex].fullPath
- -- Reinitialize the dots and reset the scroll offset.
- resetScreen(tempDots[dotIndex].fullPath)
- -- Dump the screen buffer and return from the screen.
- dumpBuffer()
- return
- end
- -- If the click was on the back button, then leave the screen.
- elseif (p2 >= screenWidth - 1 and p2 <= screenWidth) and p3 == screenHeight then
- dumpBuffer()
- return
- -- If the click was on the change color button, then bring up the change color screen.
- elseif (p2 >= 4 and p2 <= 4 + ("Change"):len()) and p3 == 14 then
- -- Change the color.
- dumpBuffer()
- modifyDotColorPropertyFile(tempDots[dotIndex].label, tempDots[dotIndex].fullPath, changeDotColor(tempDots[dotIndex]) or tempDots[dotIndex].color)
- resetScreen(currentDirectory)
- return
- end
- end
- end
- end
- end
- -- Presents a screen that allows the user to pick the color of the dot.
- function changeDotColor(dot)
- -- Draw a screen that shows the current color of the dot,
- -- the dot's name, path, and a list of colors we can change it to.
- local dotCenterPos = screenWidth/2 - (dot.label:len() + 3)/2 - 5
- clearScreen()
- term.setBackgroundColor(colors.cyan)
- term.setTextColor(colors.white)
- term.setCursorPos(dotCenterPos, 1)
- term.write(dot.label .. " :")
- term.setBackgroundColor(dot.color)
- term.write(' ')
- term.setBackgroundColor(BACKGROUND_COLOR)
- -- Draw the dot path.
- term.setCursorPos(dotCenterPos, 2)
- term.write("Path: " .. dot.fullPath)
- -- Draw the list of colors that we can change the color to.
- local colorPositions = {}
- local pos = dotCenterPos + 2
- local line = 5
- for index, color in pairs(colors) do
- if type(color) == "number" and color ~= colors.cyan and color ~= colors.red then
- term.setCursorPos(pos, line)
- term.setBackgroundColor(color)
- term.write(' ')
- -- Add the color just written to the color positions table so we can click it.
- local entry = {pos, line, color}
- table.insert(colorPositions, entry)
- -- Increment the positions for the next color.
- pos = pos + 2
- if pos >= 28 then
- pos = dotCenterPos + 2
- line = line + 2
- end
- end
- end
- -- Draw a back button that is clickable.
- term.setCursorPos(1, screenHeight)
- term.setBackgroundColor(colors.lightBlue)
- term.setTextColor(colors.white)
- term.write("<-")
- -- Handle mouse clicks so we can change the color accordingly.
- while true do
- local event, clickType, xClickPos, yClickPos = os.pullEvent("mouse_click")
- -- Check if the click was on a color. If it was, then return the color clicked.
- for index = 1, #colorPositions do
- if xClickPos == math.floor(colorPositions[index][1]) and yClickPos == colorPositions[index][2] then
- return colorPositions[index][3]
- end
- end
- -- Check if the click was on the back button. If it was, then return nil.
- if xClickPos <= 2 and yClickPos == screenHeight then
- return nil
- end
- end
- end
- -- Changes the dot property of the given dot.
- function modifyDotColorPropertyFile(dotLabel, dotPath, newColor)
- newColor = getColorFromValue(newColor)
- -- Get the specific identifier of the dot given.
- local dotIdentifier = string.gsub(dotPath, '/', '_')
- -- Open the dot property file for the given dot.
- local propertyFileHandle = fs.open(propertiesDirectory .. '/' .. dotLabel .. '_' .. dotIdentifier, 'w')
- propertyFileHandle.write(newColor)
- propertyFileHandle.close()
- end
- --=END DOT FUNCTIONS==================--
- --=CALCULATING FUNCTIONS==============--
- -- Calculates the size in spaces that the scroll bar should be depending
- -- on the amount of dots present on the screen.
- function calculateScrollBar()
- if tDots then
- local size = math.floor(18 / (#tDots / 225))
- -- Make sure that the size is not an unusable number (inf).
- if 1/0 == size then
- return nil
- -- If the size is usable, then return it to the calling function.
- else
- return size
- end
- end
- return nil
- end
- --=END CALCULATING FUNCTIONS==========--
- --=DRAWING FUNCTIONS==================--
- -- Draws a larger representation of the given dot color.
- function drawLargeDot(dotColor)
- term.setBackgroundColor(dotColor)
- for line = 5, 13 do
- for pos = 4, 12 do
- term.setCursorPos(pos, line)
- term.write(' ')
- end
- end
- -- Reset the background color to the default background color.
- term.setBackgroundColor(BACKGROUND_COLOR)
- end
- -- Draws the scroll bar to the right of thes screen.
- function drawScrollBar(scrollBarSize)
- -- Reset the sroll bar table.
- tScrollBarPositions = {}
- -- Draw the area for the scroll bar.
- term.setBackgroundColor(colors.white)
- for line = 1, screenHeight do
- term.setCursorPos(screenWidth, line)
- term.write(' ')
- end
- -- Draw the scroll bar.
- term.setBackgroundColor(colors.lightGray)
- for block = 1, scrollBarSize do
- -- Make sure that the position we're drawing is on screen.
- if scrollOffset + block <= screenHeight and scrollOffset + block >= 1 then
- term.setCursorPos(screenWidth, scrollOffset + block)
- table.insert(tScrollBarPositions, scrollOffset + block) -- Add the position drawn to the table
- -- so we may keep track of it.
- term.write(' ')
- end
- end
- -- Reset the background color to black.
- term.setBackgroundColor(colors.black)
- end
- -- Draws the back button to the screen without the screen buffer.
- function drawBackButton()
- term.setBackgroundColor(colors.lightBlue)
- term.setTextColor(colors.white)
- term.setCursorPos(1, screenHeight)
- term.write("<-")
- end
- -- Draws the current directory that we are in.
- function drawPath()
- term.setBackgroundColor(colors.lightBlue)
- term.setTextColor(colors.white)
- -- Make sure that the directory to be drawn is less than or equal 25 characters in length.
- if currentDirectory:len() <= 25 then
- term.setCursorPos(screenWidth - currentDirectory:len() - 1, screenHeight)
- term.write(currentDirectory)
- else
- term.setCursorPos(screenWidth - 26, screenHeight)
- term.write(currentDirectory:sub(1, 22) .. "...")
- end
- end
- -- Draws the delete, move, and copy options for when in a dot profile screen.
- function drawDotCommands()
- end
- --=END DRAWING FUNCTIONS==============--
- --=INPUT HANDLING FUNCTIONS===========--
- -- Handles limited amounts of character input. Returns the string that was typed.
- function limitRead( nLength, cReplaceChar )
- term.setCursorBlink( true )
- nLength = nLength or -1 -- -1 is unlimited
- sReturnString = ""
- xPos, yPos = term.getCursorPos()
- while true do
- event, char = os.pullEvent()
- if nLength ~= -1 and string.len( sReturnString ) >= nLength then term.setCursorBlink( false ); return sReturnString end -- Length check
- if event == "char" then sReturnString = sReturnString .. char
- elseif event == "key" and char == 28 then term.setCursorBlink( false ); return sReturnString -- Enter
- elseif event == "key" and char == 14 then -- Backspace
- term.setCursorPos( xPos, yPos )
- term.write( string.rep( " ", string.len( sReturnString ) ) )
- sReturnString = string.sub( sReturnString, 1, string.len( sReturnString )-1 )
- term.setCursorPos( xPos, yPos )
- if not cReplaceChar then term.write( sReturnString )
- else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
- end
- term.setCursorPos( xPos, yPos )
- term.write( string.rep( " ", string.len( sReturnString ) ) )
- term.setCursorPos( xPos, yPos )
- if not cReplaceChar then term.write( sReturnString )
- else term.write( string.rep( cReplaceChar, string.len( sReturnString ) ) ) end
- end
- end
- -- Handles mouse-wheel events.
- function handleMouseScroll(direction)
- -- Typically, the mouse wheel is used to scroll the screen.
- -- The above is how we will go about treating such an event.
- local scrollUp = (direction == - 1 and true or false)
- return scrollDots(scrollUp) -- Scroll the screen.
- end
- --=END INPUT HANDLING FUNCTIONS=======--
- --=VALIDATION FUNCTIONS===============--
- -- Checks if a mouse-drag was on the scroll bar. If the mouse was dragged
- -- on the scroll bar, then the function will handle the scrolling.
- function scrollWithDrag(xDragPos, yDragPos)
- local wasDragged = false -- Whether or not the scroll bar was dragged.
- local blockDragged = 0 -- The index of the black/position of the scroll bar dragged.
- -- Make sure that the drag was on a piece of the scroll bar.
- for index, position in ipairs(tScrollBarPositions) do
- if xDragPos == screenWidth then
- if yDragPos == position then
- wasDragged = true -- The scroll bar was dragged. Set the dragged flag to true.
- blockDragged = position -- Set the block that was dragged by the scroll bar.
- break
- end
- end
- end
- -- If the scroll bar was dragged, then calculate the direction in which it was scrolled.
- if wasDragged then
- -- Run a loop to handle the scrolling until any event other than a mouse drag is trhown.
- while true do
- local event, p1, p2, p3 = os.pullEvent()
- -- If the event pulled was a mouse drag, then scroll the screen.
- if event == "mouse_drag" then
- -- If the yPosition of this drag was greater than that of the first drag,
- -- then the scroll was down.
- if p3 > yDragPos then
- tempDots = scrollDots(false)
- -- If the yPosition of this drag was less than that of the first drag,
- -- then the scroll was up.
- else
- tempDots = scrollDots(true)
- end
- -- If the event pulled was anything but a mouse drag, then return from this function.
- else
- return
- end
- clearScreen()
- drawDots(tempDots) -- Redraw the dots that have now been scrolled.
- end
- end
- end
- -- Checks if the mouse clicked somewhere on the scroll bar.
- -- Returns false and nil if it didn't; true and the scrolled dots if it did.
- function didClickOnScrollBar(xClickPos, yClickPos)
- -- Check if the xClickPos was on the sreenWidth.
- if xClickPos == screenWidth then
- -- Make sure that the click was not on the scroll bar itself, but
- -- on the white part of it.
- for index, position in ipairs(tScrollBarPositions) do
- -- If the y position of the click was actually on one of the scroll bar blocks, then return
- -- false and nil.
- if yClickPos == position then
- return false, nil
- end
- end
- -- If the function did not return after the above loop, then place the top of the scroll bar at
- -- the y position of the click.
- scrollOffset = yClickPos -- Do not subtract one from the scroll offset after this line because that will be
- -- done when we call 'scrollDots' for an upwards scroll.
- return true, scrollDots(true)
- end
- -- If the function hasn't returned by now, then the click was not
- -- on the scroll bar. Return false and nil.
- return false, nil
- end
- -- Checks if a dot was clicked. Returns the index in the dot table of the on-screen dots.
- -- Returns nil if no dot was clicked.
- function checkIfDotWasClicked(xClickPos, yClickPos)
- -- Check if the click was on a dot. Remebering that once the dots are drawn, their positions are saved
- -- in the tempDots table as the 5th and 6th index (xPos, yPos).
- local dotIndex = nil -- The index of the dot (potentially) clicked.
- for index, dot in pairs(tempDots) do
- if xClickPos == dot.xPos and yClickPos == dot.yPos then
- dotIndex = index
- break
- end
- end
- return dotIndex
- end
- -- Checks if the back button was clicked. Returns true if so; false if not.
- function checkIfClickedOnBackButton(xClickPos, yClickPos)
- if xClickPos >=1 and xClickPos <= 2 and yClickPos == screenHeight then
- return true
- end
- -- If the function has not returned by now, then the back button was not clickecd.
- -- Return false.
- return false
- end
- --=END VALIDATION FUNCTIONS===========--
- --=DIRECTORY HANDLING FUNCTIONS=======--
- -- Moves the directory given back one level. Returns the backed up directory.
- function moveBackDirectoryLevel(directory)
- return directory:sub(1, directory:find(fs.getName(directory)) - 2)
- end
- --=END DIRECTORY HANDLING FUNCTIONS===--
- initializeDots(currentDirectory) -- Add the contents of the root directory to the dots.
- tempDots = tDots
- while true do
- clearScreen()
- drawDots(tempDots)
- redrawBuffer() -- I know this seems kind of redundant, but it is necessary for drawing items that are not dots and are in
- -- the screen buffer.
- if currentDirectory ~= "" then
- drawBackButton() -- Draws the back button if we are in a directory other than the root.
- drawPath() -- Draws the current directory that we are in.
- end
- local event, param1, param2, param3 = os.pullEvent()
- -- If the event was a mouse wheel scroll, then scroll the screen accordingly.
- if event == "mouse_scroll" then
- tempDots = handleMouseScroll(param1) -- Param1 will be the direction of the scroll in this case.
- -- If the event was a mouse drag, then handle the drag accordingly.
- elseif event == "mouse_drag" then
- -- Attempt to scroll the dots with a mouse drag. This function will return after all scrolling
- -- has been handled.
- scrollWithDrag(param2, param3)
- -- If the event was a mouse click, then handle the click accordingly.
- elseif event == "mouse_click" then
- -- Check if the mouse click was on the scroll bar. If it was, then see if we can't
- -- jump the scroll bar to the position clicked.
- local didClick, scrolledDots = didClickOnScrollBar(param2, param3)
- if didClick then
- tempDots = scrolledDots -- Set the dots to be displayed as the scrolled dot table returned by the click
- -- scroll function.
- -- If the click was not on the scroll bar/was not meant to scroll the screen...
- else
- -- Check if the click was on a dot.
- local dotClickedIndex = checkIfDotWasClicked(param2, param3)
- if dotClickedIndex then
- drawDotScreen(dotClickedIndex)
- -- If the click was not on a dot, then check if it was on the back button to return from a directory.
- else
- -- Make sure that we are not in the root directory.
- if currentDirectory ~= "" and checkIfClickedOnBackButton(param2, param3) then
- -- If the click was on the back button, then move back one level.
- currentDirectory = moveBackDirectoryLevel(currentDirectory)
- -- Reset the screen now that the directory has changed.
- resetScreen(currentDirectory)
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement