Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local base = {
- buttons = {}
- }
- local function fracture(str)
- local f = {}
- for i in string.gmatch(str, ".") do
- table.insert( f,i )
- end
- return f
- end
- function string.wrap(str, start_x, end_x, buffer)
- local str = fracture(str)
- local new_str = {}
- local curr = ""
- local c = 1
- for index, char in pairs(str) do
- if (c + (buffer*2)) == end_x then
- curr = curr..char
- table.insert(new_str, curr)
- curr = ""
- c = 0
- else
- curr = curr..char
- end
- c = c + 1
- end
- table.insert(new_str, curr)
- return new_str
- end
- function string.count(str, char)
- local count = 0
- for i in string.gmatch(str,char) do
- count = count + 1
- end
- return count
- end
- 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
- return style_obj
- end
- 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
- 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
- end
- return coordinates
- end
- function base.addButton(position, style, exec)
- paintutils.drawFilledBox(position.s_x,position.s_y,position.e_x,position.e_y, style.backgroundColor)
- term.setCursorPos(position.s_x + position.buffer_x, position.s_y + position.buffer_y)
- term.setTextColor(style.textColor)
- write(style.text)
- paintutils.drawBox(position.s_x,position.s_y,position.e_x,position.e_y, style.borderColor)
- table.insert(base.buttons, {position.s_x, position.s_y, position.e_x,position.e_y, exec})
- end
- function base:awaitButtonClick()
- local pressed = false
- while not pressed do
- local _, button, x, y = os.pullEvent("mouse_click")
- for _, obj in pairs(base.buttons) do
- local s_x, s_y, e_x, e_y, exec = table.unpack(obj)
- if button == 1 and x >= s_x and x <= e_x and y >= s_y and y <= e_y and not pressed then
- exec()
- pressed = true
- end
- end
- end
- end
- local function Desktop()
- return base
- end
- return {Desktop = Desktop, Style = Style, Position = Position}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement