| 1 |
#pragma once |
| 2 |
|
| 3 |
#include <memory> |
| 4 |
#include <functional> |
| 5 |
#include <sigc++/signal.h> |
| 6 |
#include "inode.h" |
| 7 |
|
| 8 |
namespace scene |
| 9 |
{ |
| 10 |
|
| 11 |
namespace merge |
| 12 |
{ |
| 13 |
|
| 14 |
enum class ActionType |
| 15 |
{ |
| 16 |
NoAction, |
| 17 |
AddEntity, |
| 18 |
RemoveEntity, |
| 19 |
AddKeyValue, |
| 20 |
RemoveKeyValue, |
| 21 |
ChangeKeyValue, |
| 22 |
AddChildNode, |
| 23 |
RemoveChildNode, |
| 24 |
ConflictResolution, |
| 25 |
}; |
| 26 |
|
| 27 |
enum class ConflictType |
| 28 |
{ |
| 29 |
// Not a conflict |
| 30 |
NoConflict, |
| 31 |
|
| 32 |
// Entity has been removed in target, source tries to modify it |
| 33 |
ModificationOfRemovedEntity, |
| 34 |
|
| 35 |
// Entity has been modified in target, source tries to remove it |
| 36 |
RemovalOfModifiedEntity, |
| 37 |
|
| 38 |
// Key Value has been removed in targed, source tries to change it |
| 39 |
ModificationOfRemovedKeyValue, |
| 40 |
|
| 41 |
// Key Value has been modified in targed, source tries to remove it |
| 42 |
RemovalOfModifiedKeyValue, |
| 43 |
|
| 44 |
// Both sides try to set the same key to a different value |
| 45 |
SettingKeyToDifferentValue, |
| 46 |
}; |
| 47 |
|
| 48 |
enum class ResolutionType |
| 49 |
{ |
| 50 |
Unresolved, |
| 51 |
RejectSourceChange, |
| 52 |
ApplySourceChange, |
| 53 |
}; |
| 54 |
|
| 55 |
/** |
| 56 |
* Represents a merge action, i.e. one single step of a merge operation. |
| 57 |
* Only active actions will be processed when the merge run starts. |
| 58 |
*/ |
| 59 |
class IMergeAction |
| 60 |
{ |
| 61 |
public: |
| 62 |
virtual ~IMergeAction() {} |
| 63 |
|
| 64 |
using Ptr = std::shared_ptr<IMergeAction>; |
| 65 |
|
| 66 |
// The type performed by this action |
| 67 |
virtual ActionType getType() const = 0; |
| 68 |
|
| 69 |
// Activate this action, it will be executed during the merge |
| 70 |
virtual void activate() = 0; |
| 71 |
|
| 72 |
// Deactivate this action, it will NOT be executed during the merge |
| 73 |
virtual void deactivate() = 0; |
| 74 |
|
| 75 |
// Returns the active state of this action |
| 76 |
virtual bool isActive() const = 0; |
| 77 |
|
| 78 |
// Applies all changes defined by this action (if it is active, |
| 79 |
// deactivated action will not take any effect). |
| 80 |
// It's the caller's responsibility to set up any Undo operations. |
| 81 |
// Implementations are allowed to throw std::runtime_errors on failure. |
| 82 |
virtual void applyChanges() = 0; |
| 83 |
|
| 84 |
// Returns the node this action is affecting when applied |
| 85 |
// This is used to identify the scene node and display it appropriately |
| 86 |
virtual scene::INodePtr getAffectedNode() = 0; |
| 87 |
}; |
| 88 |
|
| 89 |
class IEntityKeyValueMergeAction : |
| 90 |
public virtual IMergeAction |
| 91 |
{ |
| 92 |
public: |
| 93 |
virtual ~IEntityKeyValueMergeAction() {} |
| 94 |
|
| 95 |
using Ptr = std::shared_ptr<IEntityKeyValueMergeAction>; |
| 96 |
|
| 97 |
// Gets the key name affected by this action |
| 98 |
virtual const std::string& getKey() const = 0; |
| 99 |
|
| 100 |
// Gets the value that is going to be set by this action |
| 101 |
virtual const std::string& getValue() const = 0; |
| 102 |
|
| 103 |
// The action is usually applying its value as soon as it is inserted into |
| 104 |
// the scene for preview. It remembers the original entity key value, |
| 105 |
// use tis method to retrieve it. |
| 106 |
virtual const std::string& getUnchangedValue() const = 0; |
| 107 |
}; |
| 108 |
|
| 109 |
class IConflictResolutionAction : |
| 110 |
public virtual IMergeAction |
| 111 |
{ |
| 112 |
public: |
| 113 |
virtual ~IConflictResolutionAction() {} |
| 114 |
|
| 115 |
using Ptr = std::shared_ptr<IConflictResolutionAction>; |
| 116 |
|
| 117 |
// The exact conflict type of this node |
| 118 |
virtual ConflictType getConflictType() const = 0; |
| 119 |
|
| 120 |
// Gets the value that is going to be set by this action |
| 121 |
virtual const IMergeAction::Ptr& getSourceAction() const = 0; |
| 122 |
|
| 123 |
// The action that happened in the target (can be empty) |
| 124 |
virtual const IMergeAction::Ptr& getTargetAction() const = 0; |
| 125 |
|
| 126 |
// The source entity node causing the conflict |
| 127 |
virtual const INodePtr& getConflictingSourceEntity() const = 0; |
| 128 |
|
| 129 |
// The affected entity node in the target map |
| 130 |
virtual const INodePtr& getConflictingTargetEntity() const = 0; |
| 131 |
|
| 132 |
// Whether this action has been resolved at all, and what has been chosen |
| 133 |
virtual ResolutionType getResolution() const = 0; |
| 134 |
|
| 135 |
// Resolve this action by either accepting or rejecting the source change |
| 136 |
virtual void setResolution(ResolutionType resolution) = 0; |
| 137 |
}; |
| 138 |
|
| 139 |
// A MergeOperation groups one or more merge actions |
| 140 |
// together in order to apply a set of changes from source => target |
| 141 |
class IMergeOperation |
| 142 |
{ |
| 143 |
public: |
| 144 |
using Ptr = std::shared_ptr<IMergeOperation>; |
| 145 |
|
| 146 |
virtual ~IMergeOperation() {} |
| 147 |
|
| 148 |
// Returns the name/path of the source scene (or a string resembling it) |
| 149 |
virtual std::string getSourcePath() = 0; |
| 150 |
|
| 151 |
// Returns the name/path of the base scene (or a string resembling it) |
| 152 |
// Will return an empty string if there is no base scene defined |
| 153 |
virtual std::string getBasePath() = 0; |
| 154 |
|
| 155 |
// Executes all active actions defined in this operation |
| 156 |
virtual void applyActions() = 0; |
| 157 |
|
| 158 |
// Whether this operation has any actions to perform |
| 159 |
virtual bool hasActions() = 0; |
| 160 |
|
| 161 |
// Adds a new action to this operation |
| 162 |
virtual void addAction(const IMergeAction::Ptr& action) = 0; |
| 163 |
|
| 164 |
// Invokes the given functor for each action in this operation |
| 165 |
virtual void foreachAction(const std::function<void(const IMergeAction::Ptr&)>& visitor) = 0; |
| 166 |
|
| 167 |
// Enables or disable merging of selection groups |
| 168 |
virtual void setMergeSelectionGroups(bool enabled) = 0; |
| 169 |
|
| 170 |
// Enables or disable merging of layers |
| 171 |
virtual void setMergeLayers(bool enabled) = 0; |
| 172 |
|
| 173 |
// Signal which is emitted when an action is added to this operation |
| 174 |
virtual sigc::signal<void, const IMergeAction::Ptr&>& sig_ActionAdded() = 0; |
| 175 |
}; |
| 176 |
|
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Special scene node type representing a change conducted by a merge action, |
| 181 |
* i.e. addition, removal or changing a node in the scene. |
| 182 |
*/ |
| 183 |
class IMergeActionNode : |
| 184 |
public virtual scene::INode |
| 185 |
{ |
| 186 |
public: |
| 187 |
virtual ~IMergeActionNode() {} |
| 188 |
|
| 189 |
// Return the action type represented by this node |
| 190 |
virtual merge::ActionType getActionType() const = 0; |
| 191 |
|
| 192 |
// Return the node this action is affecting |
| 193 |
virtual scene::INodePtr getAffectedNode() = 0; |
| 194 |
|
| 195 |
// The number of merge actions associated to this node. |
| 196 |
// This can be 0 if the node has been cleared out after completing a merge operation |
| 197 |
virtual std::size_t getMergeActionCount() = 0; |
| 198 |
|
| 199 |
// Returns true if this node has one or more active actions. |
| 200 |
// If all associated actions have been deactivated, this returns false. |
| 201 |
virtual bool hasActiveActions() = 0; |
| 202 |
|
| 203 |
// Iterate over all actions of this node |
| 204 |
virtual void foreachMergeAction(const std::function<void(const merge::IMergeAction::Ptr&)>& functor) = 0; |
| 205 |
}; |
| 206 |
|
| 207 |
} |