show lua properties in the property grid - closes #562

This commit is contained in:
Mikulas Florek 2015-10-08 22:54:26 +02:00
parent 034bfec047
commit 270d5a76c2
4 changed files with 121 additions and 44 deletions

View file

@ -1,3 +1,4 @@
#include "lua_script_system.h"
#include "core/array.h"
#include "core/base_proxy_allocator.h"
#include "core/binary_array.h"
@ -59,20 +60,9 @@ public:
};
class LuaScriptScene : public IScene
class LuaScriptSceneImpl : public LuaScriptScene
{
public:
struct Property
{
Property(IAllocator& allocator)
: m_value(allocator)
{
}
string m_value;
uint32_t m_name_hash;
};
struct Script
{
Script(IAllocator& allocator)
@ -89,7 +79,7 @@ public:
public:
LuaScriptScene(LuaScriptSystem& system, Engine& engine, UniverseContext& ctx)
LuaScriptSceneImpl(LuaScriptSystem& system, Engine& engine, UniverseContext& ctx)
: m_system(system)
, m_universe_context(ctx)
, m_scripts(system.getAllocator())
@ -99,7 +89,7 @@ public:
}
~LuaScriptScene()
~LuaScriptSceneImpl()
{
unloadAllScripts();
}
@ -135,10 +125,7 @@ public:
void applyProperty(Script& script, Property& prop)
{
if (prop.m_value.length() == 0)
{
return;
}
if (prop.m_value.length() == 0) return;
lua_State* state = script.m_state;
const char* name = script.m_script->getPropertyName(prop.m_name_hash);
@ -166,12 +153,57 @@ public:
}
virtual const char* getPropertyValue(Lumix::ComponentIndex cmp, int index) const override
{
auto& script = getScript(cmp);
uint32_t hash = crc32(getPropertyName(cmp, index));
for (auto& value : script.m_properties)
{
if (value.m_name_hash == hash)
{
return value.m_value.c_str();
}
}
return "";
}
virtual void setPropertyValue(Lumix::ComponentIndex cmp,
const char* name,
const char* value) override
{
Property& prop = getScriptProperty(cmp, name);
prop.m_value = value;
if (m_scripts[cmp].m_state)
{
applyProperty(m_scripts[cmp], prop);
}
}
virtual const char* getPropertyName(Lumix::ComponentIndex cmp, int index) const override
{
auto& script = getScript(cmp);
return script.m_script ? script.m_script->getPropertiesNames()[index] : "";
}
virtual int getPropertyCount(Lumix::ComponentIndex cmp) const override
{
auto& script = getScript(cmp);
return script.m_script ? script.m_script->getPropertiesNames().size() : 0;
}
void applyProperties(Script& script)
{
if (!script.m_script)
{
return;
}
if (!script.m_script) return;
lua_State* state = script.m_state;
for (Property& prop : script.m_properties)
{
@ -188,10 +220,8 @@ public:
allocator.deallocate(ptr);
return nullptr;
}
if (nsize > 0 && ptr == nullptr)
{
return allocator.allocate(nsize);
}
if (nsize > 0 && ptr == nullptr) return allocator.allocate(nsize);
void* new_mem = allocator.allocate(nsize);
memcpy(new_mem, ptr, Math::minValue(osize, nsize));
allocator.deallocate(ptr);
@ -265,7 +295,7 @@ public:
{
if (type == LUA_SCRIPT_HASH)
{
LuaScriptScene::Script& script =
LuaScriptSceneImpl::Script& script =
m_scripts.emplace(m_system.getAllocator());
script.m_entity = entity;
script.m_script = nullptr;
@ -386,19 +416,6 @@ public:
const Script& getScript(ComponentIndex cmp) const { return m_scripts[cmp]; }
void
setPropertyValue(ComponentIndex cmp, const char* name, const char* value)
{
Property& prop = getScriptProperty(cmp, name);
prop.m_value = value;
if (m_scripts[cmp].m_state)
{
applyProperty(m_scripts[cmp], prop);
}
}
Property& getScriptProperty(ComponentIndex cmp, const char* name)
{
uint32_t name_hash = crc32(name);
@ -471,7 +488,7 @@ IAllocator& LuaScriptSystem::getAllocator()
IScene* LuaScriptSystem::createScene(UniverseContext& ctx)
{
return m_allocator.newObject<LuaScriptScene>(*this, m_engine, ctx);
return LUMIX_NEW(m_allocator, LuaScriptSceneImpl)(*this, m_engine, ctx);
}
@ -493,10 +510,10 @@ void LuaScriptSystem::registerProperties()
m_engine.registerComponentType("lua_script", "Lua script");
m_engine.registerProperty(
"lua_script",
allocator.newObject<FilePropertyDescriptor<LuaScriptScene>>(
allocator.newObject<FilePropertyDescriptor<LuaScriptSceneImpl>>(
"source",
&LuaScriptScene::getScriptPath,
&LuaScriptScene::setScriptPath,
&LuaScriptSceneImpl::getScriptPath,
&LuaScriptSceneImpl::setScriptPath,
"Lua (*.lua)",
allocator));
}

View file

@ -0,0 +1,36 @@
#pragma once
#include "core/string.h"
#include "engine/iplugin.h"
namespace Lumix
{
class LuaScriptScene : public IScene
{
public:
struct Property
{
Property(Lumix::IAllocator& allocator)
: m_value(allocator)
{
}
Lumix::string m_value;
uint32_t m_name_hash;
};
public:
virtual int getPropertyCount(Lumix::ComponentIndex cmp) const = 0;
virtual const char* getPropertyName(Lumix::ComponentIndex cmp, int index) const = 0;
virtual const char* getPropertyValue(Lumix::ComponentIndex cmp, int index) const = 0;
virtual void setPropertyValue(Lumix::ComponentIndex cmp,
const char* name,
const char* value) = 0;
};
} // namespace Lumix

View file

@ -4,6 +4,7 @@
#include "editor/world_editor.h"
#include "engine/engine.h"
#include "engine/property_descriptor.h"
#include "lua_script/lua_script_system.h"
#include "ocornut-imgui/imgui.h"
#include "terrain_editor.h"
#include "utils.h"
@ -249,6 +250,11 @@ void PropertyGrid::showComponentProperties(Lumix::ComponentUID cmp)
showProperty(*desc, -1, cmp);
}
if (cmp.type == Lumix::crc32("lua_script"))
{
onLuaScriptGui(cmp);
}
if (cmp.type == Lumix::crc32("terrain"))
{
m_terrain_editor->setComponent(cmp);
@ -257,6 +263,23 @@ void PropertyGrid::showComponentProperties(Lumix::ComponentUID cmp)
}
void PropertyGrid::onLuaScriptGui(Lumix::ComponentUID cmp)
{
auto* scene = static_cast<Lumix::LuaScriptScene*>(cmp.scene);
for (int i = 0; i < scene->getPropertyCount(cmp.index); ++i)
{
char buf[256];
Lumix::copyString(buf, scene->getPropertyValue(cmp.index, i));
const char* property_name = scene->getPropertyName(cmp.index, i);
if (ImGui::InputText(property_name, buf, sizeof(buf)))
{
scene->setPropertyValue(cmp.index, property_name, buf);
}
}
}
void PropertyGrid::showCoreProperties(Lumix::Entity entity)
{
char name[256];

View file

@ -31,6 +31,7 @@ class PropertyGrid
bool m_is_opened;
private:
void onLuaScriptGui(Lumix::ComponentUID cmp);
void showProperty(Lumix::IPropertyDescriptor& desc, int index, Lumix::ComponentUID cmp);
void showArrayProperty(Lumix::ComponentUID cmp, Lumix::IArrayDescriptor& desc);
void showComponentProperties(Lumix::ComponentUID cmp);