Add core library and element classes for Basalt framework
This commit is contained in:
8
src/libraries/colorHex.lua
Normal file
8
src/libraries/colorHex.lua
Normal file
@@ -0,0 +1,8 @@
|
||||
local colorHex = {}
|
||||
|
||||
for i = 0, 15 do
|
||||
colorHex[2^i] = ("%x"):format(i)
|
||||
colorHex[("%x"):format(i)] = 2^i
|
||||
end
|
||||
|
||||
return colorHex
|
||||
35
src/libraries/expect.lua
Normal file
35
src/libraries/expect.lua
Normal file
@@ -0,0 +1,35 @@
|
||||
local errorManager = require("errorManager")
|
||||
|
||||
-- Simple type checking without stack traces
|
||||
local function expect(position, value, expectedType)
|
||||
local valueType = type(value)
|
||||
|
||||
if expectedType == "element" then
|
||||
if valueType == "table" and value.get("type") ~= nil then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
if expectedType == "color" then
|
||||
if valueType == "number" and value >= 1 and value <= 32768 then
|
||||
return true
|
||||
end
|
||||
if valueType == "string" and colors[value] then
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
if valueType ~= expectedType then
|
||||
errorManager.header = "Basalt Type Error"
|
||||
errorManager.error(string.format(
|
||||
"Bad argument #%d: expected %s, got %s",
|
||||
position,
|
||||
expectedType,
|
||||
valueType
|
||||
))
|
||||
end
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
return expect
|
||||
27
src/libraries/utils.lua
Normal file
27
src/libraries/utils.lua
Normal file
@@ -0,0 +1,27 @@
|
||||
local floor, len = math.floor, string.len
|
||||
|
||||
local utils = {}
|
||||
|
||||
function utils.getCenteredPosition(text, totalWidth, totalHeight)
|
||||
local textLength = len(text)
|
||||
|
||||
local x = floor((totalWidth - textLength+1) / 2 + 0.5)
|
||||
local y = floor(totalHeight / 2 + 0.5)
|
||||
|
||||
return x, y
|
||||
end
|
||||
|
||||
function utils.deepCopy(obj)
|
||||
if type(obj) ~= "table" then
|
||||
return obj
|
||||
end
|
||||
|
||||
local copy = {}
|
||||
for k, v in pairs(obj) do
|
||||
copy[utils.deepCopy(k)] = utils.deepCopy(v)
|
||||
end
|
||||
|
||||
return copy
|
||||
end
|
||||
|
||||
return utils
|
||||
Reference in New Issue
Block a user