-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfvpSelectionSceneIndex.cpp
321 lines (267 loc) · 10.7 KB
/
fvpSelectionSceneIndex.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
//
// Copyright 2022 Pixar
//
// Licensed under the Apache License, Version 2.0 (the "Apache License")
// with the following modification; you may not use this file except in
// compliance with the Apache License and the following modification to it:
// Section 6. Trademarks. is deleted and replaced with:
//
// 6. Trademarks. This License does not grant permission to use the trade
// names, trademarks, service marks, or product names of the Licensor
// and its affiliates, except as required to comply with Section 4(c) of
// the License and to reproduce the content of the NOTICE file.
//
// You may obtain a copy of the Apache License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the Apache License with the above modification is
// distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the Apache License for the specific
// language governing permissions and limitations under the Apache License.
//
// Copyright 2023 Autodesk
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#include "flowViewport/sceneIndex/fvpSelectionSceneIndex.h"
#include "flowViewport/selection/fvpSelection.h"
#include <flowViewport/selection/fvpPathMapper.h>
#include <flowViewport/selection/fvpPathMapperRegistry.h>
#include "flowViewport/debugCodes.h"
#include "pxr/imaging/hd/retainedDataSource.h"
#include <pxr/imaging/hd/sceneIndexObserver.h>
#include "pxr/imaging/hd/selectionSchema.h"
#include "pxr/imaging/hd/selectionsSchema.h"
#include <ufe/pathString.h>
#include <ufe/selection.h>
PXR_NAMESPACE_USING_DIRECTIVE
namespace {
const HdDataSourceLocatorSet selectionsSchemaDefaultLocator{HdSelectionsSchema::GetDefaultLocator()};
}
namespace FVP_NS_DEF {
class _PrimSource : public HdContainerDataSource
{
public:
HD_DECLARE_DATASOURCE(_PrimSource);
_PrimSource(HdContainerDataSourceHandle const &inputSource,
const SelectionConstPtr& selection,
const SdfPath &primPath)
: _inputSource(inputSource)
, _selection(selection)
, _primPath(primPath)
{
}
TfTokenVector GetNames() override
{
TfTokenVector names = _inputSource ? _inputSource->GetNames() : TfTokenVector();
if (_selection->IsFullySelected(_primPath)) {
names.push_back(HdSelectionsSchemaTokens->selections);
}
return names;
}
HdDataSourceBaseHandle Get(const TfToken &name) override
{
if (name == HdSelectionsSchemaTokens->selections) {
return _selection->GetVectorDataSource(_primPath);
}
return _inputSource ? _inputSource->Get(name) : nullptr;
}
private:
HdContainerDataSourceHandle const _inputSource;
const SelectionConstPtr _selection;
const SdfPath _primPath;
};
SelectionSceneIndexRefPtr
SelectionSceneIndex::New(
const HdSceneIndexBaseRefPtr& inputSceneIndex,
const SelectionPtr& selection
)
{
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg("SelectionSceneIndex::New() called.\n");
return TfCreateRefPtr(new SelectionSceneIndex(inputSceneIndex, selection));
}
SelectionSceneIndex::
SelectionSceneIndex(
const HdSceneIndexBaseRefPtr& inputSceneIndex,
const SelectionPtr& selection
)
: HdSingleInputFilteringSceneIndexBase(inputSceneIndex)
, InputSceneIndexUtils(inputSceneIndex)
, _selection(selection)
{
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg("SelectionSceneIndex::SelectionSceneIndex() called.\n");
}
HdSceneIndexPrim
SelectionSceneIndex::GetPrim(const SdfPath &primPath) const
{
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg("SelectionSceneIndex::GetPrim() called.\n");
HdSceneIndexPrim result = GetInputSceneIndex()->GetPrim(primPath);
// An empty data source can be taken to mean an invalid prim, but it can
// also be an ancestor in a path to an actual prim (see
// https://forum.aousd.org/t/representation-of-invalid-hdsceneindexprim/833/2
// ). Such an ancestor prim can be selected, even if typeless, so add
// a selection data source in such a case.
if (!result.dataSource && !_selection->IsFullySelected(primPath)) {
return result;
}
result.dataSource = _PrimSource::New(
result.dataSource, _selection, primPath);
return result;
}
SdfPathVector
SelectionSceneIndex::GetChildPrimPaths(const SdfPath &primPath) const
{
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg("SelectionSceneIndex::GetChildPrimPaths() called.\n");
return GetInputSceneIndex()->GetChildPrimPaths(primPath);
}
void
SelectionSceneIndex::AddSelection(const Ufe::Path& appPath)
{
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg("SelectionSceneIndex::AddSelection(const Ufe::Path& %s) called.\n", Ufe::PathString::string(appPath).c_str());
// Call our input scene index to convert the application path to scene index paths and selection data sources.
auto primSelections = _UfePathToPrimSelections(appPath);
for (const auto& primSelection : primSelections) {
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg(" Adding %s to the Hydra selection.\n", primSelection.primPath.GetText());
}
HdSceneIndexObserver::DirtiedPrimEntries dirtiedPrims;
for (const auto& primSelection : primSelections) {
if (_selection->Add(primSelection)) {
dirtiedPrims.emplace_back(primSelection.primPath, selectionsSchemaDefaultLocator);
}
}
_SendPrimsDirtied(dirtiedPrims);
}
void SelectionSceneIndex::RemoveSelection(const Ufe::Path& appPath)
{
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg("SelectionSceneIndex::RemoveSelection(const Ufe::Path& %s) called.\n", Ufe::PathString::string(appPath).c_str());
// Call our input scene index to convert the application path to scene index paths and selection data sources.
auto primSelections = _UfePathToPrimSelections(appPath);
HdSceneIndexObserver::DirtiedPrimEntries dirtiedPrims;
for (const auto& primSelection : primSelections) {
if (_selection->Remove(primSelection)) {
dirtiedPrims.emplace_back(primSelection.primPath, selectionsSchemaDefaultLocator);
}
}
_SendPrimsDirtied(dirtiedPrims);
}
void
SelectionSceneIndex::ClearSelection()
{
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg("SelectionSceneIndex::ClearSelection() called.\n");
if (_selection->IsEmpty()) {
return;
}
HdSceneIndexObserver::DirtiedPrimEntries entries;
auto paths = _selection->GetFullySelectedPaths();
entries.reserve(paths.size());
for (const auto& path : paths) {
entries.emplace_back(path, selectionsSchemaDefaultLocator);
}
_selection->Clear();
_SendPrimsDirtied(entries);
}
void SelectionSceneIndex::ReplaceSelection(const Ufe::Selection& selection)
{
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg("SelectionSceneIndex::ReplaceSelection() called.\n");
// Process the selection replace by performing dirty notification of the
// existing selection state. We could do this more efficiently by
// accounting for overlapping previous and new selections.
HdSceneIndexObserver::DirtiedPrimEntries entries;
auto paths = _selection->GetFullySelectedPaths();
entries.reserve(paths.size() + selection.size());
for (const auto& path : paths) {
entries.emplace_back(path, selectionsSchemaDefaultLocator);
}
_selection->Clear();
PrimSelections sceneIndexSn;
sceneIndexSn.reserve(selection.size());
for (const auto& snItem : selection) {
// Call our input scene index to convert the application path to scene index paths and selection data sources.
auto primSelections = _UfePathToPrimSelections(snItem->path());
if (primSelections.empty()) {
continue;
}
for (const auto& primSelection : primSelections) {
sceneIndexSn.emplace_back(primSelection);
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg(" Adding %s to the Hydra selection.\n", primSelection.primPath.GetText());
entries.emplace_back(primSelection.primPath, selectionsSchemaDefaultLocator);
}
}
_selection->Replace(sceneIndexSn);
_SendPrimsDirtied(entries);
}
bool SelectionSceneIndex::IsFullySelected(const SdfPath& primPath) const
{
return _selection->IsFullySelected(primPath);
}
bool SelectionSceneIndex::HasFullySelectedAncestorInclusive(const SdfPath& primPath) const
{
return _selection->HasFullySelectedAncestorInclusive(primPath);
}
PrimSelections SelectionSceneIndex::_UfePathToPrimSelections(const Ufe::Path& appPath) const
{
auto primSelections = ufePathToPrimSelections(appPath);
if (primSelections.empty()) {
TF_WARN("SelectionSceneIndex::_UfePathToPrimSelections(%s) returned no path, Hydra selection will be incorrect", Ufe::PathString::string(appPath).c_str());
}
return primSelections;
}
SdfPathVector SelectionSceneIndex::GetFullySelectedPaths() const
{
return _selection->GetFullySelectedPaths();
}
void
SelectionSceneIndex::_PrimsAdded(
const HdSceneIndexBase &sender,
const HdSceneIndexObserver::AddedPrimEntries &entries)
{
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg("SelectionSceneIndex::_PrimsAdded() called.\n");
_SendPrimsAdded(entries);
}
void
SelectionSceneIndex::_PrimsDirtied(
const HdSceneIndexBase &sender,
const HdSceneIndexObserver::DirtiedPrimEntries &entries)
{
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg("SelectionSceneIndex::_PrimsDirtied() called.\n");
_SendPrimsDirtied(entries);
}
void
SelectionSceneIndex::_PrimsRemoved(
const HdSceneIndexBase &sender,
const HdSceneIndexObserver::RemovedPrimEntries &entries)
{
TF_DEBUG(FVP_SELECTION_SCENE_INDEX)
.Msg("SelectionSceneIndex::_PrimsRemoved() called.\n");
if (!_selection->IsEmpty()) {
for (const auto &entry : entries) {
_selection->RemoveHierarchy(entry.primPath);
}
}
_SendPrimsRemoved(entries);
}
}