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..']' return s..']'
end 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( console.add_command(
"tp obj:sel=$obj.id x:num~pos.x y:num~pos.y z:num~pos.z", "tp obj:sel=$obj.id x:num~pos.x y:num~pos.y z:num~pos.z",
function (args, kwargs) function (args, kwargs)

View File

@ -266,7 +266,7 @@ void Engine::loadContent() {
names = manager.assembly(names); names = manager.assembly(names);
contentPacks = manager.getAll(names); contentPacks = manager.getAll(names);
std::vector<std::pair<std::string, fs::path>> resRoots; std::vector<PathsRoot> resRoots;
for (auto& pack : contentPacks) { for (auto& pack : contentPacks) {
resRoots.push_back({pack.id, pack.folder}); 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+"'"); 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) { : mainRoot(mainRoot), roots(roots) {
} }
fs::path ResPaths::find(const std::string& filename) const { fs::path ResPaths::find(const std::string& filename) const {
for (int i = roots.size()-1; i >= 0; i--) { for (int i = roots.size()-1; i >= 0; i--) {
auto& root = roots[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)) { if (fs::exists(file)) {
return 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 { std::string ResPaths::findRaw(const std::string& filename) const {
for (int i = roots.size()-1; i >= 0; i--) { for (int i = roots.size()-1; i >= 0; i--) {
auto& root = roots[i]; auto& root = roots[i];
if (fs::exists(root.second / fs::path(filename))) { if (fs::exists(root.path / fs::path(filename))) {
return root.first+":"+filename; return root.name + ":" + filename;
} }
} }
auto resDir = mainRoot; 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)); 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> ResPaths::listdir(const std::string& folderName) const {
std::vector<fs::path> entries; std::vector<fs::path> entries;
for (int i = roots.size()-1; i >= 0; i--) { for (int i = roots.size()-1; i >= 0; i--) {
auto& root = roots[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)) if (!fs::is_directory(folder))
continue; continue;
for (const auto& entry : fs::directory_iterator(folder)) { for (const auto& entry : fs::directory_iterator(folder)) {

View File

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

View File

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

View File

@ -26,6 +26,7 @@ class LevelScreen : public Screen {
bool hudVisible = true; bool hudVisible = true;
void updateHotkeys(); void updateHotkeys();
void initializeContent();
public: public:
LevelScreen(Engine* engine, Level* level); LevelScreen(Engine* engine, Level* level);
~LevelScreen(); ~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 /// @brief Insert text at the caret. Also selected text will be erased
/// @param text Inserting text /// @param text Inserting text
void TextBox::paste(const std::wstring& text) { void TextBox::paste(const std::wstring& text) {
eraseSelected(); eraseSelected();
if (caret >= input.length()) { if (caret >= input.length()) {
input += text; input += text;
@ -203,7 +202,9 @@ void TextBox::setTextOffset(uint x) {
} }
void TextBox::typed(unsigned int codepoint) { void TextBox::typed(unsigned int codepoint) {
paste(std::wstring({(wchar_t)codepoint})); if (editable) {
paste(std::wstring({(wchar_t)codepoint}));
}
} }
bool TextBox::validate() { 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 [] = { const luaL_Reg filelib [] = {
{"resolve", lua_wrap_errors<l_file_resolve>}, {"resolve", lua_wrap_errors<l_file_resolve>},
{"find", lua_wrap_errors<l_file_find>}, {"find", lua_wrap_errors<l_file_find>},
@ -153,5 +184,6 @@ const luaL_Reg filelib [] = {
{"mkdirs", lua_wrap_errors<l_file_mkdirs>}, {"mkdirs", lua_wrap_errors<l_file_mkdirs>},
{"read_bytes", lua_wrap_errors<l_file_read_bytes>}, {"read_bytes", lua_wrap_errors<l_file_read_bytes>},
{"write_bytes", lua_wrap_errors<l_file_write_bytes>}, {"write_bytes", lua_wrap_errors<l_file_write_bytes>},
{"list", lua_wrap_errors<l_file_list>},
{NULL, NULL} {NULL, NULL}
}; };

View File

@ -204,6 +204,13 @@ static int p_get_text(UINode* node) {
return 0; 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) { static int p_get_add(UINode* node) {
if (dynamic_cast<Container*>(node)) { if (dynamic_cast<Container*>(node)) {
return state->pushcfunction(l_container_add); return state->pushcfunction(l_container_add);
@ -268,6 +275,7 @@ static int l_gui_getattr(lua_State* L) {
{"placeholder", p_get_placeholder}, {"placeholder", p_get_placeholder},
{"valid", p_is_valid}, {"valid", p_is_valid},
{"text", p_get_text}, {"text", p_get_text},
{"editable", p_get_editable},
{"value", p_get_value}, {"value", p_get_value},
{"min", p_get_min}, {"min", p_get_min},
{"max", p_get_max}, {"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))); 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) { static void p_set_value(UINode* node, int idx) {
if (auto bar = dynamic_cast<TrackBar*>(node)) { if (auto bar = dynamic_cast<TrackBar*>(node)) {
bar->setValue(state->tonumber(idx)); bar->setValue(state->tonumber(idx));
@ -397,6 +410,7 @@ static int l_gui_setattr(lua_State* L) {
{"enabled", p_set_enabled}, {"enabled", p_set_enabled},
{"placeholder", p_set_placeholder}, {"placeholder", p_set_placeholder},
{"text", p_set_text}, {"text", p_set_text},
{"editable", p_set_editable},
{"value", p_set_value}, {"value", p_set_value},
{"min", p_set_min}, {"min", p_set_min},
{"max", p_set_max}, {"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) { static int l_gui_getviewport(lua_State* L) {
lua::pushvec2_arr(L, scripting::engine->getGUI()->getContainer()->getSize()); return lua::pushvec2_arr(L, scripting::engine->getGUI()->getContainer()->getSize());
return 1;
} }
const luaL_Reg guilib [] = { const luaL_Reg guilib [] = {

View File

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

View File

@ -88,8 +88,7 @@ void Events::pollEvents() {
binding.justChange = true; binding.justChange = true;
binding.onactived.notify(); binding.onactived.notify();
} }
} } else {
else {
if (binding.state) { if (binding.state) {
binding.state = false; binding.state = false;
binding.justChange = true; 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) { 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) { bool Events::active(const std::string& name) {