Basalt2 update

- Finished themes
- Added state plugins for persistance
- Finished reactive plugin
- Added debug plugin (80% finished)
- Added benchmark plugin
- Added Tree, Table, List, Dropdown and Menu Elements
- Bugfixes
This commit is contained in:
Robert Jelic
2025-02-15 18:18:49 +01:00
parent 8ea3ca2465
commit db716636bd
32 changed files with 1700 additions and 165 deletions

View File

@@ -1,33 +1,36 @@
-- Has to be reworked
local Theme = {}
local defaultTheme = {
colors = {
primary = colors.blue,
secondary = colors.cyan,
background = colors.black,
text = colors.white,
borders = colors.gray,
error = colors.red,
success = colors.green,
default = {
background = colors.lightGray,
foreground = colors.black,
},
elementStyles = {
Button = {
background = "background",
foreground = "text",
activeBackground = "primary",
activeForeground = "text",
},
Input = {
background = "background",
foreground = "text",
focusBackground = "primary",
focusForeground = "text",
},
BaseFrame = {
background = colors.white,
foreground = colors.black,
Frame = {
background = "background",
foreground = "text"
background = colors.gray,
Button = {
background = "{self.clicked and colors.black or colors.blue}",
foreground = "{self.clicked and colors.blue or colors.white}"
}
},
Button = {
background = "{self.clicked and colors.black or colors.cyan}",
foreground = "{self.clicked and colors.cyan or colors.black}"
},
Input = {
background = "{self.focused and colors.cyan or colors.lightGray}",
foreground = colors.black,
placeholderColor = colors.gray
},
List = {
background = colors.cyan,
foreground = colors.black,
selectedColor = colors.blue
}
}
}
@@ -38,10 +41,6 @@ local themes = {
local currentTheme = "default"
function Theme.registerTheme(name, theme)
themes[name] = theme
end
local function resolveThemeValue(value, theme)
if type(value) == "string" and theme.colors[value] then
return theme.colors[value]
@@ -49,10 +48,50 @@ local function resolveThemeValue(value, theme)
return value
end
local function getThemeForElement(element)
local path = {}
local current = element
while current do
table.insert(path, 1, current.get("type"))
current = current.parent
end
local result = {}
local current = defaultTheme
for _, elementType in ipairs(path) do
if current[elementType] then
for k,v in pairs(current[elementType]) do
result[k] = v
end
current = current[elementType]
end
end
return result
end
local function applyTheme(element, props)
local theme = getThemeForElement(element)
if props then
for k,v in pairs(props) do
theme[k] = v
end
end
for k,v in pairs(theme) do
if element:getPropertyConfig(k) then
element.set(k, v)
end
end
end
local BaseElement = {
hooks = {
init = function(self)
-- Theme Properties für das Element registrieren
self.defineProperty(self, "theme", {
default = currentTheme,
type = "string",
@@ -68,7 +107,7 @@ local BaseElement = {
function BaseElement:applyTheme(themeName)
local theme = themes[themeName] or themes.default
local elementType = self.get("type")
if theme.elementStyles[elementType] then
local styles = theme.elementStyles[elementType]
for prop, value in pairs(styles) do
@@ -79,21 +118,67 @@ function BaseElement:applyTheme(themeName)
end
end
local BaseFrame = {
hooks = {
init = function(self)
applyTheme(self)
end
}
}
local Container = {
hooks = {
addChild = function(self, child)
if self.get("themeInherit") then
child.set("theme", self.get("theme"))
hooks = {
init = function(self)
for k, _ in pairs(self.basalt.getElementManager().getElementList()) do
local capitalizedName = k:sub(1,1):upper() .. k:sub(2)
if capitalizedName ~= "BaseFrame" then
local methodName = "add"..capitalizedName
local original = self[methodName]
if original then
self[methodName] = function(self, name, props)
if type(name) == "table" then
props = name
name = nil
end
local element = original(self, name)
applyTheme(element, props)
return element
end
end
end
end
end
}
}
function Container.setup(element)
element.defineProperty(element, "themeInherit", {default = true, type = "boolean"})
end
local themeAPI = {
setTheme = function(newTheme)
defaultTheme = newTheme
end,
return {
BaseElement = BaseElement,
Container = Container,
getTheme = function()
return defaultTheme
end,
loadTheme = function(path)
local file = fs.open(path, "r")
if file then
local content = file.readAll()
file.close()
defaultTheme = textutils.unserializeJSON(content)
end
end
}
local Theme = {
setup = function(basalt)
basalt.setTheme(defaultTheme)
end,
BaseElement = BaseElement,
BaseFrame = BaseFrame,
Container = Container,
API = themeAPI
}
return Theme