Skip to content

Commit

Permalink
#5746: Implement flip texture algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Sep 26, 2021
1 parent d083913 commit d3b22c7
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 3 deletions.
2 changes: 0 additions & 2 deletions radiant/textool/TexTool.cpp
Expand Up @@ -882,8 +882,6 @@ void TexTool::selectRelated(const cmd::ArgumentList& args) {
void TexTool::registerCommands()
{
GlobalCommandSystem().addCommand("TextureTool", TexTool::toggle);
GlobalCommandSystem().addCommand("TexToolFlipS", TexTool::texToolFlipS);
GlobalCommandSystem().addCommand("TexToolFlipT", TexTool::texToolFlipT);

GlobalEventManager().addRegistryToggle("TexToolToggleGrid", RKEY_GRID_STATE);
GlobalEventManager().addRegistryToggle("TexToolToggleFaceVertexScalePivot", RKEY_FACE_VERTEX_SCALE_PIVOT_IS_CENTROID);
Expand Down
68 changes: 68 additions & 0 deletions radiantcore/selection/textool/TextureToolSelectionSystem.cpp
Expand Up @@ -8,6 +8,7 @@
#include "../textool/TextureToolDragManipulator.h"
#include "selection/SelectionPool.h"
#include "string/case_conv.h"
#include "math/Matrix3.h"

namespace textool
{
Expand Down Expand Up @@ -52,6 +53,11 @@ void TextureToolSelectionSystem::initialiseModule(const IApplicationContext& ctx
std::bind(&TextureToolSelectionSystem::mergeSelectionCmd, this, std::placeholders::_1),
{ cmd::ARGTYPE_VECTOR2 | cmd::ARGTYPE_OPTIONAL });

GlobalCommandSystem().addCommand("TexToolFlipS",
std::bind(&TextureToolSelectionSystem::flipHorizontallyCmd, this, std::placeholders::_1));
GlobalCommandSystem().addCommand("TexToolFlipT",
std::bind(&TextureToolSelectionSystem::flipVerticallyCmd, this, std::placeholders::_1));

_unselectListener = GlobalRadiantCore().getMessageBus().addListener(
radiant::IMessage::Type::UnselectSelectionRequest,
radiant::TypeListener<selection::UnselectSelectionRequest>(
Expand Down Expand Up @@ -655,6 +661,68 @@ void TextureToolSelectionSystem::mergeSelectionCmd(const cmd::ArgumentList& args
}
}

void TextureToolSelectionSystem::flipSelected(int axis)
{
if (getSelectionMode() != SelectionMode::Surface)
{
rWarning() << "This command can only be executed in Surface manipulation mode" << std::endl;
return;
}

AABB selectionBounds;

// Calculate the center based on the selection
foreachSelectedNode([&](const INode::Ptr& node)
{
selectionBounds.includeAABB(node->localAABB());
return true;
});

if (!selectionBounds.isValid())
{
return;
}

// Move center to origin, flip around the specified axis, and move back
Vector2 flipCenter(selectionBounds.origin.x(), selectionBounds.origin.y());
auto flipMatrix = Matrix3::getIdentity();

if (axis == 0)
{
flipMatrix.xx() = -1;
}
else // axis == 1
{
flipMatrix.yy() = -1;
}

auto flipTransformation = Matrix3::getTranslation(-flipCenter);
flipTransformation.premultiplyBy(flipMatrix);
flipTransformation.premultiplyBy(Matrix3::getTranslation(+flipCenter));

UndoableCommand cmd("flipSelectedTexcoords " + string::to_string(axis));

foreachSelectedNode([&](const INode::Ptr& node)
{
node->beginTransformation();
node->transform(flipTransformation);
node->commitTransformation();
return true;
});

radiant::TextureChangedMessage::Send();
}

void TextureToolSelectionSystem::flipVerticallyCmd(const cmd::ArgumentList& args)
{
flipSelected(1);
}

void TextureToolSelectionSystem::flipHorizontallyCmd(const cmd::ArgumentList& args)
{
flipSelected(0);
}

module::StaticModule<TextureToolSelectionSystem> _textureToolSelectionSystemModule;

}
3 changes: 3 additions & 0 deletions radiantcore/selection/textool/TextureToolSelectionSystem.h
Expand Up @@ -90,7 +90,10 @@ class TextureToolSelectionSystem :
void selectRelatedCmd(const cmd::ArgumentList& args);
void snapSelectionToGridCmd(const cmd::ArgumentList& args);
void mergeSelectionCmd(const cmd::ArgumentList& args);
void flipHorizontallyCmd(const cmd::ArgumentList& args);
void flipVerticallyCmd(const cmd::ArgumentList& args);

void flipSelected(int axis);
void performSelectionTest(Selector& selector, SelectionTest& test);
};

Expand Down
4 changes: 3 additions & 1 deletion test/TextureTool.cpp
Expand Up @@ -1976,7 +1976,9 @@ void performFlipTestWithTwoPatches(int axis)
auto patch1 = Node_getIPatch(patchNode1);
auto patch2 = Node_getIPatch(patchNode2);

patch2->translateTexture(154, -189);
patch1->fitTexture(1, 1);
patch2->fitTexture(1, 1);
patch2->translateTexture(45, -40);

// Get the texture space bounds of both patches
auto patchBounds1 = getTextureSpaceBounds(*patch1);
Expand Down

0 comments on commit d3b22c7

Please sign in to comment.