Skip to content

Commit

Permalink
#5408: Use the passed IMapRootNode to acquire the UndoSystem the node…
Browse files Browse the repository at this point in the history
…s are connecting to
  • Loading branch information
codereader committed Oct 26, 2021
1 parent 1381730 commit 62f57ba
Show file tree
Hide file tree
Showing 34 changed files with 126 additions and 147 deletions.
3 changes: 3 additions & 0 deletions include/iundo.h
Expand Up @@ -50,6 +50,9 @@ class IUndoStateSaver
public:
virtual ~IUndoStateSaver() {}
virtual void save(IUndoable& undoable) = 0;

// Returns the undo system this saver is associated to
virtual IUndoSystem& getUndoSystem() = 0;
};

class IUndoSystem
Expand Down
21 changes: 17 additions & 4 deletions libs/ObservedUndoable.h
Expand Up @@ -33,17 +33,30 @@ class ObservedUndoable :
_debugName(debugName)
{}

void connectUndoSystem()
void connectUndoSystem(IUndoSystem& undoSystem)
{
_undoStateSaver = GlobalUndoSystem().getStateSaver(*this);
_undoStateSaver = undoSystem.getStateSaver(*this);
}

void disconnectUndoSystem()
void disconnectUndoSystem(IUndoSystem& undoSystem)
{
_undoStateSaver = nullptr;
GlobalUndoSystem().releaseStateSaver(*this);
undoSystem.releaseStateSaver(*this);
}

// Returns true if this Undoable is connected to an UndoSystem
bool isConnected() const
{
return _undoStateSaver != nullptr;
}

IUndoSystem& getUndoSystem()
{
if (!_undoStateSaver) throw std::logic_error("ObservedUndoable node connected to any UndoSystem");

return _undoStateSaver->getUndoSystem();
}

void save()
{
if (_undoStateSaver != nullptr)
Expand Down
10 changes: 6 additions & 4 deletions libs/scene/Node.cpp
Expand Up @@ -276,21 +276,23 @@ void Node::onChildRemoved(const INodePtr& child)
void Node::onInsertIntoScene(IMapRootNode& root)
{
_instantiated = true;
connectUndoSystem(root.getUndoSystem());
}

void Node::onRemoveFromScene(IMapRootNode& root)
{
disconnectUndoSystem(root.getUndoSystem());
_instantiated = false;
}

void Node::connectUndoSystem()
void Node::connectUndoSystem(IUndoSystem& undoSystem)
{
_children.connectUndoSystem();
_children.connectUndoSystem(undoSystem);
}

void Node::disconnectUndoSystem()
void Node::disconnectUndoSystem(IUndoSystem& undoSystem)
{
_children.disconnectUndoSystem();
_children.disconnectUndoSystem(undoSystem);
}

TraversableNodeSet& Node::getTraversable() {
Expand Down
8 changes: 5 additions & 3 deletions libs/scene/Node.h
Expand Up @@ -9,6 +9,8 @@
#include "math/Matrix4.h"
#include "generic/callback.h"

class IUndoSystem;

namespace scene
{

Expand Down Expand Up @@ -204,13 +206,13 @@ class Node :

TraversableNodeSet& getTraversable();

virtual void connectUndoSystem();
virtual void disconnectUndoSystem();

// Clears the TraversableNodeSet
virtual void removeAllChildNodes();

private:
void connectUndoSystem(IUndoSystem& undoSystem);
void disconnectUndoSystem(IUndoSystem& undoSystem);

void evaluateBounds() const;
void evaluateChildBounds() const;
void evaluateTransform() const;
Expand Down
18 changes: 7 additions & 11 deletions libs/scene/SelectableNode.cpp
Expand Up @@ -28,7 +28,7 @@ SelectableNode::~SelectableNode()

void SelectableNode::onInsertIntoScene(IMapRootNode& root)
{
connectUndoSystem();
connectUndoSystem(root.getUndoSystem());

Node::onInsertIntoScene(root);

Expand All @@ -51,7 +51,7 @@ void SelectableNode::onRemoveFromScene(IMapRootNode& root)
{
setSelected(false);

disconnectUndoSystem();
disconnectUndoSystem(root.getUndoSystem());

// When a node is removed from the scene with a non-empty group assignment
// we do notify the SelectionGroup to remove ourselves, but we keep the ID list
Expand Down Expand Up @@ -233,19 +233,15 @@ void SelectableNode::onSelectionStatusChange(bool changeGroupStatus)
}
}

void SelectableNode::connectUndoSystem()
void SelectableNode::connectUndoSystem(IUndoSystem& undoSystem)
{
_undoStateSaver = GlobalUndoSystem().getStateSaver(*this);

Node::connectUndoSystem();
_undoStateSaver = undoSystem.getStateSaver(*this);
}

void SelectableNode::disconnectUndoSystem()
void SelectableNode::disconnectUndoSystem(IUndoSystem& undoSystem)
{
_undoStateSaver = nullptr;
GlobalUndoSystem().releaseStateSaver(*this);

Node::disconnectUndoSystem();
_undoStateSaver = nullptr;
undoSystem.releaseStateSaver(*this);
}

void SelectableNode::undoSave()
Expand Down
6 changes: 3 additions & 3 deletions libs/scene/SelectableNode.h
Expand Up @@ -65,10 +65,10 @@ class SelectableNode :
*/
virtual void onSelectionStatusChange(bool changeGroupStatus);

virtual void connectUndoSystem() override;
virtual void disconnectUndoSystem() override;

private:
void connectUndoSystem(IUndoSystem& undoSystem);
void disconnectUndoSystem(IUndoSystem& undoSystem);

void undoSave();
};

Expand Down
8 changes: 4 additions & 4 deletions libs/scene/TraversableNodeSet.cpp
Expand Up @@ -189,15 +189,15 @@ bool TraversableNodeSet::empty() const
return _children.empty();
}

void TraversableNodeSet::connectUndoSystem()
void TraversableNodeSet::connectUndoSystem(IUndoSystem& undoSystem)
{
_undoStateSaver = GlobalUndoSystem().getStateSaver(*this);
_undoStateSaver = undoSystem.getStateSaver(*this);
}

void TraversableNodeSet::disconnectUndoSystem()
void TraversableNodeSet::disconnectUndoSystem(IUndoSystem& undoSystem)
{
_undoStateSaver = nullptr;
GlobalUndoSystem().releaseStateSaver(*this);
undoSystem.releaseStateSaver(*this);
}

void TraversableNodeSet::undoSave()
Expand Down
4 changes: 2 additions & 2 deletions libs/scene/TraversableNodeSet.h
Expand Up @@ -87,8 +87,8 @@ class TraversableNodeSet final :
*/
bool empty() const;

void connectUndoSystem();
void disconnectUndoSystem();
void connectUndoSystem(IUndoSystem& undoSystem);
void disconnectUndoSystem(IUndoSystem& undoSystem);

// Undoable implementation
IUndoMementoPtr exportState() const;
Expand Down
20 changes: 10 additions & 10 deletions radiantcore/brush/Brush.cpp
Expand Up @@ -142,24 +142,24 @@ void Brush::forEachVisibleFace(const std::function<void(Face&)>& functor) const
}
}

