fixed monitors, added setIndex4scrollbars, added dynamic values

This commit is contained in:
Robert Jelic
2022-07-08 19:46:28 +02:00
parent 8665eed03e
commit cbdf439791
17 changed files with 382 additions and 55 deletions

View File

@@ -144,7 +144,7 @@ return function(name, parent, pTerm, basalt)
return self
end;
getFrameOffset = function(self) -- internal
getOffset = function(self) -- internal
return xOffset, yOffset
end;
@@ -250,7 +250,7 @@ return function(name, parent, pTerm, basalt)
basalt.setMonitorFrame(monSide, nil)
end
end
basaltDraw = basaltDraw(termObject)
basaltDraw = BasaltDraw(termObject)
monSide = side or nil
return self;
end;

View File

@@ -1,8 +1,11 @@
local basaltEvent = require("basaltEvent")
local split = require("utils").splitString
local numberFromString = require("utils").numberFromString
return function(name)
-- Base object
local objectType = "Object" -- not changeable
local object = {}
local value
local zIndex = 1
local anchor = "topLeft"
@@ -22,7 +25,46 @@ return function(name)
local eventSystem = basaltEvent()
local object = {
local dynamicValue = {}
local dynamicValueResult = {}
local function replacePercentage(str, parentValue)
local _fullStr = str
for v in _fullStr:gmatch("%d+%%") do
local pValue = v:gsub("%%", "")
print(str)
str = str:gsub(v.."%", parentValue / 100 * math.max(math.min(tonumber(pValue),100),0))
end
return str
end
local function fToNumber(str, fTable)
for k,v in pairs(fTable)do
if(type(v)=="function")then
local nmb = v()
for _ in str:gmatch("f"..k)do
str = string.gsub(str, "f"..k, nmb)
end
end
end
str = str:gsub("f%d+", "")
return str
end
local calcDynamicValue = function(newDValue)
local val = dynamicValue[newDValue][1]
if(val~=nil)then
if(type(val)=="string")then
if(dynamicValue[newDValue][3]~=nil)then
dynamicValueResult[newDValue] = numberFromString(replacePercentage(fToNumber(val, dynamicValue[newDValue][3]), dynamicValue[newDValue][2]() or 1))
else
dynamicValueResult[newDValue] = numberFromString(replacePercentage(val, dynamicValue[newDValue][2]() or 1))
end
end
end
end
object = {
x = 1,
y = 1,
width = 1,
@@ -133,12 +175,36 @@ return function(name)
else
self.x, self.y = math.floor(xPos), math.floor(yPos)
end
if(type(xPos)=="number")then
self.x = rel and self.x+xPos or xPos
end
if(type(yPos)=="number")then
self.y = rel and self.y+yPos or yPos
end
if(type(xPos)=="string")or(type(xPos)=="table")then
dynamicValue.x = {xPos, function() return self:getParent():getX() end}
end
if(type(yPos)=="string")or(type(yPos)=="table")then
dynamicValue.y = {yPos, function() return self:getParent():getY() end}
end
self:recalculateDynamicValue()
eventSystem:sendEvent("basalt_reposition", self)
visualsChanged = true
return self
end;
getX = function(self)
return dynamicValue.x or self.x
end,
getY = function(self)
return dynamicValue.y or self.y
end,
getPosition = function(self)
return self.x, self.y
return self:getX(), self:getY()
end;
getVisibility = function(self)
@@ -151,23 +217,45 @@ return function(name)
return self
end;
setSize = function(self, width, height)
self.width, self.height = width, height
setSize = function(self, width, height, rel)
if(type(width)=="number")then
self.width = rel and self.width+width or width
end
if(type(height)=="number")then
self.height = rel and self.height+height or height
end
if(type(width)=="string")then
dynamicValue.width = {width, function() return self:getParent():getWidth() end}
end
if(type(width)=="table")then
dynamicValue.width = {width[1], function() return self:getParent():getWidth() end}
table.remove(width, 1)
dynamicValue.width[3] = width
end
if(type(height)=="string")then
dynamicValue.height = {height, function() return self:getParent():getHeight() end}
end
if(type(height)=="table")then
dynamicValue.height = {height[1], function() return self:getParent():getHeight() end}
table.remove(height, 1)
dynamicValue.height[3] = height
end
self:recalculateDynamicValue()
eventSystem:sendEvent("basalt_resize", self)
visualsChanged = true
return self
end;
getHeight = function(self)
return self.height
return dynamicValueResult["height"] or self.height
end;
getWidth = function(self)
return self.width
return dynamicValueResult["width"] or self.width
end;
getSize = function(self)
return self.width, self.height
return self:getWidth(), self:getHeight()
end;
setBackground = function(self, color)
@@ -246,16 +334,17 @@ return function(name)
if (isVisible) then
if(self.parent~=nil)then
local x, y = self:getAnchorPosition()
local w,h = self:getSize()
if(shadow)then
self.parent:drawBackgroundBox(x+1, y+self.height, self.width, 1, shadowColor)
self.parent:drawBackgroundBox(x+self.width, y+1, 1, self.height, shadowColor)
self.parent:drawForegroundBox(x+1, y+self.height, self.width, 1, shadowColor)
self.parent:drawForegroundBox(x+self.width, y+1, 1, self.height, shadowColor)
self.parent:drawBackgroundBox(x+1, y+h, w, 1, shadowColor)
self.parent:drawBackgroundBox(x+w, y+1, 1, h, shadowColor)
self.parent:drawForegroundBox(x+1, y+h, w, 1, shadowColor)
self.parent:drawForegroundBox(x+w, y+1, 1, h, shadowColor)
end
if(borderLeft)then
self.parent:drawTextBox(x-1, y, 1, self.height, "\149")
self.parent:drawForegroundBox(x-1, y, 1, self.height, borderColor)
if(self.bgColor~=false)then self.parent:drawBackgroundBox(x-1, y, 1, self.height, self.bgColor) end
self.parent:drawTextBox(x-1, y, 1, h, "\149")
self.parent:drawForegroundBox(x-1, y, 1, h, borderColor)
if(self.bgColor~=false)then self.parent:drawBackgroundBox(x-1, y, 1, h, self.bgColor) end
end
if(borderLeft)and(borderTop)then
self.parent:drawTextBox(x-1, y-1, 1, 1, "\151")
@@ -263,29 +352,29 @@ return function(name)
if(self.bgColor~=false)then self.parent:drawBackgroundBox(x-1, y-1, 1, 1, self.bgColor) end
end
if(borderTop)then
self.parent:drawTextBox(x, y-1, self.width, 1, "\131")
self.parent:drawForegroundBox(x, y-1, self.width, 1, borderColor)
if(self.bgColor~=false)then self.parent:drawBackgroundBox(x, y-1, self.width, 1, self.bgColor) end
self.parent:drawTextBox(x, y-1, w, 1, "\131")
self.parent:drawForegroundBox(x, y-1, w, 1, borderColor)
if(self.bgColor~=false)then self.parent:drawBackgroundBox(x, y-1, w, 1, self.bgColor) end
end
if(borderTop)and(borderRight)then
self.parent:drawTextBox(x+self.width, y-1, 1, 1, "\149")
self.parent:drawForegroundBox(x+self.width, y-1, 1, 1, borderColor)
self.parent:drawTextBox(x+w, y-1, 1, 1, "\149")
self.parent:drawForegroundBox(x+w, y-1, 1, 1, borderColor)
end
if(borderRight)then
self.parent:drawTextBox(x+self.width, y, 1, self.height, "\149")
self.parent:drawForegroundBox(x+self.width, y, 1, self.height, borderColor)
self.parent:drawTextBox(x+w, y, 1, h, "\149")
self.parent:drawForegroundBox(x+w, y, 1, h, borderColor)
end
if(borderRight)and(borderBottom)then
self.parent:drawTextBox(x+self.width, y+self.height, 1, 1, "\129")
self.parent:drawForegroundBox(x+self.width, y+self.height, 1, 1, borderColor)
self.parent:drawTextBox(x+w, y+h, 1, 1, "\129")
self.parent:drawForegroundBox(x+w, y+h, 1, 1, borderColor)
end
if(borderBottom)then
self.parent:drawTextBox(x, y+self.height, self.width, 1, "\131")
self.parent:drawForegroundBox(x, y+self.height, self.width, 1, borderColor)
self.parent:drawTextBox(x, y+h, w, 1, "\131")
self.parent:drawForegroundBox(x, y+h, w, 1, borderColor)
end
if(borderBottom)and(borderLeft)then
self.parent:drawTextBox(x-1, y+self.height, 1, 1, "\131")
self.parent:drawForegroundBox(x-1, y+self.height, 1, 1, borderColor)
self.parent:drawTextBox(x-1, y+h, 1, 1, "\131")
self.parent:drawForegroundBox(x-1, y+h, 1, 1, borderColor)
end
end
return true
@@ -336,20 +425,15 @@ return function(name)
x = math.floor(self.parent.width/2) + x - 1
y = math.floor(self.parent.height/2) + y - 1
end
local xO, yO = self:getOffset()
if not(ignOffset or ignOff) then
return x+xO, y+yO
if(self.parent~=nil)then
local xO, yO = self.parent:getOffset()
if not(ignOffset or ignOff) then
return x+xO, y+yO
end
end
return x, y
end;
getOffset = function(self)
if (self.parent ~= nil) then
return self.parent:getFrameOffset()
end
return 0, 0
end;
ignoreOffset = function(self, ignore)
ignOffset = ignore
if(ignore==nil)then ignOffset = true end
@@ -439,7 +523,19 @@ return function(name)
return self
end;
recalculateDynamicValue = function(self, special)
if(special==nil)then
for k in pairs(dynamicValue)do
calcDynamicValue(k)
end
else
calcDynamicValue(special)
end
return self
end,
onResize = function(self, ...)
self:recalculateValues()
for _,v in pairs(table.pack(...))do
if(type(v)=="function")then
self:registerEvent("basalt_resize", v)
@@ -515,13 +611,14 @@ return function(name)
mouseHandler = function(self, event, button, x, y)
local objX, objY = self:getAbsolutePosition(self:getAnchorPosition())
local w, h = self:getSize()
local yOff = false
if(objY-1 == y)and(self:getBorder("top"))then
y = y+1
yOff = true
end
if (objX <= x) and (objX + self.width > x) and (objY <= y) and (objY + self.height > y) and (isVisible) then
if (objX <= x) and (objX + w > x) and (objY <= y) and (objY + h > y) and (isVisible) then
if (self.parent ~= nil) then
self.parent:setFocusedObject(self)
end

View File

@@ -2,9 +2,9 @@ local curDir = fs.getDir(table.pack(...)[2]) or ""
local defaultPath = package.path
local format = "%s;/%s/?.lua;/%s/?/init.lua"
package.path = string.format(format, package.path, curDir,curDir)..string.format(format, package.path, curDir.."/libraries",curDir.."/libraries")
package.path = string.format(format, package.path, curDir,curDir)..string.format(format, package.path, curDir.."/libraries",curDir.."/libraries")..string.format(format, package.path, curDir.."/objects",curDir.."/objects")
local Basalt = require("main")
package.path = defaultPath
return Basalt
return Basalt

14
Basalt/libraries/Lerp.lua Normal file
View File

@@ -0,0 +1,14 @@
return {
lerp = function(s, e, pct)
return s + (e - s) * pct
end,
flip = function (x)
return 1 - x
end,
easeIn = function (t)
return t * t
end,
}

View File

@@ -181,4 +181,4 @@ return function(drawTerm)
end;
}
return drawHelper
end
end

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,88 @@
local function filledRectangle(x1,y1,x2,y2)
end
local function filledCircle(xC, yC, r)
local points = {}
for x=-r, r+1 do
local dy = math.floor(math.sqrt(r*r - x*x))
for y=-dy, dy+1 do
table.insert(points, {x=xC+x, y=yC+y})
end
end
return points
end
local function elipse(xC, yC, r1, r2, filled)
local rx,ry = math.ceil(math.floor(r1-0.5)/2),math.ceil(math.floor(r2-0.5)/2)
local x,y=0,ry
local d1 = ((ry * ry) - (rx * rx * ry) + (0.25 * rx * rx))
local dx = 2*ry^2*x
local dy = 2*rx^2*y
local points = {}
while dx < dy do
table.insert(points,{x=x+xC,y=y+yC})
table.insert(points,{x=-x+xC,y=y+yC})
table.insert(points,{x=x+xC,y=-y+yC})
table.insert(points,{x=-x+xC,y=-y+yC})
if filled then
for y=-y+yC+1,y+yC-1 do
table.insert(points,{x=x+xC,y=y})
table.insert(points,{x=-x+xC,y=y})
end
end
if d1 < 0 then
x = x + 1
dx = dx + 2*ry^2
d1 = d1 + dx + ry^2
else
x,y = x+1,y-1
dx = dx + 2*ry^2
dy = dy - 2*rx^2
d1 = d1 + dx - dy + ry^2
end
end
local d2 = (((ry * ry) * ((x + 0.5) * (x + 0.5))) + ((rx * rx) * ((y - 1) * (y - 1))) - (rx * rx * ry * ry))
while y >= 0 do
table.insert(points,{x=x+xC,y=y+yC})
table.insert(points,{x=-x+xC,y=y+yC})
table.insert(points,{x=x+xC,y=-y+yC})
table.insert(points,{x=-x+xC,y=-y+yC})
if filled then
for y=-y+yC,y+yC do
table.insert(points,{x=x+xC,y=y})
table.insert(points,{x=-x+xC,y=y})
end
end
if d2 > 0 then
y = y - 1
dy = dy - 2*rx^2
d2 = d2 + rx^2 - dy
else
y = y - 1
x = x + 1
dy = dy - 2*rx^2
dx = dx + 2*ry^2
d2 = d2 + dx - dy + rx^2
end
end
return points
end
local function circle(xC, yC, r, filled)
return elipse(xC, yC, r, r, filled)
end
return {
circle = function(x, y, radius, filled)
return circle(x, y, radius, filled)
end,
rectangle = function(x1,y1,x2, y2, filled)
local positions = {}
end,
elipse = function(xCenter, yCenter, radius1, radius2, filled)
return elipse(xCenter, yCenter, radius1, radius2, filled)
end
}

View File

@@ -0,0 +1,16 @@
local function PPMToBasalt()
end
local function loadPPM(path)
local image = {}
return image
end
return {
loadPPM = loadPPM
}

View File

View File

@@ -37,6 +37,22 @@ rpairs = function(t)
end, t, #t + 1
end,
splitString = function(str, sep)
if sep == nil then
sep = "%s"
end
local t={}
for v in string.gmatch(str, "([^"..sep.."]+)") do
table.insert(t, v)
end
return t
end,
numberFromString = function(str)
print(str)
return load("return " .. str)()
end,
-- shrink system is copy pasted (and slightly changed) from blittle by Bomb Bloke: http://www.computercraft.info/forums2/index.php?/topic/25354-cc-176-blittle-api/
shrink = function(bLittleData, bgColor)
local relations = { [0] = { 8, 4, 3, 6, 5 }, { 4, 14, 8, 7 }, { 6, 10, 8, 7 }, { 9, 11, 8, 0 }, { 1, 14, 8, 0 }, { 13, 12, 8, 0 }, { 2, 10, 8, 0 }, { 15, 8, 10, 11, 12, 14 },

View File

@@ -2,7 +2,7 @@ local basaltEvent = require("basaltEvent")()
local Frame = require("Frame")
local baseTerm = term.current()
local version = 2
local version = 3
local debugger = true
local projectDirectory = fs.getDir(table.pack(...)[2] or "")
@@ -10,6 +10,10 @@ local projectDirectory = fs.getDir(table.pack(...)[2] or "")
local activeKey, frames, monFrames = {}, {}, {}
local mainFrame, activeFrame, focusedObject, updaterActive
if not term.isColor or not term.isColor() then
error('Basalt requires an advanced (golden) comptuer to run.', 0)
end
local function stop()
updaterActive = false
end
@@ -112,6 +116,9 @@ end
local basalt = {}
basalt = {
getVersion = function()
return version
end,
setBaseTerm = function(_baseTerm)
baseTerm = _baseTerm

View File

@@ -1,3 +1,5 @@
local lerp = require("Lerp")
return function(name)
local object = {}
local objectType = "Animation"
@@ -232,4 +234,4 @@ return function(name)
object.__index = object
return object
end
end

View File

@@ -37,16 +37,17 @@ return function(name)
if (base.draw(self)) then
if (self.parent ~= nil) then
local obx, oby = self:getAnchorPosition()
local verticalAlign = utils.getTextVerticalAlign(self.height, textVerticalAlign)
local w,h = self:getSize()
local verticalAlign = utils.getTextVerticalAlign(h, textVerticalAlign)
if(self.bgColor~=false)then
self.parent:drawBackgroundBox(obx, oby, self.width, self.height, self.bgColor)
self.parent:drawTextBox(obx, oby, self.width, self.height, " ")
self.parent:drawBackgroundBox(obx, oby, w, h, self.bgColor)
self.parent:drawTextBox(obx, oby, w, h, " ")
end
if(self.fgColor~=false)then self.parent:drawForegroundBox(obx, oby, self.width, self.height, self.fgColor) end
for n = 1, self.height do
if(self.fgColor~=false)then self.parent:drawForegroundBox(obx, oby, w, h, self.fgColor) end
for n = 1, h do
if (n == verticalAlign) then
self.parent:setText(obx, oby + (n - 1), utils.getTextHorizontalAlign(self:getValue(), self.width, textHorizontalAlign))
self.parent:setText(obx, oby + (n - 1), utils.getTextHorizontalAlign(self:getValue(), w, textHorizontalAlign))
end
end
end

View File

@@ -0,0 +1,53 @@
local Object = require("Object")
local geometric = require("geometric")
local tHex = require("tHex")
return function(name)
-- Graphic
local base = Object(name)
local objectType = "Graphic"
base:setZIndex(2)
local graphic = {}
local graphicCache = {}
local object = {
getType = function(self)
return objectType
end;
addCircle = function(self, rad, color, x, y, filled)
table.insert(graphic, {area=geometric.circle(x or 1, y or 1, rad, filled), color=color})
return self
end;
addElipse = function(self, rad,rad2, color, x, y, filled)
table.insert(graphic, {area=geometric.elipse(x or 1, y or 1, rad, rad2, filled), color=color})
return self
end;
draw = function(self)
if (base.draw(self)) then
if (self.parent ~= nil) then
if(#graphic>0)then
local obx, oby = self:getAnchorPosition()
if(self.bgColor~=false)then
self.parent:drawBackgroundBox(obx, oby, self.width, self.height, self.bgColor)
end
for _,v in pairs(graphic)do
local col = tHex[v.color]
for _,b in pairs(v.area)do
if(b.x>=1)and(b.x<=self.width)and(b.y>=1)and(b.y<=self.height)then
self.parent:setBG(obx+b.x-1, oby+b.y-1, col)
end
end
end
end
end
self:setVisualChanged(false)
end
end;
}
return setmetatable(object, base)
end

View File

@@ -1,5 +1,5 @@
return function(name)
-- Pane
-- Image
local base = Object(name)
local objectType = "Image"
base:setZIndex(2)

View File

@@ -82,9 +82,26 @@ return function(name)
self.parent:drawTextBox(obx, oby, self.width, self.height, " ") end
if(self.fgColor~=false)then self.parent:drawForegroundBox(obx, oby, self.width, self.height, self.fgColor) end
if(fontsize==0)then
for n = 1, self.height do
if (n == verticalAlign) then
self.parent:setText(obx, oby + (n - 1), utils.getTextHorizontalAlign(self:getValue(), self.width, textHorizontalAlign))
if not(autoSize)then
local splittedText = utils.splitString(self:getValue(), " ")
local text = {}
local line = ""
for _,v in pairs(splittedText)do
if(line:len()+v:len()<=self.width)then
line = line=="" and v or line.." "..v
else
table.insert(text, line)
line = v:sub(1,self.width)
end
end
for k,v in pairs(text)do
self.parent:setText(obx, oby+k-1, v)
end
else
for n = 1, self.height do
if (n == verticalAlign) then
self.parent:setText(obx, oby + (n - 1), utils.getTextHorizontalAlign(self:getValue(), self.width, textHorizontalAlign))
end
end
end
else

View File

@@ -31,6 +31,20 @@ return function(name)
return self
end;
setIndex = function(self, _index)
index = _index
if (index < 1) then
index = 1
end
index = math.min(index, (barType == "vertical" and self.height or self.width) - (symbolSize - 1))
self:setValue(maxValue / (barType == "vertical" and self.height or self.width) * index)
return self
end,
getIndex = function(self)
return index
end,
setSymbolSize = function(self, size)
symbolSize = tonumber(size) or 1
if (barType == "vertical") then