lua: added toml library + content menu fix

This commit is contained in:
MihailRis 2024-02-05 04:05:58 +03:00
parent a7ac539429
commit bcc6fef74b
4 changed files with 89 additions and 25 deletions

View File

@ -73,3 +73,66 @@ function dofile(path)
end
return _dofile(path)
end
toml = {}
-- Convert table to TOML
function toml.from_table(tb, isinner)
local text = ""
for k, v in pairs(tb) do
local tp = type(v)
if tp ~= "table" then
text = text..k.." = "
if tp == "string" then
text = text..string.format("%q", v)
else
text = text..tostring(v)
end
text = text.."\n"
end
end
for k, v in pairs(tb) do
local tp = type(v)
if tp == "table" then
if isinner then
error("only one level of subtables supported")
end
text = text.."["..k.."]\n"..toml.from_table(v).."\n"
end
end
return text
end
-- Parse TOML to new table
function toml.parse(s)
local output = {}
local current = output
local lines = {}
for line in string.gmatch(s, "[^\r\n]+") do
line = string.gsub(line, "%s+", "")
table.insert(lines, line)
end
for i = 1,#lines do
local s = lines[i]
if string.sub(s, 1, 1) == "[" then
local section = s.sub(s, 2, #s-1)
current = {}
output[section] = current
else
for k, v in string.gmatch(s, "(%w+)=(.+)" ) do
v = string.gsub(v, "%s+", "")
if v.sub(v, 1, 1) == "\"" then
current[k] = v.sub(v, 2, #v-1)
elseif v == "true" or v == "false" then
current[k] = v == "true"
end
local num = tonumber(v)
if num ~= nil then
current[k] = num
end
end
end
end
return output
end

View File

@ -37,8 +37,6 @@ using glm::vec4;
namespace fs = std::filesystem;
using namespace gui;
const int PACKS_PANEL_WIDTH = 550;
inline uint64_t randU64() {
srand(time(NULL));
return rand() ^ (rand() << 8) ^
@ -224,8 +222,8 @@ std::shared_ptr<Panel> create_worlds_panel(Engine* engine) {
auto namews = util::str2wstr_utf8(name);
auto btn = std::make_shared<RichButton>(vec2(390, 46));
btn->setColor(vec4(1.0f, 1.0f, 1.0f, 0.1f));
btn->setHoverColor(vec4(1.0f, 1.0f, 1.0f, 0.17f));
btn->setColor(vec4(0.06f, 0.12f, 0.18f, 0.7f));
btn->setHoverColor(vec4(0.09f, 0.17f, 0.2f, 0.6f));
btn->listenAction([=](GUI*) {
open_world(name, engine);
});
@ -277,13 +275,14 @@ std::shared_ptr<Panel> create_packs_panel(
packconsumer callback
){
auto assets = engine->getAssets();
auto panel = std::make_shared<Panel>(vec2(PACKS_PANEL_WIDTH, 200), vec4(5.0f));
auto panel = std::make_shared<Panel>(vec2(550, 200), vec4(5.0f));
panel->setColor(vec4(1.0f, 1.0f, 1.0f, 0.07f));
panel->setMaxLength(400);
panel->setScrollable(true);
for (auto& pack : packs) {
auto packpanel = std::make_shared<RichButton>(vec2(390, 80));
auto packpanel = std::make_shared<RichButton>(vec2(540, 80));
packpanel->setColor(vec4(0.06f, 0.12f, 0.18f, 0.7f));
if (callback) {
packpanel->listenAction([=](GUI*) {
callback(pack);
@ -291,7 +290,9 @@ std::shared_ptr<Panel> create_packs_panel(
}
auto idlabel = std::make_shared<Label>("["+pack.id+"]");
idlabel->setColor(vec4(1, 1, 1, 0.5f));
packpanel->add(idlabel, vec2(PACKS_PANEL_WIDTH-40-idlabel->getSize().x, 2));
idlabel->setSize(vec2(300, 25));
idlabel->setAlign(Align::right);
packpanel->add(idlabel, vec2(215, 2));
auto titlelabel = std::make_shared<Label>(pack.title);
packpanel->add(titlelabel, vec2(78, 6));
@ -309,7 +310,9 @@ std::shared_ptr<Panel> create_packs_panel(
if (!pack.creator.empty()) {
auto creatorlabel = std::make_shared<Label>("@"+pack.creator);
creatorlabel->setColor(vec4(0.8f, 1.0f, 0.9f, 0.7f));
packpanel->add(creatorlabel, vec2(PACKS_PANEL_WIDTH-40-creatorlabel->getSize().x, 60));
creatorlabel->setSize(vec2(300, 20));
creatorlabel->setAlign(Align::right);
packpanel->add(creatorlabel, vec2(215, 60));
}
auto descriptionlabel = std::make_shared<Label>(pack.description);
@ -317,8 +320,6 @@ std::shared_ptr<Panel> create_packs_panel(
packpanel->add(descriptionlabel, vec2(80, 28));
packpanel->add(std::make_shared<Image>(icon, vec2(64)), vec2(8));
packpanel->setColor(vec4(0.06f, 0.12f, 0.18f, 0.7f));
panel->add(packpanel);
}
if (backbutton) {
@ -331,7 +332,7 @@ std::shared_ptr<Panel> create_packs_panel(
void create_content_panel(Engine* engine) {
auto menu = engine->getGUI()->getMenu();
auto paths = engine->getPaths();
auto mainPanel = create_page(engine, "content", PACKS_PANEL_WIDTH, 0.0f, 5);
auto mainPanel = create_page(engine, "content", 550, 0.0f, 5);
std::vector<ContentPack> scanned;
ContentPack::scan(engine->getPaths(), scanned);

View File

@ -176,27 +176,27 @@ const std::string lua::LuaState::storeAnonymous() {
return funcName;
}
void lua::LuaState::initNamespace() {
void lua::LuaState::initEnvironment() {
lua_getglobal(L, "_G");
lua_setfield(L, -1, ":G");
}
int lua::LuaState::createNamespace() {
int id = nextNamespace++;
int lua::LuaState::createEnvironment() {
int id = nextEnvironment++;
if (currentNamespace != 0) {
setNamespace(0);
if (currentEnvironment != 0) {
setEnvironment(0);
}
lua_createtable(L, 0, 0);
initNamespace();
initEnvironment();
setglobal("_N"+util::mangleid(id));
setNamespace(id);
setEnvironment(id);
return id;
}
void lua::LuaState::setNamespace(int id) {
void lua::LuaState::setEnvironment(int id) {
if (id == 0) {
getglobal(":G");
lua_setfenv(L, -1);
@ -205,7 +205,7 @@ void lua::LuaState::setNamespace(int id) {
lua_getfield(L, -1, ("_N"+util::mangleid(id)).c_str());
if (lua_isnil(L, -1)) {
lua_pop(L, -1);
throw luaerror("namespace "+std::to_string(id)+" was not found");
throw luaerror("environment "+std::to_string(id)+" was not found");
}
lua_setfenv(L, -1);
}

View File

@ -16,11 +16,11 @@ namespace lua {
class LuaState {
lua_State* L;
int nextNamespace = 1;
int currentNamespace = 0;
int nextEnvironment = 1;
int currentEnvironment = 0;
void logError(const std::string& text);
void initNamespace();
void initEnvironment();
public:
LuaState();
~LuaState();
@ -44,8 +44,8 @@ namespace lua {
bool rename(const std::string& from, const std::string& to);
void remove(const std::string& name);;
void createFuncs();
int createNamespace();
void setNamespace(int id);
int createEnvironment();
void setEnvironment(int id);
const std::string storeAnonymous();
};