Advertisement
AssortedBrunoz

api

Oct 31st, 2024 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.20 KB | None | 0 0
  1. if not fs.exists("stringtools.lua") then
  2.     term.setTextColour(colors.red)
  3.     print("stringtools not detected, it is required for button_api to work, now downloading..")
  4.     shell.run("wget https://raw.githubusercontent.com/banana-boye/CC_stringtools/refs/heads/main/stringtools.lua stringtools.lua")
  5. else
  6.     require("stringtools")()
  7. end
  8.  
  9. local base = {
  10.     buttons = {}
  11. }
  12.  
  13. ---@class Style
  14. ---@field background_color number
  15. ---@field border_color number
  16.  
  17. function Style(background_color, border_color)
  18.     local style_obj = {}
  19.     style_obj.backgroundColor = background_color and background_color or colors.white
  20.     style_obj.borderColor = border_color and border_color or style_obj.backgroundColor
  21.     style_obj.text = ""
  22.     style_obj.textColor = style_obj.backgroundColor
  23.     style_obj.animate = false
  24.     style_obj.animationType = "linear"
  25.     style_obj.animationTime = 10
  26.     style_obj.s_x = nil
  27.     style_obj.s_y = nil
  28.  
  29.     return style_obj
  30. end
  31.  
  32. ---@class Position
  33. ---@field MAX_X number
  34. ---@field MAX_Y number
  35. ---@field xtype string
  36. ---@field ytype string
  37. ---@field text string
  38. ---@field buffer_x number
  39. ---@field buffer_y number
  40.  
  41. function Position(MAX_X, MAX_Y, xtype, ytype, text, buffer_x, buffer_y)
  42.     local coordinates = {}
  43.     local text_height = text and string.count(text, "\n") or 0
  44.     buffer_x = buffer_x and buffer_x or 0
  45.     buffer_y = buffer_y and buffer_y or 0
  46.     coordinates.s_x = 1
  47.     coordinates.s_y = 1
  48.     coordinates.e_x = 1
  49.     coordinates.e_y = 1
  50.     coordinates.buffer_x = buffer_x
  51.     coordinates.buffer_y = buffer_y
  52.  
  53.     if xtype == "centered" then
  54.         coordinates.s_x = text and MAX_X/2 - #text/2 - buffer_x or MAX_X/2 - buffer_x
  55.         coordinates.e_x = text and MAX_X/2 + #text/2 + buffer_x -1 or MAX_X/2 + buffer_x
  56.     elseif xtype == "left" then
  57.         coordinates.s_x = 1
  58.         coordinates.e_x = text and #text + buffer_x*2 or 1+buffer_x*2
  59.     elseif xtype == "right" then
  60.         coordinates.s_x = text and MAX_X - #text - buffer_x*2 or MAX_X - buffer_x*2-1
  61.         coordinates.e_x = MAX_X
  62.     end
  63.  
  64.     if ytype == "centered" then
  65.         coordinates.s_y = text and MAX_Y/2-text_height/2 - buffer_y or MAX_Y/2 + buffer_y
  66.         coordinates.e_y = text and MAX_Y/2+text_height/2 + buffer_y or MAX_Y/2 + buffer_y
  67.     elseif ytype == "top" then
  68.         coordinates.s_y = 1
  69.         coordinates.e_y = text and 1 + text_height + buffer_y * 2 or 1 + buffer_y * 2
  70.     elseif ytype == "bottom" then
  71.         coordinates.s_y = text and MAX_Y - text_height - buffer_y * 2 or MAX_Y - buffer_y * 2
  72.         coordinates.e_y = MAX_Y
  73.     end
  74.  
  75.     return coordinates
  76. end
  77.  
  78. ---@field position Position
  79. ---@field style Style
  80. ---@field exec function
  81. function base.addButton(position, style, exec)    
  82.     table.insert(base.buttons, {position, style, exec})
  83. end
  84.  
  85. function base.clearButtons()
  86.     base.buttons = {}
  87. end
  88.  
  89. local function render(button)
  90.     local position, style, exec = table.unpack(button)
  91.  
  92.     paintutils.drawFilledBox(position.s_x,position.s_y,position.e_x,position.e_y, style.backgroundColor)
  93.     paintutils.drawBox(position.s_x,position.s_y,position.e_x,position.e_y, style.borderColor)
  94.     term.setCursorPos(position.s_x+position.buffer_x, position.s_y+position.buffer_y)
  95.     term.setTextColor(style.textColor)
  96.     term.setBackgroundColor(style.backgroundColor)
  97.     write(style.text)
  98. end
  99.  
  100. function base:render()
  101.  
  102.     local animations = {}
  103.  
  104.     for _, button in pairs(self.buttons) do
  105.  
  106.         local position, style, exec = table.unpack(button)
  107.  
  108.         if style.animate then
  109.             style.s_x = style.s_x and style.s_x or position.s_x
  110.             style.s_y = style.s_y and style.s_y or position.s_y
  111.  
  112.             table.insert(animations,
  113.             function()
  114.                 for i = math.abs(position.s_x - style.s_x), 0, -1 do
  115.                     if math.abs(position.s_x - i) > 0 then
  116.                         paintutils.drawFilledBox(position.s_x-i,position.s_y,position.e_x-i,position.e_y, style.backgroundColor)
  117.                         term.setCursorPos(position.s_x+position.buffer_x-i, position.s_y+position.buffer_y)
  118.                         term.setTextColor(style.textColor)
  119.                         write(style.text)
  120.                         paintutils.drawBox(position.s_x-i,position.s_y,position.e_x-i,position.e_y, style.borderColor)
  121.                     end
  122.                     os.sleep(style.animationTime)
  123.                     paintutils.drawFilledBox(position.s_x-i,position.s_y,position.e_x-i,position.e_y, colors.black)
  124.                 end
  125.             end)
  126.             table.insert(animations,
  127.             function()
  128.                 for i = math.abs(position.s_y - style.s_y), 0, -1 do
  129.                     if math.abs(position.s_y - i) > 0 then
  130.                         paintutils.drawFilledBox(position.s_x,position.s_y-i,position.e_x,position.e_y-i, style.backgroundColor)
  131.                         term.setCursorPos(position.s_x+position.buffer_x, position.s_y+position.buffer_y-i)
  132.                         term.setTextColor(style.textColor)
  133.                         write(style.text)
  134.                         paintutils.drawBox(position.s_x,position.s_y-i,position.e_x,position.e_y-i, style.borderColor)
  135.                     end
  136.                     os.sleep(style.animationTime)
  137.                     paintutils.drawFilledBox(position.s_x,position.s_y-i,position.e_x,position.e_y-i, colors.black)
  138.                 end
  139.             end)
  140.  
  141.         else
  142.  
  143.             render(button)
  144.  
  145.         end
  146.    
  147.     end
  148.     if next(animations) ~= nil then
  149.         parallel.waitForAll(table.unpack(animations))
  150.     end
  151. end
  152.  
  153. function base:awaitButtonClick()
  154.     local pressed = false
  155.     while not pressed do
  156.         local _, button, x, y = os.pullEvent("mouse_click")
  157.  
  158.         for _, obj in pairs(self.buttons) do
  159.             local position, style, exec = table.unpack(obj)
  160.             if button == 1 and x >= position.s_x and x <= position.e_x and y >= position.s_y and y <= position.e_y and not pressed then
  161.                 exec()
  162.                 pressed = true
  163.             end
  164.         end
  165.     end
  166. end
  167.  
  168. ---@class Desktop
  169.  
  170. local function Desktop()
  171.     return base
  172. end
  173.  
  174. return {Desktop = Desktop, Style = Style, Position = Position}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement