Skip to content

Commit

Permalink
#5746: The active texture tool material name is tracked by the Textur…
Browse files Browse the repository at this point in the history
…eToolSceneGraph now, no need to do this in the UI code.
  • Loading branch information
codereader committed Sep 24, 2021
1 parent 52d7368 commit f12d2fe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 6 deletions.
5 changes: 5 additions & 0 deletions include/itexturetoolmodel.h
Expand Up @@ -143,6 +143,11 @@ class ITextureToolSceneGraph :
public:
virtual ~ITextureToolSceneGraph() {}

// Returns the name of the single material that all the tex tool nodes are sharing.
// This is an empty string if this scene graph is empty, i.e. the map selection doesn't
// boil down to a single selected material.
virtual const std::string& getActiveMaterial() = 0;

// 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;
Expand Down
11 changes: 9 additions & 2 deletions radiantcore/selection/textool/TextureToolSceneGraph.cpp
Expand Up @@ -57,6 +57,13 @@ void TextureToolSceneGraph::foreachNode(const std::function<bool(const INode::Pt
}
}

const std::string& TextureToolSceneGraph::getActiveMaterial()
{
ensureSceneIsAnalysed();

return _activeMaterial;
}

void TextureToolSceneGraph::ensureSceneIsAnalysed()
{
if (!_selectionNeedsRescan) return;
Expand All @@ -65,8 +72,8 @@ void TextureToolSceneGraph::ensureSceneIsAnalysed()

_nodes.clear();

auto selectedShader = selection::getShaderFromSelection();
if (selectedShader.empty()) return;
_activeMaterial = selection::getShaderFromSelection();
if (_activeMaterial.empty()) return;

if (GlobalSelectionSystem().countSelectedComponents() > 0)
{
Expand Down
5 changes: 5 additions & 0 deletions radiantcore/selection/textool/TextureToolSceneGraph.h
Expand Up @@ -17,6 +17,9 @@ class TextureToolSceneGraph :

std::list<INode::Ptr> _nodes;

// The single active material. Is empty if the scene graph has no items
std::string _activeMaterial;

public:
TextureToolSceneGraph();

Expand All @@ -27,6 +30,8 @@ class TextureToolSceneGraph :

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

const std::string& getActiveMaterial() override;

private:
void onSceneSelectionChanged(const ISelectable& selectable);
void ensureSceneIsAnalysed();
Expand Down
18 changes: 14 additions & 4 deletions test/TextureTool.cpp
Expand Up @@ -106,23 +106,27 @@ class SelectionChangedCatcher
// Checks that changing the regular scene selection will have an effect on the tex tool scene
TEST_F(TextureToolTest, SceneGraphObservesSelection)
{
std::string material = "textures/numbers/1";
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 brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0,0,0), material);
auto brush2 = algorithm::createCubicBrush(worldspawn, Vector3(0,256,256), material);

// Empty tex tool scenegraph on empty scene selection
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 0) << "Non-empty selection at startup";
EXPECT_EQ(getTextureToolNodeCount(), 0) << "There shouldn't be any textool nodes when the scene is empty";
EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), "") << "Active material shoud be empty";

Node_setSelected(brush1, true);
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 Brush must be selected";

// We don't know how many tex tool nodes there are, but it should be more than 0
auto nodeCount = getTextureToolNodeCount();
EXPECT_GT(nodeCount, 0) << "There should be some tex tool nodes now";
EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), material) << "Active material mismatch";

Node_setSelected(brush2, true);
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 2) << "2 Brushes must be selected";
EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), material) << "Active material mismatch";

// Should be even more now
auto nodeCount2 = getTextureToolNodeCount();
Expand All @@ -131,16 +135,20 @@ TEST_F(TextureToolTest, SceneGraphObservesSelection)
GlobalSelectionSystem().setSelectedAll(false);
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 0) << "Non-empty selection at shutdown";
EXPECT_EQ(getTextureToolNodeCount(), 0) << "There shouldn't be any textool nodes when the scene is empty";
EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), "") << "Active material should be empty again";
}

TEST_F(TextureToolTest, SceneGraphNeedsUniqueShader)
{
std::string material1 = "textures/numbers/1";
std::string material2 = "textures/numbers/2";
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/2");
auto brush1 = algorithm::createCubicBrush(worldspawn, Vector3(0, 0, 0), material1);
auto brush2 = algorithm::createCubicBrush(worldspawn, Vector3(0, 256, 256), material2);

Node_setSelected(brush1, true);
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 Brush must be selected";
EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), material1) << "Active material mismatch";

// 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";
Expand All @@ -149,11 +157,13 @@ TEST_F(TextureToolTest, SceneGraphNeedsUniqueShader)
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 2) << "2 Brushes must be selected";

EXPECT_EQ(getTextureToolNodeCount(), 0) << "There should be no nodes now, since the material is non unique";
EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), "") << "Active material mismatch";

// Deselect brush 1, now only brush 2 is selected
Node_setSelected(brush1, false);
EXPECT_EQ(GlobalSelectionSystem().countSelected(), 1) << "1 Brush must be selected";
EXPECT_GT(getTextureToolNodeCount(), 0) << "There should be some tex tool nodes again";
EXPECT_EQ(GlobalTextureToolSceneGraph().getActiveMaterial(), material2) << "Active material mismatch";
}

TEST_F(TextureToolTest, SceneGraphRecognisesBrushes)
Expand Down

0 comments on commit f12d2fe

Please sign in to comment.