Skip to content

Instantly share code, notes, and snippets.

@Ruin0x11
Created March 7, 2022 07:47
Show Gist options
  • Save Ruin0x11/03c4f780ec58a3989afa50b44e98a57e to your computer and use it in GitHub Desktop.
Save Ruin0x11/03c4f780ec58a3989afa50b44e98a57e to your computer and use it in GitHub Desktop.
lss.lua
local inspect = require "inspect"
local Rule = {}
local function rule(...)
local classes = {}
local elementId = nil
local pseudos = {}
local count = select("#", ...)
if count == 0 then
error "No rules provided."
end
local body = nil
for i = 1, count do
local rule = select(i, ...)
if type(rule) ~= "string" then
error("Invalid rule " .. tostring(rule))
end
if rule:match "^%." then
classes[#classes + 1] = rule:sub(2)
elseif rule:match "^#" then
if elementId then
error("elementId declared twice in rules list (" .. elementId .. ", " .. rule:sub(2) .. ")")
end
elementId = rule:sub(2)
elseif rule:match "^:" then
pseudos[#pseudos + 1] = rule:sub(2)
else
error("Invalid rule " .. rule)
end
if body then
body = body .. " " .. rule
else
body = rule
end
end
local t = {
body = body,
classes = classes,
elementId = elementId,
pseudos = pseudos,
}
return setmetatable(t, Rule)
end
function Rule:__call(arg)
assert(self.properties == nil, "Properties already declared for rule: " .. self.body)
self.properties = arg
end
function Rule:__tostring()
return self.body
end
local function font(t)
if type(t) ~= "table" then
error("Invalid font table " .. tostring(t))
end
return {
name = t.name,
size = t.size,
style = t.style or {},
}
end
local Element = {}
function Element.new(name)
local t = {
type = "element",
name = name,
selector = false,
properties = false,
}
return setmetatable(t, Element)
end
function Element:__index(k)
return Element.new(self.name .. "." .. k)
end
function Element:__tostring()
local s = self.name
if self.selector then
s = s .. "(" .. tostring(self.selector) .. ")"
end
return s
end
__declaredProperties = {}
__declaredRules = {}
function Element:__call(arg, ...)
if type(arg) == "string" then
if self.selector then
error("Selector already declared for element: " .. self.name)
end
self.selector = rule(arg, ...)
return self
elseif type(arg) == "table" then
if self.properties then
error("Properties already declared for element: " .. self.name .. "\n" .. inspect(self.properties))
end
self.properties = arg
__declaredRules[#__declaredRules + 1] = self
else
error("Invalid element argument " .. tostring(arg))
end
end
local _G_mt = {}
function _G_mt:__index(k)
if __declaredProperties[k] then
return __declaredProperties[k]
end
return Element.new(k)
end
function _G_mt:__newindex(k, v)
__declaredProperties[k] = v
end
setmetatable(_G, _G_mt)
_ {
rule ".windowPanel" {
panel = "AssetWindow",
},
rule ".borderedWindowPanel" {
panel = "AssetWindowBordered",
},
}
PanelContainer ".hotbarPanel" {
panel = "AssetHotbarBackground",
}
Button(".class1", ":disabled") {
Label {
fontColor = "#E5E5E581",
},
}
MyMod.UI.Controls.IconBar ".active" {
background = "#888800",
}
notoSansDisplayBold14 = font {
name = "Noto Sans Display",
size = 14,
style = { "Bold" },
}
colorGold = "#A88B5E"
Label ".windowTitle" {
fontColor = colorGold,
font = notoSansDisplayBold14,
}
ContainerButton ".button" {
rule ":normal" {
modulateSelf = colorNormal,
},
rule ":hover" {
modulateSelf = colorHover,
},
rule ":pressed" {
modulateSelf = colorPressed,
},
rule ":disabled" {
modulateSelf = colorDisabled,
},
}
ItemList {
background = "#334455",
itemBackground = "#AABB88",
disabledItemBackground = "#888888",
selectedItemBackground = "#88BBAA",
rule ".transparentItemList" {
background = "#FFFFFF00",
itemBackground = "#FFFFFF00",
disabledItemBackground = "#FFFFFF00",
selectedItemBackground = "#FFFFFF00",
},
}
print(inspect(__declaredProperties))
print(inspect(__declaredRules))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment