From 69322ff91a54cb85dc30910c23e00be0bcc5a6c9 Mon Sep 17 00:00:00 2001 From: Mikulas Florek Date: Mon, 23 Oct 2023 21:41:28 +0200 Subject: [PATCH] Editor.createEntityEx matches World.createEntityEx --- src/editor/studio_app.cpp | 29 ++++++++++++++++------------ src/editor/utils.cpp | 2 +- src/engine/lua_wrapper.cpp | 21 ++++++++++++++++++++ src/engine/lua_wrapper.h | 1 + src/lua_script/lua_script_system.cpp | 28 +++------------------------ 5 files changed, 43 insertions(+), 38 deletions(-) diff --git a/src/editor/studio_app.cpp b/src/editor/studio_app.cpp index c66e43b21a..7e6ae3367e 100644 --- a/src/editor/studio_app.cpp +++ b/src/editor/studio_app.cpp @@ -2910,11 +2910,16 @@ struct StudioAppImpl final : StudioApp } - struct SetPropertyVisitor : reflection::IPropertyVisitor - { + struct SetPropertyVisitor : reflection::IPropertyVisitor { + static bool isSameProperty(const char* name, const char* lua_name) { + char tmp[128]; + LuaWrapper::convertPropertyToLuaName(name, Span(tmp)); + return equalStrings(tmp, lua_name); + } + void visit(const reflection::Property& prop) override { - if (!equalStrings(property_name, prop.name)) return; + if (!isSameProperty(prop.name, property_name)) return; if (!lua_isnumber(L, -1)) return; if(reflection::getAttribute(prop, reflection::IAttribute::ENUM)) { @@ -2927,7 +2932,7 @@ struct StudioAppImpl final : StudioApp void visit(const reflection::Property& prop) override { - if (!equalStrings(property_name, prop.name)) return; + if (!isSameProperty(prop.name, property_name)) return; if (!lua_isnumber(L, -1)) return; const u32 val = (u32)lua_tointeger(L, -1); @@ -2936,7 +2941,7 @@ struct StudioAppImpl final : StudioApp void visit(const reflection::Property& prop) override { - if (!equalStrings(property_name, prop.name)) return; + if (!isSameProperty(prop.name, property_name)) return; if (!lua_isnumber(L, -1)) return; float val = (float)lua_tonumber(L, -1); @@ -2945,7 +2950,7 @@ struct StudioAppImpl final : StudioApp void visit(const reflection::Property& prop) override { - if (!equalStrings(property_name, prop.name)) return; + if (!isSameProperty(prop.name, property_name)) return; if (!LuaWrapper::isType(L, -1)) return; const Vec2 val = LuaWrapper::toType(L, -1); @@ -2954,7 +2959,7 @@ struct StudioAppImpl final : StudioApp void visit(const reflection::Property& prop) override { - if (!equalStrings(property_name, prop.name)) return; + if (!isSameProperty(prop.name, property_name)) return; if (!LuaWrapper::isType(L, -1)) return; const Vec3 val = LuaWrapper::toType(L, -1); @@ -2963,7 +2968,7 @@ struct StudioAppImpl final : StudioApp void visit(const reflection::Property& prop) override { - if (!equalStrings(property_name, prop.name)) return; + if (!isSameProperty(prop.name, property_name)) return; if (!LuaWrapper::isType(L, -1)) return; const IVec3 val = LuaWrapper::toType(L, -1); @@ -2972,7 +2977,7 @@ struct StudioAppImpl final : StudioApp void visit(const reflection::Property& prop) override { - if (!equalStrings(property_name, prop.name)) return; + if (!isSameProperty(prop.name, property_name)) return; if (!LuaWrapper::isType(L, -1)) return; const Vec4 val = LuaWrapper::toType(L, -1); @@ -2981,7 +2986,7 @@ struct StudioAppImpl final : StudioApp void visit(const reflection::Property& prop) override { - if (!equalStrings(property_name, prop.name)) return; + if (!isSameProperty(prop.name, property_name)) return; if (!lua_isstring(L, -1)) return; const char* str = lua_tostring(L, -1); @@ -2991,7 +2996,7 @@ struct StudioAppImpl final : StudioApp void visit(const reflection::Property& prop) override { - if (!equalStrings(property_name, prop.name)) return; + if (!isSameProperty(prop.name, property_name)) return; if (!lua_isstring(L, -1)) return; const char* str = lua_tostring(L, -1); @@ -3001,7 +3006,7 @@ struct StudioAppImpl final : StudioApp void visit(const reflection::Property& prop) override { - if (!equalStrings(property_name, prop.name)) return; + if (!isSameProperty(prop.name, property_name)) return; if (!lua_isboolean(L, -1)) return; bool val = lua_toboolean(L, -1) != 0; diff --git a/src/editor/utils.cpp b/src/editor/utils.cpp index e4303b6341..e0b88b9681 100644 --- a/src/editor/utils.cpp +++ b/src/editor/utils.cpp @@ -2595,7 +2595,7 @@ bool beginCenterStrip(const char* str_id, u32 lines) { ImGui::SetNextWindowPos(vp->GetCenter(), ImGuiCond_Always, ImVec2(0.5f, 0.5f)); ImGui::SetNextWindowSize(ImVec2(vp->Size.x - style.FramePadding.x * 2, ImGui::GetTextLineHeightWithSpacing() * lines)); ImGuiWindowFlags flags = ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoMove | - ImGuiWindowFlags_NoSavedSettings | ImGuiWindowFlags_NoFocusOnAppearing; + ImGuiWindowFlags_NoSavedSettings; return ImGui::BeginPopupModal(str_id, nullptr, flags); } diff --git a/src/engine/lua_wrapper.cpp b/src/engine/lua_wrapper.cpp index fb81cb1134..508ae09402 100644 --- a/src/engine/lua_wrapper.cpp +++ b/src/engine/lua_wrapper.cpp @@ -58,6 +58,27 @@ int traceback(lua_State *L) { return 1; } +void convertPropertyToLuaName(const char* src, Span out) { + const u32 max_size = out.length(); + ASSERT(max_size > 0); + char* dest = out.begin(); + while (*src && dest - out.begin() < max_size - 1) { + if (isLetter(*src)) { + *dest = isUpperCase(*src) ? *src - 'A' + 'a' : *src; + ++dest; + } + else if (isNumeric(*src)) { + *dest = *src; + ++dest; + } + else { + *dest = '_'; + ++dest; + } + ++src; + } + *dest = 0; +} bool pcall(lua_State* L, int nargs, int nres) { diff --git a/src/engine/lua_wrapper.h b/src/engine/lua_wrapper.h index 9ae3b093b2..edfb43e147 100644 --- a/src/engine/lua_wrapper.h +++ b/src/engine/lua_wrapper.h @@ -56,6 +56,7 @@ LUMIX_ENGINE_API int traceback (lua_State *L); LUMIX_ENGINE_API bool pcall(lua_State* L, int nargs, int nres); LUMIX_ENGINE_API bool execute(lua_State* L, StringView content, const char* name, int nresults); LUMIX_ENGINE_API int getField(lua_State* L, int idx, const char* k); +LUMIX_ENGINE_API void convertPropertyToLuaName(const char* src, Span out); // create reference to a value on top of stack, so it's not garbage collected and can be easily pushed to stack. Similar to luaL_ref LUMIX_ENGINE_API RefHandle createRef(lua_State* L); diff --git a/src/lua_script/lua_script_system.cpp b/src/lua_script/lua_script_system.cpp index 5fcc5b04fe..8f2f81bd1b 100644 --- a/src/lua_script/lua_script_system.cpp +++ b/src/lua_script/lua_script_system.cpp @@ -45,28 +45,6 @@ static const char* toString(InputSystem::Event::Type type) { return "N/A"; } -static void convertPropertyToLuaName(const char* src, Span out) { - const u32 max_size = out.length(); - ASSERT(max_size > 0); - char* dest = out.begin(); - while (*src && dest - out.begin() < max_size - 1) { - if (isLetter(*src)) { - *dest = isUpperCase(*src) ? *src - 'A' + 'a' : *src; - ++dest; - } - else if (isNumeric(*src)) { - *dest = *src; - ++dest; - } - else { - *dest = '_'; - ++dest; - } - ++src; - } - *dest = 0; -} - struct ArrayItemSetVisitor : reflection::IPropertyVisitor { void visit(const reflection::Property& prop) override { set(prop); } void visit(const reflection::Property& prop) override { set(prop); } @@ -86,7 +64,7 @@ struct ArrayItemSetVisitor : reflection::IPropertyVisitor { template void set(const reflection::Property& prop) { char tmp[50]; - convertPropertyToLuaName(prop.name, Span(tmp)); + LuaWrapper::convertPropertyToLuaName(prop.name, Span(tmp)); i32 type = lua_getfield(L, -1, tmp); if (type == LUA_TNIL) { lua_pop(L, 1); @@ -1222,7 +1200,7 @@ struct LuaScriptModuleImpl final : LuaScriptModule static bool isSameProperty(const char* name, const char* lua_name) { char tmp[50]; - convertPropertyToLuaName(name, Span(tmp)); + LuaWrapper::convertPropertyToLuaName(name, Span(tmp)); return equalStrings(tmp, lua_name); } @@ -1360,7 +1338,7 @@ struct LuaScriptModuleImpl final : LuaScriptModule { bool isSameProperty(const char* name, const char* lua_name) { char tmp[50]; - convertPropertyToLuaName(name, Span(tmp)); + LuaWrapper::convertPropertyToLuaName(name, Span(tmp)); if (equalStrings(tmp, lua_name)) { found = true; return true;