Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI("class")
- -- Screen
- screen = class.class(function(self, monitor)
- self.monitor = monitor
- self.drawables = {}
- end)
- function screen:addDrawable(component)
- if component = nil then
- print("[Screen] Not adding nil drawable.")
- return self
- end
- local i = table.getn(self.drawables)
- self.drawables[i] = component
- return self
- end
- function screen:draw()
- local arrLength = table.getn(self.drawables)
- for i=0, arrLength do
- self.drawables[i]:draw(self.monitor)
- end
- end
- -- Button
- button = class.class(function(self, text, clickEvent)
- self.text = text
- self.x = 0
- self.y = 0
- self.width = 1
- self.height = 1
- self.paddedText = text
- self.emptyRow = ""
- self.clickEvent = clickEvent
- end)
- function button:pos()
- local t = {}
- t.x = self.x
- t.y = self.y
- return t
- end
- function button:text()
- return self.text
- end
- function button:setText(text)
- self.text = text
- self:updateTextData()
- return self
- end
- function button:setPos(x, y)
- self.x = x
- self.y = y
- return self
- end
- function button:updateTextData()
- local missingLength = self.width - string.len(self.text);
- if missingLength < 0 then
- self.paddedText = self.text
- print("[Button] Error, text too big for button")
- else
- local beg = math.floor(missingLength / 2)
- self.paddedText = string.rep(" ", beg) .. self.text .. string.rep(" ", missingLength - beg)
- end
- if self.height > 1 then
- self.emptyRow = string.rep(" ", self.width)
- end
- end
- function button:setSize(width, height)
- if width < 1 or height < 1 then
- print("[Button] Error, invalid size")
- return self;
- end
- self.width = width
- self.height = height
- self:updateTextData()
- return self
- end
- function button:draw(mon)
- mon.setBackgroundColor(colors.red)
- local yText = math.floor(self.height / 2 + self.y)
- for y = self.y, self.y+self.height-1 do
- mon.setCursorPos(self.x, y)
- if y == yText then
- mon.write(self.paddedText)
- else
- mon.write(self.emptyRow)
- end
- end
- return self
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement