some fixes

- added logsystem (basalt.log(text))
- changed programs: using now shell.execute instead of os.run
- added basalt.logging boolean which turns on the logsystem (currently only logs errors, will add more in the near future)
- fixed a bug with setTransparent
- changed the default background color of sliders and labels to false
This commit is contained in:
Robert Jelic
2022-08-20 21:26:43 +02:00
parent d6a4c06363
commit 535dddb41d
10 changed files with 93 additions and 81 deletions

View File

@@ -1,6 +1,5 @@
local Object = require("Object") local Object = require("Object")
local _OBJECTS = require("loadObjects") local _OBJECTS = require("loadObjects")
local log = require("basaltLogs")
local BasaltDraw = require("basaltDraw") local BasaltDraw = require("basaltDraw")
local utils = require("utils") local utils = require("utils")
local layout = require("layout") local layout = require("layout")
@@ -43,6 +42,7 @@ return function(name, parent, pTerm, basalt)
local focusedObject local focusedObject
local autoSize = true local autoSize = true
local autoScroll = true local autoScroll = true
local initialized = false
local activeEvents = {} local activeEvents = {}
@@ -461,7 +461,6 @@ return function(name, parent, pTerm, basalt)
local obx, oby = self:getAnchorPosition() local obx, oby = self:getAnchorPosition()
self.parent:setCursor(_blink or false, (_xCursor or 0)+obx-1, (_yCursor or 0)+oby-1, color or cursorColor) self.parent:setCursor(_blink or false, (_xCursor or 0)+obx-1, (_yCursor or 0)+oby-1, color or cursorColor)
else else
log(_blink)
local obx, oby = self:getAbsolutePosition(self:getAnchorPosition(self:getX(), self:getY(), true)) local obx, oby = self:getAbsolutePosition(self:getAnchorPosition(self:getX(), self:getY(), true))
cursorBlink = _blink or false cursorBlink = _blink or false
if (_xCursor ~= nil) then if (_xCursor ~= nil) then
@@ -744,9 +743,7 @@ return function(name, parent, pTerm, basalt)
self:mouseHandler(1, p2, p3, true) self:mouseHandler(1, p2, p3, true)
end end
end end
if (event == "terminate") then if (event == "terminate")and(self.parent==nil)then
termObject.setCursorPos(1, 1)
termObject.clear()
basalt.stop() basalt.stop()
end end
end, end,
@@ -873,7 +870,7 @@ return function(name, parent, pTerm, basalt)
return false return false
end, end,
keyHandler = function(self, key) keyHandler = function(self, key, isHolding)
if (self:isFocused())or(self.parent==nil)then if (self:isFocused())or(self.parent==nil)then
local val = self:getEventSystem():sendEvent("key", self, "key", key) local val = self:getEventSystem():sendEvent("key", self, "key", key)
if(val==false)then return false end if(val==false)then return false end
@@ -882,7 +879,7 @@ return function(name, parent, pTerm, basalt)
if (events["key"][index] ~= nil) then if (events["key"][index] ~= nil) then
for _, value in rpairs(events["key"][index]) do for _, value in rpairs(events["key"][index]) do
if (value.keyHandler ~= nil) then if (value.keyHandler ~= nil) then
if (value:keyHandler(key)) then if (value:keyHandler(key, isHolding)) then
return true return true
end end
end end
@@ -1087,14 +1084,17 @@ return function(name, parent, pTerm, basalt)
end, end,
init = function(self) init = function(self)
if (parent ~= nil) then if not(initialized)then
base.width, base.height = parent:getSize() if (parent ~= nil) then
self:setBackground(parent:getTheme("FrameBG")) base.width, base.height = parent:getSize()
self:setForeground(parent:getTheme("FrameText")) self:setBackground(parent:getTheme("FrameBG"))
else self:setForeground(parent:getTheme("FrameText"))
base.width, base.height = termObject.getSize() else
self:setBackground(basalt.getTheme("BasaltBG")) base.width, base.height = termObject.getSize()
self:setForeground(basalt.getTheme("BasaltText")) self:setBackground(basalt.getTheme("BasaltBG"))
self:setForeground(basalt.getTheme("BasaltText"))
end
initialized = true
end end
end, end,
} }

View File

