Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- os.loadAPI( "./lib/draw" )
- os.loadAPI( "./lib/util" )
- out = term
- function setOutput( monitor )
- out = monitor
- end
- Window = { x = 1, y = 1, xs = 15, ys = 10, color = colors.blue, fcolor = colors.black, visible = true, uuid = 0, text = {"",1,1} }
- windowTable = {}
- function drawWindows( tWindows )
- tWindows = tWindows or windowTable
- for i=#tWindows, 1, -1 do
- tWindows[i]:draw()
- end
- end
- function Window:new ( o )
- o = o or {} -- create object if user does not provide one
- setmetatable( o, self )
- self.__index = self
- o.uuid = math.random(1,10000)
- o:setTitle( o.uuid )
- return o
- end
- function Window:setVisible( visible )
- self.visible = visible
- end
- function Window:setColor( color )
- self.color = color
- end
- function Window:setTitle( title )
- if string.len(title) > 6 then title = string.sub( title, 1, 6 ) end
- self.title = title
- end
- function Window:register( tablePosition )
- for i = 1, #windowTable do
- if( windowTable[i].uuid == self.uuid ) then
- table.remove( windowTable, i )
- break
- end
- end
- if tablePosition < #windowTable then
- for i = #windowTable, tablePosition ,-1 do
- windowTable[i+1] = windowTable[i]
- end
- end
- windowTable[tablePosition] = self
- end
- function Window:vanish()
- self:setVisible( false )
- for j = 0, self.ys - 1 do
- for i = 0, self.xs - 1 do
- out.setCursorPos( self.x + i, self.y + j )
- out.setBackgroundColor( colors.black )
- out.write( " " )
- end
- end
- end
- function Window:setPos( x,y )
- self.x = x
- self.y = y
- end
- function Window:getPos()
- return self.x, self.y
- end
- function Window:setSize( xs, ys )
- if xs < 15 then xs = 15 end
- if ys < 5 then ys = 5 end
- self.xs = xs
- self.ys = ys
- end
- function Window:isClicked( xCoord, yCoord )
- if( self.x <= xCoord and xCoord < (self.x + self.xs) ) and ( self.y <= yCoord and yCoord < (self.y + self.ys) ) then
- return true
- else
- return false
- end
- end
- function Window:getClick( xCoord, yCoord )
- return self:isClicked( xCoord, yCoord ), xCoord - self.x + 1, yCoord - self.y +1
- end
- function Window:setText( text, xPos, yPos )
- xPos = xPos or 1
- yPos = yPos or 1
- self.text = {text, xPos, yPos}
- end
- function Window:getText()
- return self.text
- end
- function Window:draw()
- if not self.visible then
- return 0
- end
- for j = 0, self.ys - 1 do
- for i = 0, self.xs - 1 do
- out.setCursorPos( self.x + i, self.y + j )
- if ( j == 0 or j == self.ys - 1 or i == 0 or i == self.xs - 1 ) then
- out.setBackgroundColor( self.color )
- if( j == 0 and i == 0 ) then
- out.write( "[" )
- elseif( j == 0 and i == 1 ) then
- out.write( "X" )
- elseif( j == 0 and i == 2 ) then
- out.write( "-" )
- elseif( j == 0 and i == 3 ) or ( j == self.ys - 1 and i == 0 ) or ( j == 0 and i == self.xs - 1) then
- out.write( "+" )
- elseif( j == self.ys - 1 and i == self.xs - 1) then
- out.write( "v" )
- elseif( j == 0 and i == 4 ) then
- out.write( "]" )
- elseif( j == self.ys - 1 ) then
- out.write( "-" )
- elseif( j == 0 ) then
- out.write( "-" )
- else
- out.write( "|" )
- end
- else
- out.setBackgroundColor( self.fcolor )
- out.write( " " )
- end
- end
- end
- --DEBUG
- out.setCursorPos( self.x + 5, self.y )
- out.write( "["..self.title.."]" )
- out.setBackgroundColor( colors.black )
- if #self.text[1] > self.xs - 2 then
- local line = 0
- for i=1, #self.text[1], self.xs - 2 do
- if line > self.ys - 3 then
- break
- end
- out.setCursorPos(self.x+self.text[2],self.y+self.text[3]+line)
- out.write(util.trim(string.sub(self.text[1], i, i+self.xs - 3)))
- line = line + 1
- end
- else
- out.setCursorPos( self.x + self.text[2], self.y + self.text[3])
- out.write( self.text[1] )
- end
- out.setBackgroundColor( colors.black )
- end
Advertisement
Add Comment
Please, Sign In to add comment