Skip to content

Commit

Permalink
#5408: Replace the second occurrence of GlobalUndoSystem() event subs…
Browse files Browse the repository at this point in the history
…criptions in the entity KeyValue class.
  • Loading branch information
codereader committed Oct 26, 2021
1 parent 7c99e2c commit cbfae9f
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 29 deletions.
24 changes: 17 additions & 7 deletions libs/ObservedUndoable.h
Expand Up @@ -12,23 +12,25 @@ class ObservedUndoable :
public IUndoable
{
typedef std::function<void (const Copyable&)> ImportCallback;
typedef std::function<void()> RestoreFinishedCallback;

Copyable& _object;
ImportCallback _importCallback;
RestoreFinishedCallback _finishedCallback;
IUndoStateSaver* _undoStateSaver;

std::string _debugName;

public:
ObservedUndoable<Copyable>(Copyable& object, const ImportCallback& importCallback) :
_object(object),
_importCallback(importCallback),
_undoStateSaver(nullptr)
ObservedUndoable<Copyable>(Copyable& object, const ImportCallback& importCallback) :
ObservedUndoable(object, importCallback, RestoreFinishedCallback(), "")
{}

ObservedUndoable<Copyable>(Copyable& object, const ImportCallback& importCallback, const std::string& debugName) :
ObservedUndoable<Copyable>(Copyable& object, const ImportCallback& importCallback,
const RestoreFinishedCallback& finishedCallback, const std::string& debugName) :
_object(object),
_importCallback(importCallback),
_finishedCallback(finishedCallback),
_undoStateSaver(nullptr),
_debugName(debugName)
{}
Expand Down Expand Up @@ -65,17 +67,25 @@ class ObservedUndoable :
}
}

IUndoMementoPtr exportState() const
IUndoMementoPtr exportState() const override
{
return IUndoMementoPtr(new BasicUndoMemento<Copyable>(_object));
}

void importState(const IUndoMementoPtr& state)
void importState(const IUndoMementoPtr& state) override
{
save();

_importCallback(std::static_pointer_cast<BasicUndoMemento<Copyable> >(state)->data());
}

void onOperationRestored() override
{
if (_finishedCallback)
{
_finishedCallback();
}
}
};

} // namespace
11 changes: 2 additions & 9 deletions radiantcore/entity/KeyValue.cpp
Expand Up @@ -10,7 +10,8 @@ KeyValue::KeyValue(const std::string& value, const std::string& empty,
const std::function<void(const std::string&)>& valueChanged) :
_value(value),
_emptyValue(empty),
_undo(_value, std::bind(&KeyValue::importState, this, std::placeholders::_1), "KeyValue"),
_undo(_value, std::bind(&KeyValue::importState, this, std::placeholders::_1),
std::bind(&KeyValue::onUndoRedoOperationFinished, this), "KeyValue"),
_valueChanged(valueChanged)
{}

Expand Down Expand Up @@ -83,19 +84,11 @@ void KeyValue::notify()
void KeyValue::importState(const std::string& string)
{
// We notify our observers after the entire undo rollback is done
_undoHandler = GlobalUndoSystem().signal_postUndo().connect(
sigc::mem_fun(this, &KeyValue::onUndoRedoOperationFinished));
_redoHandler = GlobalUndoSystem().signal_postRedo().connect(
sigc::mem_fun(this, &KeyValue::onUndoRedoOperationFinished));

_value = string;
}

void KeyValue::onUndoRedoOperationFinished()
{
_undoHandler.disconnect();
_redoHandler.disconnect();

notify();
}

Expand Down
18 changes: 7 additions & 11 deletions radiantcore/entity/KeyValue.h
Expand Up @@ -4,21 +4,19 @@
#include "ObservedUndoable.h"
#include "string/string.h"
#include <vector>
#include <sigc++/connection.h>
#include <sigc++/trackable.h>

namespace entity
{

class SpawnArgs;

/// \brief A key/value pair of strings.
/// \brief
/// Represents the string value of an entity key.
///
/// - Notifies observers when value changes - value changes to "" on destruction.
/// - Provides undo support through the global undo system.
/// - Provides undo support through the map's undo system.
class KeyValue final :
public EntityKeyValue,
public sigc::trackable
public EntityKeyValue
{
private:
typedef std::vector<KeyObserver*> KeyObservers;
Expand All @@ -27,8 +25,6 @@ class KeyValue final :
std::string _value;
std::string _emptyValue;
undo::ObservedUndoable<std::string> _undo;
sigc::connection _undoHandler;
sigc::connection _redoHandler;

// This is a specialised callback pointing to the owning SpawnArgs
std::function<void(const std::string&)> _valueChanged;
Expand Down Expand Up @@ -60,9 +56,9 @@ class KeyValue final :
void onNameChange(const std::string& oldName, const std::string& newName);

private:
// Gets called after a undo/redo operation is fully completed.
// This triggers a keyobserver refresh, to allow for reconnection to Namespaces and such.
void onUndoRedoOperationFinished();
// Gets called after a undo/redo operation is fully completed.
// This triggers a keyobserver refresh, to allow for reconnection to Namespaces and such.
void onUndoRedoOperationFinished();
};

} // namespace entity
6 changes: 4 additions & 2 deletions radiantcore/entity/SpawnArgs.cpp
Expand Up @@ -10,7 +10,8 @@ namespace entity

SpawnArgs::SpawnArgs(const IEntityClassPtr& eclass) :
_eclass(eclass),
_undo(_keyValues, std::bind(&SpawnArgs::importState, this, std::placeholders::_1), "EntityKeyValues"),
_undo(_keyValues, std::bind(&SpawnArgs::importState, this, std::placeholders::_1),
std::function<void()>(), "EntityKeyValues"),
_observerMutex(false),
_isContainer(!eclass->isFixedSize()),
_attachments(eclass->getName())
Expand All @@ -22,7 +23,8 @@ SpawnArgs::SpawnArgs(const IEntityClassPtr& eclass) :
SpawnArgs::SpawnArgs(const SpawnArgs& other) :
Entity(other),
_eclass(other.getEntityClass()),
_undo(_keyValues, std::bind(&SpawnArgs::importState, this, std::placeholders::_1), "EntityKeyValues"),
_undo(_keyValues, std::bind(&SpawnArgs::importState, this, std::placeholders::_1),
std::function<void()>(), "EntityKeyValues"),
_observerMutex(false),
_isContainer(other._isContainer),
_attachments(other._attachments)
Expand Down

0 comments on commit cbfae9f

Please sign in to comment.