@@ -314,7 +314,9 @@ return function(name)
end; end;
setTransparent = function(self, color) setTransparent = function(self, color)
transparentColor = color or false self.transparentColor = color or false
self.bgSymbol = false
self.bgSymbolColor = false
self:updateDraw() self:updateDraw()
return self return self
end; end;
@@ -780,10 +782,10 @@ return function(name)
return false return false
end, end,
keyHandler = function(self, key) keyHandler = function(self, key, isHolding)
if(isEnabled)and(isVisible)then if(isEnabled)and(isVisible)then
if (self:isFocused()) then if (self:isFocused()) then
local val = eventSystem:sendEvent("key", self, "key", key) local val = eventSystem:sendEvent("key", self, "key", key, isHolding)
if(val==false)then return false end if(val==false)then return false end
return true return true
end end
@@ -818,7 +820,8 @@ return function(name)
end; end;
eventHandler = function(self, event, p1, p2, p3, p4) eventHandler = function(self, event, p1, p2, p3, p4)
eventSystem:sendEvent("other_event", self, event, p1, p2, p3, p4) local val = eventSystem:sendEvent("other_event", self, event, p1, p2, p3, p4)
if(val~=nil)then return val end
return true return true
end; end;

View File

@@ -163,11 +163,15 @@ return function(drawTerm)
end end
end; end;
writeText = function(x, y, text, bgCol, fgCol) writeText = function(x, y, text, bgCol, fgCol)
bgCol = bgCol or terminal.getBackgroundColor() if(text~=nil)then
fgCol = fgCol or terminal.getTextColor() setText(x, y, text)
setText(x, y, text) if(bgCol~=nil)and(bgCol~=false)then
setBG(x, y, rep(tHex[bgCol], text:len())) setBG(x, y, rep(tHex[bgCol], text:len()))
setFG(x, y, rep(tHex[fgCol], text:len())) end
if(fgCol~=nil)and(fgCol~=false)then
setFG(x, y, rep(tHex[fgCol], text:len()))
end
end
end; end;
update = function() update = function()

View File

@@ -3,12 +3,12 @@ local process = {}
local processId = 0 local processId = 0
function process:new(path, window, ...) function process:new(path, window, ...)
local args = table.pack(...) local args = {...}
local newP = setmetatable({ path = path }, { __index = self }) local newP = setmetatable({ path = path }, { __index = self })
newP.window = window newP.window = window
newP.processId = processId newP.processId = processId
newP.coroutine = coroutine.create(function() newP.coroutine = coroutine.create(function()
os.run({ basaltProcess = true }, path, table.unpack(args)) shell.execute(path, table.unpack(args))
end) end)
processes[processId] = newP processes[processId] = newP
processId = processId + 1 processId = processId + 1

View File

@@ -2,9 +2,11 @@ local basaltEvent = require("basaltEvent")()
local Frame = require("Frame") local Frame = require("Frame")
local theme = require("theme") local theme = require("theme")
local utils = require("utils") local utils = require("utils")
local log = require("basaltLogs")
local uuid = utils.uuid local uuid = utils.uuid
local createText = utils.createText local createText = utils.createText
local baseTerm = term.current() local baseTerm = term.current()
local version = 5 local version = 5
local debugger = true local debugger = true
@@ -14,12 +16,16 @@ local projectDirectory = fs.getDir(table.pack(...)[2] or "")
local activeKey, frames, monFrames, variables, schedules = {}, {}, {}, {}, {} local activeKey, frames, monFrames, variables, schedules = {}, {}, {}, {}, {}
local mainFrame, activeFrame, focusedObject, updaterActive local mainFrame, activeFrame, focusedObject, updaterActive
local basalt = {}
if not term.isColor or not term.isColor() then 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 function stop() local function stop()
updaterActive = false updaterActive = false
baseTerm.clear()
baseTerm.setCursorPos(1, 1)
end end
local setVariable = function(name, var) local setVariable = function(name, var)
@@ -93,34 +99,19 @@ local basaltError = function(errMsg)
baseTerm.setBackgroundColor(colors.black) baseTerm.setBackgroundColor(colors.black)
baseTerm.setTextColor(colors.red) baseTerm.setTextColor(colors.red)
local w,h = baseTerm.getSize() local w,h = baseTerm.getSize()
if(basalt.logging)then
local splitString = function(str, sep) log(errMsg, "Error")
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
local words = splitString(errMsg, " ")
local line = "Basalt error: "
local yPos = 1
for n=1,#words do
baseTerm.setCursorPos(1,yPos)
if(#line+#words[n]<w)then
line = line.." "..words[n]
else
baseTerm.write(line)
line = words[n]
yPos = yPos + 1
end
if(n==#words)then
baseTerm.write(line)
end
end end
local text = createText("Basalt error: "..errMsg, w)
local yPos = 1
for k,v in pairs(text)do
baseTerm.setCursorPos(1,yPos)
baseTerm.write(v)
yPos = yPos + 1
end
baseTerm.setCursorPos(1,yPos+1) baseTerm.setCursorPos(1,yPos+1)
updaterActive = false
end end
local function handleSchedules(event, p1, p2, p3, p4) local function handleSchedules(event, p1, p2, p3, p4)
@@ -145,6 +136,7 @@ local function handleSchedules(event, p1, p2, p3, p4)
end end
local function drawFrames() local function drawFrames()
if(updaterActive==false)then return end
if(mainFrame~=nil)then if(mainFrame~=nil)then
mainFrame:draw() mainFrame:draw()
mainFrame:updateTerm() mainFrame:updateTerm()
@@ -192,12 +184,17 @@ local function basaltUpdateEvent(event, p1, p2, p3, p4)
end end
if(event == "key")then if(event == "key")then
if(activeFrame~=nil)then if(activeFrame~=nil)then
activeFrame:keyHandler(p1) activeFrame:keyHandler(p1, p2)
end end
activeKey[p1] = true activeKey[p1] = true
end end
if(event == "terminate")then
if(event~="mouse_click")and(event~="mouse_up")and(event~="mouse_scroll")and(event~="mouse_drag")and(event~="key")and(event~="key_up")and(event~="char")then if(activeFrame~=nil)then
activeFrame:eventHandler(event)
if(updaterActive==false)then return end
end
end
if(event~="mouse_click")and(event~="mouse_up")and(event~="mouse_scroll")and(event~="mouse_drag")and(event~="key")and(event~="key_up")and(event~="char")and(event~="terminate")then
for k, v in pairs(frames) do for k, v in pairs(frames) do
v:eventHandler(event, p1, p2, p3, p4) v:eventHandler(event, p1, p2, p3, p4)
end end
@@ -206,8 +203,8 @@ local function basaltUpdateEvent(event, p1, p2, p3, p4)
drawFrames() drawFrames()
end end
local basalt = {}
basalt = { basalt = {
logging = false,
setTheme = setTheme, setTheme = setTheme,
getTheme = getTheme, getTheme = getTheme,
drawFrames = drawFrames, drawFrames = drawFrames,
@@ -222,24 +219,29 @@ basalt = {
baseTerm = _baseTerm baseTerm = _baseTerm
end, end,
log = function(...)
log(...)
end,
autoUpdate = function(isActive) autoUpdate = function(isActive)
local pCall = pcall
updaterActive = isActive updaterActive = isActive
if(isActive==nil)then updaterActive = true end if(isActive==nil)then updaterActive = true end
drawFrames() local function f()
while updaterActive do drawFrames()
local event, p1, p2, p3, p4 = os.pullEventRaw() while updaterActive do
local ok, err = pCall(basaltUpdateEvent, event, p1, p2, p3, p4) basaltUpdateEvent(os.pullEventRaw())
if not(ok)then
basaltError(err)
return
end end
end end
local ok, err = xpcall(f, debug.traceback)
if not(ok)then
basaltError(err)
return
end
end, end,
update = function(event, p1, p2, p3, p4) update = function(event, p1, p2, p3, p4)
if (event ~= nil) then if (event ~= nil) then
local ok, err = pcall(basaltUpdateEvent, event, p1, p2, p3, p4) local ok, err = xpcall(basaltUpdateEvent, debug.traceback, event, p1, p2, p3, p4)
if not(ok)then if not(ok)then
basaltError(err) basaltError(err)
return return

View File

@@ -52,6 +52,8 @@ local lerp = {
easeInOutSine = easeInOutSine, easeInOutSine = easeInOutSine,
} }
local activeAnimations = {}
return function(name) return function(name)
local object = {} local object = {}
local objectType = "Animation" local objectType = "Animation"

View File

@@ -33,8 +33,6 @@ return function(name)
if (autoSize) then if (autoSize) then
self.width = text:len() self.width = text:len()
end end
if not(fgColChanged)then self.fgColor = self.parent:getForeground() or colors.white end
if not(bgColChanged)then self.bgColor = self.parent:getBackground() or colors.black end
self:updateDraw() self:updateDraw()
return self return self
end; end;
@@ -94,10 +92,6 @@ return function(name)
local obx, oby = self:getAnchorPosition() local obx, oby = self:getAnchorPosition()
local w,h = self:getSize() local w,h = self:getSize()
local verticalAlign = utils.getTextVerticalAlign(h, textVerticalAlign) local verticalAlign = utils.getTextVerticalAlign(h, textVerticalAlign)
if(self.bgColor~=false)then
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, w, h, self.fgColor) end
if(fontsize==0)then if(fontsize==0)then
if not(autoSize)then if not(autoSize)then
local text = createText(self:getValue(), self:getWidth()) local text = createText(self:getValue(), self:getWidth())
@@ -125,7 +119,6 @@ return function(name)
for i = 1, cY do for i = 1, cY do
self.parent:setFG(obx, oby + i + n - 2, utils.getTextHorizontalAlign(tData[2][i], w, textHorizontalAlign)) self.parent:setFG(obx, oby + i + n - 2, utils.getTextHorizontalAlign(tData[2][i], w, textHorizontalAlign))
self.parent:setBG(obx, oby + i + n - 2, utils.getTextHorizontalAlign(tData[3][i], w, textHorizontalAlign, tHex[self.bgColor or colors.black]))
self.parent:setText(obx, oby + i + n - 2, utils.getTextHorizontalAlign(tData[1][i], w, textHorizontalAlign)) self.parent:setText(obx, oby + i + n - 2, utils.getTextHorizontalAlign(tData[1][i], w, textHorizontalAlign))
end end
end end
@@ -134,6 +127,10 @@ return function(name)
end end
end end
end, end,
init = function(self)
self.bgColor = self.parent:getTheme("LabelBG")
self.fgColor = self.parent:getTheme("LabelText")
end
} }

View File

@@ -445,14 +445,14 @@ return function(name, parent)
end end
end end
local function keyEvent(self, event, key) local function keyEvent(self, event, key, isHolding)
if (curProcess == nil) then if (curProcess == nil) then
return false return false
end end
if not (curProcess:isDead()) then if not (curProcess:isDead()) then
if not (paused) then if not (paused) then
if (self.draw) then if (self.draw) then
curProcess:resume(event, key) curProcess:resume(event, key, isHolding)
updateCursor(self) updateCursor(self)
end end
end end
@@ -633,9 +633,9 @@ return function(name, parent)
return false return false
end, end,
keyHandler = function(self, key) keyHandler = function(self, key, isHolding)
if(base.keyHandler(self, key))then if(base.keyHandler(self, key, isHolding))then
keyEvent(self, "key", key) keyEvent(self, "key", key, isHolding)
return true return true
end end
return false return false
@@ -720,8 +720,10 @@ return function(name, parent)
end end
end end
if (event == "terminate") and (self:isFocused()) then if (event == "terminate") then
self:stop() curProcess:resume(event)
self.parent:setCursor(false)
return true
end end
end end
else else

View File

@@ -1,4 +1,5 @@
local Object = require("Object") local Object = require("Object")
local log = require("basaltLogs")
local xmlValue = require("utils").getValueFromXML local xmlValue = require("utils").getValueFromXML
return function(name) return function(name)
@@ -154,7 +155,6 @@ return function(name)
if (barType == "vertical") then if (barType == "vertical") then
for n = 0, h - 1 do for n = 0, h - 1 do
if (index == n + 1) then if (index == n + 1) then
for curIndexOffset = 0, math.min(symbolSize - 1, h) do for curIndexOffset = 0, math.min(symbolSize - 1, h) do
self.parent:writeText(obx, oby + n + curIndexOffset, symbol, symbolColor, symbolColor) self.parent:writeText(obx, oby + n + curIndexOffset, symbol, symbolColor, symbolColor)

View File

@@ -39,5 +39,7 @@ return { -- The default main theme for basalt!
SwitchBGSymbol = colors.black, SwitchBGSymbol = colors.black,
SwitchInactive = colors.red, SwitchInactive = colors.red,
SwitchActive = colors.green, SwitchActive = colors.green,
LabelBG = false,
LabelText = colors.black
} }