NeOzay

panel.lua

Aug 4th, 2021 (edited)
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.36 KB | None | 0 0
  1. local Color = require("color")
  2. local Button = require("button")
  3.  
  4. local TO = require("textObject")
  5. local TextObject = TO.TextObject
  6. local drawtO = TO.drawtO
  7.  
  8.  
  9.  
  10.  
  11.  
  12. ---staticText
  13. ---@param tO textObject
  14. ---@param x number
  15. ---@param y number
  16. local function staticText(tO, x, y)
  17.     ---@class  staticText
  18.     ---@field tO textObject
  19.     ---@field x number
  20.     ---@field y number
  21.     local sT = {}
  22.     sT.tO = tO
  23.     sT.x = x
  24.     sT.y = y
  25.     return sT
  26. end
  27.  
  28. ---@param tO textObject
  29. ---@param x number
  30. ---@param y number
  31. local function fieldText(tO,x,y)
  32.     ---@class fieldText : staticText
  33.     local fT = {}
  34.  
  35.  
  36.     return fT
  37. end
  38.  
  39. local function Panel()
  40.     ---@class panel
  41.     ---@field x number
  42.     ---@field y number
  43.     ---@field hight number
  44.     ---@field width number
  45.     ---@field title staticText
  46.     ---@field statics staticText[]
  47.     ---@field field
  48.     ---@field button button[]
  49.     ---@field defautBackgroundColor color
  50.     ---@field defautForegroundColor color
  51.     local p = {}
  52.     p.statics = {}
  53.     p.button = {}
  54.     p.x = 1
  55.     p.y = 1
  56.     p.hight = 1
  57.     p.width = 1
  58.     p.defautForegroundColor = Color("white")
  59.     p.defautBackgroundColor = Color("black")
  60.     ---@param x number
  61.     ---@param y number
  62.     function p:setPos(x, y)
  63.         self.x = x
  64.         self.y = y
  65.     end
  66.  
  67.     ---@param pcolor color
  68.     function p:setDefautBackgroundColor(pcolor)
  69.         self.defautBackgroundColor = pcolor
  70.     end
  71.  
  72.     ---@param pcolor color
  73.     function p:setDefautForegroundColor(pcolor)
  74.         self.defautForegroundColor = pcolor
  75.     end
  76.  
  77.     ---@param width number
  78.     ---@param hight number
  79.     function p:setSize(width, hight)
  80.         self.hight = hight
  81.         self.width = width
  82.     end
  83.  
  84.     ---@param tO textObject
  85.     ---@param x number
  86.     ---@param y number
  87.     function p:setTitle(tO, x, y)
  88.         self.title = staticText(tO, x, y)
  89.     end
  90.  
  91.     ---@param field staticText
  92.     function p:centerText(field)
  93.         local textWidth = #field.tO.text / 2
  94.         local width = self.width / 2
  95.         field.x = width - textWidth
  96.     end
  97.  
  98.     ---addStatic
  99.     ---@param tO textObject
  100.     ---@param x number
  101.     ---@param y number
  102.     function p:addStatic(tO, x, y)
  103.         local sT = staticText(tO, x, y)
  104.         table.insert(self.statics, sT)
  105.     end
  106.  
  107.     ---addButton
  108.     ---@param button button
  109.     ---@param x number
  110.     ---@param y number
  111.     function p:addButton(button, x, y)
  112.         if x and y then
  113.             button:setPos(x, y)
  114.         end
  115.         button:setOffset(self.x,self.y)
  116.         table.insert(self.button, button)
  117.     end
  118.  
  119.     ---@param tO textObject
  120.     ---@param x number
  121.     ---@param y number
  122.     function p:drawtO(tO, x, y)
  123.         local bg
  124.         local fg
  125.         if tO.foregroundColor then
  126.             fg = tO.foregroundColor
  127.         else
  128.             fg = self.defautForegroundColor
  129.         end
  130.  
  131.         if tO.backgroundColor then
  132.             bg = tO.backgroundColor
  133.         else
  134.             bg = self.defautBackgroundColor
  135.         end
  136.         --print(bg.color)
  137.         gpu.setBackground(bg.color, bg.isPaletteIndex)
  138.         gpu.setForeground(fg.color, fg.isPaletteIndex)
  139.         gpu.set(x, y, tO.text)
  140.     end
  141.  
  142.     function p:drawTitle()
  143.         p:drawtO(self.title.tO, self.title.x + self.x, self.title.y + self.y)
  144.     end
  145.  
  146.     ---@param frameColor color
  147.     ---@param backgroundColor color
  148.     ---@overload fun(frameColor:color)
  149.     ---@overload fun()
  150.     function p:drawFrame(frameColor, backgroundColor)
  151.         if not backgroundColor then
  152.             backgroundColor = self.defautBackgroundColor
  153.         end
  154.         if not frameColor then
  155.             frameColor = self.defautForegroundColor
  156.         end
  157.         gpu.setBackground(frameColor.color, frameColor.isPaletteIndex)
  158.         gpu.fill(self.x, self.y, self.width, self.hight, " ")
  159.  
  160.         gpu.setBackground(backgroundColor.color, backgroundColor.isPaletteIndex)
  161.         gpu.fill(self.x + 1, self.y + 1, self.width - 2, self.hight - 2, " ")
  162.  
  163.     end
  164.  
  165.     function p:drawStatics()
  166.         for i, sT in ipairs(self.statics) do
  167.             p:drawtO(sT.tO, sT.x + self.x, sT.y + self.y)
  168.         end
  169.     end
  170.  
  171.     function p:drawButtons()
  172.         for k, button in pairs(self.button) do
  173.             if button.status then
  174.                 for i, color in ipairs(button.textureON) do
  175.                     drawtO(TextObject(" ", color, color), self.x+button.x + i - 1, self.y+button.y)
  176.                 end
  177.             else
  178.                 for i, color in ipairs(button.textureOFF) do
  179.                     drawtO(TextObject(" ", color, color), self.x+button.x + i - 1, self.y+button.y)
  180.                 end
  181.             end
  182.         end
  183.     end
  184.  
  185.  
  186.     function p:hide()
  187.         gpu.setBackground(0x000000)
  188.         gpu.fill(self.x, self.y, self.width, self.hight, " ")
  189.     end
  190.  
  191.     function p:drawAll()
  192.         self:drawTitle()
  193.         self:drawStatics()
  194.         self:drawButtons()
  195.     end
  196.  
  197.     return p
  198. end
  199.  
  200. return Panel
Add Comment
Please, Sign In to add comment