Skip to content

Commit

Permalink
#123 Implement array serialize on editor
Browse files Browse the repository at this point in the history
  • Loading branch information
ducphamhong committed May 24, 2022
1 parent c9fc8b3 commit 5d47db0
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ namespace Skylicht
{
CArraySerializable* arrayObject = (CArraySerializable*)object;

if (arrayObject->OnCreateElement != nullptr)
if (arrayObject->haveCreateElementFunction())
{
// add input to add elements
CSubject<int>* count = new CSubject<int>(arrayObject->getElementCount());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,18 @@ namespace Skylicht

CIndirectLighting::CIndirectLighting() :
m_type(SH9),
m_autoSH(true)
m_autoSH(true),
m_internalLM(false)
{

}

CIndirectLighting::~CIndirectLighting()
{

if (m_internalLM && m_lightmap)
{
CTextureManager::getInstance()->removeTexture(m_lightmap);
}
}

void CIndirectLighting::initComponent()
Expand Down Expand Up @@ -123,29 +127,21 @@ namespace Skylicht
enumType->addEnumString("SH9", EIndirectType::SH9);
object->autoRelease(enumType);

CArraySerializable* textureArray = new CArraySerializable("LMTextures", object);
textureArray->OnCreateElement = [textureArray]()
CArrayTypeSerializable<CFilePathProperty>* textureArray = new CArrayTypeSerializable<CFilePathProperty>("LMTextures", object);
textureArray->OnCreateElement = [](CValueProperty* element)
{
CFilePathProperty* element = new CFilePathProperty(textureArray, "Element");
textureArray->autoRelease(element);
return element;
CFilePathProperty* fileProperty = dynamic_cast<CFilePathProperty*>(element);
fileProperty->Exts.push_back("tga");
fileProperty->Exts.push_back("png");
};
object->autoRelease(textureArray);

// Sync lightmap path
std::vector<std::string> textureExts = { "tga","png" };
// lightmap path
textureArray->resize((int)m_lightmapPaths.size());
for (u32 i = 0, n = (u32)m_lightmapPaths.size(); i < n; i++)
{
char name[43];
sprintf(name, "%d", i);

textureArray->autoRelease(
new CFilePathProperty(
textureArray,
name,
m_lightmapPaths[i].c_str(),
textureExts)
);
CFilePathProperty* fileProperty = dynamic_cast<CFilePathProperty*>(textureArray->getElement(i));
fileProperty->set(m_lightmapPaths[i]);
}

return object;
Expand All @@ -155,25 +151,54 @@ namespace Skylicht
{
CComponentSystem::loadSerializable(object);

bool lightmapChanged = false;

EIndirectType type = object->get<EIndirectType>("type", EIndirectType::SH9);

CArraySerializable* textureArray = (CArraySerializable*)object->getProperty("LMTextures");
if (textureArray != NULL)
{
std::vector<std::string> old = m_lightmapPaths;
m_lightmapPaths.clear();

int count = textureArray->getElementCount();
for (int i = 0; i < count; i++)
{
char name[43];
sprintf(name, "%d", i);

std::string path = textureArray->get<std::string>(name, std::string());
std::string path = textureArray->getElementValue<std::string>(i, std::string());
m_lightmapPaths.push_back(path);
}

if (!isLightmapEmpty())
lightmapChanged = isLightmapChanged(old);
}

setIndirectLightingType(type, lightmapChanged);
}

bool CIndirectLighting::isLightmapEmpty()
{
for (std::string& s : m_lightmapPaths)
{
if (s.empty())
return true;
}
return false;
}

bool CIndirectLighting::isLightmapChanged(const std::vector<std::string>& paths)
{
if (paths.size() != m_lightmapPaths.size())
return true;

for (int i = 0, n = (int)paths.size(); i < n; i++)
{
if (paths[i] != m_lightmapPaths[i])
{
return true;
}
}

setIndirectLightingType(type);
return false;
}

void CIndirectLighting::setSH(core::vector3df* sh)
Expand All @@ -185,7 +210,11 @@ namespace Skylicht

void CIndirectLighting::setLightmap(ITexture* texture)
{
if (m_internalLM && m_lightmap)
CTextureManager::getInstance()->removeTexture(m_lightmap);

m_lightmap = texture;
m_internalLM = false;
}

void CIndirectLighting::setAutoSH(bool b)
Expand All @@ -199,7 +228,7 @@ namespace Skylicht
}
}

void CIndirectLighting::setIndirectLightingType(EIndirectType type)
void CIndirectLighting::setIndirectLightingType(EIndirectType type, bool loadLightmap)
{
m_type = type;

Expand All @@ -208,8 +237,14 @@ namespace Skylicht
if (m_type == LightmapArray)
{
// Load lightmap texture array
if (m_lightmapPaths.size() > 0 && m_lightmap == NULL)
if (m_lightmapPaths.size() > 0 && loadLightmap)
{
if (m_internalLM && m_lightmap)
CTextureManager::getInstance()->removeTexture(m_lightmap);

m_lightmap = CTextureManager::getInstance()->getTextureArray(m_lightmapPaths);
m_internalLM = true;
}

data->Type = CIndirectLightingData::LightmapArray;
data->LightmapTexture = m_lightmap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ namespace Skylicht
std::vector<std::string> m_lightmapPaths;

ITexture* m_lightmap;
bool m_internalLM;

public:
CIndirectLighting();
Expand All @@ -77,7 +78,7 @@ namespace Skylicht

void setAutoSH(bool b);

void setIndirectLightingType(EIndirectType type);
void setIndirectLightingType(EIndirectType type, bool loadLightmap = true);

void setLightmap(ITexture* texture);

Expand All @@ -98,6 +99,9 @@ namespace Skylicht
return m_data;
}

bool isLightmapEmpty();
bool isLightmapChanged(const std::vector<std::string>& paths);

DECLARE_GETTYPENAME(CIndirectLighting)
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace Skylicht

bool CArraySerializable::resize(int count)
{
if (OnCreateElement == nullptr || count < 0)
if (count < 0)
return false;

int numElement = getElementCount();
Expand All @@ -65,12 +65,21 @@ namespace Skylicht
// need grow
for (int i = numElement; i < count; i++)
{
CValueProperty* value = OnCreateElement();
CValueProperty* value = createElement();
if (!value)
return false;
}
}

// rename
char name[32];
numElement = getElementCount();
for (int i = 0; i < numElement; i++)
{
sprintf(name, "[%d]", i);
getElement(i)->Name = name;
}

return true;
}
}
66 changes: 62 additions & 4 deletions Projects/Skylicht/Engine/Source/Serializable/CArraySerializable.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ namespace Skylicht
{
class CArraySerializable : public CObjectSerializable
{
public:

std::function<CValueProperty* ()> OnCreateElement;

public:
CArraySerializable(const char* name);

CArraySerializable(const char* name, CObjectSerializable* parent);

virtual ~CArraySerializable();

template<class T>
T getElementValue(int i, T defaultValue)
{
return get<T>(getElement(i), defaultValue);
}

int getElementCount()
{
return getNumProperty();
Expand All @@ -52,11 +54,67 @@ namespace Skylicht
return getPropertyID(i);
}

virtual bool haveCreateElementFunction()
{
return false;
}

virtual CValueProperty* createElement()
{
return NULL;
}

bool resize(int count);

virtual bool isArray()
{
return true;
}
};

template <class T>
class CArrayTypeSerializable : public CArraySerializable
{
public:
std::function<void(CValueProperty*)> OnCreateElement;

public:
CArrayTypeSerializable(const char* name) :
CArraySerializable(name)
{

}

CArrayTypeSerializable(const char* name, CObjectSerializable* parent) :
CArraySerializable(name, parent)
{

}

virtual bool haveCreateElementFunction()
{
return true;
}

virtual CValueProperty* createElement()
{
T* t = new T();
CValueProperty* element = dynamic_cast<CValueProperty*>(t);
if (element == NULL)
{
delete t;
return NULL;
}

addProperty(element);
autoRelease(element);

if (OnCreateElement != nullptr)
{
OnCreateElement(element);
}

return element;
}
};
}
10 changes: 10 additions & 0 deletions Projects/Skylicht/Engine/Source/Serializable/CObjectSerializable.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,16 @@ namespace Skylicht
return t->get();
}

template<class T>
T get(CValueProperty* p, T defaultValue)
{
CValuePropertyTemplate<T>* t = dynamic_cast<CValuePropertyTemplate<T>*>(p);
if (t == NULL)
return defaultValue;

return t->get();
}

virtual void serialize(io::IAttributes* io);

virtual void deserialize(io::IAttributes* io);
Expand Down
Loading

0 comments on commit 5d47db0

Please sign in to comment.