mirror of
https://github.com/LDDestroier/CC/
synced 2025-01-08 08:20:27 +00:00
Replaced config with default, changed alwaysRender behavior
This commit is contained in:
parent
b5f59a325d
commit
cd61d513ae
180
windont.lua
180
windont.lua
@ -11,21 +11,29 @@ local lval = {
|
|||||||
to_blit = {},
|
to_blit = {},
|
||||||
to_colors = {},
|
to_colors = {},
|
||||||
|
|
||||||
|
expect = function(value, default, valueType)
|
||||||
|
if value == nil or (valueType and type(value) ~= valueType) then
|
||||||
|
return default
|
||||||
|
else
|
||||||
|
return value
|
||||||
|
end
|
||||||
|
end,
|
||||||
|
|
||||||
getTime = function()
|
getTime = function()
|
||||||
return 24 * os.day() + os.time()
|
return 24 * os.day() + os.time()
|
||||||
end,
|
end,
|
||||||
|
|
||||||
-- check if space on screenBuffer is transparent
|
-- check if space on screenBuffer is transparent
|
||||||
check = function(buff, x, y, blitLayer)
|
check = function(buff, x, y, blitLayer)
|
||||||
if buff[blitLayer or 1][y] then
|
if buff[blitLayer or 1][y] then
|
||||||
return (blitLayer or buff[1][y][x]) and (
|
return (blitLayer or buff[1][y][x]) and (
|
||||||
(not buff[blitLayer or 2][y][x] or buff[blitLayer or 2][y][x] ~= "-") or
|
(not buff[blitLayer or 2][y][x] or buff[blitLayer or 2][y][x] ~= "-") or
|
||||||
(not buff[blitLayer or 3][y][x] or buff[blitLayer or 3][y][x] ~= "-")
|
(not buff[blitLayer or 3][y][x] or buff[blitLayer or 3][y][x] ~= "-")
|
||||||
) and (
|
) and (
|
||||||
not (buff[1][y][x] == " " and buff[3][y][x] == "-")
|
not (buff[1][y][x] == " " and buff[3][y][x] == "-")
|
||||||
)
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for i = 1, 16 do
|
for i = 1, 16 do
|
||||||
@ -35,11 +43,14 @@ end
|
|||||||
lval.to_blit[0], lval.to_colors["-"] = "-", 0
|
lval.to_blit[0], lval.to_colors["-"] = "-", 0
|
||||||
|
|
||||||
local windont = {
|
local windont = {
|
||||||
baseTerm = term.current(), -- default base terminal for all windows
|
doClearScreen = false, -- if true, will clear the screen during render
|
||||||
config = {
|
default = {
|
||||||
defaultTextColor = "0", -- default text color (what " " corresponds to in term.blit's second argument)
|
baseTerm = term.current(), -- default base terminal for all windows
|
||||||
defaultBackColor = "f", -- default background color (what " " corresponds to in term.blit's third argument)
|
textColor = "0", -- default text color (what " " corresponds to in term.blit's second argument)
|
||||||
clearScreen = false, -- if true, will clear the screen during render
|
backColor = "f", -- default background color (what " " corresponds to in term.blit's third argument)
|
||||||
|
blink = true,
|
||||||
|
visible = true,
|
||||||
|
alwaysRender = true, -- if true, new windows will always render if they are written to
|
||||||
},
|
},
|
||||||
info = {
|
info = {
|
||||||
BLIT_CALLS = 0, -- amount of term.blit calls during the last render
|
BLIT_CALLS = 0, -- amount of term.blit calls during the last render
|
||||||
@ -52,7 +63,7 @@ local windont = {
|
|||||||
-- draws one or more windon't objects
|
-- draws one or more windon't objects
|
||||||
-- should not draw over any terminal space that isn't occupied by a window
|
-- should not draw over any terminal space that isn't occupied by a window
|
||||||
|
|
||||||
windont.render = function(...)
|
windont.render = function(onlyX1, onlyX2, onlyY, ...)
|
||||||
local windows = {...}
|
local windows = {...}
|
||||||
local bT
|
local bT
|
||||||
local check = lval.check
|
local check = lval.check
|
||||||
@ -61,6 +72,21 @@ windont.render = function(...)
|
|||||||
local blitList = {} -- list of blit commands per line
|
local blitList = {} -- list of blit commands per line
|
||||||
local c = 1 -- current blitList entry
|
local c = 1 -- current blitList entry
|
||||||
|
|
||||||
|
if type(onlyY) == "table" then
|
||||||
|
table.insert(windows, 1, onlyY)
|
||||||
|
onlyY = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(onlyX2) == "table" then
|
||||||
|
table.insert(windows, 1, onlyX2)
|
||||||
|
onlyX2 = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if type(onlyX1) == "table" then
|
||||||
|
table.insert(windows, 1, onlyX1)
|
||||||
|
onlyX1 = nil
|
||||||
|
end
|
||||||
|
|
||||||
local cTime = lval.getTime()
|
local cTime = lval.getTime()
|
||||||
|
|
||||||
local AMNT_OF_BLITS = 0 -- how many blit calls are there?
|
local AMNT_OF_BLITS = 0 -- how many blit calls are there?
|
||||||
@ -82,21 +108,20 @@ windont.render = function(...)
|
|||||||
bT = output.meta.baseTerm
|
bT = output.meta.baseTerm
|
||||||
end
|
end
|
||||||
scr_x, scr_y = bT.getSize()
|
scr_x, scr_y = bT.getSize()
|
||||||
for y = 1, scr_y do
|
for y = onlyY or 1, onlyY or scr_y do
|
||||||
screenBuffer[1][y] = {}
|
screenBuffer[1][y] = {}
|
||||||
screenBuffer[2][y] = {}
|
screenBuffer[2][y] = {}
|
||||||
screenBuffer[3][y] = {}
|
screenBuffer[3][y] = {}
|
||||||
blitList = {}
|
blitList = {}
|
||||||
c = 1
|
c = 1
|
||||||
for x = 1, scr_x do
|
for x = onlyX1 or 1, math.min(scr_x, onlyX2 or scr_x) do
|
||||||
isWindowHere = false
|
|
||||||
for i = #windows, 1, -1 do
|
for i = #windows, 1, -1 do
|
||||||
if bT_list[i] then
|
if bT_list[i] then
|
||||||
newChar, newText, newBack = nil
|
newChar, newText, newBack = nil
|
||||||
if windows[i].meta.visible then
|
if windows[i].meta.visible then
|
||||||
buffer = windows[i].meta.buffer
|
buffer = windows[i].meta.buffer
|
||||||
cx = x - windows[i].meta.x + 1
|
cx = 1 + x + -windows[i].meta.x
|
||||||
cy = y - windows[i].meta.y + 1
|
cy = 1 + y + -windows[i].meta.y
|
||||||
char_cx, text_cx, back_cx = cx, cx, cx
|
char_cx, text_cx, back_cx = cx, cx, cx
|
||||||
char_cy, text_cy, back_cy = cy, cy, cy
|
char_cy, text_cy, back_cy = cy, cy, cy
|
||||||
|
|
||||||
@ -133,11 +158,11 @@ windont.render = function(...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if windont.config.clearScreen then
|
if windont.doClearScreen then
|
||||||
screenBuffer[1][y][x] = screenBuffer[1][y][x] or " "
|
screenBuffer[1][y][x] = screenBuffer[1][y][x] or " "
|
||||||
end
|
end
|
||||||
screenBuffer[2][y][x] = screenBuffer[2][y][x] or windont.config.defaultBackColor -- intentionally not the default text color
|
screenBuffer[2][y][x] = screenBuffer[2][y][x] or windont.default.backColor -- intentionally not the default text color
|
||||||
screenBuffer[3][y][x] = screenBuffer[3][y][x] or windont.config.defaultBackColor
|
screenBuffer[3][y][x] = screenBuffer[3][y][x] or windont.default.backColor
|
||||||
|
|
||||||
if check(screenBuffer, x, y) then
|
if check(screenBuffer, x, y) then
|
||||||
if check(screenBuffer, x - 1, y) then
|
if check(screenBuffer, x - 1, y) then
|
||||||
@ -162,8 +187,8 @@ windont.render = function(...)
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
windont.info.BLIT_CALLS = AMNT_OF_BLITS
|
|
||||||
windont.info.LAST_RENDER_AMOUNT = #windows
|
windont.info.LAST_RENDER_AMOUNT = #windows
|
||||||
|
windont.info.BLIT_CALLS = AMNT_OF_BLITS
|
||||||
windont.info.LAST_RENDER_WINDOWS = windows
|
windont.info.LAST_RENDER_WINDOWS = windows
|
||||||
windont.info.LAST_RENDER_TIME = cTime
|
windont.info.LAST_RENDER_TIME = cTime
|
||||||
windont.info.LAST_RENDER_DURATION = lval.getTime() - cTime
|
windont.info.LAST_RENDER_DURATION = lval.getTime() - cTime
|
||||||
@ -189,28 +214,28 @@ windont.newWindow = function( x, y, width, height, misc )
|
|||||||
local output = {}
|
local output = {}
|
||||||
misc = misc or {}
|
misc = misc or {}
|
||||||
local meta = {
|
local meta = {
|
||||||
x = x or 1, -- x position of the window
|
x = lval.expect(x, 1), -- x position of the window
|
||||||
y = y or 1, -- y position of the window
|
y = lval.expect(y, 1), -- y position of the window
|
||||||
width = width, -- width of the buffer
|
width = width, -- width of the buffer
|
||||||
height = height, -- height of the buffer
|
height = height, -- height of the buffer
|
||||||
buffer = {}, -- stores contents of terminal in buffer[1][y][x] format
|
buffer = lval.expect(misc.buffer, {}, "table"), -- stores contents of terminal in buffer[1][y][x] format
|
||||||
renderBuddies = {}, -- renders any other window objects stored here after rendering here
|
renderBuddies = lval.expect(misc.renderBuddies, {}, "table"), -- renders any other window objects stored here after rendering here
|
||||||
baseTerm = misc.baseTerm or windont.baseTerm, -- base terminal for which this window draws on
|
baseTerm = lval.expect(misc.baseTerm, windont.default.baseTerm, "table"), -- base terminal for which this window draws on
|
||||||
|
isColor = lval.expect(misc.isColor, term.isColor(), "boolean"), -- if true, then it's an advanced computer
|
||||||
|
|
||||||
charTransformation = nil, -- function that transforms the characters of the window
|
charTransformation = lval.expect(misc.charTransformation, nil, "function"), -- function that transforms the characters of the window
|
||||||
textTransformation = nil, -- function that transforms the text colors of the window
|
textTransformation = lval.expect(misc.textTransformation, nil, "function"), -- function that transforms the text colors of the window
|
||||||
backTransformation = nil, -- function that transforms the BG colors of the window
|
backTransformation = lval.expect(misc.backTransformation, nil, "function"), -- function that transforms the BG colors of the window
|
||||||
|
|
||||||
cursorX = misc.cursorX or 1,
|
cursorX = lval.expect(misc.cursorX, 1),
|
||||||
cursorY = misc.cursorY or 1,
|
cursorY = lval.expect(misc.cursorY, 1),
|
||||||
|
|
||||||
textColor = misc.textColor or windont.config.defaultTextColor, -- current text color
|
textColor = lval.expect(misc.textColor, windont.default.textColor, "string"), -- current text color
|
||||||
backColor = misc.backColor or windont.config.defaultBackColor, -- current background color
|
backColor = lval.expect(misc.backColor, windont.default.backColor, "string"), -- current background color
|
||||||
|
|
||||||
blink = true, -- cursor blink
|
blink = lval.expect(misc.blink, windont.default.blink, "boolean"), -- cursor blink
|
||||||
isColor = term.isColor(), -- if true, then it's an advanced computer
|
alwaysRender = lval.expect(misc.alwaysRender, windont.default.alwaysRender, "boolean"), -- render after every terminal operation
|
||||||
alwaysRender = true, -- render after every terminal operation
|
visible = lval.expect(misc.visible, windont.default.visible, "boolean"), -- if false, don't render ever
|
||||||
visible = true, -- if false, don't render ever
|
|
||||||
|
|
||||||
-- make a new buffer (optionally uses an existing buffer as a reference)
|
-- make a new buffer (optionally uses an existing buffer as a reference)
|
||||||
newBuffer = function(width, height, char, text, back, drawAtop)
|
newBuffer = function(width, height, char, text, back, drawAtop)
|
||||||
@ -237,49 +262,38 @@ windont.newWindow = function( x, y, width, height, misc )
|
|||||||
output.meta = meta
|
output.meta = meta
|
||||||
|
|
||||||
output.write = function(text)
|
output.write = function(text)
|
||||||
assert(type(text) == "string", "argument must be string")
|
assert(type(text) == "string" or type(text) == "number", "expected string, got " .. type(text))
|
||||||
for i = 1, #text do
|
local initX = meta.cursorX
|
||||||
|
for i = 1, #tostring(text) do
|
||||||
if meta.cursorX >= 1 and meta.cursorX <= meta.width and meta.cursorY >= 1 and meta.cursorY <= meta.height then
|
if meta.cursorX >= 1 and meta.cursorX <= meta.width and meta.cursorY >= 1 and meta.cursorY <= meta.height then
|
||||||
meta.buffer[1][meta.cursorY][meta.cursorX] = text:sub(i,i)
|
if not meta.buffer[1] then
|
||||||
|
error("what the fuck happened")
|
||||||
|
end
|
||||||
|
meta.buffer[1][meta.cursorY][meta.cursorX] = tostring(text):sub(i,i)
|
||||||
meta.buffer[2][meta.cursorY][meta.cursorX] = meta.textColor
|
meta.buffer[2][meta.cursorY][meta.cursorX] = meta.textColor
|
||||||
meta.buffer[3][meta.cursorY][meta.cursorX] = meta.backColor
|
meta.buffer[3][meta.cursorY][meta.cursorX] = meta.backColor
|
||||||
meta.cursorX = meta.cursorX + 1
|
|
||||||
end
|
end
|
||||||
|
meta.cursorX = meta.cursorX + 1
|
||||||
end
|
end
|
||||||
if meta.alwaysRender then
|
if meta.alwaysRender then
|
||||||
if #text ~= 0 then
|
output.redraw(meta.x + initX - 1, meta.x + meta.cursorX - 1, meta.y + meta.cursorY - 1)
|
||||||
--local limit = math.max(0, meta.width - meta.cursorX + 1)
|
|
||||||
bT.setCursorPos(meta.x, meta.y + meta.cursorY - 1)
|
|
||||||
bT.blit(
|
|
||||||
table.concat(meta.buffer[1][meta.cursorY]),
|
|
||||||
table.concat(meta.buffer[2][meta.cursorY]),
|
|
||||||
table.concat(meta.buffer[3][meta.cursorY])
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
output.blit = function(char, text, back)
|
output.blit = function(char, text, back)
|
||||||
assert(type(char) == "string" and type(text) == "string" and type(back) == "string", "all arguments must be strings")
|
assert(type(char) == "string" and type(text) == "string" and type(back) == "string", "all arguments must be strings")
|
||||||
assert(#char == #text and #text == #back, "arguments must be same length")
|
assert(#char == #text and #text == #back, "arguments must be same length")
|
||||||
|
local initX = meta.cursorX
|
||||||
for i = 1, #char do
|
for i = 1, #char do
|
||||||
if meta.cursorX >= 1 and meta.cursorX <= meta.width and meta.cursorY >= 1 and meta.cursorY <= meta.height then
|
if meta.cursorX >= 1 and meta.cursorX <= meta.width and meta.cursorY >= 1 and meta.cursorY <= meta.height then
|
||||||
meta.buffer[1][meta.cursorY][meta.cursorX] = char:sub(i,i)
|
meta.buffer[1][meta.cursorY][meta.cursorX] = char:sub(i,i)
|
||||||
meta.buffer[2][meta.cursorY][meta.cursorX] = text:sub(i,i) == " " and windont.config.defaultTextColor or text:sub(i,i)
|
meta.buffer[2][meta.cursorY][meta.cursorX] = text:sub(i,i) == " " and windont.default.textColor or text:sub(i,i)
|
||||||
meta.buffer[3][meta.cursorY][meta.cursorX] = back:sub(i,i) == " " and windont.config.defaultBackColor or back:sub(i,i)
|
meta.buffer[3][meta.cursorY][meta.cursorX] = back:sub(i,i) == " " and windont.default.backColor or back:sub(i,i)
|
||||||
meta.cursorX = meta.cursorX + 1
|
meta.cursorX = meta.cursorX + 1
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
if meta.alwaysRender then
|
if meta.alwaysRender then
|
||||||
if #char ~= 0 then
|
output.redraw(meta.x + initX - 1, meta.x + meta.cursorX - 1, meta.y + meta.cursorY - 1)
|
||||||
--local limit = math.max(0, meta.width - meta.cursorX + 1)
|
|
||||||
bT.setCursorPos(meta.x, meta.y + meta.cursorY - 1)
|
|
||||||
bT.blit(
|
|
||||||
table.concat(meta.buffer[1][meta.cursorY]),
|
|
||||||
table.concat(meta.buffer[2][meta.cursorY]),
|
|
||||||
table.concat(meta.buffer[3][meta.cursorY])
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -340,7 +354,9 @@ windont.newWindow = function( x, y, width, height, misc )
|
|||||||
end
|
end
|
||||||
|
|
||||||
output.clearLine = function()
|
output.clearLine = function()
|
||||||
meta.buffer[meta.cursorY] = nil
|
meta.buffer[1][meta.cursorY] = nil
|
||||||
|
meta.buffer[2][meta.cursorY] = nil
|
||||||
|
meta.buffer[3][meta.cursorY] = nil
|
||||||
meta.buffer = meta.newBuffer(meta.width, meta.height, " ", meta.textColor, meta.backColor, meta.buffer)
|
meta.buffer = meta.newBuffer(meta.width, meta.height, " ", meta.textColor, meta.backColor, meta.buffer)
|
||||||
if meta.alwaysRender then
|
if meta.alwaysRender then
|
||||||
bT.setCursorPos(meta.x, meta.y + meta.cursorY - 1)
|
bT.setCursorPos(meta.x, meta.y + meta.cursorY - 1)
|
||||||
@ -360,25 +376,22 @@ windont.newWindow = function( x, y, width, height, misc )
|
|||||||
|
|
||||||
output.scroll = function(amplitude)
|
output.scroll = function(amplitude)
|
||||||
if math.abs(amplitude) < meta.height then -- minor optimization
|
if math.abs(amplitude) < meta.height then -- minor optimization
|
||||||
if amplitude > 0 then
|
local blank = {{}, {}, {}}
|
||||||
for i = 1, math.floor(amplitude) do
|
for x = 1, meta.width do
|
||||||
table.remove(meta.buffer[1], 1)
|
blank[1][x] = " "
|
||||||
table.remove(meta.buffer[2], 1)
|
blank[2][x] = meta.textColor
|
||||||
table.remove(meta.buffer[3], 1)
|
blank[3][x] = meta.backColor
|
||||||
end
|
end
|
||||||
else
|
for y = 1, meta.height do
|
||||||
for i = 1, math.floor(-amplitude) do
|
meta.buffer[1][y] = meta.buffer[1][y + amplitude] or blank[1]
|
||||||
table.insert(meta.buffer[1], 1, false)
|
meta.buffer[2][y] = meta.buffer[2][y + amplitude] or blank[2]
|
||||||
table.insert(meta.buffer[2], 1, false)
|
meta.buffer[3][y] = meta.buffer[3][y + amplitude] or blank[3]
|
||||||
table.insert(meta.buffer[3], 1, false)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
meta.buffer = meta.newBuffer(meta.width, meta.height, " ", meta.textColor, meta.backColor, meta.buffer)
|
|
||||||
else
|
else
|
||||||
meta.buffer = meta.newBuffer(meta.width, meta.height, " ", meta.textColor, meta.backColor)
|
meta.buffer = meta.newBuffer(meta.width, meta.height, " ", meta.textColor, meta.backColor)
|
||||||
end
|
end
|
||||||
if meta.alwaysRender then
|
if meta.alwaysRender then
|
||||||
if math.floor(amplitude) ~= 0 then
|
if math.floor(amplitude) ~= 0 then
|
||||||
output.redraw()
|
output.redraw()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -435,12 +448,13 @@ windont.newWindow = function( x, y, width, height, misc )
|
|||||||
output.getPaletteColor = bT.getPaletteColor
|
output.getPaletteColor = bT.getPaletteColor
|
||||||
output.getPaletteColour = bT.getPaletteColour
|
output.getPaletteColour = bT.getPaletteColour
|
||||||
|
|
||||||
output.redraw = function()
|
output.redraw = function(x1, x2, y)
|
||||||
if #meta.renderBuddies > 0 then
|
if #meta.renderBuddies > 0 then
|
||||||
windont.render(output, table.unpack(meta.renderBuddies))
|
windont.render(x1, x2, y, output, table.unpack(meta.renderBuddies))
|
||||||
else
|
else
|
||||||
windont.render(output)
|
windont.render(x1, x2, y, output)
|
||||||
end
|
end
|
||||||
|
output.restoreCursor()
|
||||||
end
|
end
|
||||||
|
|
||||||
if meta.alwaysRender then
|
if meta.alwaysRender then
|
||||||
|
Loading…
Reference in New Issue
Block a user