Skip to content

Commit

Permalink
#5746: Every textool node is selectable, add foreachSelectedNode method
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Sep 14, 2021
1 parent b224625 commit 5d314e9
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 2 deletions.
8 changes: 7 additions & 1 deletion include/itexturetoolmodel.h
Expand Up @@ -3,6 +3,7 @@
#include "imodule.h"
#include "inode.h"
#include "Bounded.h"
#include "iselection.h"

class Matrix3;

Expand Down Expand Up @@ -31,7 +32,8 @@ class ITransformable
// The base element of every node in the ITextureToolSceneGraph
class INode :
public ITransformable,
public Bounded
public Bounded,
public ISelectable
{
public:
virtual ~INode() {}
Expand Down Expand Up @@ -60,6 +62,10 @@ class ITextureToolSceneGraph :
// Iterate over every node in this graph calling the given functor
// Collection should not be modified during iteration
virtual void foreachNode(const std::function<bool(const INode::Ptr&)>& functor) = 0;

// Iterate over every selected node in this graph calling the given functor
// Collection should not be modified during iteration
virtual void foreachSelectedNode(const std::function<bool(const INode::Ptr&)>& functor) = 0;
};

}
Expand Down
@@ -1,6 +1,7 @@
#include "TextureToolRotateManipulator.h"

#include "iselectiontest.h"
#include "itexturetoolmodel.h"
#include "selection/BestPoint.h"
#include "selection/SelectionPool.h"
#include "pivot.h"
Expand Down Expand Up @@ -145,7 +146,6 @@ void TextureToolRotateManipulator::renderComponents(const Matrix4& pivot2World)

void TextureToolRotateManipulator::rotateSelected(double angle)
{
// TODO
}

}
14 changes: 14 additions & 0 deletions radiantcore/selection/textool/NodeBase.h
@@ -1,13 +1,27 @@
#pragma once

#include "itexturetoolmodel.h"
#include "../BasicSelectable.h"

namespace textool
{

class NodeBase :
public INode
{
private:
selection::BasicSelectable _selectable;

public:
virtual void setSelected(bool select) override
{
_selectable.setSelected(select);
}

virtual bool isSelected() const override
{
return _selectable.isSelected();
}
};

}
15 changes: 15 additions & 0 deletions radiantcore/selection/textool/TextureToolSceneGraph.cpp
Expand Up @@ -57,6 +57,21 @@ void TextureToolSceneGraph::foreachNode(const std::function<bool(const INode::Pt
}
}

void TextureToolSceneGraph::foreachSelectedNode(const std::function<bool(const INode::Ptr&)>& functor)
{
ensureSceneIsAnalysed();

for (const auto& node : _nodes)
{
if (!node->isSelected()) continue;

if (!functor(node))
{
break;
}
}
}

void TextureToolSceneGraph::ensureSceneIsAnalysed()
{
if (!_selectionNeedsRescan) return;
Expand Down
1 change: 1 addition & 0 deletions radiantcore/selection/textool/TextureToolSceneGraph.h
Expand Up @@ -26,6 +26,7 @@ class TextureToolSceneGraph :
void shutdownModule() override;

void foreachNode(const std::function<bool(const INode::Ptr&)>& functor) override;
void foreachSelectedNode(const std::function<bool(const INode::Ptr&)>& functor) override;

private:
void onSceneSelectionChanged(const ISelectable& selectable);
Expand Down
44 changes: 44 additions & 0 deletions test/TextureTool.cpp
@@ -1,5 +1,6 @@
#include "RadiantTest.h"

#include <set>
#include "imap.h"
#include "ipatch.h"
#include "iselectable.h"
Expand Down Expand Up @@ -153,4 +154,47 @@ TEST_F(TextureToolTest, PatchNodeBounds)
"Bounds mismatch, got " << node->localAABB().getExtents() << " instead of " << checkedBounds.getExtents();
}

TEST_F(TextureToolTest, ForeachSelectedNode)
{
auto worldspawn = GlobalMapModule().findOrInsertWorldspawn();
auto brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0, 0, 0), "textures/numbers/1");
auto brush2 = algorithm::createCubicBrush(worldspawn, Vector3(0, 256, 256), "textures/numbers/1");
auto patchNode = GlobalPatchModule().createPatch(patch::PatchDefType::Def2);
Node_getIPatch(patchNode)->setDims(3, 3);
Node_getIPatch(patchNode)->setShader("textures/numbers/1");

Node_setSelected(brush1, true);
Node_setSelected(brush2, true);
Node_setSelected(patchNode, true);
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 3) << "3 items must be selected";

// We don't know how many tex tool nodes there are, but it should be more than 0
EXPECT_GT(getTextureToolNodeCount(), 0) << "There should be some tex tool nodes now";

std::set<textool::INode::Ptr> selectedNodes;
std::size_t i = 0;

// Selected every odd node
GlobalTextureToolSceneGraph().foreachNode([&](const textool::INode::Ptr& node)
{
if (++i % 2 == 1)
{
node->setSelected(true);
selectedNodes.emplace(node);
}

return true;
});

std::size_t selectedCount = 0;
GlobalTextureToolSceneGraph().foreachSelectedNode([&](const textool::INode::Ptr& node)
{
++selectedCount;
EXPECT_TRUE(selectedNodes.count(node) > 0) << "Node shouldn't be selected";
return true;
});

EXPECT_EQ(selectedCount, selectedNodes.size()) << "Selection count didn't match";
}

}

0 comments on commit 5d314e9

Please sign in to comment.