Advertisement
Ceziy

AppLib.lua

Apr 13th, 2024
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.83 KB | None | 0 0
  1. callUpdate = true
  2. bgColor = colors.black
  3. space = term
  4.  
  5. objects = {}
  6. buttons = {}
  7.  
  8. function centerText(w, ln)
  9.     return math.ceil((w - ln) / 2)
  10.  end
  11.  
  12. --класс текста
  13. Text={}
  14. function Text:new(t, x, y, col)
  15.     local obj = {text = t or "", x = x or 0, y = y or 0, color = col or colors.white}
  16.  
  17.     function obj:blit()
  18.         table.insert(objects, self)
  19.     end
  20.     function obj:draw()
  21.         space.setCursorPos(obj.x, obj.y)
  22.         space.setTextColor(obj.color)
  23.         space.write(obj.text)
  24.     end
  25.     function obj:getPos()
  26.         return self.x, self.y
  27.     end
  28.     function obj:setPos(x, y)
  29.         obj.x = x
  30.         obj.y = y
  31.     end
  32.     function obj:setColor(col)
  33.         obj.color = col
  34.     end
  35.  
  36.     setmetatable(obj, self)
  37.     self.__index = self
  38.     return obj
  39. end
  40.  
  41. --класс кнопки
  42. Button={}
  43. function Button:new(x, y, endx, endy, col, prcol, text, func)
  44.     local obj = {}
  45.     obj.x = x or 0
  46.     obj.y = y or 0
  47.     obj.endx = endx or 0
  48.     obj.endy = endy or 0
  49.     obj.color = col or colors.white
  50.     obj.prColor = prcol or obj.color
  51.     obj.buffCol = obj.color
  52.     obj.pressed = false
  53.     obj.func = func or function() end
  54.     obj.text = text or ""
  55.  
  56.     function obj:blit()
  57.         table.insert(objects, self)
  58.         table.insert(buttons, self)
  59.     end
  60.     function obj:draw()
  61.         paintutils.drawFilledBox(obj.x,obj.y,obj.endx,obj.endy,obj.color)
  62.         if string.len(obj.text) <= (obj.endx - obj.x) and string.len(obj.text) ~= 0 then
  63.             space.setBackgroundColor(obj.color)
  64.             space.setCursorPos(obj.x + centerText(obj.endx - obj.x, string.len(obj.text)), obj.y + centerText(obj.endy - obj.y, 0))
  65.             space.setTextColor(colors.white)
  66.             space.write(obj.text)
  67.             space.setBackgroundColor(bgColor)
  68.         end
  69.     end
  70.     function obj:getPos()
  71.         return self.x, self.y
  72.     end
  73.     function obj:setPos(x, y)
  74.         obj.endx = x + obj.endx - obj.x
  75.         obj.endy = y + obj.y
  76.         obj.x = x
  77.         obj.y = y
  78.     end
  79.     function obj:setColor(col)
  80.         --paintutils.drawFilledBox(obj.x,obj.y,obj.endx,obj.endy,col)
  81.         callUpdate = true
  82.     end
  83.  
  84.     setmetatable(obj, self)
  85.     self.__index = self
  86.     return obj
  87. end
  88.  
  89. --Луп функция
  90. function mainloop()
  91.     while true do
  92.         if callUpdate == true then
  93.             space.clear()
  94.             for i = 1, table.getn(objects) do
  95.                 objects[i].draw()
  96.             end
  97.             callUpdate = false
  98.             space.setBackgroundColor(bgColor)
  99.         end
  100.         parallel.waitForAny(
  101.             function ()
  102.                 local mdevent, mdbutton, mdx, mdy = os.pullEvent("mouse_click")
  103.                 if mdbutton == 1 then
  104.                     for i = 1, table.getn(buttons) do
  105.                         o = buttons[i]
  106.                         if mdx >= o.x and mdy >= o.y and mdx <= o.endx and mdy <= o.endy then
  107.                             buttons[i].color = buttons[i].prColor
  108.                             callUpdate = true
  109.                             if buttons[i].pressed == false then
  110.                                 buttons[i].func()
  111.                             end
  112.                             buttons[i].pressed = true
  113.                         end
  114.                     end
  115.                 end
  116.             end,
  117.            
  118.             function()
  119.                 local muevent, mubutton, mux, muy = os.pullEvent("mouse_up")
  120.                 if mubutton == 1 then
  121.                     for i = 1, table.getn(buttons) do
  122.                         o = buttons[i]
  123.                         buttons[i].color = buttons[i].buffCol
  124.                         buttons[i].pressed = false
  125.                         callUpdate = true
  126.                     end
  127.                 end
  128.             end
  129.         )
  130.        
  131.         os.sleep(0);
  132.     end
  133. end
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement