Advertisement
Jummit

Computercraft: Multitasking Windows API

Apr 12th, 2018
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.31 KB | None | 0 0
  1. local width, height = term.getSize()
  2. local programs = {}
  3. local userEvents = {"mouse_click", "mouse_up", "mouse_drag", "char", "key", "monitor_touch", "key_up", "paste", "terminate"}
  4.  
  5. programs.new = function(func, x, y, w, h)
  6.   local x = x or 1
  7.   local y = y or 1
  8.   local w = w or widht
  9.   local h = h or height
  10.   local program = {
  11.     x = x, y = y, w = w, h = h,
  12.     term = window.create(
  13.       term.current(), x, y, w, h
  14.     ),
  15.     selected = false,
  16.     coroutine = coroutine.create(func),
  17.     reposition = function(self, x, y)
  18.       self.x, self.y = x, y
  19.       self.term.reposition(x, y)
  20.     end,
  21.     resize = function(self, w, h)
  22.       oldX, oldY = self.term.getPosition()
  23.       self.term.reposition(oldX, oldY, w, h)
  24.       os.queueEvent("term_resize")
  25.     end,
  26.     reset = function(self, x, y, w, h)
  27.       self.x, self.y, self.w, self.h = x, y, w, h
  28.       self.term.reposition(x, y, w, h)
  29.       os.queueEvent("term_resize")
  30.     end
  31.   }
  32.   return program
  33. end
  34.  
  35. local updateProgram = function(programs, programNum, event, var1, var2, var3, isUserEvent)
  36.   local program = programs[programNum]
  37.   local event, var1, var2, var3 = event, var1, var2, var3
  38.  
  39.   -- redirect to programs terminal
  40.   local oldTerm = term.redirect(program.term)
  41.  
  42.   -- give the mouse click as seen from the program window
  43.   if string.sub(event, 1, #"mouse") == "mouse" then
  44.     var2 = var2-program.x+1
  45.     var3 = var3-program.y+1
  46.   end
  47.  
  48.   -- find out if the program window is clicked
  49.   if event == "mouse_click" and var2>=0 and var3>=0 and var2<=program.w and var3<=program.h then
  50.     -- select this program and deselect every other one
  51.     for programNum = 1, #programs do
  52.       programs[programNum].selected = false
  53.     end
  54.     program.selected = true
  55.     if var3 == 0 then
  56.       program.barSelected = true
  57.       program.barSelectedX = var2
  58.       if var2 == 1 then
  59.         program.resizeIconSelected = true
  60.       end
  61.       if var2 == program.w then
  62.         table.remove(programs, programNum)
  63.         term.redirect(oldTerm)
  64.         return
  65.       end
  66.     end
  67.  
  68.     -- resort program table
  69.  
  70.     local selectedProgram
  71.     for i = 1, #programs do
  72.       if programs[i].selected then
  73.         selectedProgram = programs[i]
  74.         table.remove(programs, i)
  75.         break
  76.       end
  77.     end
  78.     table.insert(programs, selectedProgram)
  79.   end
  80.  
  81.   -- move window when mouse is dragged
  82.   if event == "mouse_drag" and program.barSelected then
  83.     if program.resizeIconSelected then
  84.       program:reset(program.x + var2-program.barSelectedX, program.y+var3, program.w-var2+1, program.h-var3)
  85.     else
  86.       program:reposition(program.x + var2-program.barSelectedX, program.y+var3)
  87.     end
  88.   end
  89.  
  90.   -- deselect bar if mouse is released
  91.   if event == "mouse_up" then
  92.     program.barSelected = false
  93.     program.resizeIconSelected = false
  94.   end
  95.  
  96.   -- only give program user events if selected
  97.   if isUserEvent and not program.selected then
  98.     event, var1, var2, var3 = ""
  99.   end
  100.  
  101.   -- resume program
  102.   coroutine.resume(program.coroutine, event, var1, var2, var3)
  103.  
  104.   -- delete program if it is finished
  105.   if coroutine.status(program.coroutine) == "dead" then
  106.     table.remove(programs, programNum)
  107.     term.redirect(oldTerm)
  108.     return true
  109.   end
  110.  
  111.   program.term.redraw()
  112.   term.redirect(oldTerm)
  113.  
  114.   -- draw line above program
  115.   if program.selected then
  116.     term.setBackgroundColor(colors.lightGray)
  117.     term.setTextColor(colors.gray)
  118.   else
  119.     term.setBackgroundColor(colors.gray)
  120.     term.setTextColor(colors.lightGray)
  121.   end
  122.   paintutils.drawLine(program.x, program.y-1, program.x+program.w-1, program.y-1)
  123.  
  124.   -- draw resize icon
  125.   term.setCursorPos(program.x, program.y-1)
  126.   term.write("/")
  127.  
  128.   -- draw close icon
  129.   term.setCursorPos(program.x+program.w-1, program.y-1)
  130.   term.setTextColor(colors.orange)
  131.   term.write("x")
  132. end
  133.  
  134. programs.update = function(programs, event, var1, var2, var3)
  135.   -- check if event is made from the user
  136.   local isUserEvent = false
  137.   for userEventNum = 1, #userEvents do
  138.     local userEvent = userEvents[userEventNum]
  139.     if event == userEvent then
  140.       isUserEvent = true
  141.       break
  142.     end
  143.   end
  144.  
  145.   -- update every program
  146.   for programNum = 1, #programs do
  147.     if updateProgram(programs, programNum, event, var1, var2, var3, isUserEvent) then break end
  148.   end
  149. end
  150.  
  151. return programs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement