Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- GLOBALS
- termWidth, termHeight = term.getSize()
- -- FUNCTIONS
- ---- BUTTON
- Button = {
- x = 0,
- y = 0,
- w = 0,
- h = 0,
- callBack = nil,
- backColor = nil,
- textColor = nil,
- label = "button"
- }
- function Button:new (o, x, y, w, h, label, textColor, backColor, callBack)
- local o = setmetatable({}, {__index = self})
- o.__index = o
- o.x = x or 1
- if o.x < 1 or o.x > termWidth then o.x = 1 end
- o.y = y or 1
- if o.y < 1 or o.y > termHeight then o.y = 1 end
- o.w = w or 10
- if o.x + o.w > termWidth or o.w < 1 then o.w = 1 end
- o.h = h or 10
- if o.y + o.h > termHeight or o.h < 1 then o.h = 1 end
- o.label = label or "button"
- o.textColor = textColor or colors.white
- o.backColor = backColor or colors.green
- o.callBack = callBack
- return o
- end
- function Button:draw ()
- term.setCursorPos(self.x, self.y)
- local textLength = #self.label
- local buttonLength = math.max(self.w, textLength)
- for i = 1, self.h, 1 do
- term.blit(string.rep(" ", buttonLength), string.rep(colors.toBlit(self.textColor), buttonLength), string.rep(colors.toBlit(self.backColor), buttonLength))
- term.setCursorPos(self.x, self.y + i)
- end
- term.setCursorPos(buttonLength/2 - textLength/2 + self.x, self.h/2 + self.y)
- term.setTextColor(self.textColor)
- term.blit(self.label, string.rep(colors.toBlit(self.textColor), textLength), string.rep(colors.toBlit(self.backColor), textLength))
- end
- function Button:isClicked(x, y)
- if x >= self.x and x <= self.x + self.w - 1 and y >= self.y and y <= self.y + self.h - 1 then
- if type(self.callBack) == "function" then
- return self.callBack()
- elseif type(self.callBack) == "table" then
- return self.callBack[1](self.callBack[2])
- end
- local func = load(self.callBack)
- return func()
- end
- end
- ---- RECTANGLE
- function drawRectangle(x, y, w, h, color, text, textcolor)
- term.setCursorPos(x, y)
- for i = 1, h, 1 do
- term.blit(string.rep(" ", w), string.rep(1, w), string.rep(colors.toBlit(color), w))
- term.setCursorPos(x, y + i)
- end
- if text ~= nil then
- term.setCursorPos(w / 2 - #text / 2 + x, h / 2 + x)
- term.blit(text, string.rep(colors.toBlit(textcolor), #text), string.rep(colors.toBlit(color), #text))
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement