Skip to content

Commit

Permalink
Save target object name for Undo/Redo and use safe UID instead of Obj…
Browse files Browse the repository at this point in the history
…ect*
  • Loading branch information
Benualdo committed Sep 30, 2024
1 parent 0b773b5 commit a51b97e
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 45 deletions.
2 changes: 2 additions & 0 deletions src/core/IUndoRedo.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ namespace vg::core

virtual void AddSubEntry(IUndoRedoEntry * _subEntry) = 0;
virtual const vector<IUndoRedoEntry *> * GetSubEntries() const = 0;

virtual IObject * GetObject() const = 0;
};

struct UndoRedoTarget
Expand Down
3 changes: 0 additions & 3 deletions src/core/Object/Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,6 @@ namespace vg::core
{
IFactory * factory = Kernel::getFactory();

//if (!m_originalUID)
// m_originalUID = m_uid;

SetUID(factory->RegisterUID(this));
return HasValidUID();
}
Expand Down
5 changes: 2 additions & 3 deletions src/core/UndoRedo/Entry/UndoRedoPropertyEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ namespace vg::core
//--------------------------------------------------------------------------------------
class UndoRedoPropertyEntry final : public UndoRedoEntry
{
using super = UndoRedoEntry;

public:
UndoRedoPropertyEntry(IObject * _object, const IProperty * _prop, IObject * _originalObject = nullptr, IGameObject * _prefab = nullptr, IDynamicProperty * _propOverride = nullptr);

Expand All @@ -31,10 +33,7 @@ namespace vg::core
io::Buffer m_original;
io::Buffer m_modified;

IObject * m_object;
IProperty * m_prop;

IObject * m_originalObject;
IGameObject * m_prefab;
IDynamicProperty * m_propOverride;
};
Expand Down
72 changes: 43 additions & 29 deletions src/core/UndoRedo/Entry/UndoRedoPropertyEntry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
namespace vg::core
{
//--------------------------------------------------------------------------------------
UndoRedoPropertyEntry::UndoRedoPropertyEntry(IObject * _object, const IProperty * _prop, IObject * _originalObject, IGameObject * _prefab, IDynamicProperty * _propOverride)
UndoRedoPropertyEntry::UndoRedoPropertyEntry(IObject * _object, const IProperty * _prop, IObject * _originalObject, IGameObject * _prefab, IDynamicProperty * _propOverride) :
UndoRedoEntry(_object)
{
m_object = _object;
VG_ASSERT(m_object);
if (nullptr != _originalObject)
m_objectName = _originalObject->GetName();

m_prop = (IProperty *)_prop;
VG_ASSERT(m_prop);

m_originalObject = _originalObject;
m_prefab = _prefab;
m_propOverride = _propOverride;
}
Expand All @@ -24,14 +24,14 @@ namespace vg::core
void UndoRedoPropertyEntry::BeforeChange()
{
Factory * factory = (Factory *)Kernel::getFactory();
factory->serializePropertyToMemory(m_object, m_prop, m_original);
factory->serializePropertyToMemory(GetObject(), m_prop, m_original);
}

//--------------------------------------------------------------------------------------
void UndoRedoPropertyEntry::AfterChange()
{
Factory * factory = (Factory *)Kernel::getFactory();
factory->serializePropertyToMemory(m_object, m_prop, m_modified);
factory->serializePropertyToMemory(GetObject(), m_prop, m_modified);
}

//--------------------------------------------------------------------------------------
Expand All @@ -43,7 +43,7 @@ namespace vg::core
//--------------------------------------------------------------------------------------
string UndoRedoPropertyEntry::GetObjectName() const
{
return m_object->GetName();
return super::GetObjectName();
}

//--------------------------------------------------------------------------------------
Expand All @@ -55,44 +55,58 @@ namespace vg::core
//--------------------------------------------------------------------------------------
void UndoRedoPropertyEntry::Undo()
{
// Restore the original value serialized in memory before change
VG_INFO("[Undo/Redo] Undo Property \"%s\" (0x%016X) from Object \"%s\" (0x%016X)", m_prop->GetName(), m_prop, m_originalObject? m_originalObject->GetName().c_str() : m_object->GetName().c_str(), m_object);
if (auto * object = GetObject())
{
// Restore the original value serialized in memory before change
VG_INFO("[Undo/Redo] Undo Property \"%s\" (0x%016X) from Object \"%s\" (0x%016X)", m_prop->GetName(), m_prop, m_objectName.c_str(), object);

m_original.resetRead();
Factory * factory = (Factory *)Kernel::getFactory();
factory->serializePropertyFromMemory(m_object, m_prop, m_original);
m_original.resetRead();
Factory * factory = (Factory *)Kernel::getFactory();
factory->serializePropertyFromMemory(object, m_prop, m_original);

if (m_prefab)
{
auto & children = m_prefab->GetChildren();
for (uint i = 0; i < children.size(); ++i)
m_prefab->OverrideGameObjectProperties(children[i], m_propOverride);
if (m_prefab)
{
auto & children = m_prefab->GetChildren();
for (uint i = 0; i < children.size(); ++i)
m_prefab->OverrideGameObjectProperties(children[i], m_propOverride);
}
else
{
object->OnPropertyChanged(object, *m_prop);
}
}
else
{
m_object->OnPropertyChanged(m_object, *m_prop);
VG_ERROR("[Undo/Redo] Cannot Undo Property \"%s\" (0x%016X) from Object \"%s\" (UID=0x%08X)", m_prop->GetName(), m_prop, m_objectName.c_str(), m_objectUID);
}
}

//--------------------------------------------------------------------------------------
void UndoRedoPropertyEntry::Redo()
{
// Restore the modified value serialized in memory before change
VG_INFO("[Undo/Redo] Redo Property \"%s\" (0x%016X) from Object \"%s\" (0x%016X)", m_prop->GetName(), m_prop, m_originalObject ? m_originalObject->GetName().c_str() : m_object->GetName().c_str(), m_object);
if (auto * object = GetObject())
{
// Restore the modified value serialized in memory before change
VG_INFO("[Undo/Redo] Redo Property \"%s\" (0x%016X) from Object \"%s\" (0x%016X)", m_prop->GetName(), m_prop, m_objectName.c_str(), object);

m_modified.resetRead();
Factory * factory = (Factory *)Kernel::getFactory();
factory->serializePropertyFromMemory(m_object, m_prop, m_modified);
m_modified.resetRead();
Factory * factory = (Factory *)Kernel::getFactory();
factory->serializePropertyFromMemory(object, m_prop, m_modified);

if (m_prefab)
{
auto & children = m_prefab->GetChildren();
for (uint i = 0; i < children.size(); ++i)
m_prefab->OverrideGameObjectProperties(children[i], m_propOverride);
if (m_prefab)
{
auto & children = m_prefab->GetChildren();
for (uint i = 0; i < children.size(); ++i)
m_prefab->OverrideGameObjectProperties(children[i], m_propOverride);
}
else
{
object->OnPropertyChanged(object, *m_prop);
}
}
else
{
m_object->OnPropertyChanged(m_object, *m_prop);
VG_ERROR("[Undo/Redo] Cannot Redo Property \"%s\" (0x%016X) from Object \"%s\" (UID=0x%08X)", m_prop->GetName(), m_prop, m_objectName.c_str(), m_objectUID);
}
}
}
15 changes: 11 additions & 4 deletions src/core/UndoRedo/UndoRedoEntry.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ namespace vg::core
class UndoRedoEntry : public IUndoRedoEntry
{
public:
UndoRedoEntry() {}
~UndoRedoEntry() {}
UndoRedoEntry(IObject * _object);
~UndoRedoEntry();

void BeforeChange() override = 0;
void AfterChange() override = 0;
Expand All @@ -21,7 +21,14 @@ namespace vg::core
string GetEntryName() const override = 0;
string GetDescription() const override = 0;

void AddSubEntry(IUndoRedoEntry * _subEntry) final override { VG_ASSERT(false); }
const vector<IUndoRedoEntry *> * GetSubEntries() const final override { return nullptr; }
void AddSubEntry(IUndoRedoEntry * _subEntry) override { VG_ASSERT(false); }
const vector<IUndoRedoEntry *> * GetSubEntries() const override { return nullptr; }

IObject * GetObject() const override;
string GetObjectName() const override;

protected:
UID m_objectUID;
string m_objectName;
};
}
29 changes: 29 additions & 0 deletions src/core/UndoRedo/UndoRedoEntry.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,34 @@

namespace vg::core
{
//--------------------------------------------------------------------------------------
UndoRedoEntry::UndoRedoEntry(IObject * _object)
{
if (nullptr != _object)
{
m_objectUID = _object->GetUID();
VG_ASSERT(m_objectUID);

m_objectName = _object->GetName();
}
}

//--------------------------------------------------------------------------------------
UndoRedoEntry::~UndoRedoEntry()
{

}

//--------------------------------------------------------------------------------------
IObject * UndoRedoEntry::GetObject() const
{
IFactory * factory = Kernel::getFactory();
return factory->FindByUID(m_objectUID);
}

//--------------------------------------------------------------------------------------
string UndoRedoEntry::GetObjectName() const
{
return m_objectName;
}
}
6 changes: 3 additions & 3 deletions src/core/UndoRedo/UndoRedoEntryGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ namespace vg::core
class IProperty;
class IDynamicProperty;

class UndoRedoEntryGroup : public IUndoRedoEntry
class UndoRedoEntryGroup : public UndoRedoEntry
{
public:
UndoRedoEntryGroup(const string & _name);
UndoRedoEntryGroup(const string & _entryName);
~UndoRedoEntryGroup();

void BeforeChange() override;
Expand All @@ -26,7 +26,7 @@ namespace vg::core
const vector<IUndoRedoEntry *> * GetSubEntries() const final override;

private:
string m_name;
string m_entryName;
core::vector<IUndoRedoEntry *> m_subEntries;
};
}
7 changes: 4 additions & 3 deletions src/core/UndoRedo/UndoRedoEntryGroup.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ namespace vg::core
{
//--------------------------------------------------------------------------------------
UndoRedoEntryGroup::UndoRedoEntryGroup(const string & _name) :
m_name(_name)
UndoRedoEntry(nullptr),
m_entryName(_name)
{

}

//--------------------------------------------------------------------------------------
Expand Down Expand Up @@ -56,7 +57,7 @@ namespace vg::core
//--------------------------------------------------------------------------------------
string UndoRedoEntryGroup::GetEntryName() const
{
return m_name;
return m_entryName;
}

//--------------------------------------------------------------------------------------
Expand Down

0 comments on commit a51b97e

Please sign in to comment.