Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- NOTE: process.runThreads() has a timer that will trigger every second in the case that the user
- -- is not doing anything in order to update threads that may be running in the background.
- os.loadAPI("process")
- -- Variables --
- local windows = {} -- This is the table where all of the windows that are created will be kept.
- -- Varibales --
- -- Window functions --
- -- Returns the current window stack.
- function getWindows()
- return windows
- end
- -- Creates a new window using the process API. When this window is created, it is added to the
- -- 'windows' table. However, this window cannot be resized, but can be moved. The index of this
- -- window in the windows table is returned. NOTE: The index returned is also the the hash for
- -- its thread in the threads table.
- function newWindow(x, y, width, height, title, method)
- -- Capture the newly created window object.
- local window = process.newWindow(x, y, width, height, title, method)
- windows[window.threadID] = window
- return window.threadID
- end
- -- Moves the window at the given hash. NOTE: It is best to get the window object using getWindow(), rather than
- -- continuing to call on this function. However, this is okay to use for quick operations.
- -- Returns true if the window was moved successfully; false if not.
- function moveWindow(windowHash, x, y)
- if windows[windowHash] then
- windows[windowHash].x = x
- windows[windowHash].y = y
- end
- end
- -- Returns the window object at the given hash.
- function getWindow(windowHash)
- return windows[windowHash]
- end
- -- Removes the window at the given hash. Returns true if the window as removed successfully; false if not.
- function removeWindow(windowHash)
- if windows[windowHash] then
- windows[windowHash] = nil
- return true
- end
- return false
- end
- -- Iterates through all of the windows in the windows table and cleans out any of the dead ones.
- function scrapDeadWindows()
- for windowHash, window in pairs(windows) do
- if not window.running then
- windows[windowHash] = nil
- end
- end
- end
- -- Window functions --
- -- Native threads/windows/background processes --
- -- This is the base desktop window that sits on the bottom of the stack.
- -- The desktop thread has a listener for the following events: key, mouse_click, timer
- function initDesktop()
- local desktop = {}
- desktop.SCREEN_WIDTH, desktop.SCREEN_HEIGHT = term.getSize()
- desktop.BACKGROUND_COLOR = colors.white
- desktop.TEXT_COLOR = colors.black
- desktop.BOTTOM_BAR_COLOR = colors.lightGray
- -- Clears the desktop.
- function desktop.clear()
- term.setBackgroundColor(desktop.BACKGROUND_COLOR)
- for line = 1, desktop.SCREEN_HEIGHT do
- term.setCursorPos(1, line)
- term.clearLine()
- end
- -- Redraw bottom bar.
- term.setBackgroundColor(desktop.BOTTOM_BAR_COLOR)
- term.setCursorPos(1, desktop.SCREEN_HEIGHT - 1)
- term.clearLine()
- end
- -- Redraws the current time at the bottom right of the screen.
- function desktop.drawTime()
- term.setTextColor(desktop.TEXT_COLOR)
- local currentTime = textutils.formatTime(os.time(), false)
- term.setCursorPos(desktop.SCREEN_WIDTH - currentTime:len(), desktop.SCREEN_HEIGHT - 1)
- term.write(currentTime)
- end
- -- Redraws the desktop.
- function desktop.draw()
- desktop.clear()
- desktop.drawTime()
- end
- -- This is the function that will be used to create the desktop thread object.
- function desktop.main()
- desktop.draw()
- while true do
- desktop.drawTime()
- local event = {os.pullEvent()}
- end
- end
- -- Create a window for the desktop and add its hash to the desktop table.
- local windowThread = process.newWindow(1, 1, desktop.SCREEN_WIDTH, desktop.SCREEN_HEIGHT, "Desktop", desktop.main)
- process.addListenerToThread(windowThread.threadID, "timer")
- --process.addListenerToThread(windowThread.threadID, "key")
- --process.addListenerToThread(windowThread.threadID, "char")
- end
- -- Native threads/background processes --
- function edit()
- term.setCursorPos(1, 1)
- while true do
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- local s = read()
- end
- end
- initDesktop()
- process.newWindow(5, 5, 20, 12, "Edit", edit)
- process.runThreads()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement