-
Notifications
You must be signed in to change notification settings - Fork 47
/
Group.cpp
630 lines (510 loc) · 15 KB
/
Group.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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
#include "Group.h"
#include "i18n.h"
#include <set>
#include "igroupnode.h"
#include "imainframe.h"
#include "itextstream.h"
#include "iselectiongroup.h"
#include "selectionlib.h"
#include "entitylib.h"
#include "map/Map.h"
#include "scene/SelectableNode.h"
#include "wxutil/dialog/MessageBox.h"
#include "selection/algorithm/Entity.h"
namespace selection {
namespace algorithm {
ISelectionGroupManager& getMapSelectionGroupManager()
{
assert(GlobalMapModule().getRoot());
return GlobalMapModule().getRoot()->getSelectionGroupManager();
}
void convertSelectedToFuncStatic(const cmd::ArgumentList& args)
{
UndoableCommand command("convertSelectedToFuncStatic");
// Attempt to create a func_static entity
try
{
createEntityFromSelection("func_static", Vector3(0,0,0));
}
catch (EntityCreationException& e)
{
wxutil::Messagebox::ShowError(e.what());
}
}
void revertGroupToWorldSpawn(const cmd::ArgumentList& args)
{
UndoableCommand cmd("revertToWorldspawn");
GroupNodeCollector walker;
GlobalSelectionSystem().foreachSelected(walker);
if (walker.getList().empty())
{
return; // nothing to do!
}
// Deselect all, the children get selected after reparenting
GlobalSelectionSystem().setSelectedAll(false);
// Get the worldspawn node
scene::INodePtr worldspawnNode = GlobalMap().findOrInsertWorldspawn();
Entity* worldspawn = Node_getEntity(worldspawnNode);
if (!worldspawn)
{
return; // worldspawn not an entity?
}
for (const scene::INodePtr& groupNode : walker.getList())
{
Entity* parent = Node_getEntity(groupNode);
if (!parent) continue; // not an entity
ParentPrimitivesToEntityWalker reparentor(worldspawnNode);
groupNode->traverseChildren(reparentor);
// Perform the reparenting, this also checks for empty parent nodes
// being left behind after this operation
reparentor.reparent();
// Select the reparented primitives after moving them to worldspawn
reparentor.selectReparentedPrimitives();
}
// Flag the map as changed
GlobalMap().setModified(true);
}
void ParentPrimitivesToEntityWalker::reparent()
{
for (const scene::INodePtr& i : _childrenToReparent)
{
// Remove this path from the old parent
scene::removeNodeFromParent(i);
// Insert the child node into the parent node
_parent->addChildNode(i);
}
rMessage() << "Reparented " << _childrenToReparent.size()
<< " primitives." << std::endl;
// Update parent node/subgraph visibility after reparenting
scene::UpdateNodeVisibilityWalker updater(_parent->getRootNode());
// Update the new parent too
_parent->traverse(updater);
for (const scene::INodePtr& i : _oldParents)
{
i->traverse(updater);
}
// Now check if any parents were left behind empty
for (const scene::INodePtr& oldParent : _oldParents)
{
if (!scene::hasChildPrimitives(oldParent))
{
// Is empty, but make sure we're not removing the worldspawn
if (Node_isWorldspawn(oldParent)) continue;
// Is empty now, remove it
scene::removeNodeFromParent(oldParent);
}
}
// Update the scene
SceneChangeNotify();
}
void ParentPrimitivesToEntityWalker::selectReparentedPrimitives()
{
for (const scene::INodePtr& i : _childrenToReparent)
{
Node_setSelected(i, true);
}
}
void ParentPrimitivesToEntityWalker::visit(const scene::INodePtr& node) const
{
// Don't reparent instances to themselves
if (_parent == node) return;
if (Node_isPrimitive(node))
{
// Got a child, add it to the list
_childrenToReparent.push_back(node);
// Mark the entity for later emptiness check
_oldParents.insert(node->getParent());
}
}
bool ParentPrimitivesToEntityWalker::pre(const scene::INodePtr& node)
{
// Don't reparent nodes to themselves
if (_parent != node && Node_isPrimitive(node))
{
// Got a child, add it to the list
_childrenToReparent.push_back(node);
// Mark the entity for later emptiness check
_oldParents.insert(node->getParent());
return false; // don't traverse primitives any further
}
return true;
}
void GroupNodeCollector::visit(const scene::INodePtr& node) const
{
if (scene::hasChildPrimitives(node))
{
_groupNodes.push_back(node);
}
}
GroupNodeChecker::GroupNodeChecker() :
_onlyGroups(true),
_numGroups(0)
{}
void GroupNodeChecker::visit(const scene::INodePtr& node) const
{
if (!scene::hasChildPrimitives(node))
{
_onlyGroups = false;
}
else
{
_numGroups++;
if (_firstGroupNode == NULL)
{
_firstGroupNode = node;
}
}
}
bool GroupNodeChecker::onlyGroupsAreSelected() const
{
return _numGroups > 0 && _onlyGroups;
}
std::size_t GroupNodeChecker::selectedGroupCount() const
{
return _numGroups;
}
scene::INodePtr GroupNodeChecker::getFirstSelectedGroupNode() const
{
return _firstGroupNode;
}
bool curSelectionIsSuitableForReparent()
{
// Retrieve the selection information structure
const SelectionInfo& info = GlobalSelectionSystem().getSelectionInfo();
if (info.totalCount <= 1 || info.entityCount != 1)
{
return false;
}
scene::INodePtr lastSelected = GlobalSelectionSystem().ultimateSelected();
Entity* entity = Node_getEntity(lastSelected);
// Reject non-entities or models
if (entity == nullptr || entity->isModel())
{
return false;
}
// Accept only group nodes as parent
if (!Node_getGroupNode(lastSelected))
{
return false;
}
return true;
}
// re-parents the selected brushes/patches
void parentSelection(const cmd::ArgumentList& args)
{
// Retrieve the selection information structure
if (!curSelectionIsSuitableForReparent())
{
wxutil::Messagebox::ShowError(_("Cannot reparent primitives to entity. "
"Please select at least one brush/patch and exactly one func_* entity. "
"(The entity has to be selected last.)"));
return;
}
UndoableCommand undo("parentSelectedPrimitives");
// Take the last selected item (this is an entity)
ParentPrimitivesToEntityWalker visitor(
GlobalSelectionSystem().ultimateSelected()
);
GlobalSelectionSystem().foreachSelected(visitor);
visitor.reparent();
}
void parentSelectionToWorldspawn(const cmd::ArgumentList& args) {
UndoableCommand undo("parentSelectedPrimitives");
scene::INodePtr world = GlobalMap().findOrInsertWorldspawn();
if (world == NULL) return;
// Take the last selected item (this should be an entity)
ParentPrimitivesToEntityWalker visitor(world);
GlobalSelectionSystem().foreachSelected(visitor);
visitor.reparent();
}
class GroupNodeChildSelector :
public SelectionSystem::Visitor,
public scene::NodeVisitor
{
typedef std::list<scene::INodePtr> NodeList;
mutable NodeList _groupNodes;
public:
/**
* greebo: The destructor takes care of the actual selection changes. During
* selection traversal, the selection itself cannot be changed without
* invalidating the SelectionSystem's internal iterators.
*/
~GroupNodeChildSelector() {
for (NodeList::iterator i = _groupNodes.begin(); i != _groupNodes.end(); i++) {
// De-select the groupnode
Node_setSelected(*i, false);
// Select all the child nodes using self as visitor
(*i)->traverseChildren(*this);
}
}
// SelectionSystem::Visitor implementation
void visit(const scene::INodePtr& node) const {
// Don't traverse hidden elements, just to be sure
if (!node->visible()) {
return;
}
// Is this a selected groupnode?
if (Node_isSelected(node) &&
Node_getGroupNode(node) != NULL)
{
// Marke the groupnode for de-selection
_groupNodes.push_back(node);
}
}
bool pre(const scene::INodePtr& node) {
// Don't process starting point node or invisible nodes
if (node->visible()) {
Node_setSelected(node, true);
}
return true;
}
};
void selectChildren(const cmd::ArgumentList& args) {
// Traverse the selection and identify the groupnodes
GlobalSelectionSystem().foreachSelected(
GroupNodeChildSelector()
);
}
/**
* greebo: This walker traverses the entire subgraph,
* searching for entities with selected child primitives.
* If such an entity is found, it is traversed and all
* child primitives are selected.
*/
class ExpandSelectionToSiblingsWalker :
public scene::NodeVisitor
{
public:
bool pre(const scene::INodePtr& node) override
{
Entity* entity = Node_getEntity(node);
if (entity != nullptr)
{
// We have an entity, traverse and select children if any child is selected
return entity->isContainer() && (Node_isSelected(node) || Node_hasSelectedChildNodes(node));
}
else if (Node_isPrimitive(node))
{
// We have a primitive, select it
Node_setSelected(node, true);
// Don't traverse any deeper
return false;
}
return true;
}
};
void expandSelectionToSiblings(const cmd::ArgumentList& args)
{
ExpandSelectionToSiblingsWalker walker;
GlobalSceneGraph().root()->traverse(walker);
}
/**
* greebo: This walker traverses the entire subgraph,
* searching for entities with selected child primitives.
* If such an entity is found, it will be selected in place
* of the child primitive.
*/
class PropagateSelectionToParentEntityWalker :
public scene::NodeVisitor
{
public:
bool pre(const scene::INodePtr& node) override
{
Entity* entity = Node_getEntity(node);
if (entity != nullptr)
{
if (entity->isContainer() && !entity->isWorldspawn() && Node_hasSelectedChildNodes(node))
{
// De-select all child primitives
node->foreachNode([](const scene::INodePtr& child)->bool
{
Node_setSelected(child, false);
return true;
});
// Select the entity instead
Node_setSelected(node, true);
}
return false; // don't traverse entities, that's covered by Node_hasSelectedChildNodes
}
return true;
}
};
void selectParentEntitiesOfSelected(const cmd::ArgumentList& args)
{
PropagateSelectionToParentEntityWalker walker;
GlobalSceneGraph().root()->traverse(walker);
}
void mergeSelectedEntities(const cmd::ArgumentList& args)
{
// Check the current selection, must consist of group nodes only
GroupNodeChecker walker;
GlobalSelectionSystem().foreachSelected(walker);
if (walker.onlyGroupsAreSelected() && walker.selectedGroupCount() > 1)
{
UndoableCommand cmd("mergeEntities");
scene::INodePtr newParent = walker.getFirstSelectedGroupNode();
// Gather all group nodes in the selection
GroupNodeCollector collector;
GlobalSelectionSystem().foreachSelected(collector);
// Traverse all group nodes using a ParentPrimitivesToEntityWalker
for (GroupNodeCollector::GroupNodeList::const_iterator i = collector.getList().begin();
i != collector.getList().end(); ++i)
{
if (*i == newParent) continue;
ParentPrimitivesToEntityWalker reparentor(newParent);
(*i)->traverseChildren(reparentor);
reparentor.reparent();
}
rMessage() << collector.getList().size() << " group nodes merged." << std::endl;
}
else
{
wxutil::Messagebox::ShowError(_("Cannot merge entities, "
"the selection must consist of func_* entities only.\n"
"(The first selected entity will be preserved.)"));
}
}
void checkGroupSelectedAvailable()
{
if (!GlobalMap().getRoot())
{
throw CommandNotAvailableException(_("No map loaded"));
}
if (GlobalSelectionSystem().Mode() != SelectionSystem::ePrimitive &&
GlobalSelectionSystem().Mode() != SelectionSystem::eGroupPart)
{
throw CommandNotAvailableException(_("Groups can be formed in Primitive and Group Part selection mode only"));
}
if (GlobalSelectionSystem().getSelectionInfo().totalCount == 0)
{
throw CommandNotAvailableException(_("Nothing selected, cannot group anything"));
}
if (GlobalSelectionSystem().getSelectionInfo().totalCount == 1)
{
throw CommandNotAvailableException(_("Select more than one element to form a group"));
return;
}
// Check if the current selection already is member of the same group
std::set<std::size_t> groupIds;
bool hasUngroupedNode = false;
GlobalSelectionSystem().foreachSelected([&](const scene::INodePtr& node)
{
std::shared_ptr<IGroupSelectable> selectable = std::dynamic_pointer_cast<IGroupSelectable>(node);
if (!selectable) return;
if (!selectable->getGroupIds().empty())
{
groupIds.insert(selectable->getMostRecentGroupId());
}
else
{
hasUngroupedNode = true;
}
});
if (!hasUngroupedNode && groupIds.size() == 1)
{
throw CommandNotAvailableException(_("The selected elements already form a group"));
}
}
void groupSelected()
{
// This will throw exceptions
checkGroupSelectedAvailable();
UndoableCommand cmd("GroupSelected");
ISelectionGroupPtr group = getMapSelectionGroupManager().createSelectionGroup();
GlobalSelectionSystem().foreachSelected([&](const scene::INodePtr& node)
{
group->addNode(node);
});
GlobalMainFrame().updateAllWindows();
}
void checkUngroupSelectedAvailable()
{
if (!GlobalMap().getRoot())
{
throw CommandNotAvailableException(_("No map loaded"));
}
if (GlobalSelectionSystem().Mode() != SelectionSystem::ePrimitive &&
GlobalSelectionSystem().Mode() != SelectionSystem::eGroupPart)
{
throw CommandNotAvailableException(_("Groups can be dissolved in Primitive and Group Part selection mode only"));
}
if (GlobalSelectionSystem().getSelectionInfo().totalCount == 0)
{
throw CommandNotAvailableException(_("Nothing selected, cannot un-group anything"));
}
// Check if the current selection already is member of the same group
bool hasOnlyUngroupedNodes = true;
GlobalSelectionSystem().foreachSelected([&](const scene::INodePtr& node)
{
std::shared_ptr<IGroupSelectable> selectable = std::dynamic_pointer_cast<IGroupSelectable>(node);
if (!selectable) return;
if (!selectable->getGroupIds().empty())
{
hasOnlyUngroupedNodes = false;
}
});
if (hasOnlyUngroupedNodes)
{
throw CommandNotAvailableException(_("The selected elements aren't part of any group"));
}
}
void ungroupSelected()
{
// Will throw exceptions if not available
checkUngroupSelectedAvailable();
UndoableCommand cmd("UngroupSelected");
// Collect all the latest group Ids from all selected nodes
std::set<std::size_t> ids;
GlobalSelectionSystem().foreachSelected([&](const scene::INodePtr& node)
{
std::shared_ptr<scene::SelectableNode> selectable = std::dynamic_pointer_cast<scene::SelectableNode>(node);
if (!selectable) return;
if (selectable->isGroupMember())
{
ids.insert(selectable->getMostRecentGroupId());
}
});
auto& selGroupMgr = getMapSelectionGroupManager();
// Now remove the found group by ID (maybe convert them to a selection set before removal?)
std::for_each(ids.begin(), ids.end(), [&](std::size_t id)
{
selGroupMgr.deleteSelectionGroup(id);
});
GlobalMainFrame().updateAllWindows();
}
void deleteAllSelectionGroupsCmd(const cmd::ArgumentList& args)
{
if (!GlobalMap().getRoot())
{
rError() << "No map loaded, cannot delete groups." << std::endl;
return;
}
UndoableCommand cmd("DeleteAllSelectionGroups");
getMapSelectionGroupManager().deleteAllSelectionGroups();
}
void groupSelectedCmd(const cmd::ArgumentList& args)
{
try
{
groupSelected();
}
catch (CommandNotAvailableException & ex)
{
rError() << ex.what() << std::endl;
wxutil::Messagebox::ShowError(ex.what());
}
}
void ungroupSelectedCmd(const cmd::ArgumentList& args)
{
try
{
ungroupSelected();
}
catch (CommandNotAvailableException & ex)
{
rError() << ex.what() << std::endl;
wxutil::Messagebox::ShowError(ex.what());
}
}
} // namespace algorithm
} // namespace selection