Advertisement
asshousotn

kernel

Apr 29th, 2025 (edited)
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.65 KB | None | 0 0
  1. -- kernel.lua
  2. local window = require("window")
  3.  
  4. local windowManager = {
  5.     windows = {},
  6.     nextWindowId = 1
  7. }
  8.  
  9. function windowManager.addWindow(x, y, width, height, title)
  10.     local win = window.create(x, y, width, height, title)
  11.     table.insert(windowManager.windows, win)
  12.     return win
  13. end
  14.  
  15. function windowManager.drawAll()
  16.     term.setBackgroundColour(colours.black)
  17.     term.clear()
  18.    
  19.     -- Сортируем окна по zIndex
  20.     table.sort(windowManager.windows, function(a, b) return a.zIndex < b.zIndex end)
  21.    
  22.     for _, win in ipairs(windowManager.windows) do
  23.         if win.active and not win.minimized then
  24.             window.draw(win)
  25.         end
  26.     end
  27. end
  28.  
  29. function windowManager.handleEvents()
  30.     while true do
  31.         windowManager.drawAll()
  32.         local event = {os.pullEvent()}
  33.        
  34.         -- Обрабатываем события для всех окон (сверху вниз)
  35.         local handled = false
  36.         for i = #windowManager.windows, 1, -1 do
  37.             local win = windowManager.windows[i]
  38.             if win.active then
  39.                 handled = window.handleEvent(win, event, windowManager.windows)
  40.                 if handled then break end
  41.             end
  42.         end
  43.        
  44.         -- Глобальные горячие клавиши
  45.         if not handled and event[1] == "key" then
  46.             if event[2] == keys.q then
  47.                 term.clear()
  48.                 term.setCursorPos(1, 1)
  49.                 print("Shutting down...")
  50.                 return
  51.             elseif event[2] == keys.n then
  52.                 local win = windowManager.addWindow(
  53.                     math.random(5, 15),
  54.                     math.random(3, 10),
  55.                     math.random(20, 40),
  56.                     math.random(10, 20),
  57.                     "Window " .. windowManager.nextWindowId
  58.                 )
  59.                 windowManager.nextWindowId = windowManager.nextWindowId + 1
  60.                
  61.                 -- Заполняем окно тестовым содержимым
  62.                 window.setBackgroundColour(win, colours.blue)
  63.                 window.setTextColour(win, colours.white)
  64.                 window.setCursorPos(win, 3, 3)
  65.                 window.write(win, "Hello from Window " .. (windowManager.nextWindowId - 1))
  66.             end
  67.         end
  68.     end
  69. end
  70.  
  71. -- Инициализация и запуск
  72. local function init()
  73.     term.clear()
  74.     term.setCursorPos(1, 1)
  75.     print("MyOS v0.3 (Native Windows)")
  76.     print("Press: n-new window, q-quit")
  77.    
  78.     -- Запускаем менеджер окон
  79.     windowManager.handleEvents()
  80. end
  81.  
  82. init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement