New features

- Added palette support for base frames
- Finished bimg support: image can now display bimg-animation
- Finished Graphic Object: This object can draw into a bimg table via code
- From now on the default image is bimg
This commit is contained in:
Robert Jelic
2022-10-16 16:47:12 +02:00
parent 97ec92aac2
commit 90895374a0
8 changed files with 431 additions and 75 deletions

View File

@@ -49,6 +49,8 @@ return function(name, parent, pTerm, basalt)
local activeEvents = {} local activeEvents = {}
local colorTheme = {}
base:setZIndex(10) base:setZIndex(10)
local basaltDraw = BasaltDraw(termObject) local basaltDraw = BasaltDraw(termObject)
@@ -426,6 +428,25 @@ return function(name, parent, pTerm, basalt)
return theme[name] or (self.parent~=nil and self.parent:getTheme(name) or basalt.getTheme(name)) return theme[name] or (self.parent~=nil and self.parent:getTheme(name) or basalt.getTheme(name))
end, end,
getThemeColor = function(self, col)
return colorTheme[col]
end,
setThemeColor = function(self, col, ...)
if(self.parent==nil)then
if(type(col)=="string")then
colorTheme[col] = ...
termObject.setPaletteColor(colors[col], ...)
elseif(type(col)=="table")then
for k,v in pairs(col)do
colorTheme[k] = v
termObject.setPaletteColor(k, type(v)=="number" and v or table.unpack(v))
end
end
end
return self
end,
setPosition = function(self, x, y, rel) setPosition = function(self, x, y, rel)
base.setPosition(self, x, y, rel) base.setPosition(self, x, y, rel)
self:recalculateDynamicValues() self:recalculateDynamicValues()
@@ -524,6 +545,14 @@ return function(name, parent, pTerm, basalt)
show = function(self) show = function(self)
base.show(self) base.show(self)
if(self.parent==nil)then if(self.parent==nil)then
for k,v in pairs(colors)do
if(type(v)=="number")then
termObject.setPaletteColor(v, colors.packRGB(term.nativePaletteColor((v))))
end
end
for k,v in pairs(colorTheme)do
termObject.setPaletteColor(colors[k], type(v)=="number" and v or table.unpack(v))
end
basalt.setActiveFrame(self) basalt.setActiveFrame(self)
if(isMonitor)and not(isGroupedMonitor)then if(isMonitor)and not(isGroupedMonitor)then
basalt.setMonitorFrame(monSide, self) basalt.setMonitorFrame(monSide, self)

View File

@@ -1,10 +1,12 @@
local sub,rep = string.sub,string.rep local sub,rep = string.sub,string.rep
return function() local function frame(base, manager)
local w, h = 0, 0 local w, h = 0, 0
local t,fg,bg = {}, {}, {} local t,fg,bg = {}, {}, {}
local x, y = 1,1 local x, y = 1,1
local data = {}
local function recalculateSize() local function recalculateSize()
for y=1,h do for y=1,h do
if(t[y]==nil)then if(t[y]==nil)then
@@ -35,7 +37,7 @@ return function()
end end
if(#t[y]>w)then w = #t[y] end if(#t[y]>w)then w = #t[y] end
if(y > h)then h = y end if(y > h)then h = y end
recalculateSize() manager.updateSize(w, h)
end end
local addBg = function(b, _x, _y) local addBg = function(b, _x, _y)
@@ -48,7 +50,7 @@ return function()
end end
if(#bg[y]>w)then w = #bg[y] end if(#bg[y]>w)then w = #bg[y] end
if(y > h)then h = y end if(y > h)then h = y end
recalculateSize() manager.updateSize(w, h)
end end
local addFg = function(f, _x, _y) local addFg = function(f, _x, _y)
@@ -61,15 +63,61 @@ return function()
end end
if(#fg[y]>w)then w = #fg[y] end if(#fg[y]>w)then w = #fg[y] end
if(y > h)then h = y end if(y > h)then h = y end
recalculateSize() manager.updateSize(w, h)
end end
local public = { local function setFrame(frm)
data = {}
t, fg, bg = {}, {}, {}
for k,v in pairs(base)do
if(type(k)=="string")then
data[k] = v
else
t[k], fg[k], bg[k] = v[1], v[2], v[3]
end
end
manager.updateSize(w, h)
end
if(base~=nil)then
setFrame(base)
end
return {
recalculateSize = recalculateSize,
setFrame = setFrame,
getFrame = function()
local f = {}
for k,v in pairs(data)do
f[k] = v
end
for k,v in pairs(t)do
table.insert(f, {v, fg[k], bg[k]})
end
return f
end,
getImage = function()
local i = {}
for k,v in pairs(t)do
table.insert(i, {v, fg[k], bg[k]})
end
return i
end,
setFrameData = function(key, value)
data[key] = value
end,
blit = function(text, fgCol, bgCol, x, y) blit = function(text, fgCol, bgCol, x, y)
addText(text, x, y) addText(text, x, y)
addFg(fgCol, x, y) addFg(fgCol, x, y)
addBg(bgCol, x, y) addBg(bgCol, x, y)
end, end,
text = addText, text = addText,
fg = addFg, fg = addFg,
bg = addBg, bg = addBg,
@@ -100,24 +148,142 @@ return function()
t, fg, bg = nt, nfg, nbg t, fg, bg = nt, nfg, nbg
w, h = _w, _h w, h = _w, _h
end, end,
}
setBimgData = function(data) end
w, h = 0, 0
for k,v in pairs(data[1])do return function(img)
t[k], fg[k], bg[k] = v[1], v[2], v[3] local frames = {}
if(#v[1] > w)then w = #v[1] end local metadata = {creator="Bimg Library by NyoriE", date=os.date("!%Y-%m-%dT%TZ")}
local width,height = 0, 0
local manager = {}
local function addFrame(id, data)
id = id or #frames+1
frames[id] = frame(data, manager)
frames[id].setSize(width, height)
end
manager = {
updateSize = function(w, h)
local changed = false
if(w > width)then changed = true width = w end
if(h > height)then changed = true height = h end
if(changed)then
for k,v in pairs(frames)do
v.setSize(width, height)
v.recalculateSize()
end
end end
h = #data[1]
recalculateSize()
end, end,
getBimgData = function() text = function(frame, text, x, y)
local data = {} local f = frames[frame]
for k,v in pairs(t)do if(f==nil)then
data[k] = {t[k], fg[k], bg[k]} f = addFrame(frame)
end end
return {[1]=data,creator="Basalt Graphic 1.0",version="1.0.0"} f.text(text, x, y)
end,
fg = function(frame, fg, x, y)
local f = frames[frame]
if(f==nil)then
f = addFrame(frame)
end end
f.fg(fg, x, y)
end,
bg = function(frame, bg, x, y)
local f = frames[frame]
if(f==nil)then
f = addFrame(frame)
end
f.bg(bg, x, y)
end,
blit = function(frame, text, fg, bg, x, y)
local f = frames[frame]
if(f==nil)then
f = addFrame(frame)
end
f.blit(text, fg, bg, x, y)
end,
setSize = function(w, h)
width = w
height = h
for k,v in pairs(frames)do
v.setSize(w, h)
end
end,
getFrame = function(id)
if(frames[id]~=nil)then
return frames[id].getFrame()
end
end,
getFrameObjects = function()
return frames
end,
getFrameObject = function(id)
return frames[id]
end,
addFrame = function(id)
local f = frame()
if(#frames<=1)then
metadata.animated = true
metadata.secondsPerFrame = 1
end
addFrame(id)
return f
end,
setFrameData = function(id, key, value)
if(frames[id]~=nil)then
frames[id].setFrameData(key, value)
end
end,
getSize = function()
return width, height
end,
setAnimation = function(anim)
metadata.animation = anim
end,
setMetadata = function(key, val)
meta[key] = val
end,
createBimg = function()
local bimg = {}
for k,v in pairs(frames)do
table.insert(bimg, v.getFrame())
end
for k,v in pairs(metadata)do
bimg[k] = v
end
bimg.width = width
bimg.height = height
return bimg
end,
} }
return public
if(img~=nil)then
for k,v in pairs(img)do
if(type(k)=="string")then
metadata[k] = v
else
addFrame(k, v)
end
end
else
addFrame(1, manager)
end
return manager
end end

View File

@@ -1,4 +1,8 @@
local sub = string.sub local sub,floor = string.sub,math.floor
local function loadNFPAsBimg(path)
return {[1]={{}, {}, paintutils.loadImage(path)}}, "bimg"
end
local function loadNFP(path) local function loadNFP(path)
return paintutils.loadImage(path), "nfp" return paintutils.loadImage(path), "nfp"
@@ -6,13 +10,21 @@ end
local function loadBIMG(path) local function loadBIMG(path)
local f = fs.open(path, "rb") local f = fs.open(path, "rb")
local content = load("return "..f.readAll())() local content = textutils.unserialize(f.readAll())
f.close() f.close()
if(content~=nil)then if(content~=nil)then
return content, "bimg" return content, "bimg"
end end
end end
local function loadBBF(path)
end
local function loadBBFAsBimg(path)
end
local function loadImage(path, f) local function loadImage(path, f)
if(f==nil)then if(f==nil)then
if(path:find(".bimg"))then if(path:find(".bimg"))then
@@ -26,6 +38,18 @@ local function loadImage(path, f)
-- ... -- ...
end end
local function loadImageAsBimg(path, f)
if(f==nil)then
if(path:find(".bimg"))then
return loadBIMG(path)
elseif(path:find(".bbf"))then
return loadBBFAsBimg(path)
else
return loadNFPAsBimg(path)
end
end
end
local function resizeBIMG(source, w, h) local function resizeBIMG(source, w, h)
local oW, oH = #source[1][1][1], #source[1] local oW, oH = #source[1][1][1], #source[1]
local newImg = {{}} local newImg = {{}}
@@ -33,10 +57,10 @@ local function resizeBIMG(source, w, h)
local img = source[1] local img = source[1]
for y=1, h do for y=1, h do
local xT,xFG,xBG = "","","" local xT,xFG,xBG = "","",""
local yR = math.floor(y / h * oH + 0.5) local yR = floor(y / h * oH + 0.5)
if(img[yR]~=nil)then if(img[yR]~=nil)then
for x=1, w do for x=1, w do
local xR = math.floor(x / w * oW + 0.5) local xR = floor(x / w * oW + 0.5)
xT = xT..sub(img[yR][1], xR,xR) xT = xT..sub(img[yR][1], xR,xR)
xFG = xFG..sub(img[yR][2], xR,xR) xFG = xFG..sub(img[yR][2], xR,xR)
xBG = xBG..sub(img[yR][3], xR,xR) xBG = xBG..sub(img[yR][3], xR,xR)
@@ -52,5 +76,6 @@ return {
loadBIMG = loadBIMG, loadBIMG = loadBIMG,
loadImage = loadImage, loadImage = loadImage,
resizeBIMG = resizeBIMG, resizeBIMG = resizeBIMG,
loadImageAsBimg = loadImageAsBimg,
} }

View File

@@ -10,7 +10,7 @@ local moveThrottle = 300
local dragThrottle = 50 local dragThrottle = 50
local baseTerm = term.current() local baseTerm = term.current()
local version = "1.6.2" local version = "1.6.4"
local projectDirectory = fs.getDir(table.pack(...)[2] or "") local projectDirectory = fs.getDir(table.pack(...)[2] or "")
@@ -23,13 +23,26 @@ if not term.isColor or not term.isColor() then
error('Basalt requires an advanced (golden) computer to run.', 0) error('Basalt requires an advanced (golden) computer to run.', 0)
end end
local defaultColors = {}
for k,v in pairs(colors)do
if(type(v)=="number")then
defaultColors[k] = {baseTerm.getPaletteColor(v)}
end
end
local function stop() local function stop()
updaterActive = false updaterActive = false
baseTerm.clear() baseTerm.clear()
baseTerm.setCursorPos(1, 1) baseTerm.setCursorPos(1, 1)
for k,v in pairs(colors)do
if(type(v)=="number")then
baseTerm.setPaletteColor(v, colors.packRGB(table.unpack(defaultColors[k])))
end
end
end end
local basaltError = function(errMsg) local function basaltError(errMsg)
baseTerm.clear() baseTerm.clear()
baseTerm.setBackgroundColor(colors.black) baseTerm.setBackgroundColor(colors.black)
baseTerm.setTextColor(colors.red) baseTerm.setTextColor(colors.red)

52
Basalt/objects/Graph.lua Normal file
View File

@@ -0,0 +1,52 @@
local Object = require("Object")
local utils = require("utils")
local xmlValue = utils.getValueFromXML
local tHex = require("tHex")
return function(name)
-- Graph
local base = Object(name)
local objectType = "Graph"
local graphAmount = {}
local lastTime = 0
base:setZIndex(5)
local object = {
init = function(self)
if(base.init(self))then
self.bgColor = self.parent:getTheme("GraphBG")
self.fgColor = self.parent:getTheme("GraphText")
end
end,
getType = function(self)
return objectType
end;
setValuesByXMLData = function(self, data)
base.setValuesByXMLData(self, data)
return self
end,
add = function(self, val)
return self
end,
clear = function(self)
return self
end,
draw = function(self)
if (base.draw(self)) then
end
end,
}
return setmetatable(object, base)
end

View File

@@ -11,7 +11,9 @@ return function(name)
local base = Object(name) local base = Object(name)
local objectType = "Graphic" local objectType = "Graphic"
local imgData = bimgLib() local imgData = bimgLib()
local bimgFrame = imgData.getFrameObject(1)
local bimg local bimg
local selectedFrame = 1
base:setZIndex(5) base:setZIndex(5)
local xOffset, yOffset = 0, 0 local xOffset, yOffset = 0, 0
@@ -43,11 +45,25 @@ return function(name)
return self return self
end, end,
setPixel = function(self, text, fg, bg, _x, _y) selectFrame = function(self, id)
if(imgData.getFrameObject(id)==nil)then
imgData.addFrame(id)
end
bimgFrame = imgData.getFrameObject(id)
bimg = bimgFrame.getImage(id)
selectedFrame = id
self:updateDraw()
end,
getSelectedFrame = function(self)
return selectedFrame
end,
blit = function(self, text, fg, bg, _x, _y)
x = _x or x x = _x or x
y = _y or y y = _y or y
imgData.blit(text, fg, bg, x, y) bimgFrame.blit(text, fg, bg, x, y)
bimg = imgData.getBimgData() bimg = bimgFrame.getImage()
self:updateDraw() self:updateDraw()
return self return self
end, end,
@@ -55,8 +71,8 @@ return function(name)
setText = function(self, text, _x, _y) setText = function(self, text, _x, _y)
x = _x or x x = _x or x
y = _y or y y = _y or y
imgData.text(text, x, y) bimgFrame.text(text, x, y)
bimg = imgData.getBimgData() bimg = imgData.getFrame()
self:updateDraw() self:updateDraw()
return self return self
end, end,
@@ -64,8 +80,8 @@ return function(name)
setBg = function(self, bg, _x, _y) setBg = function(self, bg, _x, _y)
x = _x or x x = _x or x
y = _y or y y = _y or y
imgData.bg(bg, x, y) bimgFrame.bg(bg, x, y)
bimg = imgData.getBimgData() bimg = bimgFrame.getImage()
self:updateDraw() self:updateDraw()
return self return self
end, end,
@@ -73,8 +89,8 @@ return function(name)
setFg = function(self, fg, _x, _y) setFg = function(self, fg, _x, _y)
x = _x or x x = _x or x
y = _y or y y = _y or y
imgData.fg(fg, x, y) bimgFrame.fg(fg, x, y)
bimg = imgData.getBimgData() bimg = bimgFrame.getImage()
self:updateDraw() self:updateDraw()
return self return self
end, end,
@@ -85,21 +101,29 @@ return function(name)
setImageSize = function(self, w, h) setImageSize = function(self, w, h)
imgData.setSize(w, h) imgData.setSize(w, h)
bimg = imgData.getBimgData() bimg = bimgFrame.getImage()
self:updateDraw() self:updateDraw()
return self return self
end, end,
resizeImage = function(self, w, h) resizeImage = function(self, w, h)
bimg = images.resizeBIMG(bimg, w, h) local newBimg = images.resizeBIMG(imgData.createBimg(), w, h)
imgData.setBimgData(bimg) imgData = bimgLib(newBimg)
selectedFrame = 1
bimgFrame = imgData.getFrameObject(1)
bimg = bimgFrame.getImage()
self:updateDraw()
return self return self
end, end,
loadImage = function(self, path, _format) loadImage = function(self, path)
if(fs.exists(path))then if(fs.exists(path))then
bimg = images.loadBIMG(path, _format) local newBimg = images.loadBIMG(path)
imgData.setBimgData(bimg) imgData = bimgLib(newBimg)
selectedFrame = 1
bimgFrame = imgData.getFrameObject(1)
bimg = bimgFrame.getImage()
self:updateDraw()
end end
return self return self
end, end,
@@ -112,7 +136,7 @@ return function(name)
end, end,
getImage = function(self) getImage = function(self)
return imgData.getBimgData() return imgData.createBimg()
end, end,
draw = function(self) draw = function(self)
@@ -121,7 +145,7 @@ return function(name)
local obx, oby = self:getAnchorPosition() local obx, oby = self:getAnchorPosition()
local w,h = self:getSize() local w,h = self:getSize()
if(bimg~=nil)then if(bimg~=nil)then
for k,v in pairs(bimg[1])do for k,v in pairs(bimg)do
if(k<=h-yOffset)and(k+yOffset>=1)then if(k<=h-yOffset)and(k+yOffset>=1)then
self.parent:blit(obx+xOffset, oby+k-1+yOffset, v[1], v[2], v[3]) self.parent:blit(obx+xOffset, oby+k-1+yOffset, v[1], v[2], v[3])
end end

View File

@@ -1,6 +1,7 @@
local Object = require("Object") local Object = require("Object")
local xmlValue = require("utils").getValueFromXML local xmlValue = require("utils").getValueFromXML
local images = require("images") local images = require("images")
local log = require("basaltLogs")
local unpack,sub = table.unpack,string.sub local unpack,sub = table.unpack,string.sub
return function(name) return function(name)
@@ -10,7 +11,12 @@ return function(name)
base:setZIndex(2) base:setZIndex(2)
local originalImage local originalImage
local image local image
local format = "nfp" local curFrame = 1
local infinitePlay = false
local animTimer
base.width = 24
base.height = 8
local object = { local object = {
init = function(self) init = function(self)
@@ -23,33 +29,75 @@ return function(name)
end; end;
loadImage = function(self, path, f) loadImage = function(self, path, f)
originalImage, _format = images.loadImage(path, f) if not(fs.exists(path))then error("No valid path: "..path) end
originalImage = images.loadImageAsBimg(path, f)
curFrame = 1
image = originalImage image = originalImage
if(_format~=nil)then
format = _format
end
self:updateDraw() self:updateDraw()
return self return self
end; end;
setImage = function(self, data, _format) play = function(self, inf)
originalImage = data if(originalImage.animated)then
format = _format local t = originalImage[curFrame].duration or originalImage.secondsPerFrame or 0.2
self.parent:addEvent("other_event", self)
animTimer = os.startTimer(t)
infinitePlay = inf or false
end
return self
end, end,
getImageData = function(self) selectFrame = function(self, fr)
return image if(originalImage[fr]~=nil)then
curFrame = fr
if(animTimer~=nil)then
os.cancelTimer(animTimer)
end
self:updateDraw()
end
end,
eventHandler = function(self, event, timerId, ...)
base.eventHandler(self, event, timerId, ...)
if(event=="timer")then
if(timerId==animTimer)then
if(originalImage[curFrame+1]~=nil)then
curFrame = curFrame + 1
local t = originalImage[curFrame].duration or originalImage.secondsPerFrame or 0.2
animTimer = os.startTimer(t)
else
if(infinitePlay)then
curFrame = 1
local t = originalImage[curFrame].duration or originalImage.secondsPerFrame or 0.2
animTimer = os.startTimer(t)
end
end
self:updateDraw()
end
end
end,
getMetadata = function(self, key)
return originalImage[key]
end,
setImage = function(self, data)
originalImage = data
image = originalImage
curFrame = 1
if(animTimer~=nil)then
os.cancelTimer(animTimer)
end
self:updateDraw()
end, end,
getImageSize = function(self) getImageSize = function(self)
return #image[1][1][1], #image[1] return originalImage.width, originalImage.height
end, end,
resizeImage = function(self, w, h) resizeImage = function(self, w, h)
if(format=="bimg")then
image = images.resizeBIMG(originalImage, w, h) image = images.resizeBIMG(originalImage, w, h)
self:updateDraw() self:updateDraw()
end
return self return self
end, end,
@@ -64,10 +112,8 @@ return function(name)
if (image ~= nil) then if (image ~= nil) then
local obx, oby = self:getAnchorPosition() local obx, oby = self:getAnchorPosition()
local w,h = self:getSize() local w,h = self:getSize()
if(format=="nfp")then for y,v in ipairs(image[curFrame])do
log(unpack(v))
elseif(format=="bimg")then
for y,v in ipairs(image[1])do
local t, f, b = unpack(v) local t, f, b = unpack(v)
t = sub(t, 1,w) t = sub(t, 1,w)
f = sub(f, 1,w) f = sub(f, 1,w)
@@ -77,7 +123,6 @@ return function(name)
end end
end end
end end
end
end, end,
} }

View File

@@ -40,6 +40,8 @@ return { -- The default main theme for basalt!
SwitchInactive = colors.red, SwitchInactive = colors.red,
SwitchActive = colors.green, SwitchActive = colors.green,
LabelBG = false, LabelBG = false,
LabelText = colors.black LabelText = colors.black,
GraphBG = colors.gray,
GraphText = colors.black
} }