Something went wrong. Try again.
System for ComputerCraft
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203local windowManager = {}
local parentTerm = term.current()local w, h = parentTerm.getSize()
-- tProcesseslocal processList = {}local currentProcess = nil
local function resumeProcess(processName, event, ...) local process = processList[processName] local filter = process.filter if filter == nil or filter == event or event == "terminate" then local nPreviousProcess = currentProcess currentProcess = processName term.redirect(process.terminal) local ok, result = coroutine.resume(process.co, event, ...) process.terminal = term.current() if ok then process.sFilter = result else print(result) end currentProcess = nPreviousProcess endend
local function launchProcess(focusWindow, env, path, ...) local args = table.pack(...) local nProcess = #processList + 1 local newProcess = {} newProcess.window = window.create(parentTerm, 1, 1, w, h, false) newProcess.co = coroutine.create(function() os.run(env, path, table.unpack(args, 1, args.n)) end) newProcess.filter = nil newProcess.terminal = newProcess.window newProcess[nProcess] = newProcess if focusWindow then windowManager.selectProcess(nProcess) end resumeProcess(nProcess) return nProcessend
function windowManager.selectProcess(newProcessName) if currentProcess ~= newProcessName then if currentProcess then local oldProcess = processList[currentProcess] oldProcess.window.setVisible(false) end currentProcess = newProcessName if currentProcess then local process = processList[currentProcess] process.window.setVisible(true) end endend
local function resizeWindows() local windowY, windowHeight windowY = 1 windowHeight = h
for i = 1, #processList do local process = processList[i] local x, y = process.window.getCursorPos() if y > windowHeight then process.window.scroll(y - windowHeight) process.window.setCursorPos(x, windowHeight) end process.window.reposition(1, windowY, w, windowHeight) end bWindowsResized = trueend
function windowManager.launch(focus, env, path, ...) local previousTerm = term.current() local result = launchProcess(focus, env, path, ...) term.redirect(previousTerm) return resultend
function windowManager.getCurrentProcess() return currentProcessend
function windowManager.getCount() return #processListend
parentTerm.clear()setMenuVisible(false)launchProcess(true, { ["shell"] = shell, ["multishell"] = multishell,}, "/rom/programs/shell.lua")
-- Run processeswhile #tProcesses > 0 do -- Get the event local tEventData = table.pack(os.pullEventRaw()) local sEvent = tEventData[1] if sEvent == "term_resize" then -- Resize event w, h = parentTerm.getSize() resizeWindows() redrawMenu()
elseif sEvent == "char" or sEvent == "key" or sEvent == "key_up" or sEvent == "paste" or sEvent == "terminate" then -- Keyboard event -- Passthrough to current process resumeProcess(nCurrentProcess, table.unpack(tEventData, 1, tEventData.n)) if cullProcess(nCurrentProcess) then setMenuVisible(#tProcesses >= 2) redrawMenu() end
elseif sEvent == "mouse_click" then -- Click event local button, x, y = tEventData[2], tEventData[3], tEventData[4] if bShowMenu and y == 1 then -- Switch process if x == 1 and nScrollPos ~= 1 then nScrollPos = nScrollPos - 1 redrawMenu() elseif bScrollRight and x == term.getSize() then nScrollPos = nScrollPos + 1 redrawMenu() else local tabStart = 1 if nScrollPos ~= 1 then tabStart = 2 end for n = nScrollPos, #tProcesses do local tabEnd = tabStart + #tProcesses[n].sTitle + 1 if x >= tabStart and x <= tabEnd then selectProcess(n) redrawMenu() break end tabStart = tabEnd + 1 end end else -- Passthrough to current process resumeProcess(nCurrentProcess, sEvent, button, x, bShowMenu and y - 1 or y) if cullProcess(nCurrentProcess) then setMenuVisible(#tProcesses >= 2) redrawMenu() end end
elseif sEvent == "mouse_drag" or sEvent == "mouse_up" or sEvent == "mouse_scroll" then -- Other mouse event local p1, x, y = tEventData[2], tEventData[3], tEventData[4] if bShowMenu and sEvent == "mouse_scroll" and y == 1 then if p1 == -1 and nScrollPos ~= 1 then nScrollPos = nScrollPos - 1 redrawMenu() elseif bScrollRight and p1 == 1 then nScrollPos = nScrollPos + 1 redrawMenu() end elseif not (bShowMenu and y == 1) then -- Passthrough to current process resumeProcess(nCurrentProcess, sEvent, p1, x, bShowMenu and y - 1 or y) if cullProcess(nCurrentProcess) then setMenuVisible(#tProcesses >= 2) redrawMenu() end end
else -- Other event -- Passthrough to all processes local nLimit = #tProcesses -- Storing this ensures any new things spawned don't get the event for n = 1, nLimit do resumeProcess(n, table.unpack(tEventData, 1, tEventData.n)) end if cullProcesses() then setMenuVisible(#tProcesses >= 2) redrawMenu() end end
if bWindowsResized then -- Pass term_resize to all processes local nLimit = #tProcesses -- Storing this ensures any new things spawned don't get the event for n = 1, nLimit do resumeProcess(n, "term_resize") end bWindowsResized = false if cullProcesses() then setMenuVisible(#tProcesses >= 2) redrawMenu() end endend
-- Shutdownterm.redirect(parentTerm)
return windowManager