void Brush::connectUndoSystem()
void Brush::connectUndoSystem(IUndoSystem& undoSystem)
{
assert(_undoStateSaver == nullptr);

_undoStateSaver = GlobalUndoSystem().getStateSaver(*this);
_undoStateSaver = undoSystem.getStateSaver(*this);

forEachFace([&](Face& face) { face.connectUndoSystem(); });
forEachFace([&](Face& face) { face.connectUndoSystem(undoSystem); });
}

void Brush::disconnectUndoSystem()
void Brush::disconnectUndoSystem(IUndoSystem& undoSystem)
{
assert(_undoStateSaver != nullptr);

// Notify each face
forEachFace([&](Face& face) { face.disconnectUndoSystem(); });
forEachFace([&](Face& face) { face.disconnectUndoSystem(undoSystem); });

_undoStateSaver = nullptr;
GlobalUndoSystem().releaseStateSaver(*this);
undoSystem.releaseStateSaver(*this);
}

void Brush::setShader(const std::string& newShader) {
Expand Down Expand Up @@ -429,7 +429,7 @@ void Brush::push_back(Faces::value_type face) {

if (_undoStateSaver)
{
m_faces.back()->connectUndoSystem();
m_faces.back()->connectUndoSystem(_undoStateSaver->getUndoSystem());
}

for (Observers::iterator i = m_observers.begin(); i != m_observers.end(); ++i) {
Expand All @@ -442,7 +442,7 @@ void Brush::pop_back()
{
if (_undoStateSaver)
{
m_faces.back()->disconnectUndoSystem();
m_faces.back()->disconnectUndoSystem(_undoStateSaver->getUndoSystem());
}

m_faces.pop_back();
Expand All @@ -456,7 +456,7 @@ void Brush::erase(std::size_t index)
{
if (_undoStateSaver)
{
m_faces[index]->disconnectUndoSystem();
m_faces[index]->disconnectUndoSystem(_undoStateSaver->getUndoSystem());
}

m_faces.erase(m_faces.begin() + index);
Expand Down Expand Up @@ -499,7 +499,7 @@ void Brush::clear()
undoSave();
if (_undoStateSaver)
{
forEachFace([&](Face& face) { face.disconnectUndoSystem(); });
forEachFace([&](Face& face) { face.disconnectUndoSystem(_undoStateSaver->getUndoSystem()); });
}

m_faces.clear();
Expand Down
4 changes: 2 additions & 2 deletions radiantcore/brush/Brush.h
Expand Up @@ -176,8 +176,8 @@ class Brush :
// but are forcedly visible due to the brush being selected)
void forEachVisibleFace(const std::function<void(Face&)>& functor) const;

void connectUndoSystem();
void disconnectUndoSystem();
void connectUndoSystem(IUndoSystem& undoSystem);
void disconnectUndoSystem(IUndoSystem& undoSystem);

// Face observer callbacks
void onFacePlaneChanged();
Expand Down
5 changes: 3 additions & 2 deletions radiantcore/brush/BrushNode.cpp
Expand Up @@ -5,6 +5,7 @@
#include "iradiant.h"
#include "icounter.h"
#include "iclipper.h"
#include "imap.h"
#include "ientity.h"
#include "math/Frustum.h"
#include "math/Hash.h"
Expand Down Expand Up @@ -269,7 +270,7 @@ scene::INodePtr BrushNode::clone() const {

void BrushNode::onInsertIntoScene(scene::IMapRootNode& root)
{
m_brush.connectUndoSystem();
m_brush.connectUndoSystem(root.getUndoSystem());
GlobalCounters().getCounter(counterBrushes).increment();

// Update the origin information needed for transformations
Expand All @@ -289,7 +290,7 @@ void BrushNode::onRemoveFromScene(scene::IMapRootNode& root)
setSelectedComponents(false, selection::ComponentSelectionMode::Face);

GlobalCounters().getCounter(counterBrushes).decrement();
m_brush.disconnectUndoSystem();
m_brush.disconnectUndoSystem(root.getUndoSystem());

SelectableNode::onRemoveFromScene(root);
}
Expand Down
8 changes: 4 additions & 4 deletions radiantcore/brush/Face.cpp
Expand Up @@ -146,20 +146,20 @@ void Face::realiseShader()
_owner.onFaceShaderChanged();
}

void Face::connectUndoSystem()
void Face::connectUndoSystem(IUndoSystem& undoSystem)
{
assert(!_undoStateSaver);

_shader.setInUse(true);

_undoStateSaver = GlobalUndoSystem().getStateSaver(*this);
_undoStateSaver = undoSystem.getStateSaver(*this);
}

void Face::disconnectUndoSystem()
void Face::disconnectUndoSystem(IUndoSystem& undoSystem)
{
assert(_undoStateSaver);
_undoStateSaver = nullptr;
GlobalUndoSystem().releaseStateSaver(*this);
undoSystem.releaseStateSaver(*this);

_shader.setInUse(false);
}
Expand Down
4 changes: 2 additions & 2 deletions radiantcore/brush/Face.h
Expand Up @@ -86,8 +86,8 @@ class Face :
// greebo: Emits the updated normals to the Winding class.
void updateWinding();

void connectUndoSystem();
void disconnectUndoSystem();
void connectUndoSystem(IUndoSystem& undoSystem);
void disconnectUndoSystem(IUndoSystem& undoSystem);

void undoSave();

Expand Down
9 changes: 5 additions & 4 deletions radiantcore/entity/EntityNode.cpp
Expand Up @@ -4,6 +4,7 @@
#include "itextstream.h"
#include "icounter.h"
#include "imodel.h"
#include "imap.h"
#include "itransformable.h"
#include "math/Hash.h"
#include "string/case_conv.h"
Expand Down Expand Up @@ -295,8 +296,8 @@ void EntityNode::onInsertIntoScene(scene::IMapRootNode& root)
{
GlobalCounters().getCounter(counterEntities).increment();

_spawnArgs.connectUndoSystem();
_modelKey.connectUndoSystem();
_spawnArgs.connectUndoSystem(root.getUndoSystem());
_modelKey.connectUndoSystem(root.getUndoSystem());

SelectableNode::onInsertIntoScene(root);
TargetableNode::onInsertIntoScene(root);
Expand All @@ -307,8 +308,8 @@ void EntityNode::onRemoveFromScene(scene::IMapRootNode& root)
TargetableNode::onRemoveFromScene(root);
SelectableNode::onRemoveFromScene(root);

_modelKey.disconnectUndoSystem();
_spawnArgs.disconnectUndoSystem();
_modelKey.disconnectUndoSystem(root.getUndoSystem());
_spawnArgs.disconnectUndoSystem(root.getUndoSystem());

GlobalCounters().getCounter(counterEntities).decrement();
}
Expand Down
8 changes: 4 additions & 4 deletions radiantcore/entity/KeyValue.cpp
Expand Up @@ -19,14 +19,14 @@ KeyValue::~KeyValue()
assert(_observers.empty());
}

void KeyValue::connectUndoSystem()
void KeyValue::connectUndoSystem(IUndoSystem& undoSystem)
{
_undo.connectUndoSystem();
_undo.connectUndoSystem(undoSystem);
}

void KeyValue::disconnectUndoSystem()
void KeyValue::disconnectUndoSystem(IUndoSystem& undoSystem)
{
_undo.disconnectUndoSystem();
_undo.disconnectUndoSystem(undoSystem);
}

void KeyValue::attach(KeyObserver& observer)
Expand Down
4 changes: 2 additions & 2 deletions radiantcore/entity/KeyValue.h
Expand Up @@ -41,8 +41,8 @@ class KeyValue final :

~KeyValue();

void connectUndoSystem();
void disconnectUndoSystem();
void connectUndoSystem(IUndoSystem& undoSystem);
void disconnectUndoSystem(IUndoSystem& undoSystem);

void attach(KeyObserver& observer);
void detach(KeyObserver& observer);
Expand Down

0 comments on commit 62f57ba

Please sign in to comment.