Advertisement
asshousotn

wm

Apr 30th, 2025
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.17 KB | None | 0 0
  1. -- wm.lua - менеджер окон
  2. local window = require("window")
  3.  
  4. local wm = {
  5.     windows = {},
  6.     focused = nil
  7. }
  8.  
  9. -- Создание нового окна
  10. function wm:createWindow(x, y, width, height, title)
  11.     local win = window.create(x, y, width, height, title)
  12.     table.insert(self.windows, win)
  13.     self:setFocused(win)
  14.     return win
  15. end
  16.  
  17. -- Установка фокуса
  18. function wm:setFocused(win)
  19.     if self.focused then
  20.         self.focused:setActive(false)
  21.     end
  22.     self.focused = win
  23.     if win then
  24.         win:setActive(true)
  25.         -- Перемещаем окно наверх
  26.         for i, w in ipairs(self.windows) do
  27.             if w == win then
  28.                 table.remove(self.windows, i)
  29.                 table.insert(self.windows, 1, win)
  30.                 break
  31.             end
  32.         end
  33.     end
  34. end
  35.  
  36. -- Обработка событий
  37. function wm:handleEvent(event, ...)
  38.     if event == "mouse_click" then
  39.         local x, y, button = ...
  40.         local handled = false
  41.        
  42.         -- Проверяем окна сверху вниз (обратный порядок для Z-индекса)
  43.         for i = #self.windows, 1, -1 do
  44.             local win = self.windows[i]
  45.             if win:handleClick(x, y, button) then
  46.                 self:setFocused(win)
  47.                 handled = true
  48.                 break
  49.             end
  50.         end
  51.        
  52.         return handled
  53.     elseif event == "mouse_drag" then
  54.         if self.focused then
  55.             return self.focused:handleDrag(...)
  56.         end
  57.     end
  58.     return false
  59. end
  60.  
  61. -- Отрисовка всех окон
  62. function wm:drawAll()
  63.     term.setBackgroundColor(colors.black)
  64.     term.clear()
  65.    
  66.     -- Рисуем окна снизу вверх (правильный порядок для Z-индекса)
  67.     for i = #self.windows, 1, -1 do
  68.         self.windows[i]:draw()
  69.     end
  70. end
  71.  
  72. -- Основной цикл обработки событий
  73. function wm:run()
  74.     while true do
  75.         self:drawAll()
  76.         local event = {os.pullEvent()}
  77.         self:handleEvent(event[1], table.unpack(event, 2))
  78.     end
  79. end
  80.  
  81. return wm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement