Skip to content

Commit

Permalink
Editor.createEntityEx matches World.createEntityEx
Browse files Browse the repository at this point in the history
  • Loading branch information
nem0 committed Oct 23, 2023
1 parent ae91719 commit 69322ff
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 38 deletions.
29 changes: 17 additions & 12 deletions src/editor/studio_app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>& 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)) {
Expand All @@ -2927,7 +2932,7 @@ struct StudioAppImpl final : StudioApp

void visit(const reflection::Property<u32>& 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);
Expand All @@ -2936,7 +2941,7 @@ struct StudioAppImpl final : StudioApp

void visit(const reflection::Property<float>& 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);
Expand All @@ -2945,7 +2950,7 @@ struct StudioAppImpl final : StudioApp

void visit(const reflection::Property<Vec2>& prop) override
{
if (!equalStrings(property_name, prop.name)) return;
if (!isSameProperty(prop.name, property_name)) return;
if (!LuaWrapper::isType<Vec2>(L, -1)) return;

const Vec2 val = LuaWrapper::toType<Vec2>(L, -1);
Expand All @@ -2954,7 +2959,7 @@ struct StudioAppImpl final : StudioApp

void visit(const reflection::Property<Vec3>& prop) override
{
if (!equalStrings(property_name, prop.name)) return;
if (!isSameProperty(prop.name, property_name)) return;
if (!LuaWrapper::isType<Vec3>(L, -1)) return;

const Vec3 val = LuaWrapper::toType<Vec3>(L, -1);
Expand All @@ -2963,7 +2968,7 @@ struct StudioAppImpl final : StudioApp

void visit(const reflection::Property<IVec3>& prop) override
{
if (!equalStrings(property_name, prop.name)) return;
if (!isSameProperty(prop.name, property_name)) return;
if (!LuaWrapper::isType<IVec3>(L, -1)) return;

const IVec3 val = LuaWrapper::toType<IVec3>(L, -1);
Expand All @@ -2972,7 +2977,7 @@ struct StudioAppImpl final : StudioApp

void visit(const reflection::Property<Vec4>& prop) override
{
if (!equalStrings(property_name, prop.name)) return;
if (!isSameProperty(prop.name, property_name)) return;
if (!LuaWrapper::isType<Vec4>(L, -1)) return;

const Vec4 val = LuaWrapper::toType<Vec4>(L, -1);
Expand All @@ -2981,7 +2986,7 @@ struct StudioAppImpl final : StudioApp

void visit(const reflection::Property<const char*>& 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);
Expand All @@ -2991,7 +2996,7 @@ struct StudioAppImpl final : StudioApp

void visit(const reflection::Property<Path>& 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);
Expand All @@ -3001,7 +3006,7 @@ struct StudioAppImpl final : StudioApp

void visit(const reflection::Property<bool>& 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;
Expand Down
2 changes: 1 addition & 1 deletion src/editor/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
21 changes: 21 additions & 0 deletions src/engine/lua_wrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ int traceback(lua_State *L) {
return 1;
}

void convertPropertyToLuaName(const char* src, Span<char> 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)
{
Expand Down
1 change: 1 addition & 0 deletions src/engine/lua_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<char> 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);
Expand Down
28 changes: 3 additions & 25 deletions src/lua_script/lua_script_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,28 +45,6 @@ static const char* toString(InputSystem::Event::Type type) {
return "N/A";
}

static void convertPropertyToLuaName(const char* src, Span<char> 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<float>& prop) override { set(prop); }
void visit(const reflection::Property<int>& prop) override { set(prop); }
Expand All @@ -86,7 +64,7 @@ struct ArrayItemSetVisitor : reflection::IPropertyVisitor {
template <typename T>
void set(const reflection::Property<T>& 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);
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 69322ff

Please sign in to comment.