Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local colourToRGB = {
- [colours.white] = 0xf0f0f0,
- [colours.orange] = 0xf2b233,
- [colours.magenta] = 0xe57fd8,
- [colours.lightBlue] = 0x99b2f2,
- [colours.yellow] = 0xdede6c,
- [colours.lime] = 0x7fcc19,
- [colours.pink] = 0xf2b2cc,
- [colours.grey] = 0x4c4c4c,
- [colours.lightGrey] = 0x999999,
- [colours.cyan] = 0x4c99b2,
- [colours.purple] = 0xb266e5,
- [colours.blue] = 0x3366cc,
- [colours.brown] = 0x7f664c,
- [colours.green] = 0x57a64e,
- [colours.red] = 0xcc4c4c,
- [colours.black] = 0x000000,
- }
- local rgbToColour = {}
- for ccColour, glassesHex in pairs(colourToRGB) do
- rgbToColour[glassesHex] = ccColour
- end
- local hexValues = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}
- local hexToColour = {}
- for i = 1, 16 do
- hexToColour[hexValues[i]] = colourToRGB[2^(i-1)]
- end
- local charOffset = {
- ["!"] = 2, ["'"] = 2, ["("] = 1, [")"] = 1, ["*"] = 1, [","] = 2, ["."] = 2, [":"] = 2,
- [";"] = 2, ["<"] = 1, [">"] = 1, ["I"] = 1, ["["] = 1, ["["] = 1, ["`"] = 2, ["f"] = 1,
- ["i"] = 2, ["k"] = 1, ["l"] = 2, ["t"] = 1, ["{"] = 1, ["|"] = 2, ["}"] = 1,
- }
- function new(surface, width, height, xOffset, yOffset, zOffset, opacity)
- local surface = surface
- local width, height = width or 51, height or 19
- local xOffset, yOffset, zOffset = xOffset or -(math.floor(width*6/2)), yOffset or -(math.floor(height*9/2)), zOffset or 1
- local map = {}
- local clickCatcherID = false
- local clickCatcherObject = false
- local cursorX, cursorY = 1, 1
- local cursorBlink = false
- local cursorObject = false
- local currentTextColour = colourToRGB[colours.white]
- local currentBackgroundColour = colourToRGB[colours.black]
- local isDrawn = false
- local isVisible = true
- local opacity = opacity or 1
- local hasChanged = false
- local horizontalAlign, verticalAlign = "MIDDLE", "MIDDLE"
- local bezelDrawn = false
- local topBezel, bottomBezel, leftBezel, rightBezel = false, false, false, false
- local function drawBezel()
- topBezel = surface.addBox(xOffset - 6, yOffset - 9, (width + 2)*6, 9, 0xdcd56c, opacity)
- topBezel.setScreenAnchor(horizontalAlign, verticalAlign)
- topBezel.setVisible(isVisible)
- topBezel.setZ(zOffset)
- bottomBezel = surface.addBox(xOffset - 6, yOffset + height*9, (width + 2)*6, 9, 0xdcd56c, opacity)
- bottomBezel.setScreenAnchor(horizontalAlign, verticalAlign)
- bottomBezel.setVisible(isVisible)
- bottomBezel.setZ(zOffset)
- leftBezel = surface.addBox(xOffset - 6, yOffset, 6, height*9, 0xdcd56c, opacity)
- leftBezel.setScreenAnchor(horizontalAlign, verticalAlign)
- leftBezel.setVisible(isVisible)
- leftBezel.setZ(zOffset)
- rightBezel = surface.addBox(xOffset + (width*6), yOffset, 6, height*9, 0xdcd56c, opacity)
- rightBezel.setScreenAnchor(horizontalAlign, verticalAlign)
- rightBezel.setVisible(isVisible)
- rightBezel.setZ(zOffset)
- end
- local function eraseBezel()
- if topBezel then
- topBezel.delete()
- topBezel = false
- end
- if bottomBezel then
- bottomBezel.delete()
- bottomBezel = false
- end
- if leftBezel then
- leftBezel.delete()
- leftBezel = false
- end
- if rightBezel then
- rightBezel.delete()
- rightBezel = false
- end
- end
- local function drawClickCatcher()
- clickCatcherObject = surface.addBox(xOffset, yOffset, width*6, height*9, 0x000000, 0)
- clickCatcherObject.setScreenAnchor(horizontalAlign, verticalAlign)
- clickCatcherObject.setVisible(isVisible)
- clickCatcherObject.setZ(zOffset + 3)
- clickCatcherID = clickCatcherObject.getId()
- end
- local function eraseClickCatcher()
- if clickCatcherObject then
- clickCatcherObject.delete()
- clickCatcherObject = false
- end
- clickCatcherID = false
- end
- local function drawText(xPos, yPos, text, textColour)
- local textObject = surface.addText((xPos-1)*6 + xOffset + (charOffset[text] or 0), (yPos-1)*9 + yOffset, text, textColour)
- textObject.setScreenAnchor(horizontalAlign, verticalAlign)
- textObject.setVisible(isVisible)
- textObject.setZ(zOffset + 1)
- return textObject
- end
- local function drawBackground(xPos, yPos, backgroundColour)
- local backgroundObject = surface.addBox((xPos-1)*6 + xOffset, (yPos-1)*9 + yOffset, 6, 9, backgroundColour, opacity)
- backgroundObject.setScreenAnchor(horizontalAlign, verticalAlign)
- backgroundObject.setVisible(isVisible)
- backgroundObject.setZ(zOffset)
- return backgroundObject
- end
- local function drawCursor()
- cursorObject = surface.addText((cursorX-1)*6 + xOffset, (cursorY-1)*9 + yOffset, "_", currentTextColour)
- cursorObject.setScreenAnchor(horizontalAlign, verticalAlign)
- cursorObject.setVisible((isVisible and cursorBlink and (map[cursorY] and map[cursorY][cursorX]) and true) or false)
- cursorObject.setZ(zOffset + 2)
- end
- local function createCoord(xPos, yPos, text, textColour, backgroundColour)
- local coord = {
- text = text,
- textCol = textColour,
- backCol = backgroundColour,
- textObj = false,
- backObj = false,
- }
- if isDrawn then
- coord.textObj = drawText(xPos, yPos, text, textColour)
- coord.backObj = drawBackground(xPos, yPos, backgroundColour)
- end
- return coord
- end
- local updateTextObj
- local function updateCoord(xPos, yPos, text, textColour, backgroundColour)
- local coord = map[yPos] and map[yPos][xPos]
- if coord then
- if coord.text ~= text or coord.textCol ~= textColour or coord.backCol ~= backgroundColour then
- coord.text = text
- coord.textCol = textColour
- coord.backCol = backgroundColour
- if isDrawn then
- updateTextObj = coord.textObj
- updateTextObj.setText(text)
- updateTextObj.setColor(textColour)
- updateTextObj.setX((xPos-1)*6 + xOffset + (charOffset[text] or 0))
- coord.backObj.setColor(backgroundColour)
- end
- hasChanged = true
- end
- end
- end
- local function updateCursor()
- if isDrawn and cursorObject then
- cursorObject.setX((cursorX-1)*6 + xOffset)
- cursorObject.setY((cursorY-1)*9 + yOffset)
- cursorObject.setZ(zOffset + 2)
- cursorObject.setVisible((isVisible and cursorBlink and (map[cursorY] and map[cursorY][cursorX]) and true) or false)
- cursorObject.setColor(currentTextColour)
- end
- end
- local glassWindow = {}
- glassWindow.getDrawn = function()
- return isDrawn
- end
- glassWindow.setDrawn = function(setDrawn)
- if type(setDrawn) == "boolean" and isDrawn ~= setDrawn then
- if setDrawn == true then
- local coord
- for yPos = 1, height do
- for xPos = 1,width do
- coord = map[yPos][xPos]
- coord.textObj = drawText(xPos, yPos, coord.text, coord.textCol)
- coord.backObj = drawBackground(xPos, yPos, coord.backCol)
- end
- end
- if bezelDrawn then
- drawBezel()
- end
- drawClickCatcher()
- drawCursor()
- else
- local coord
- for yPos = 1, height do
- for xPos = 1,width do
- coord = map[yPos][xPos]
- coord.textObj.delete()
- coord.backObj.delete()
- coord.textObj = false
- coord.backObj = false
- end
- end
- cursorObject.delete()
- cursorObject = false
- eraseClickCatcher()
- eraseBezel()
- end
- isDrawn = setDrawn
- hasChanged = true
- return true
- end
- return false
- end
- glassWindow.getAlignment = function()
- return horizontalAlign, verticalAlign
- end
- glassWindow.setAlignment = function(horizontal, vertical)
- --if check args then
- if isDrawn then
- local coord
- for yPos = 1, height do
- for xPos = 1,width do
- coord = map[yPos][xPos]
- coord.textObj.setScreenAnchor(horizontal, vertical)
- coord.backObj.setScreenAnchor(horizontal, vertical)
- end
- end
- if bezelDrawn then
- topBezel.setScreenAnchor(horizontal, vertical)
- bottomBezel.setScreenAnchor(horizontal, vertical)
- leftBezel.setScreenAnchor(horizontal, vertical)
- rightBezel.setScreenAnchor(horizontal, vertical)
- end
- clickCatcherObject.setScreenAnchor(horizontal, vertical)
- cursorObject.setScreenAnchor(horizontal, vertical)
- hasChanged = true
- end
- horizontalAlign, verticalAlign = horizontal, vertical
- return true
- --end
- --return false
- end
- glassWindow.getVisible = function()
- return isVisible
- end
- glassWindow.setVisible = function(setVisible)
- if type(setVisible) == "boolean" and isVisible ~= setVisible then
- if isDrawn then
- local coord
- for yPos = 1, height do
- for xPos = 1,width do
- coord = map[yPos][xPos]
- coord.textObj.setVisible(setVisible)
- coord.backObj.setVisible(setVisible)
- end
- end
- if bezelDrawn then
- topBezel.setVisible(setVisible)
- bottomBezel.setVisible(setVisible)
- leftBezel.setVisible(setVisible)
- rightBezel.setVisible(setVisible)
- end
- clickCatcherObject.setVisible(setVisible)
- cursorObject.setVisible(setVisible and cursorBlink)
- hasChanged = true
- end
- isVisible = setVisible
- return true
- end
- return false
- end
- glassWindow.getOpacity = function()
- return opacity
- end
- glassWindow.setOpacity = function(newOpacity)
- if type(newOpacity) == "number" and newOpacity >= 0 and newOpacity <= 1 then
- if isDrawn then
- local coord
- for yPos = 1, height do
- for xPos = 1,width do
- coord = map[yPos][xPos]
- coord.backObj.setOpacity(newOpacity)
- end
- end
- if bezelDrawn then
- topBezel.setOpacity(newOpacity)
- bottomBezel.setOpacity(newOpacity)
- leftBezel.setOpacity(newOpacity)
- rightBezel.setOpacity(newOpacity)
- end
- hasChanged = true
- end
- opacity = newOpacity
- return true
- end
- return false
- end
- glassWindow.getSurface = function()
- return surface
- end
- glassWindow.setSurface = function(newSurface, redraw)
- surface = newSurface
- if redraw == true and isDrawn then
- glassWindow.setDrawn(false)
- glassWindow.setDrawn(true)
- end
- end
- glassWindow.getOffset = function()
- return xOffset, yOffset, zOffset
- end
- glassWindow.setOffset = function(xOffsetNew, yOffsetNew, zOffsetNew)
- xOffset = math.floor(tonumber(xOffsetNew) or xOffset)
- yOffset = math.floor(tonumber(yOffsetNew) or yOffset)
- zOffset = math.min(250, math.max(1, math.floor(tonumber(zOffsetNew) or zOffset)))
- if isDrawn then
- for yPos, line in ipairs(map) do
- for xPos, coord in ipairs(line) do
- coord.textObj.setX((xPos-1)*6 + xOffset + (charOffset[coord.curText] or 0))
- coord.textObj.setY((yPos-1)*9 + yOffset)
- coord.textObj.setZ(zOffset + 1)
- coord.backObj.setX((xPos-1)*6 + xOffset)
- coord.backObj.setY((yPos-1)*9 + yOffset)
- coord.backObj.setZ(zOffset)
- end
- end
- clickCatcherObject.setX(xOffset)
- clickCatcherObject.setY(yOffset)
- clickCatcherObject.setZ(zOffset + 3)
- if bezelDrawn then
- eraseBezel()
- drawBezel()
- end
- updateCursor()
- hasChanged = true
- end
- return true
- end
- glassWindow.getSize = function()
- return width, height
- end
- glassWindow.setSize = function(newWidth, newHeight)
- for yPos = 1, math.max(newHeight, height) do
- if yPos <= newHeight then
- map[yPos] = map[yPos] or {}
- for xPos = 1, math.max(newWidth, width) do
- if xPos <= newWidth then
- if not map[yPos][xPos] then
- map[yPos][xPos] = createCoord(xPos, yPos, " ", currentTextColour, currentBackgroundColour)
- end
- elseif map[yPos][xPos] then
- local coord = map[yPos][xPos]
- if coord.textObj then
- coord.textObj.delete()
- end
- if coord.backObj then
- coord.backObj.delete()
- end
- map[yPos][xPos] = nil
- end
- end
- elseif map[yPos] then
- for xPos = 1, math.max(newWidth, width) do
- if map[yPos][xPos] then
- local coord = map[yPos][xPos]
- if coord.textObj then
- coord.textObj.delete()
- end
- if coord.backObj then
- coord.backObj.delete()
- end
- map[yPos][xPos] = nil
- end
- end
- map[yPos] = nil
- end
- end
- width, height = newWidth, newHeight
- if isDrawn then
- clickCatcherObject.delete()
- drawClickCatcher()
- if bezelDrawn then
- eraseBezel()
- drawBezel()
- end
- end
- updateCursor()
- hasChanged = true
- end
- glassWindow.getClick = function(objectID, xPos, yPos)
- if objectID == clickCatcherID then
- return math.ceil((xPos+1)/6), math.ceil((yPos+1)/9)
- else
- return false, false
- end
- end
- glassWindow.getBezelDrawn = function()
- return bezelDrawn
- end
- glassWindow.setBezelDrawn = function(isBezelDrawn)
- if type(isBezelDrawn) == "boolean" and bezelDrawn ~= isBezelDrawn then
- bezelDrawn = isBezelDrawn
- if isDrawn then
- if isBezelDrawn then
- drawBezel()
- else
- eraseBezel()
- end
- hasChanged = true
- end
- return true
- end
- return false
- end
- glassWindow.hasChanged = function()
- return hasChanged
- end
- glassWindow.reset = function()
- hasChanged = false
- end
- local newTerm = {
- write = function(text)
- local textType = type(text)
- if textType == "string" or textType == "number" then
- local text = string.gsub(text, "%c", " ")
- local length = string.len(text)
- if length > 0 then
- if map[cursorY] then
- for xPos = 1, length do
- updateCoord(xPos - 1 + cursorX, cursorY, string.sub(text, xPos, xPos), currentTextColour, currentBackgroundColour)
- end
- end
- cursorX = cursorX + length
- updateCursor()
- end
- end
- end,
- blit = function(text, textColour, backgroundColour)
- if type(text) ~= "string" or type(textColour) ~= "string" or type(backgroundColour) ~= "string" then
- error("Expected string, string, string", 2)
- end
- if #text ~= #textColour or #text ~= #backgroundColour then
- error("Arguments must be the same length", 2)
- end
- local text = string.gsub(text, "%c", " ")
- local length = string.len(text)
- if length > 0 then
- if map[cursorY] then
- for xPos = 1, length do
- updateCoord(xPos - 1 + cursorX, cursorY, string.sub(text, xPos, xPos), hexToColour[string.sub(textColour, xPos, xPos)] or currentTextColour, hexToColour[string.sub(backgroundColour, xPos, xPos)] or currentBackgroundColour)
- end
- end
- cursorX = cursorX + length
- updateCursor()
- end
- end,
- clear = function()
- for yPos = 1, height do
- for xPos = 1, width do
- updateCoord(xPos, yPos, " ", currentTextColour, currentBackgroundColour)
- end
- end
- end,
- clearLine = function()
- if map[cursorY] then
- for xPos = 1, width do
- updateCoord(xPos, cursorY, " ", currentTextColour, currentBackgroundColour)
- end
- end
- end,
- getCursorPos = function()
- return cursorX, cursorY
- end,
- setCursorPos = function(xPos, yPos)
- cursorX = math.floor(tonumber(xPos) or cursorX)
- cursorY = math.floor(tonumber(yPos) or cursorY)
- updateCursor()
- end,
- setCursorBlink = function(blink)
- if type(blink) == "boolean" then
- cursorBlink = blink
- updateCursor()
- end
- end,
- isColour = function()
- return true
- end,
- getSize = function()
- return width, height
- end,
- scroll = function(noOfLines)
- local n = math.floor(tonumber(noOfLines) or 0)
- if n ~= 0 and height > 0 then
- for yPos = (n > 0 and 1) or height, (n < 0 and 1) or height, n/math.abs(n) do
- for xPos = 1, width do
- if map[yPos + n] then
- local coord = map[yPos + n][xPos].data
- updateCoord(xPos, yPos, coord.text, coord.textColour, coord.backgroundColour)
- else
- updateCoord(xPos, yPos, " ", currentTextColour, currentBackgroundColour)
- end
- end
- end
- end
- end,
- setTextColour = function(colour)
- local newColour = colourToRGB[tonumber(colour)]
- if newColour then
- currentTextColour = newColour
- updateCursor()
- end
- end,
- getTextColour = function()
- return rgbToColour[currentTextColour]
- end,
- setBackgroundColour = function(colour)
- local newColour = colourToRGB[tonumber(colour)]
- if newColour then
- currentBackgroundColour = newColour
- end
- end,
- getBackgroundColour = function()
- return rgbToColour[currentBackgroundColour]
- end,
- }
- newTerm.isColor = newTerm.isColour
- newTerm.setTextColor = newTerm.setTextColour
- newTerm.getTextColor = newTerm.getTextColour
- newTerm.setBackgroundColor = newTerm.setBackgroundColour
- newTerm.getBackgroundColor = newTerm.getBackgroundColour
- glassWindow.term = newTerm
- glassWindow.setSize(width, height)
- return glassWindow
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement