/
TextureToolSceneGraph.cpp
143 lines (115 loc) · 3.66 KB
/
TextureToolSceneGraph.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "TextureToolSceneGraph.h"
#include "iselection.h"
#include "ibrush.h"
#include "ipatch.h"
#include "module/StaticModule.h"
#include "selectionlib.h"
#include "FaceNode.h"
#include "PatchNode.h"
namespace textool
{
TextureToolSceneGraph::TextureToolSceneGraph() :
_selectionNeedsRescan(true),
_activeMaterialNeedsRescan(true)
{}
const std::string& TextureToolSceneGraph::getName() const
{
static std::string _name(MODULE_TEXTOOL_SCENEGRAPH);
return _name;
}
const StringSet& TextureToolSceneGraph::getDependencies() const
{
static StringSet _dependencies{ MODULE_SELECTIONSYSTEM };
return _dependencies;
}
void TextureToolSceneGraph::initialiseModule(const IApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;
_sceneSelectionChanged = GlobalSelectionSystem().signal_selectionChanged().connect(
sigc::mem_fun(this, &TextureToolSceneGraph::onSceneSelectionChanged)
);
_textureChangedHandler = GlobalRadiantCore().getMessageBus().addListener(
radiant::IMessage::Type::TextureChanged,
radiant::TypeListener<radiant::TextureChangedMessage>(
sigc::mem_fun(this, &TextureToolSceneGraph::onTextureChanged)));
}
void TextureToolSceneGraph::shutdownModule()
{
_selectionNeedsRescan = false;
_activeMaterialNeedsRescan = false;
_nodes.clear();
_sceneSelectionChanged.disconnect();
GlobalRadiantCore().getMessageBus().removeListener(_textureChangedHandler);
}
void TextureToolSceneGraph::foreachNode(const std::function<bool(const INode::Ptr&)>& functor)
{
ensureSceneIsAnalysed();
for (const auto& node : _nodes)
{
if (!functor(node))
{
break;
}
}
}
const std::string& TextureToolSceneGraph::getActiveMaterial()
{
ensureSceneIsAnalysed();
return _activeMaterial;
}
void TextureToolSceneGraph::ensureSceneIsAnalysed()
{
// First, check if the material has changed
if (_activeMaterialNeedsRescan)
{
_activeMaterialNeedsRescan = false;
auto material = selection::getShaderFromSelection();
if (material != _activeMaterial)
{
_activeMaterial = std::move(material);
// The material changed, we need to rebuild the nodes
_selectionNeedsRescan = true;
}
}
if (!_selectionNeedsRescan) return;
_selectionNeedsRescan = false;
_nodes.clear();
// No unique material, leave everything empty
if (_activeMaterial.empty()) return;
if (GlobalSelectionSystem().countSelectedComponents() > 0)
{
// Check each selected face
GlobalSelectionSystem().foreachFace([&](IFace& face)
{
_nodes.emplace_back(std::make_shared<FaceNode>(face));
});
}
GlobalSelectionSystem().foreachSelected([&](const scene::INodePtr& node)
{
if (Node_isBrush(node))
{
auto brush = Node_getIBrush(node);
assert(brush);
for (auto i = 0; i < brush->getNumFaces(); ++i)
{
_nodes.emplace_back(std::make_shared<FaceNode>(brush->getFace(i)));
}
}
else if (Node_isPatch(node))
{
_nodes.emplace_back(std::make_shared<PatchNode>(*Node_getIPatch(node)));
}
});
}
void TextureToolSceneGraph::onSceneSelectionChanged(const ISelectable& selectable)
{
// Mark our own selection as dirty
_activeMaterialNeedsRescan = true;
_selectionNeedsRescan = true;
}
void TextureToolSceneGraph::onTextureChanged(radiant::TextureChangedMessage& msg)
{
_activeMaterialNeedsRescan = true;
}
module::StaticModule<TextureToolSceneGraph> _textureToolSceneGraphModule;
}