Advertisement
AssortedBrunoz

api

Oct 31st, 2024
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.23 KB | None | 0 0
  1. local base = {
  2.     buttons = {}
  3. }
  4.  
  5. local function fracture(str)
  6.     local f = {}
  7.     for i in string.gmatch(str, ".") do
  8.         table.insert( f,i )
  9.     end
  10.     return f
  11. end
  12.  
  13. function string.wrap(str, start_x, end_x, buffer)
  14.     local str = fracture(str)
  15.     local new_str = {}
  16.     local curr = ""
  17.     local c = 1
  18.     for index, char in pairs(str) do
  19.         if (c + (buffer*2)) == end_x then
  20.             curr = curr..char
  21.             table.insert(new_str, curr)
  22.             curr = ""
  23.             c = 0
  24.         else
  25.             curr = curr..char
  26.         end
  27.         c = c + 1
  28.     end
  29.     table.insert(new_str, curr)
  30.     return new_str
  31. end
  32.  
  33. function string.count(str, char)
  34.     local count = 0
  35.     for i in string.gmatch(str,char) do
  36.         count = count + 1
  37.     end
  38.     return count
  39. end
  40.  
  41. function Style(background_color, border_color)
  42.     local style_obj = {}
  43.     style_obj.backgroundColor = background_color and background_color or colors.white
  44.     style_obj.borderColor = border_color and border_color or style_obj.backgroundColor
  45.     style_obj.text = ""
  46.     style_obj.textColor = style_obj.backgroundColor
  47.  
  48.     return style_obj
  49. end
  50.  
  51. function Position(MAX_X, MAX_Y, xtype, ytype, text, buffer_x, buffer_y)
  52.     local coordinates = {}
  53.     local text_height = text and string.count(text, "\n") or 0
  54.     buffer_x = buffer_x and buffer_x or 0
  55.     buffer_y = buffer_y and buffer_y or 0
  56.     coordinates.s_x = 1
  57.     coordinates.s_y = 1
  58.     coordinates.e_x = 1
  59.     coordinates.e_y = 1
  60.     coordinates.buffer_x = buffer_x
  61.     coordinates.buffer_y = buffer_y
  62.  
  63.     if xtype == "centered" then
  64.         coordinates.s_x = text and MAX_X/2 - #text/2 - buffer_x or MAX_X/2 - buffer_x
  65.         coordinates.e_x = text and MAX_X/2 + #text/2 + buffer_x -1 or MAX_X/2 + buffer_x
  66.     elseif xtype == "left" then
  67.         coordinates.s_x = 1
  68.         coordinates.e_x = text and #text + buffer_x*2 or 1+buffer_x*2
  69.     end
  70.  
  71.     if ytype == "centered" then
  72.         coordinates.s_y = text and MAX_Y/2-text_height/2 - buffer_y or MAX_Y/2 + buffer_y
  73.         coordinates.e_y = text and MAX_Y/2+text_height/2 + buffer_y or MAX_Y/2 + buffer_y
  74.     end
  75.  
  76.     return coordinates
  77. end
  78.  
  79. function base.addButton(position, style, exec)
  80.     paintutils.drawFilledBox(position.s_x,position.s_y,position.e_x,position.e_y, style.backgroundColor)
  81.     term.setCursorPos(position.s_x + position.buffer_x, position.s_y + position.buffer_y)
  82.     term.setTextColor(style.textColor)
  83.     write(style.text)
  84.     paintutils.drawBox(position.s_x,position.s_y,position.e_x,position.e_y, style.borderColor)
  85.    
  86.     table.insert(base.buttons, {position.s_x, position.s_y, position.e_x,position.e_y, exec})
  87. end
  88.  
  89. function base:awaitButtonClick()
  90.     local pressed = false
  91.     while not pressed do
  92.         local _, button, x, y = os.pullEvent("mouse_click")
  93.  
  94.         for _, obj in pairs(base.buttons) do
  95.             local s_x, s_y, e_x, e_y, exec = table.unpack(obj)
  96.             if button == 1 and x >= s_x and x <= e_x and y >= s_y and y <= e_y and not pressed then
  97.                 exec()
  98.                 pressed = true
  99.             end
  100.         end
  101.     end
  102. end
  103.  
  104. local function Desktop()
  105.     return base
  106. end
  107.  
  108. return {Desktop = Desktop, Style = Style, Position = Position}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement