gui.template test

This commit is contained in:
MihailRis 2024-04-16 22:23:46 +03:00
parent 0863e40b3b
commit e059b9395f
3 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,15 @@
<container onclick='%{callback}' size='540,80' color='#0F1E2DB2'>
<label pos='215,2' color='#FFFFFF80' size='300,25' align='right'>
[%{id}]
</label>
<label pos='78,6'>%{title}</label>
<label if='%{creator}' color='#CCFFE5B2' size='300,20' align='right' pos='215,60'>
%{creator}
</label>
<label pos='80,28' color='#FFFFFFB2'>
%{description}
</label>
<button if='%{remover}' onclick='%{remover}' color='0' hover-color='#FFFFFF2B'>
<image src='gui/cross' size='32,32'/>
</button>
</container>

View File

@ -201,6 +201,19 @@ function time.post_runnable(runnable)
table.insert(__post_runnables, runnable)
end
function gui.template(name, params)
local text = file.read(file.find("layouts/templates/"..name..".xml"))
for k,v in pairs(params) do
text = text:gsub("(%%{"..k.."})", tostring(v))
end
text = text:gsub("if%s*=%s*'%%{%w+}'", "if=''")
text = text:gsub("if%s*=%s*\"%%{%w+}\"", "if=\"\"")
-- remove unsolved properties: attr='%{var}'
text = text:gsub("%w+%s*=%s*'%%{%w+}'%s?", "")
text = text:gsub("%w+%s*=%s*\"%%{%w+}\"%s?", "")
return text
end
-- Deprecated functions
block_index = block.index
block_name = block.name

View File

@ -19,6 +19,27 @@ static fs::path resolve_path(lua_State* L, const std::string& path) {
}
}
// TODO: move to ResPaths
static int l_file_find(lua_State* L) {
std::string path = lua_tostring(L, 1);
auto& packs = scripting::engine->getContentPacks();
for (int i = packs.size()-1; i >= 0; i--) {
auto& pack = packs[i];
if (fs::exists(pack.folder / fs::path(path))) {
lua_pushstring(L, (pack.id+":"+path).c_str());
return 1;
}
}
auto resDir = scripting::engine->getResPaths()->getMainRoot();
if (fs::exists(resDir / fs::path(path))) {
lua_pushstring(L, ("core:"+path).c_str());
return 1;
}
luaL_error(L, "file not found %q", path.c_str());
return 0;
}
static int l_file_resolve(lua_State* L) {
fs::path path = resolve_path(L, lua_tostring(L, 1));
lua_pushstring(L, path.u8string().c_str());
@ -145,6 +166,7 @@ static int l_file_write_bytes(lua_State* L) {
const luaL_Reg filelib [] = {
{"resolve", lua_wrap_errors<l_file_resolve>},
{"find", lua_wrap_errors<l_file_find>},
{"read", lua_wrap_errors<l_file_read>},
{"write", lua_wrap_errors<l_file_write>},
{"exists", lua_wrap_errors<l_file_exists>},