Merge branch 'main' into devtools

This commit is contained in:
MihailRis 2024-05-12 20:07:55 +03:00
commit ee853aab4c
11 changed files with 114 additions and 19 deletions

View File

@ -276,6 +276,15 @@ function table.tostring(t)
return s..']'
end
function file.readlines(path)
local str = file.read(path)
local lines = {}
for s in str:gmatch("[^\r\n]+") do
table.insert(lines, s)
end
return lines
end
console.add_command(
"tp obj:sel=$obj.id x:num~pos.x y:num~pos.y z:num~pos.z",
function (args, kwargs)

View File

@ -266,7 +266,7 @@ void Engine::loadContent() {
names = manager.assembly(names);
contentPacks = manager.getAll(names);
std::vector<std::pair<std::string, fs::path>> resRoots;
std::vector<PathsRoot> resRoots;
for (auto& pack : contentPacks) {
resRoots.push_back({pack.id, pack.folder});

View File

@ -164,14 +164,14 @@ fs::path EnginePaths::resolve(std::string path) {
throw files_access_error("unknown entry point '"+prefix+"'");
}
ResPaths::ResPaths(fs::path mainRoot, std::vector<std::pair<std::string, fs::path>> roots)
ResPaths::ResPaths(fs::path mainRoot, std::vector<PathsRoot> roots)
: mainRoot(mainRoot), roots(roots) {
}
fs::path ResPaths::find(const std::string& filename) const {
for (int i = roots.size()-1; i >= 0; i--) {
auto& root = roots[i];
fs::path file = root.second / fs::u8path(filename);
fs::path file = root.path / fs::u8path(filename);
if (fs::exists(file)) {
return file;
}
@ -182,8 +182,8 @@ fs::path ResPaths::find(const std::string& filename) const {
std::string ResPaths::findRaw(const std::string& filename) const {
for (int i = roots.size()-1; i >= 0; i--) {
auto& root = roots[i];
if (fs::exists(root.second / fs::path(filename))) {
return root.first+":"+filename;
if (fs::exists(root.path / fs::path(filename))) {
return root.name + ":" + filename;
}
}
auto resDir = mainRoot;
@ -193,11 +193,35 @@ std::string ResPaths::findRaw(const std::string& filename) const {
throw std::runtime_error("could not to find file "+util::quote(filename));
}
std::vector<std::string> ResPaths::listdirRaw(const std::string& folderName) const {
std::vector<std::string> entries;
for (int i = roots.size()-1; i >= 0; i--) {
auto& root = roots[i];
fs::path folder = root.path / fs::u8path(folderName);
if (!fs::is_directory(folder))
continue;
for (const auto& entry : fs::directory_iterator(folder)) {
auto name = entry.path().filename().u8string();
entries.push_back(root.name+":"+folderName+"/"+name);
}
}
{
fs::path folder = mainRoot / fs::u8path(folderName);
if (!fs::is_directory(folder))
return entries;
for (const auto& entry : fs::directory_iterator(folder)) {
auto name = entry.path().filename().u8string();
entries.push_back("core:"+folderName+"/"+name);
}
}
return entries;
}
std::vector<fs::path> ResPaths::listdir(const std::string& folderName) const {
std::vector<fs::path> entries;
for (int i = roots.size()-1; i >= 0; i--) {
auto& root = roots[i];
fs::path folder = root.second / fs::u8path(folderName);
fs::path folder = root.path / fs::u8path(folderName);
if (!fs::is_directory(folder))
continue;
for (const auto& entry : fs::directory_iterator(folder)) {

View File

@ -42,18 +42,24 @@ public:
fs::path resolve(std::string path);
};
struct PathsRoot {
std::string name;
fs::path path;
};
class ResPaths {
fs::path mainRoot;
std::vector<std::pair<std::string, fs::path>> roots;
std::vector<PathsRoot> roots;
public:
ResPaths(
fs::path mainRoot,
std::vector<std::pair<std::string, fs::path>> roots
std::vector<PathsRoot> roots
);
fs::path find(const std::string& filename) const;
std::string findRaw(const std::string& filename) const;
std::vector<fs::path> listdir(const std::string& folder) const;
std::vector<std::string> listdirRaw(const std::string& folder) const;
const fs::path& getMainRoot() const;
};

View File

@ -24,9 +24,9 @@
static debug::Logger logger("level-screen");
LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine) {
postProcessing = std::make_unique<PostProcessing>();
LevelScreen::LevelScreen(Engine* engine, Level* level)
: Screen(engine), postProcessing(std::make_unique<PostProcessing>())
{
auto& settings = engine->getSettings();
auto assets = engine->getAssets();
auto menu = engine->getGUI()->getMenu();
@ -48,7 +48,11 @@ LevelScreen::LevelScreen(Engine* engine, Level* level) : Screen(engine) {
animator = std::make_unique<TextureAnimator>();
animator->addAnimations(assets->getAnimations());
auto content = level->content;
initializeContent();
}
void LevelScreen::initializeContent() {
auto content = controller->getLevel()->content;
for (auto& entry : content->getPacks()) {
auto pack = entry.second.get();
const ContentPack& info = pack->getInfo();

View File

@ -26,6 +26,7 @@ class LevelScreen : public Screen {
bool hudVisible = true;
void updateHotkeys();
void initializeContent();
public:
LevelScreen(Engine* engine, Level* level);
~LevelScreen();

View File

@ -130,7 +130,6 @@ void TextBox::drawBackground(const DrawContext* pctx, Assets*) {
/// @brief Insert text at the caret. Also selected text will be erased
/// @param text Inserting text
void TextBox::paste(const std::wstring& text) {
eraseSelected();
if (caret >= input.length()) {
input += text;
@ -203,7 +202,9 @@ void TextBox::setTextOffset(uint x) {
}
void TextBox::typed(unsigned int codepoint) {
paste(std::wstring({(wchar_t)codepoint}));
if (editable) {
paste(std::wstring({(wchar_t)codepoint}));
}
}
bool TextBox::validate() {

View File

@ -140,6 +140,37 @@ static int l_file_write_bytes(lua_State* L) {
}
}
static int l_file_list_all_res(lua_State* L, const std::string& path) {
auto files = scripting::engine->getResPaths()->listdirRaw(path);
lua_createtable(L, files.size(), 0);
for (size_t i = 0; i < files.size(); i++) {
lua_pushstring(L, files[i].c_str());
lua_rawseti(L, -2, i+1);
}
return 1;
}
static int l_file_list(lua_State* L) {
std::string dirname = lua_tostring(L, 1);
if (dirname.find(':') == std::string::npos) {
return l_file_list_all_res(L, dirname);
}
fs::path path = resolve_path(L, dirname);
if (!fs::is_directory(path)) {
throw std::runtime_error(util::quote(path.u8string())+" is not a directory");
}
lua_createtable(L, 0, 0);
size_t index = 1;
for (auto& entry : fs::directory_iterator(path)) {
auto name = entry.path().filename().u8string();
auto file = dirname + "/" + name;
lua_pushstring(L, file.c_str());
lua_rawseti(L, -2, index);
index++;
}
return 1;
}
const luaL_Reg filelib [] = {
{"resolve", lua_wrap_errors<l_file_resolve>},
{"find", lua_wrap_errors<l_file_find>},
@ -153,5 +184,6 @@ const luaL_Reg filelib [] = {
{"mkdirs", lua_wrap_errors<l_file_mkdirs>},
{"read_bytes", lua_wrap_errors<l_file_read_bytes>},
{"write_bytes", lua_wrap_errors<l_file_write_bytes>},
{"list", lua_wrap_errors<l_file_list>},
{NULL, NULL}
};

View File

@ -204,6 +204,13 @@ static int p_get_text(UINode* node) {
return 0;
}
static int p_get_editable(UINode* node) {
if (auto box = dynamic_cast<TextBox*>(node)) {
return state->pushboolean(box->isEditable());
}
return 0;
}
static int p_get_add(UINode* node) {
if (dynamic_cast<Container*>(node)) {
return state->pushcfunction(l_container_add);
@ -268,6 +275,7 @@ static int l_gui_getattr(lua_State* L) {
{"placeholder", p_get_placeholder},
{"valid", p_is_valid},
{"text", p_get_text},
{"editable", p_get_editable},
{"value", p_get_value},
{"min", p_get_min},
{"max", p_get_max},
@ -325,6 +333,11 @@ static void p_set_text(UINode* node, int idx) {
box->setText(util::str2wstr_utf8(state->tostring(idx)));
}
}
static void p_set_editable(UINode* node, int idx) {
if (auto box = dynamic_cast<TextBox*>(node)) {
box->setEditable(state->toboolean(idx));
}
}
static void p_set_value(UINode* node, int idx) {
if (auto bar = dynamic_cast<TrackBar*>(node)) {
bar->setValue(state->tonumber(idx));
@ -397,6 +410,7 @@ static int l_gui_setattr(lua_State* L) {
{"enabled", p_set_enabled},
{"placeholder", p_set_placeholder},
{"text", p_set_text},
{"editable", p_set_editable},
{"value", p_set_value},
{"min", p_set_min},
{"max", p_set_max},
@ -459,8 +473,7 @@ static int l_gui_get_locales_info(lua_State* L) {
}
static int l_gui_getviewport(lua_State* L) {
lua::pushvec2_arr(L, scripting::engine->getGUI()->getContainer()->getSize());
return 1;
return lua::pushvec2_arr(L, scripting::engine->getGUI()->getContainer()->getSize());
}
const luaL_Reg guilib [] = {

View File

@ -1,4 +1,5 @@
#include "api_lua.hpp"
#include "lua_util.hpp"
#include "lua_commons.hpp"
#include "LuaState.hpp"
#include "../scripting.hpp"
@ -39,9 +40,14 @@ static int l_add_callback(lua_State* L) {
return 0;
}
static int l_get_mouse_pos(lua_State* L) {
return lua::pushvec2_arr(L, Events::cursor);
}
const luaL_Reg inputlib [] = {
{"keycode", lua_wrap_errors<l_keycode>},
{"add_callback", lua_wrap_errors<l_add_callback>},
{"get_mouse_pos", lua_wrap_errors<l_get_mouse_pos>},
{NULL, NULL}
};

View File

@ -88,8 +88,7 @@ void Events::pollEvents() {
binding.justChange = true;
binding.onactived.notify();
}
}
else {
} else {
if (binding.state) {
binding.state = false;
binding.justChange = true;
@ -107,7 +106,7 @@ void Events::bind(const std::string& name, inputtype type, mousecode code) {
}
void Events::bind(const std::string& name, inputtype type, int code) {
bindings[name] = Binding(type, code);
bindings.emplace(name, Binding(type, code));
}
bool Events::active(const std::string& name) {