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
|
#include "Prefab.h"
#include "i18n.h"
#include "iselection.h"
#include "ipatch.h"
#include "igrid.h"
#include "itextstream.h"
#include "iorthoview.h"
#include "ishaderclipboard.h"
#include "scenelib.h"
#include "shaderlib.h"
#include "patch/Patch.h"
#include "selection/algorithm/General.h"
#include "selectionlib.h"
#include "command/ExecutionFailure.h"
#include "string/case_conv.h"
namespace patch
{
namespace algorithm
{
namespace
{
// Gets the active/selected shader or the default fallback value
inline std::string getSelectedShader()
{
auto selectedShader = GlobalShaderClipboard().getShaderName();
if (selectedShader.empty())
{
selectedShader = texdef_name_default();
}
return selectedShader;
}
}
void constructPrefab(const AABB& aabb, const std::string& shader, EPatchPrefab eType,
OrthoOrientation viewType, std::size_t width, std::size_t height)
{
GlobalSelectionSystem().setSelectedAll(false);
scene::INodePtr node(GlobalPatchModule().createPatch(patch::PatchDefType::Def2));
GlobalMapModule().findOrInsertWorldspawn()->addChildNode(node);
Patch* patch = Node_getPatch(node);
patch->setShader(shader);
patch->ConstructPrefab(aabb, eType, viewType, width, height);
patch->controlPointsChanged();
Node_setSelected(node, true);
}
AABB getDefaultBoundsFromSelection()
{
AABB aabb = GlobalSelectionSystem().getWorkZone().bounds;
float gridSize = GlobalGrid().getGridSize();
if (aabb.extents[0] == 0)
{
aabb.extents[0] = gridSize;
}
if (aabb.extents[1] == 0)
{
aabb.extents[1] = gridSize;
}
if (aabb.extents[2] == 0)
{
aabb.extents[2] = gridSize;
}
if (aabb.isValid())
{
return aabb;
}
return AABB(Vector3(0, 0, 0), Vector3(64, 64, 64));
}
void createPrefabInternal(EPatchPrefab prefabType, const std::string& undoCmdName)
{
UndoableCommand undo(undoCmdName);
constructPrefab(getDefaultBoundsFromSelection(),
getSelectedShader(),
prefabType,
GlobalOrthoViewManager().getActiveViewType());
}
void createPrefab(const cmd::ArgumentList& args)
{
if (args.size() != 1)
{
rError() << "Usage: createPatchPrefab <type>" << std::endl
<< " with <type> being one of the following: " << std::endl
<< "cylinder, densecylinder, verydensecylinder, squarecylinder," << std::endl
<< "sphere, endcap, bevel, cone" << std::endl;
return;
}
std::string typeStr = string::to_lower_copy(args[0].getString());
if (typeStr == "cylinder")
{
createPrefabInternal(eCylinder, "patchCreateCylinder");
}
else if (typeStr == "densecylinder")
{
createPrefabInternal(eDenseCylinder, "patchCreateDenseCylinder");
}
else if (typeStr == "verydensecylinder")
{
createPrefabInternal(eVeryDenseCylinder, "patchCreateVeryDenseCylinder");
}
else if (typeStr == "squarecylinder")
{
createPrefabInternal(eSqCylinder, "patchCreateSquareCylinder");
}
else if (typeStr == "sphere")
{
createPrefabInternal(eSphere, "patchCreateSphere");
}
else if (typeStr == "endcap")
{
createPrefabInternal(eEndCap, "patchCreateCaps");
}
else if (typeStr == "bevel")
{
createPrefabInternal(eBevel, "patchCreateBevel");
}
else if (typeStr == "cone")
{
createPrefabInternal(eCone, "patchCreateCone");
}
}
void createCylinder(const cmd::ArgumentList& args)
{
createPrefabInternal(eCylinder, "patchCreateCylinder");
}
void createDenseCylinder(const cmd::ArgumentList& args)
{
createPrefabInternal(eDenseCylinder, "patchCreateDenseCylinder");
}
void createVeryDenseCylinder(const cmd::ArgumentList& args)
{
createPrefabInternal(eVeryDenseCylinder, "patchCreateVeryDenseCylinder");
}
void createSquareCylinder(const cmd::ArgumentList& args)
{
createPrefabInternal(eSqCylinder, "patchCreateSquareCylinder");
}
void createSphere(const cmd::ArgumentList& args)
{
createPrefabInternal(eSphere, "patchCreateSphere");
}
void createEndcap(const cmd::ArgumentList& args)
{
createPrefabInternal(eEndCap, "patchCreateCaps");
}
void createBevel(const cmd::ArgumentList& args)
{
createPrefabInternal(eBevel, "patchCreateBevel");
}
void createCone(const cmd::ArgumentList& args)
{
createPrefabInternal(eCone, "patchCreateCone");
}
// Sanitise the integer to specify a valid patch dimension
// will return 0 if the input is invalid
std::size_t checkPatchDimension(int input)
{
// Must be an odd number in [3..15]
if (input < 3 || input > 15 || input % 2 == 0)
{
return 0;
}
// all good
return static_cast<std::size_t>(input);
}
void createSimplePatch(const cmd::ArgumentList& args)
{
std::size_t width = 0;
std::size_t height = 0;
bool removeSelectedBrush = false;
if (args.empty() || args.size() > 3)
{
throw cmd::ExecutionFailure(_("Invalid number of arguments"));
}
if (args.size() == 1)
{
// Try to convert the arguments to actual integers and do the range checks
width = height = checkPatchDimension(args[0].getInt());
}
else if (args.size() == 2)
{
width = checkPatchDimension(args[0].getInt());
height = checkPatchDimension(args[1].getInt());
}
else if (args.size() == 3)
{
width = checkPatchDimension(args[0].getInt());
height = checkPatchDimension(args[1].getInt());
removeSelectedBrush = args[2].getBoolean();
}
// Only fire the dialog if no or invalid command arguments are given
if (width != 0 && height != 0)
{
UndoableCommand undo("patchCreatePlane");
// Retrieve the boundaries before any delete operation
AABB bounds = getDefaultBoundsFromSelection();
if (removeSelectedBrush)
{
// Delete the selection, the should be only one brush selected
selection::algorithm::deleteSelection();
}
// Call the PatchConstruct routine (GtkRadiant legacy)
constructPrefab(bounds,
getSelectedShader(),
ePlane, GlobalOrthoViewManager().getActiveViewType(),
width, height);
}
}
scene::INodePtr constructCap(const IPatch& sourcePatch, CapType capType, bool front, const std::string& material)
{
auto cap = GlobalPatchModule().createPatch(PatchDefType::Def2);
auto& capPatch = *Node_getPatch(cap);
auto width = sourcePatch.getWidth();
auto height = sourcePatch.getHeight();
std::vector<Vector3> points(sourcePatch.getWidth());
auto row = front ? 0 : height - 1;
for (auto i = 0; i < width; i++)
{
const auto& ctrl = sourcePatch.ctrlAt(row, i);
points[front ? i : width - 1 - i] = ctrl.vertex;
}
// Inherit the same fixed tesselation as the source patch
if (sourcePatch.subdivisionsFixed())
{
const auto& subdivisions = sourcePatch.getSubdivisions();
if (capType == CapType::InvertedEndCap)
{
capPatch.setFixedSubdivisions(true, subdivisions);
}
else
{
// Flip the subdivision X/Y values for all other cap types
capPatch.setFixedSubdivisions(true, { subdivisions.y(), subdivisions.x() });
}
}
capPatch.constructSeam(capType, points, width);
// greebo: Avoid creating "degenerate" patches (all vertices merged in one 3D point)
if (capPatch.isDegenerate())
{
return {};
}
// greebo: Apply natural texture to that patch, to fix the texcoord==1.#INF bug.
capPatch.setShader(material);
capPatch.scaleTextureNaturally();
return cap;
}
void createCaps(const IPatch& patch, const scene::INodePtr& parent, CapType type, const std::string& shader)
{
if ((type == CapType::EndCap || type == CapType::InvertedEndCap) && patch.getWidth() != 5)
{
throw cmd::ExecutionFailure(_("Cannot create end-cap, patch must have a width of 5."));
}
if ((type == CapType::Bevel || type == CapType::InvertedBevel) && patch.getWidth() != 3)
{
throw cmd::ExecutionFailure(_("Cannot create bevel-cap, patch must have a width of 3."));
}
if (type == CapType::Cylinder && patch.getWidth() != 9)
{
throw cmd::ExecutionFailure(_("Cannot create cylinder-cap, patch must have a width of 9."));
}
assert(parent);
// We do this once for the front and once for the back patch
for (auto front : { true, false })
{
auto cap = constructCap(patch, type, front, shader);
if (cap)
{
parent->addChildNode(cap);
Node_setSelected(cap, true);
}
}
}
}
} // namespace
|