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
|
#include "Group.h"
#include <set>
#include "i18n.h"
#include "igroupnode.h"
#include "ientity.h"
#include "itextstream.h"
#include "iselectiongroup.h"
#include "imap.h"
#include "selectionlib.h"
#include "entitylib.h"
#include "scene/SelectableNode.h"
#include "scene/GroupNodeChecker.h"
#include "command/ExecutionFailure.h"
#include "command/ExecutionNotPossible.h"
#include "selection/algorithm/Entity.h"
#include "scene/Group.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
GlobalEntityModule().createEntityFromSelection("func_static", Vector3(0,0,0));
}
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 = GlobalMapModule().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();
}
}
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()->getLayerManager());
// 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);
}
}
// re-parents the selected brushes/patches
void parentSelection(const cmd::ArgumentList& args)
{
// Retrieve the selection information structure
if (!curSelectionIsSuitableForReparent())
{
throw cmd::ExecutionNotPossible(_("Cannot reparent primitives to entity. "
"Please select at least one brush/patch and exactly one func_* entity. "
"(The entity has to be selected last.)"));
}
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 = GlobalMapModule().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
scene::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
{
throw cmd::ExecutionNotPossible(_("Cannot merge entities, "
"the selection must consist of func_* entities only.\n"
"(The first selected entity will be preserved.)"));
}
}
void deleteAllSelectionGroupsCmd(const cmd::ArgumentList& args)
{
if (!GlobalMapModule().getRoot())
{
rError() << "No map loaded, cannot delete groups." << std::endl;
return;
}
UndoableCommand cmd("DeleteAllSelectionGroups");
getMapSelectionGroupManager().deleteAllSelectionGroups();
}
void groupSelectedCmd(const cmd::ArgumentList& args)
{
groupSelected();
}
void ungroupSelectedCmd(const cmd::ArgumentList& args)
{
ungroupSelected();
}
} // namespace algorithm
} // namespace selection
|