Navigation Menu

Skip to content

Commit

Permalink
#5623: Merge action nodes automatically set the active/inactive state…
Browse files Browse the repository at this point in the history
… of the encapsulated actions when they are inserted into or removed from the scene, respectively.
  • Loading branch information
codereader committed May 29, 2021
1 parent bbd0466 commit 0ebf036
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 5 deletions.
13 changes: 12 additions & 1 deletion include/imergeaction.h
Expand Up @@ -22,6 +22,7 @@ enum class ActionType

/**
* Represents a merge action, i.e. one single step of a merge operation.
* Only active actions will be processed when the merge run starts.
*/
class IMergeAction
{
Expand All @@ -33,7 +34,17 @@ class IMergeAction
// The type performed by this action
virtual ActionType getType() const = 0;

// Applies all changes defined by this action.
// Activate this action, it will be executed during the merge
virtual void activate() = 0;

// Deactivate this action, it will NOT be executed during the merge
virtual void deactivate() = 0;

// Returns the active state of this action
virtual bool isActive() const = 0;

// Applies all changes defined by this action (if it is active,
// deactivated action will not take any effect).
// It's the caller's responsibility to set up any Undo operations.
// Implementations are allowed to throw std::runtime_errors on failure.
virtual void applyChanges() = 0;
Expand Down
26 changes: 25 additions & 1 deletion libs/scene/merge/MergeAction.h
Expand Up @@ -20,9 +20,12 @@ class MergeAction :
private:
ActionType _type;

bool _isActive;

protected:
MergeAction(ActionType type) :
_type(type)
_type(type),
_isActive(true)
{}

public:
Expand All @@ -32,6 +35,21 @@ class MergeAction :
{
return _type;
}

virtual void activate() override
{
_isActive = true;
}

virtual void deactivate() override
{
_isActive = false;
}

virtual bool isActive() const override
{
return _isActive;
}
};

// Various implementations of the above MergeAction base type following
Expand All @@ -58,6 +76,8 @@ class RemoveNodeFromParentAction :

void applyChanges() override
{
if (!isActive()) return;

removeNodeFromParent(_nodeToRemove);
}

Expand Down Expand Up @@ -124,6 +144,8 @@ class AddCloneToParentAction :
public:
void applyChanges() override
{
if (!isActive()) return;

addNodeToContainer(_cloneToBeInserted, _parent);
}

Expand Down Expand Up @@ -184,6 +206,8 @@ class SetEntityKeyValueAction :

void applyChanges() override
{
if (!isActive()) return;

// No post-clone callback since we don't care about selection groups right now
auto entity = Node_getEntity(_node);

Expand Down
2 changes: 1 addition & 1 deletion libs/scene/merge/MergeOperation.h
Expand Up @@ -35,7 +35,7 @@ class MergeOperation

void addAction(const MergeAction::Ptr& action);

// Executes all actions defined in this operation
// Executes all active actions defined in this operation
void applyActions();

void foreachAction(const std::function<void(const MergeAction::Ptr&)>& visitor);
Expand Down
1 change: 1 addition & 0 deletions radiant/ui/merge/MergeControlDialog.h
@@ -1,5 +1,6 @@
#pragma once

#include "iselection.h"
#include <sigc++/connection.h>
#include "wxutil/window/TransientWindow.h"
#include "wxutil/XmlResourceBasedWidget.h"
Expand Down
25 changes: 23 additions & 2 deletions radiantcore/map/Map.cpp
Expand Up @@ -168,10 +168,26 @@ void Map::loadMapResourceFromLocation(const MapLocation& location)

void Map::finishMergeOperation()
{
if (getEditMode() != EditMode::Merge)
{
rWarning() << "Not in merge edit mode, cannot finish any operation" << std::endl;
return;
}

if (!_mergeOperation)
{
rError() << "Cannot merge, no active operation attached to this map." << std::endl;
return;
}

UndoableCommand cmd("mergeMap");
_mergeOperation->applyActions();

cleanupMergeOperation();
setEditMode(EditMode::Normal);
}

void Map::abortMergeOperation()
void Map::cleanupMergeOperation()
{
for (const auto& mergeAction : _mergeActionNodes)
{
Expand All @@ -180,7 +196,12 @@ void Map::abortMergeOperation()

_mergeActionNodes.clear();
_mergeOperation.reset();
setEditMode(EditMode::Normal);
}

void Map::abortMergeOperation()
{
// Remove the nodes and switch back to normal without applying the operation
cleanupMergeOperation();
}

void Map::setMapName(const std::string& newName)
Expand Down
2 changes: 2 additions & 0 deletions radiantcore/map/Map.h
Expand Up @@ -266,6 +266,8 @@ class Map :
void emitMapEvent(MapEvent ev);

void clearMapResource();

void cleanupMergeOperation();
};

} // namespace map
Expand Down
9 changes: 9 additions & 0 deletions radiantcore/map/MergeActionNode.h
Expand Up @@ -44,6 +44,13 @@ class MergeActionNode final :
});
}

void onInsertIntoScene(scene::IMapRootNode& rootNode) override
{
SelectableNode::onInsertIntoScene(rootNode);

_action->activate();
}

void onRemoveFromScene(scene::IMapRootNode& rootNode) override
{
auto addNodeAction = std::dynamic_pointer_cast<scene::merge::AddCloneToParentAction>(_action);
Expand All @@ -62,6 +69,8 @@ class MergeActionNode final :
return true;
});

_action->deactivate();

SelectableNode::onRemoveFromScene(rootNode);
}

Expand Down

0 comments on commit 0ebf036

Please sign in to comment.