Skip to content

Commit

Permalink
Remove BrushFaceReferenceException
Browse files Browse the repository at this point in the history
  • Loading branch information
kduske committed Sep 26, 2023
1 parent bdc1027 commit cec054e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
6 changes: 0 additions & 6 deletions common/src/Exceptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@ class Exception : public std::exception
const char* what() const noexcept override;
};

class BrushFaceReferenceException : public Exception
{
public:
using Exception::Exception;
};

class EntityAttributeException : public Exception
{
public:
Expand Down
19 changes: 10 additions & 9 deletions common/src/Model/BrushFaceReference.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@

#include "BrushFaceReference.h"

#include "Exceptions.h"
#include "Error.h"
#include "Model/Brush.h"
#include "Model/BrushFace.h"
#include "Model/BrushNode.h"

#include <kdl/result.h>
#include <kdl/result_fold.h>
#include <kdl/vector_utils.h>

#include <vecmath/plane_io.h>

#include <cassert>

namespace TrenchBroom::Model
Expand All @@ -37,16 +41,13 @@ BrushFaceReference::BrushFaceReference(BrushNode* node, const BrushFace& face)
assert(m_node != nullptr);
}

BrushFaceHandle BrushFaceReference::resolve() const
Result<BrushFaceHandle> BrushFaceReference::resolve() const
{
if (const auto faceIndex = m_node->brush().findFace(m_facePlane))
{
return BrushFaceHandle(m_node, *faceIndex);
}
else
{
throw BrushFaceReferenceException();
}
return Error{"Cannot resolve brush face reference"};
}

std::vector<BrushFaceReference> createRefs(const std::vector<BrushFaceHandle>& handles)
Expand All @@ -56,10 +57,10 @@ std::vector<BrushFaceReference> createRefs(const std::vector<BrushFaceHandle>& h
});
}

std::vector<BrushFaceHandle> resolveAllRefs(
Result<std::vector<BrushFaceHandle>> resolveAllRefs(
const std::vector<BrushFaceReference>& faceRefs)
{
return kdl::vec_transform(
faceRefs, [](const auto& faceRef) { return faceRef.resolve(); });
return kdl::fold_results(
kdl::vec_transform(faceRefs, [](const auto& faceRef) { return faceRef.resolve(); }));
}
} // namespace TrenchBroom::Model
14 changes: 5 additions & 9 deletions common/src/Model/BrushFaceReference.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "FloatType.h"
#include "Model/BrushFaceHandle.h"
#include "Result.h"

#include <vecmath/plane.h>

Expand Down Expand Up @@ -55,11 +56,9 @@ class BrushFaceReference
BrushFaceReference(Model::BrushNode* node, const Model::BrushFace& face);

/**
* Resolves the referenced brush face.
*
* @throws BrushFaceReferenceException if the face cannot be resolved
* Resolves the referenced brush face or an error if this reference cannot be resolved.
*/
BrushFaceHandle resolve() const;
Result<BrushFaceHandle> resolve() const;
};

/**
Expand All @@ -69,12 +68,9 @@ std::vector<BrushFaceReference> createRefs(const std::vector<BrushFaceHandle>& h

/**
* Returns a vector brush face handles representing the faces to which the given face
* references are resolved.
*
* @throws BrushFAceReferenceException if any of the given face references cannot be
* resolved
* references are resolved or an error if any reference cannot be resolved.
*/
std::vector<BrushFaceHandle> resolveAllRefs(
Result<std::vector<BrushFaceHandle>> resolveAllRefs(
const std::vector<BrushFaceReference>& faceRefs);

} // namespace TrenchBroom::Model
37 changes: 25 additions & 12 deletions common/src/View/SelectionCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "SelectionCommand.h"

#include "Ensure.h"
#include "Error.h"
#include "Macros.h"
#include "Model/BrushFace.h"
#include "Model/BrushFaceHandle.h"
Expand All @@ -29,6 +30,7 @@
#include "Model/WorldNode.h"
#include "View/MapDocumentCommandFacade.h"

#include <kdl/result.h>
#include <kdl/string_format.h>
#include <kdl/vector_utils.h>

Expand Down Expand Up @@ -159,30 +161,37 @@ std::unique_ptr<CommandResult> SelectionCommand::doPerformDo(
{
case Action::SelectNodes:
document->performSelect(m_nodes);
break;
return std::make_unique<CommandResult>(true);
case Action::SelectFaces:
document->performSelect(Model::resolveAllRefs(m_faceRefs));
break;
return std::make_unique<CommandResult>(
Model::resolveAllRefs(m_faceRefs)
.transform([&](const auto& faceHandles) { document->performSelect(faceHandles); })
.transform_error([&](const auto& e) { document->error() << e.msg; })
.is_success());
case Action::SelectAllNodes:
document->performSelectAllNodes();
break;
return std::make_unique<CommandResult>(true);
case Action::SelectAllFaces:
document->performSelectAllBrushFaces();
break;
return std::make_unique<CommandResult>(true);
case Action::ConvertToFaces:
document->performConvertToBrushFaceSelection();
break;
return std::make_unique<CommandResult>(true);
case Action::DeselectNodes:
document->performDeselect(m_nodes);
break;
return std::make_unique<CommandResult>(true);
case Action::DeselectFaces:
document->performDeselect(Model::resolveAllRefs(m_faceRefs));
break;
return std::make_unique<CommandResult>(
Model::resolveAllRefs(m_faceRefs)
.transform(
[&](const auto& faceHandles) { document->performDeselect(faceHandles); })
.transform_error([&](const auto& e) { document->error() << e.msg; })
.is_success());
case Action::DeselectAll:
document->performDeselectAll();
break;
return std::make_unique<CommandResult>(true);
switchDefault();
}
return std::make_unique<CommandResult>(true);
}

std::unique_ptr<CommandResult> SelectionCommand::doPerformUndo(
Expand All @@ -195,7 +204,11 @@ std::unique_ptr<CommandResult> SelectionCommand::doPerformUndo(
}
if (!m_previouslySelectedFaceRefs.empty())
{
document->performSelect(Model::resolveAllRefs(m_previouslySelectedFaceRefs));
return std::make_unique<CommandResult>(
Model::resolveAllRefs(m_previouslySelectedFaceRefs)
.transform([&](const auto& faceHandles) { document->performSelect(faceHandles); })
.transform_error([&](const auto& e) { document->error() << e.msg; })
.is_success());
}
return std::make_unique<CommandResult>(true);
}
Expand Down

0 comments on commit cec054e

Please sign in to comment.