Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --tables--
- buttons = {}
- labels = {}
- bars = {}
- frames = {}
- --Button--
- Button = {
- name = "btn",
- text = "Button",
- bounds = {
- x1 = 1,
- x2 = 0,
- y1 = 1,
- y2 = 1
- },
- colorActive = colors.green,
- colorInactive = colors.red,
- foreColor = colors.white,
- active = false,
- onchange = nil
- }
- function Button:new(data)
- setmetatable({}, self)
- self.__index = self
- for k, v in pairs(data) do
- self[k] = v
- end
- if self.bounds.x2 == 0 then
- self.bounds.x2 = self.bounds.x1 + #tostring(self.text) - 1
- end
- buttons[self.name] = self
- return self
- end
- function Button:toggle()
- self.active = not self.active
- self:draw()
- end
- function Button:checkTouchEvent(x, y)
- if x >= self.bounds.x1 and x <= self.bounds.x2 and y >= self.bounds.y1 and y <= self.bounds.y2 then
- if self['onchange'] ~= nil then
- self.onchange(self, x, y)
- end
- return true
- end
- return false
- end
- function Button:draw()
- local xSpot = math.floor((self.bounds.x2 - self.bounds.x1 - #tostring(self.text)) /2) + 1 + self.bounds.x1
- local ySpot = math.floor((self.bounds.y1 + self.bounds.y2)/2)
- paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.active and self.colorActive or self.colorInactive)
- term.setCursorPos(xSpot, ySpot)
- term.setTextColor(self.foreColor)
- term.write(self.text)
- end
- --Frame--
- Frame = {
- name = "frm",
- bounds = {
- x1 = 1,
- x2 = 10,
- y1 = 1,
- y2 = 10
- },
- backColor = colors.black,
- borderColor = colors.white
- }
- function Frame:new(data)
- setmetatable({}, self)
- self.__index = self
- for k, v in pairs(data) do
- self[k] = v
- end
- frames[self.name] = self
- return self
- end
- function Frame:draw()
- paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.backColor)
- paintutils.drawBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.borderColor)
- end
- --Bar--
- Bar = {
- name = "bar",
- bounds = {
- x1 = 1,
- x2 = 5,
- y1 = 1,
- y2 = 1
- },
- percent = 0,
- direction = 'right',
- backColor = colors.gray,
- foreColor = colors.lime
- }
- function Bar:new(data)
- setmetatable({}, self)
- self.__index = self
- for k, v in pairs(data) do
- self[k] = v
- end
- bars[self.name] = self
- return self
- end
- function Bar:draw()
- paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.backColor)
- if self.direction == 'right' then
- f = self.bounds.x1 + math.floor((self.bounds.x2 - self.bounds.x1 + 1) / 100 * self.percent + 0.5) - 1
- paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, f, self.bounds.y2, self.foreColor)
- elseif self.direction == 'left' then
- f = self.bounds.x2 - math.floor((self.bounds.x2 - self.bounds.x1 + 1) / 100 * self.percent + 0.5) + 1
- paintutils.drawFilledBox(f, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.foreColor)
- elseif self.direction == 'up' then
- f = self.bounds.y2 - math.floor((self.bounds.y2 - self.bounds.y1 + 1) / 100 * self.percent + 0.5) + 1
- paintutils.drawFilledBox(self.bounds.x1, f, self.bounds.x2, self.bounds.y2, self.foreColor)
- elseif self.direction == 'down' then
- f = self.bounds.y1 + math.floor((self.bounds.x2 - self.bounds.x1 + 1) / 100 * self.percent + 0.5) - 1
- paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, f, self.foreColor)
- end
- end
- --Label--
- Label = {
- name = "lbl",
- text = "Label",
- bounds = {
- x1 = 1,
- x2 = 0,
- y1 = 1,
- y2 = 1
- },
- backColor = colors.black,
- foreColor = colors.white
- }
- function Label:new(data)
- setmetatable({}, self)
- self.__index = self
- for k, v in pairs(data) do
- self[k] = v
- end
- if self.bounds.x2 == 0 then
- self.bounds.x2 = self.bounds.x1 + #self.text - 1
- end
- labels[self.name] = self
- return self
- end
- function Label:draw()
- local xSpot = math.floor((self.bounds.x2 - self.bounds.x1 - #tostring(self.text)) /2) + 1 + self.bounds.x1
- local ySpot = math.floor((self.bounds.y1 + self.bounds.y2)/2)
- paintutils.drawFilledBox(self.bounds.x1, self.bounds.y1, self.bounds.x2, self.bounds.y2, self.backColor)
- term.setCursorPos(xSpot, ySpot)
- term.setTextColor(self.foreColor)
- term.write(self.text)
- end
- --Draw function--
- function draw()
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.clear()
- term.setCursorPos(1,1)
- for k, v in pairs(frames) do
- v:draw()
- end
- for k, v in pairs(bars) do
- v:draw()
- end
- for k, v in pairs(buttons) do
- v:draw()
- end
- for k, v in pairs(labels) do
- v:draw()
- end
- end
- --Check for a touch event--
- function run()
- while true do
- local e, side, x, y = os.pullEvent("monitor_touch")
- for k, v in pairs(buttons) do
- v:checkTouchEvent(x, y)
- end
- end
- end
Add Comment
Please, Sign In to add comment