Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- if not fs.exists("stringtools.lua") then
- term.setTextColour(colors.red)
- print("stringtools not detected, it is required for button_api to work, now downloading..")
- shell.run("wget https://raw.githubusercontent.com/banana-boye/CC_stringtools/refs/heads/main/stringtools.lua stringtools.lua")
- else
- require("stringtools")()
- end
- local base = {
- buttons = {}
- }
- ---@class Style
- ---@field background_color number
- ---@field border_color number
- function Style(background_color, border_color)
- local style_obj = {}
- style_obj.backgroundColor = background_color and background_color or colors.white
- style_obj.borderColor = border_color and border_color or style_obj.backgroundColor
- style_obj.text = ""
- style_obj.textColor = style_obj.backgroundColor
- style_obj.animate = false
- style_obj.animationType = "linear"
- style_obj.animationTime = 10
- style_obj.s_x = nil
- style_obj.s_y = nil
- return style_obj
- end
- ---@class Position
- ---@field MAX_X number
- ---@field MAX_Y number
- ---@field xtype string
- ---@field ytype string
- ---@field text string
- ---@field buffer_x number
- ---@field buffer_y number
- function Position(MAX_X, MAX_Y, xtype, ytype, text, buffer_x, buffer_y)
- local coordinates = {}
- local text_height = text and string.count(text, "\n") or 0
- buffer_x = buffer_x and buffer_x or 0
- buffer_y = buffer_y and buffer_y or 0
- coordinates.s_x = 1
- coordinates.s_y = 1
- coordinates.e_x = 1
- coordinates.e_y = 1
- coordinates.buffer_x = buffer_x
- coordinates.buffer_y = buffer_y
- if xtype == "centered" then
- coordinates.s_x = text and MAX_X/2 - #text/2 - buffer_x or MAX_X/2 - buffer_x
- coordinates.e_x = text and MAX_X/2 + #text/2 + buffer_x -1 or MAX_X/2 + buffer_x
- elseif xtype == "left" then
- coordinates.s_x = 1
- coordinates.e_x = text and #text + buffer_x*2 or 1+buffer_x*2
- elseif xtype == "right" then
- coordinates.s_x = text and MAX_X - #text - buffer_x*2 or MAX_X - buffer_x*2-1
- coordinates.e_x = MAX_X
- end
- if ytype == "centered" then
- coordinates.s_y = text and MAX_Y/2-text_height/2 - buffer_y or MAX_Y/2 + buffer_y
- coordinates.e_y = text and MAX_Y/2+text_height/2 + buffer_y or MAX_Y/2 + buffer_y
- elseif ytype == "top" then
- coordinates.s_y = 1
- coordinates.e_y = text and 1 + text_height + buffer_y * 2 or 1 + buffer_y * 2
- elseif ytype == "bottom" then
- coordinates.s_y = text and MAX_Y - text_height - buffer_y * 2 or MAX_Y - buffer_y * 2
- coordinates.e_y = MAX_Y
- end
- return coordinates
- end
- ---@field position Position
- ---@field style Style
- ---@field exec function
- function base.addButton(position, style, exec)
- table.insert(base.buttons, {position, style, exec})
- end
- function base.clearButtons()
- base.buttons = {}
- end
- local function render(button)
- local position, style, exec = table.unpack(button)
- paintutils.drawFilledBox(position.s_x,position.s_y,position.e_x,position.e_y, style.backgroundColor)
- paintutils.drawBox(position.s_x,position.s_y,position.e_x,position.e_y, style.borderColor)
- term.setCursorPos(position.s_x+position.buffer_x, position.s_y+position.buffer_y)
- term.setTextColor(style.textColor)
- term.setBackgroundColor(style.backgroundColor)
- write(style.text)
- end
- function base:render()
- local animations = {}
- for _, button in pairs(self.buttons) do
- local position, style, exec = table.unpack(button)
- if style.animate then
- style.s_x = style.s_x and style.s_x or position.s_x
- style.s_y = style.s_y and style.s_y or position.s_y
- table.insert(animations,
- function()
- for i = math.abs(position.s_x - style.s_x), 0, -1 do
- if math.abs(position.s_x - i) > 0 then
- paintutils.drawFilledBox(position.s_x-i,position.s_y,position.e_x-i,position.e_y, style.backgroundColor)
- term.setCursorPos(position.s_x+position.buffer_x-i, position.s_y+position.buffer_y)
- term.setTextColor(style.textColor)
- write(style.text)
- paintutils.drawBox(position.s_x-i,position.s_y,position.e_x-i,position.e_y, style.borderColor)
- end
- os.sleep(style.animationTime)
- paintutils.drawFilledBox(position.s_x-i,position.s_y,position.e_x-i,position.e_y, colors.black)
- end
- end)
- table.insert(animations,
- function()
- for i = math.abs(position.s_y - style.s_y), 0, -1 do
- if math.abs(position.s_y - i) > 0 then
- paintutils.drawFilledBox(position.s_x,position.s_y-i,position.e_x,position.e_y-i, style.backgroundColor)
- term.setCursorPos(position.s_x+position.buffer_x, position.s_y+position.buffer_y-i)
- term.setTextColor(style.textColor)
- write(style.text)
- paintutils.drawBox(position.s_x,position.s_y-i,position.e_x,position.e_y-i, style.borderColor)
- end
- os.sleep(style.animationTime)
- paintutils.drawFilledBox(position.s_x,position.s_y-i,position.e_x,position.e_y-i, colors.black)
- end
- end)
- else
- render(button)
- end
- end
- if next(animations) ~= nil then
- parallel.waitForAll(table.unpack(animations))
- end
- end
- function base:awaitButtonClick()
- local pressed = false
- while not pressed do
- local _, button, x, y = os.pullEvent("mouse_click")
- for _, obj in pairs(self.buttons) do
- local position, style, exec = table.unpack(obj)
- 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
- exec()
- pressed = true
- end
- end
- end
- end
- ---@class Desktop
- local function Desktop()
- return base
- end
- return {Desktop = Desktop, Style = Style, Position = Position}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement