Skip to content

Commit

Permalink
#5746: Implement "Merge selected" for patch vertices
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Sep 26, 2021
1 parent d5e40d2 commit 2b76d72
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 12 deletions.
1 change: 0 additions & 1 deletion radiant/textool/TexTool.cpp
Expand Up @@ -882,7 +882,6 @@ void TexTool::selectRelated(const cmd::ArgumentList& args) {
void TexTool::registerCommands()
{
GlobalCommandSystem().addCommand("TextureTool", TexTool::toggle);
GlobalCommandSystem().addCommand("TexToolMergeItems", TexTool::texToolMergeItems);
GlobalCommandSystem().addCommand("TexToolFlipS", TexTool::texToolFlipS);
GlobalCommandSystem().addCommand("TexToolFlipT", TexTool::texToolFlipT);

Expand Down
10 changes: 9 additions & 1 deletion radiantcore/selection/textool/PatchNode.h
Expand Up @@ -196,7 +196,15 @@ class PatchNode :

void mergeComponentsWith(const Vector2& center) override
{
// TODO
for (auto& vertex : _vertices)
{
if (vertex.isSelected())
{
vertex.getTexcoord() = center;
}
}

_patch.updateTesselation(true);
}

private:
Expand Down
47 changes: 46 additions & 1 deletion radiantcore/selection/textool/TextureToolSelectionSystem.cpp
Expand Up @@ -48,6 +48,9 @@ void TextureToolSelectionSystem::initialiseModule(const IApplicationContext& ctx
std::bind(&TextureToolSelectionSystem::selectRelatedCmd, this, std::placeholders::_1));
GlobalCommandSystem().addCommand("TexToolSnapToGrid",
std::bind(&TextureToolSelectionSystem::snapSelectionToGridCmd, this, std::placeholders::_1));
GlobalCommandSystem().addCommand("TexToolMergeItems",
std::bind(&TextureToolSelectionSystem::mergeSelectionCmd, this, std::placeholders::_1),
{ cmd::ARGTYPE_VECTOR2 | cmd::ARGTYPE_OPTIONAL });

_unselectListener = GlobalRadiantCore().getMessageBus().addListener(
radiant::IMessage::Type::UnselectSelectionRequest,
Expand Down Expand Up @@ -574,7 +577,6 @@ void TextureToolSelectionSystem::snapSelectionToGridCmd(const cmd::ArgumentList&
{
UndoableCommand cmd("snapTexcoordsToGrid");

// Accumulate all selected nodes in a copied list, we're going to alter the selection
foreachSelectedNodeOfAnyType([&](const INode::Ptr& node)
{
node->beginTransformation();
Expand All @@ -600,6 +602,49 @@ void TextureToolSelectionSystem::snapSelectionToGridCmd(const cmd::ArgumentList&
radiant::TextureChangedMessage::Send();
}

void TextureToolSelectionSystem::mergeSelectionCmd(const cmd::ArgumentList& args)
{
if (getSelectionMode() != SelectionMode::Vertex)
{
rWarning() << "This command can only be executed in Vertex manipulation mode" << std::endl;
return;
}

AABB selectionBounds;

foreachSelectedComponentNode([&](const INode::Ptr& node)
{
auto componentSelectable = std::dynamic_pointer_cast<IComponentSelectable>(node);
if (!componentSelectable) return true;

selectionBounds.includeAABB(componentSelectable->getSelectedComponentBounds());

return true;
});

if (selectionBounds.isValid())
{
UndoableCommand cmd("mergeSelectedTexcoords");

foreachSelectedNodeOfAnyType([&](const INode::Ptr& node)
{
node->beginTransformation();

auto componentTransformable = std::dynamic_pointer_cast<IComponentTransformable>(node);

if (componentTransformable)
{
componentTransformable->mergeComponentsWith({ selectionBounds.origin.x(), selectionBounds.origin.y() });
}

node->commitTransformation();
return true;
});

radiant::TextureChangedMessage::Send();
}
}

module::StaticModule<TextureToolSelectionSystem> _textureToolSelectionSystemModule;

}
1 change: 1 addition & 0 deletions radiantcore/selection/textool/TextureToolSelectionSystem.h
Expand Up @@ -89,6 +89,7 @@ class TextureToolSelectionSystem :
void toggleSelectionModeCmd(const cmd::ArgumentList& args);
void selectRelatedCmd(const cmd::ArgumentList& args);
void snapSelectionToGridCmd(const cmd::ArgumentList& args);
void mergeSelectionCmd(const cmd::ArgumentList& args);

void performSelectionTest(Selector& selector, SelectionTest& test);
};
Expand Down
18 changes: 9 additions & 9 deletions test/TextureTool.cpp
Expand Up @@ -1667,7 +1667,7 @@ TEST_F(TextureToolTest, SnapPatchToGrid)
auto patch = Node_getIPatch(patchNode);

patch->fitTexture(1, 1);
patch->translateTexture(0.133f, 0.111f);
patch->translateTexture(13, 11);

// Get the texture space bounds of this patch
auto bounds = getTextureSpaceBounds(*patch);
Expand Down Expand Up @@ -1752,7 +1752,7 @@ TEST_F(TextureToolTest, MergePatchVertices)

patch1->fitTexture(1, 1);
patch2->fitTexture(1, 1);
patch2->translateTexture(0.2f, -0.2f);
patch2->translateTexture(20, -20);

// Get the texture space bounds of both patches
auto bounds = getTextureSpaceBounds(*patch1);
Expand All @@ -1768,8 +1768,8 @@ TEST_F(TextureToolTest, MergePatchVertices)
// Get the texcoords of the first vertex
auto definedRow = 2;
auto definedCol = 1;
auto vertex1 = patch1->ctrlAt(definedRow, definedCol).texcoord;
auto vertex2 = patch1->ctrlAt(definedRow, definedCol).texcoord;
const auto& vertex1 = patch1->ctrlAt(definedRow, definedCol).texcoord;
const auto& vertex2 = patch2->ctrlAt(definedRow, definedCol).texcoord;

performPointSelection(vertex1, view);
performPointSelection(vertex2, view);
Expand Down Expand Up @@ -1813,8 +1813,8 @@ TEST_F(TextureToolTest, MergeFaceVertices)
GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

// Get the texcoords of the first vertex
auto vertex1 = face1->getWinding()[0].texcoord;
auto vertex2 = face2->getWinding()[0].texcoord;
const auto& vertex1 = face1->getWinding()[0].texcoord;
const auto& vertex2 = face2->getWinding()[0].texcoord;

performPointSelection(vertex1, view);
performPointSelection(vertex2, view);
Expand Down Expand Up @@ -1859,11 +1859,11 @@ TEST_F(TextureToolTest, MergeTwoVerticesOfSameFace)
GlobalTextureToolSelectionSystem().setSelectionMode(textool::SelectionMode::Vertex);

// Get the texcoords of two vertices
auto vertex1 = face1->getWinding()[0].texcoord;
auto vertex2 = face2->getWinding()[0].texcoord;
const auto& vertex1 = face1->getWinding()[0].texcoord;
const auto& vertex2 = face2->getWinding()[0].texcoord;

// The third vertex is from the first face
auto vertex3 = face1->getWinding()[1].texcoord;
const auto& vertex3 = face1->getWinding()[1].texcoord;

performPointSelection(vertex1, view);
performPointSelection(vertex2, view);
Expand Down

0 comments on commit 2b76d72

Please sign in to comment.