Advertisement
PaymentOption

Kernel

Jan 20th, 2013
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.58 KB | None | 0 0
  1. -- NOTE: process.runThreads() has a timer that will trigger every second in the case that the user
  2. -- is not doing anything in order to update threads that may be running in the background.
  3. os.loadAPI("process")
  4.  
  5.  
  6. -- Variables --
  7. local windows = {} -- This is the table where all of the windows that are created will be kept.
  8. -- Varibales --
  9.  
  10.  
  11.  
  12. -- Window functions --
  13. -- Returns the current window stack.
  14. function getWindows()
  15.  
  16.     return windows
  17.  
  18. end
  19.  
  20. -- Creates a new window using the process API. When this window is created, it is added to the
  21. -- 'windows' table. However, this window cannot be resized, but can be moved. The index of this
  22. -- window in the windows table is returned. NOTE: The index returned is also the the hash for
  23. -- its thread in the threads table.
  24. function newWindow(x, y, width, height, title, method)
  25.  
  26.     -- Capture the newly created window object.
  27.     local window = process.newWindow(x, y, width, height, title, method)
  28.     windows[window.threadID] = window
  29.    
  30.     return window.threadID
  31.    
  32. end
  33.  
  34. -- Moves the window at the given hash. NOTE: It is best to get the window object using getWindow(), rather than
  35. -- continuing to call on this function. However, this is okay to use for quick operations.
  36. -- Returns true if the window was moved successfully; false if not.
  37. function moveWindow(windowHash, x, y)
  38.  
  39.     if windows[windowHash] then
  40.         windows[windowHash].x = x
  41.         windows[windowHash].y = y
  42.     end
  43.  
  44. end
  45.  
  46. -- Returns the window object at the given hash.
  47. function getWindow(windowHash)
  48.  
  49.     return windows[windowHash]
  50.  
  51. end
  52.  
  53. -- Removes the window at the given hash. Returns true if the window as removed successfully; false if not.
  54. function removeWindow(windowHash)
  55.  
  56.     if windows[windowHash] then
  57.         windows[windowHash] = nil
  58.         return true
  59.     end
  60.    
  61.     return false
  62.  
  63. end
  64.  
  65. -- Iterates through all of the windows in the windows table and cleans out any of the dead ones.
  66. function scrapDeadWindows()
  67.  
  68.     for windowHash, window in pairs(windows) do
  69.         if not window.running then
  70.             windows[windowHash] = nil
  71.         end
  72.     end
  73.  
  74. end
  75. -- Window functions --
  76.  
  77.  
  78.  
  79. -- Native threads/windows/background processes --
  80. -- This is the base desktop window that sits on the bottom of the stack.
  81. -- The desktop thread has a listener for the following events: key, mouse_click, timer
  82. function initDesktop()
  83.  
  84.     local desktop = {}
  85.    
  86.     desktop.SCREEN_WIDTH, desktop.SCREEN_HEIGHT = term.getSize()
  87.     desktop.BACKGROUND_COLOR = colors.white
  88.     desktop.TEXT_COLOR       = colors.black
  89.     desktop.BOTTOM_BAR_COLOR = colors.lightGray
  90.        
  91.     -- Clears the desktop.
  92.     function desktop.clear()
  93.        
  94.         term.setBackgroundColor(desktop.BACKGROUND_COLOR)
  95.         for line = 1, desktop.SCREEN_HEIGHT do
  96.             term.setCursorPos(1, line)
  97.             term.clearLine()
  98.         end
  99.        
  100.         -- Redraw bottom bar.
  101.         term.setBackgroundColor(desktop.BOTTOM_BAR_COLOR)
  102.         term.setCursorPos(1, desktop.SCREEN_HEIGHT - 1)
  103.         term.clearLine()
  104.        
  105.     end
  106.    
  107.     -- Redraws the current time at the bottom right of the screen.
  108.     function desktop.drawTime()
  109.    
  110.         term.setTextColor(desktop.TEXT_COLOR)
  111.         local currentTime = textutils.formatTime(os.time(), false)
  112.        
  113.         term.setCursorPos(desktop.SCREEN_WIDTH - currentTime:len(), desktop.SCREEN_HEIGHT - 1)
  114.         term.write(currentTime)
  115.    
  116.     end
  117.        
  118.     -- Redraws the desktop.
  119.     function desktop.draw()
  120.    
  121.         desktop.clear()
  122.         desktop.drawTime()
  123.    
  124.     end
  125.        
  126.     -- This is the function that will be used to create the desktop thread object.
  127.     function desktop.main()
  128.    
  129.         desktop.draw()
  130.         while true do
  131.             desktop.drawTime()
  132.             local event = {os.pullEvent()}
  133.         end
  134.    
  135.     end
  136.    
  137.     -- Create a window for the desktop and add its hash to the desktop table.
  138.     local windowThread = process.newWindow(1, 1, desktop.SCREEN_WIDTH, desktop.SCREEN_HEIGHT, "Desktop", desktop.main)
  139.     process.addListenerToThread(windowThread.threadID, "timer")
  140.     --process.addListenerToThread(windowThread.threadID, "key")
  141.     --process.addListenerToThread(windowThread.threadID, "char")
  142.  
  143. end
  144. -- Native threads/background processes --
  145.  
  146. function edit()
  147.  
  148.     term.setCursorPos(1, 1)
  149.     while true do
  150.         term.setBackgroundColor(colors.white)
  151.         term.setTextColor(colors.black)
  152.         local s = read()
  153.     end
  154.  
  155. end
  156.  
  157. initDesktop()
  158. process.newWindow(5, 5, 20, 12, "Edit", edit)
  159. process.runThreads()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement