/
ShaderClipboard.cpp
249 lines (196 loc) · 5.1 KB
/
ShaderClipboard.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
#include "ShaderClipboard.h"
#include "i18n.h"
#include "imap.h"
#include "iselectiontest.h"
#include "iscenegraph.h"
#include "iclipboard.h"
#include "ClosestTexturableFinder.h"
#include "util/ScopedBoolLock.h"
#include "patch/PatchNode.h"
#include "brush/BrushNode.h"
#include "module/StaticModule.h"
namespace selection
{
namespace
{
const char* const LAST_USED_MATERIAL_KEY = "LastShaderClipboardMaterial";
}
ShaderClipboard::ShaderClipboard() :
_updatesDisabled(false)
{}
ShaderClipboard::SourceType ShaderClipboard::getSourceType() const
{
if (_source.empty())
{
return SourceType::Empty;
}
if (_source.isFace())
{
return SourceType::Face;
}
else if (_source.isPatch())
{
return SourceType::Patch;
}
else if (_source.isShader())
{
return SourceType::Shader;
}
return SourceType::Empty;
}
void ShaderClipboard::sourceChanged()
{
util::ScopedBoolLock lock(_updatesDisabled); // prevent loopbacks
_signalSourceChanged.emit();
}
void ShaderClipboard::clear()
{
if (_updatesDisabled) return;
_source.clear();
sourceChanged();
}
void ShaderClipboard::onUndoRedoOperation()
{
// Check if the source is still valid
if (!_source.checkValid())
{
clear();
}
}
Texturable ShaderClipboard::getTexturable(SelectionTest& test)
{
// Initialise an empty Texturable structure
Texturable returnValue;
algorithm::ClosestTexturableFinder finder(test, returnValue);
GlobalSceneGraph().root()->traverseChildren(finder);
return returnValue;
}
std::string ShaderClipboard::getShaderName()
{
return getSource().getShader();
}
void ShaderClipboard::pickFromSelectionTest(SelectionTest& test)
{
if (_updatesDisabled) return; // loopback guard
_source = getTexturable(test);
// Copy the material name to the system clipboard when picking
if (!_source.empty() && module::GlobalModuleRegistry().moduleExists(MODULE_CLIPBOARD))
{
GlobalClipboard().setString(_source.getShader());
}
sourceChanged();
}
void ShaderClipboard::pasteShader(SelectionTest& test, PasteMode mode, bool pasteToAllFaces)
{
selection::algorithm::pasteShader(test, mode == PasteMode::Projected, pasteToAllFaces);
}
void ShaderClipboard::pasteTextureCoords(SelectionTest& test)
{
selection::algorithm::pasteTextureCoords(test);
}
void ShaderClipboard::pasteMaterialName(SelectionTest& test)
{
selection::algorithm::pasteShaderName(test);
}
ShaderClipboard& ShaderClipboard::Instance()
{
return static_cast<ShaderClipboard&>(GlobalShaderClipboard());
}
void ShaderClipboard::setSourceShader(const std::string& shader)
{
if (_updatesDisabled) return; // loopback guard
_source.clear();
_source.shader = shader;
sourceChanged();
}
void ShaderClipboard::setSource(Patch& sourcePatch)
{
if (_updatesDisabled) return; // loopback guard
_source.clear();
_source.patch = &sourcePatch;
_source.node = sourcePatch.getPatchNode().shared_from_this();
sourceChanged();
}
void ShaderClipboard::setSource(Face& sourceFace)
{
if (_updatesDisabled) return; // loopback guard
_source.clear();
_source.face = &sourceFace;
_source.node = sourceFace.getBrush().getBrushNode().shared_from_this();
sourceChanged();
}
Texturable& ShaderClipboard::getSource()
{
return _source;
}
sigc::signal<void>& ShaderClipboard::signal_sourceChanged()
{
return _signalSourceChanged;
}
void ShaderClipboard::onMapEvent(IMap::MapEvent ev)
{
switch (ev)
{
case IMap::MapUnloading:
// Clear the shaderclipboard, the references are most probably invalid now
clear();
break;
case IMap::MapSaving:
// Write the current value to the map properties on save
if (!_source.empty() && GlobalMapModule().getRoot())
{
GlobalMapModule().getRoot()->setProperty(LAST_USED_MATERIAL_KEY, _source.getShader());
}
break;
case IMap::MapLoaded:
// Try to load the last used material name from the properties
if (GlobalMapModule().getRoot())
{
auto shader = GlobalMapModule().getRoot()->getProperty(LAST_USED_MATERIAL_KEY);
if (!shader.empty())
{
setSourceShader(shader);
break;
}
}
clear();
break;
default:
return;
};
}
const std::string& ShaderClipboard::getName() const
{
static std::string _name(MODULE_SHADERCLIPBOARD);
return _name;
}
const StringSet& ShaderClipboard::getDependencies() const
{
static StringSet _dependencies;
if (_dependencies.empty())
{
_dependencies.insert(MODULE_UNDOSYSTEM);
_dependencies.insert(MODULE_MAP);
}
return _dependencies;
}
void ShaderClipboard::initialiseModule(const IApplicationContext& ctx)
{
rMessage() << getName() << "::initialiseModule called." << std::endl;
_postUndoConn = GlobalUndoSystem().signal_postUndo().connect(
sigc::mem_fun(this, &ShaderClipboard::onUndoRedoOperation));
_postRedoConn = GlobalUndoSystem().signal_postRedo().connect(
sigc::mem_fun(this, &ShaderClipboard::onUndoRedoOperation));
_mapEventConn = GlobalMapModule().signal_mapEvent().connect(
sigc::mem_fun(this, &ShaderClipboard::onMapEvent));
clear();
}
void ShaderClipboard::shutdownModule()
{
_postUndoConn.disconnect();
_postRedoConn.disconnect();
_mapEventConn.disconnect();
}
// Define the static module
module::StaticModule<ShaderClipboard> shaderClipboardModule;
} // namespace