Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Point = {}
- Point.new = function(x, y)
- -- Private
- local self = {}
- local X = x or 0
- local Y = y or 0
- -- Get/Set
- self.x = function(x)
- X = x or X
- return X
- end
- self.y = function(y)
- Y = y or Y
- return Y
- end
- return self
- end
- Button = {}
- Button.new = function(pos, size, text, color, callback)
- -- Private Variables
- local self = {}
- local Pos = pos or Point.new(0,0)
- local Size = size or Point.new(0,0)
- local Text = text or ""
- local Color = color or colors.gray
- local callback = callback or function() end
- -- Get/Set
- self.pos = function(pos)
- Pos = pos or Pos
- return Pos
- end
- self.size = function(size)
- Size = size or Size
- return Size
- end
- self.text = function(text)
- Text = text or Text
- return Text
- end
- self.color = function(color)
- Color = color or Color
- return Color
- end
- self.callback = function(callback)
- Callback = callback or Callback
- return Callback
- end
- -- Methods
- self.render = function(monitor)
- local lineCount = 0
- local lines = {}
- for line in string.gmatch(Text, "[^%|]+") do
- lineCount = lineCount + 1
- lines[lineCount] = line
- end
- local textPos = {}
- textPos.empty = true
- for i = 1, lineCount do
- textPos.empty = false
- local textX = Pos.x() + math.floor((Size.x() - string.len(lines[i]))/2)
- local textY = Pos.y() + math.floor((Size.y() - lineCount)/2) + i - 1
- if textX < Pos.x() then
- textX = Pos.x()
- end
- textPos[i] = Point.new(textX, textY)
- end
- monitor.setBackgroundColor(Color)
- local curLine = 1
- monitor.setCursorPos(Pos.x(), Pos.y())
- local x, y = monitor.getCursorPos()
- while not (x == Pos.x() and y == Pos.y() + Size.y()) do
- if textPos.empty or curLine > lineCount then
- monitor.write(" ")
- elseif textPos[curLine].y() == y and textPos[curLine].x() == x then
- monitor.write(lines[curLine])
- curLine = curLine + 1
- else
- monitor.write(" ")
- end
- x, y = monitor.getCursorPos()
- if x > Pos.x() + Size.x() - 1 then
- x = Pos.x()
- y = y + 1
- monitor.setCursorPos(x, y)
- end
- end
- end
- self.isClicked = function(click)
- if Pos.x() <= click.x() and Pos.x() + Size.x() >= click.x() then
- if Pos.y() <= click.y() and Pos.y() + Size.y() >= click.y() then
- return true
- end
- end
- return false
- end
- return self
- end
Advertisement
Add Comment
Please, Sign In to add comment