Skip to content

Commit

Permalink
#123 Implement ArraySerializable for Array UI Property (update CLight…
Browse files Browse the repository at this point in the history
…Probes save/load data)
  • Loading branch information
ducphamhong committed May 21, 2022
1 parent 18485b4 commit 71659eb
Show file tree
Hide file tree
Showing 13 changed files with 361 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Projects/Editor/Source/Editor/CEditor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ namespace Skylicht
CAssetPropertyController::releaseInstance();
CAssetCreateController::releaseInstance();
CPropertyController::releaseInstance();
CSceneController::releaseInstance();
CSelection::releaseInstance();
CSceneController::releaseInstance();
CProjectSettings::releaseInstance();

delete m_spriteIcon;
Expand Down
3 changes: 3 additions & 0 deletions Projects/Editor/Source/Selection/CSelection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ namespace Skylicht
delete selectObject;

m_selected.clear();

CSceneHistory* history = CSceneController::getInstance()->getHistory();
history->endSaveHistory();
}

std::vector<CSelectObject*> CSelection::getSelectedByType(CSelectObject::ESelectType type)
Expand Down
57 changes: 25 additions & 32 deletions Projects/Skylicht/Engine/Source/LightProbes/CLightProbes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ This file is part of the "Skylicht Engine".
#include "CLightProbes.h"
#include "CLightProbeData.h"
#include "CLightProbeRender.h"
#include "CProbeSerializable.h"

#include "Entity/CEntityManager.h"
#include "GameObject/CGameObject.h"
#include "Transform/CWorldTransformData.h"
Expand Down Expand Up @@ -74,34 +76,28 @@ namespace Skylicht
{
CObjectSerializable* object = CComponentSystem::createSerializable();

// num probes
int numProbes = (int)m_entities.size();
object->addAutoRelease(new CIntProperty(object, "numprobes", numProbes));

// probes container
CObjectSerializable* probes = new CObjectSerializable("probes");
CArraySerializable* probes = new CArraySerializable("Probes");
object->addProperty(probes);
object->addAutoRelease(probes);

// entity data
int numProbes = (int)m_entities.size();
for (int i = 0; i < numProbes; i++)
{
CObjectSerializable* shData = new CObjectSerializable("data");
probes->addProperty(shData);
probes->addAutoRelease(shData);
CProbeSerializable* probeData = new CProbeSerializable(probes);
probes->addAutoRelease(probeData);

CWorldTransformData* world = (CWorldTransformData*)m_entities[i]->getDataByIndex(CWorldTransformData::DataTypeIndex);
CLightProbeData* light = (CLightProbeData*)m_entities[i]->getDataByIndex(CLightProbeData::DataTypeIndex);

shData->addAutoRelease(new CStringProperty(shData, "entityID", m_entities[i]->getID().c_str()));
shData->addAutoRelease(new CMatrixProperty(shData, "transform", world->Relative));
// save id
probeData->EntityID.set(m_entities[i]->getID());

// save transform
probeData->Transform.set(world->Relative);

// save sh data
for (int j = 0; j < 9; j++)
{
char name[64];
sprintf(name, "sh%d", j);
shData->addAutoRelease(new CVector3Property(shData, name, light->SH[j]));
}
probeData->SH[j]->set(light->SH[j]);
}

return object;
Expand All @@ -111,39 +107,36 @@ namespace Skylicht
{
CComponentSystem::loadSerializable(object);

int numProbes = object->get<int>("numprobes", 0);

CObjectSerializable* probes = (CObjectSerializable*)object->getProperty("probes");
CArraySerializable* probes = (CArraySerializable*)object->getProperty("Probes");
int numProbes = probes->getElementCount();

clearAll();

if (probes == NULL)
return;

if (probes->getNumProperty() < numProbes)
if (probes->getElementCount() < numProbes)
return;

for (int i = 0; i < numProbes; i++)
{
CObjectSerializable* shData = (CObjectSerializable*)probes->getPropertyID(i);
CProbeSerializable* shData = (CProbeSerializable*)probes->getElement(i);
if (shData == NULL)
return;

CEntity* entity = addLightProbe(core::vector3df());
CWorldTransformData* world = (CWorldTransformData*)entity->getDataByIndex(CWorldTransformData::DataTypeIndex);
CLightProbeData* light = (CLightProbeData*)entity->getDataByIndex(CLightProbeData::DataTypeIndex);

CWorldTransformData* world = (CWorldTransformData*)m_entities[i]->getDataByIndex(CWorldTransformData::DataTypeIndex);
CLightProbeData* light = (CLightProbeData*)m_entities[i]->getDataByIndex(CLightProbeData::DataTypeIndex);
// set id
entity->setID(shData->EntityID.getString());

std::string entityID = shData->get<std::string>("entityID", std::string());
m_entities[i]->setID(entityID.c_str());
// set transform
world->Relative = shData->Transform.get();

world->Relative = shData->get<core::matrix4>("transform", core::IdentityMatrix);
// set sh data
for (int j = 0; j < 9; j++)
{
char name[64];
sprintf(name, "sh%d", j);
light->SH[j] = shData->get<core::vector3df>(name, core::vector3df());
}
light->SH[j] = shData->SH[j]->get();
}
}

Expand Down
64 changes: 64 additions & 0 deletions Projects/Skylicht/Engine/Source/LightProbes/CProbeSerializable.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
!@
MIT License
Copyright (c) 2022 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CProbeSerializable.h"

namespace Skylicht
{
SERIALIZABLE_REGISTER(CProbeSerializable);

CProbeSerializable::CProbeSerializable() :
CObjectSerializable(getTypeName().c_str()),
EntityID(this, "entity_id"),
Transform(this, "transform")
{
char name[64];
for (int i = 0; i < 9; i++)
{
sprintf(name, "sh%d", i);
SH[i] = new CVector3Property(this, name);
addAutoRelease(SH[i]);
}
}

CProbeSerializable::CProbeSerializable(CObjectSerializable* parent) :
CObjectSerializable(getTypeName().c_str(), parent),
EntityID(this, "entity_id"),
Transform(this, "transform")
{
char name[64];
for (int i = 0; i < 9; i++)
{
sprintf(name, "sh%d", i);
SH[i] = new CVector3Property(this, name);
addAutoRelease(SH[i]);
}
}

CProbeSerializable::~CProbeSerializable()
{

}
}
47 changes: 47 additions & 0 deletions Projects/Skylicht/Engine/Source/LightProbes/CProbeSerializable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
!@
MIT License
Copyright (c) 2022 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "Serializable/CArraySerializable.h"

namespace Skylicht
{
class CProbeSerializable : public CObjectSerializable
{
public:
CStringProperty EntityID;
CMatrixProperty Transform;
CVector3Property* SH[9];

public:
CProbeSerializable();

CProbeSerializable(CObjectSerializable* parent);

virtual ~CProbeSerializable();

DECLARE_GETTYPENAME(CProbeSerializable);
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
!@
MIT License
Copyright (c) 2022 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#include "pch.h"
#include "CArraySerializable.h"
#include "CValuePropertyTemplate.h"

namespace Skylicht
{
CArraySerializable::CArraySerializable(const char* name) :
CObjectSerializable(name)
{
}

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

CArraySerializable::~CArraySerializable()
{

}

void CArraySerializable::removeElement(CValueProperty* element)
{
std::vector<CValueProperty*>::iterator i = m_value.begin(), e = m_value.end();
while (i != e)
{
if ((*i) == element)
{
m_value.erase(i);
break;
}
++i;
}

i = m_autoRelease.begin();
e = m_autoRelease.end();

while (i != e)
{
if ((*i) == element)
{
m_autoRelease.erase(i);
delete (*i);
break;
}
++i;
}
}
}
62 changes: 62 additions & 0 deletions Projects/Skylicht/Engine/Source/Serializable/CArraySerializable.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
!@
MIT License
Copyright (c) 2022 Skylicht Technology CO., LTD
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify,
merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
This file is part of the "Skylicht Engine".
https://github.com/skylicht-lab/skylicht-engine
!#
*/

#pragma once

#include "CObjectSerializable.h"
#include <functional>

namespace Skylicht
{
class CArraySerializable : public CObjectSerializable
{
public:

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

public:
CArraySerializable(const char* name);

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

virtual ~CArraySerializable();

int getElementCount()
{
return getNumProperty();
}

CValueProperty* getElement(int i)
{
return getPropertyID(i);
}

virtual void removeElement(CValueProperty* element);

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

0 comments on commit 71659eb

Please sign in to comment.