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
|
#pragma once
#include <cstddef>
#include "inode.h"
#include "iundo.h"
#include "ientity.h"
#include "ibrush.h"
#include "ipatch.h"
#include "imodel.h"
namespace test::algorithm
{
// Returns the n-th child of the given parent node
inline scene::INodePtr getNthChild(const scene::INodePtr& parent, std::size_t index)
{
std::size_t n = 0;
scene::INodePtr candidate;
parent->foreachNode([&](const scene::INodePtr& node)
{
if (n == index)
{
candidate = node;
return false;
}
++n;
return true;
});
return candidate;
}
inline scene::INodePtr findFirstNode(const scene::INodePtr& parent,
const std::function<bool(const scene::INodePtr&)>& predicate)
{
scene::INodePtr candidate;
parent->foreachNode([&](const scene::INodePtr& node)
{
if (!candidate && predicate(node))
{
candidate = node;
return false;
}
return true;
});
return candidate;
}
// Finds the first matching child brush of the given parent node matching the given predicate
inline scene::INodePtr findFirstBrush(const scene::INodePtr& parent,
const std::function<bool(const IBrushNodePtr&)>& predicate)
{
scene::INodePtr candidate;
parent->foreachNode([&](const scene::INodePtr& node)
{
auto brushNode = std::dynamic_pointer_cast<IBrushNode>(node);
if (!candidate && brushNode && predicate(brushNode))
{
candidate = node;
return false;
}
return true;
});
return candidate;
}
// Produces a predicate object to check if a node is a brush with a certain material
inline std::function<bool(const scene::INodePtr&)> brushHasMaterial(const std::string& material)
{
return [material](const scene::INodePtr& node) { return Node_isBrush(node) && Node_getIBrush(node)->hasShader(material); };
}
inline std::function<bool(const scene::INodePtr&)> patchHasMaterial(const std::string& material)
{
return [material](const scene::INodePtr& node) { return Node_isPatch(node) && Node_getIPatch(node)->getShader() == material; };
}
// Finds the first matching child brush of the given parent node, with any of the brush's faces matching the given material
inline scene::INodePtr findFirstBrushWithMaterial(const scene::INodePtr& parent, const std::string& material)
{
return findFirstBrush(parent, [&](const IBrushNodePtr& brush)
{
return brush->getIBrush().hasShader(material);
});
}
// Finds the first matching child patch of the given parent node matching the given predicate
inline scene::INodePtr findFirstPatch(const scene::INodePtr& parent,
const std::function<bool(const IPatchNodePtr&)>& predicate)
{
scene::INodePtr candidate;
parent->foreachNode([&](const scene::INodePtr& node)
{
auto patchNode = std::dynamic_pointer_cast<IPatchNode>(node);
if (!candidate && patchNode && predicate(patchNode))
{
candidate = node;
return false;
}
return true;
});
return candidate;
}
// Finds the first matching child patch of the given parent node, with the given material
inline scene::INodePtr findFirstPatchWithMaterial(const scene::INodePtr& parent, const std::string& material)
{
return findFirstPatch(parent, [&](const IPatchNodePtr& patch)
{
return patch->getPatch().getShader() == material;
});
}
inline scene::INodePtr findFirstEntity(const scene::INodePtr& parent,
const std::function<bool(const IEntityNodePtr&)>& predicate)
{
IEntityNodePtr candidate;
parent->foreachNode([&](const scene::INodePtr& node)
{
auto entity = std::dynamic_pointer_cast<IEntityNode>(node);
if (entity && predicate(entity))
{
candidate = entity;
return false;
}
return true;
});
return candidate;
}
inline scene::INodePtr findWorldspawn(const scene::INodePtr& root)
{
return findFirstEntity(root, [&](const IEntityNodePtr& entity)
{
return entity->getEntity().isWorldspawn();
});
}
inline scene::INodePtr getEntityByName(const scene::INodePtr& parent, const std::string& name)
{
return findFirstEntity(parent, [&](const IEntityNodePtr& entity)
{
return entity->getEntity().getKeyValue("name") == name;
});
}
// Modifies a key/value of the worldspawn entity (in an Undoable transaction)
inline void setWorldspawnKeyValue(const std::string& key, const std::string& value)
{
auto entity = GlobalMapModule().findOrInsertWorldspawn();
UndoableCommand cmd("modifyKeyValue");
Node_getEntity(entity)->setKeyValue(key, value);
}
inline scene::INodePtr findChildModelNode(const scene::INodePtr& parent)
{
scene::INodePtr candidate;
parent->foreachNode([&](const scene::INodePtr& node)
{
auto model = Node_getModel(node);
if (model)
{
candidate = node;
return false;
}
return true;
});
return candidate;
}
inline model::ModelNodePtr findChildModel(const scene::INodePtr& parent)
{
return Node_getModel(findChildModelNode(parent));
}
// Returns the number of children of the given parent node matching the given predicate
inline std::size_t getChildCount(const scene::INodePtr& parent,
const std::function<bool(const scene::INodePtr&)>& predicate)
{
std::size_t count = 0;
parent->foreachNode([&](const scene::INodePtr& node)
{
if (predicate(node))
{
++count;
}
return true;
});
return count;
}
// Returns the number of children of the given parent node
inline std::size_t getChildCount(const scene::INodePtr& parent)
{
return getChildCount(parent, [](const scene::INodePtr&) { return true; });
}
}
|