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

View File

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

View File

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