Skip to content

Commit

Permalink
1961: Restore undo collation for vertex operations (TrenchBroom#1962)
Browse files Browse the repository at this point in the history
* 1961: Restore undo collation for vertex commands.

* 1961: Minor cleanup.
  • Loading branch information
kduske authored Jan 9, 2018
1 parent 20c49d4 commit 05e49a4
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 18 deletions.
7 changes: 6 additions & 1 deletion common/src/View/MapDocumentCommandFacade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ namespace TrenchBroom {

invalidateSelectionBounds();

VectorUtils::sortAndRemoveDuplicates(newVertexPositions);
return newVertexPositions;
}

Expand All @@ -744,7 +745,10 @@ namespace TrenchBroom {
const Edge3::List newPositions = brush->moveEdges(m_worldBounds, oldPositions, delta);
VectorUtils::append(newEdgePositions, newPositions);
}


invalidateSelectionBounds();

VectorUtils::sortAndRemoveDuplicates(newEdgePositions);
return newEdgePositions;
}

Expand All @@ -765,6 +769,7 @@ namespace TrenchBroom {

invalidateSelectionBounds();

VectorUtils::sortAndRemoveDuplicates(newFacePositions);
return newFacePositions;
}

Expand Down
12 changes: 9 additions & 3 deletions common/src/View/MoveBrushEdgesCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,15 @@ namespace TrenchBroom {
}

bool MoveBrushEdgesCommand::doCollateWith(UndoableCommand::Ptr command) {
// Don't collate vertex moves. Collation changes the path along which the vertices are moved, and as a result
// changes the outcome of the entire operation.
return false;
MoveBrushEdgesCommand* other = static_cast<MoveBrushEdgesCommand*>(command.get());

if (!VectorUtils::equals(m_newEdgePositions, other->m_oldEdgePositions))
return false;

m_newEdgePositions = other->m_newEdgePositions;
m_delta += other->m_delta;

return true;
}

void MoveBrushEdgesCommand::doSelectNewHandlePositions(VertexHandleManagerBaseT<Edge3>& manager) const {
Expand Down
12 changes: 9 additions & 3 deletions common/src/View/MoveBrushFacesCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,15 @@ namespace TrenchBroom {
}

bool MoveBrushFacesCommand::doCollateWith(UndoableCommand::Ptr command) {
// Don't collate vertex moves. Collation changes the path along which the vertices are moved, and as a result
// changes the outcome of the entire operation.
return false;
MoveBrushFacesCommand* other = static_cast<MoveBrushFacesCommand*>(command.get());

if (!VectorUtils::equals(m_newFacePositions, other->m_oldFacePositions))
return false;

m_newFacePositions = other->m_newFacePositions;
m_delta += other->m_delta;

return true;
}


Expand Down
12 changes: 9 additions & 3 deletions common/src/View/MoveBrushVerticesCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,15 @@ namespace TrenchBroom {
}

bool MoveBrushVerticesCommand::doCollateWith(UndoableCommand::Ptr command) {
// Don't collate vertex moves. Collation changes the path along which the vertices are moved, and as a result
// changes the outcome of the entire operation.
return false;
MoveBrushVerticesCommand* other = static_cast<MoveBrushVerticesCommand*>(command.get());

if (!VectorUtils::equals(m_newVertexPositions, other->m_oldVertexPositions))
return false;

m_newVertexPositions = other->m_newVertexPositions;
m_delta += other->m_delta;

return true;
}

void MoveBrushVerticesCommand::doSelectNewHandlePositions(VertexHandleManagerBaseT<Vec3>& manager) const {
Expand Down
36 changes: 28 additions & 8 deletions common/src/View/VertexCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,40 @@ namespace TrenchBroom {
}

bool VertexCommand::doPerformDo(MapDocumentCommandFacade* document) {
if (!doCanDoVertexOperation(document))
return false;

takeSnapshot();
return doVertexOperation(document);
if (m_snapshot != nullptr) {
restoreAndTakeNewSnapshot(document);
return true;
} else {
if (!doCanDoVertexOperation(document))
return false;

takeSnapshot();
return doVertexOperation(document);
}
}

bool VertexCommand::doPerformUndo(MapDocumentCommandFacade* document) {
ensure(m_snapshot != nullptr, "snapshot is null");
document->restoreSnapshot(m_snapshot);
deleteSnapshot();
restoreAndTakeNewSnapshot(document);
return true;
}

void VertexCommand::restoreAndTakeNewSnapshot(MapDocumentCommandFacade* document) {
ensure(m_snapshot != nullptr, "snapshot is null");

Model::Snapshot *snapshot = nullptr;
try {
using std::swap;
swap(m_snapshot, snapshot);
takeSnapshot();

document->restoreSnapshot(snapshot);
delete snapshot;
} catch (...) {
delete snapshot;
throw;
}
}

bool VertexCommand::doIsRepeatable(MapDocumentCommandFacade* document) const {
return false;
}
Expand Down
1 change: 1 addition & 0 deletions common/src/View/VertexCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ namespace TrenchBroom {
private:
bool doPerformDo(MapDocumentCommandFacade* document);
bool doPerformUndo(MapDocumentCommandFacade* document);
void restoreAndTakeNewSnapshot(MapDocumentCommandFacade* document);
bool doIsRepeatable(MapDocumentCommandFacade* document) const;
private:
void takeSnapshot();
Expand Down

0 comments on commit 05e49a4

Please sign in to comment.