Skip to content

Commit

Permalink
#5659: Refresh all entities referencing the model that has just been …
Browse files Browse the repository at this point in the history
…exported
  • Loading branch information
codereader committed Aug 22, 2021
1 parent f92ad92 commit 6153f08
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 36 deletions.
67 changes: 33 additions & 34 deletions radiantcore/map/algorithm/Export.cpp
Expand Up @@ -19,6 +19,7 @@
#include "registry/registry.h"
#include "scene/Traverse.h"
#include "command/ExecutionFailure.h"
#include "Models.h"

namespace map
{
Expand Down Expand Up @@ -101,40 +102,38 @@ void exportSelectedAsModel(const ModelExportOptions& options)

expFormat->exportToPath(outputPath, outputFile);

if (!options.replaceSelectionWithModel)
{
return; // done here
}

std::string relativeModelPath = os::getRelativePath(absOutputPath, rootPath);

UndoableCommand command("replaceModel");

// Remove the selection
selection::algorithm::deleteSelection();

// Create a func_static in its place
try
{
// Place the model in the world origin, unless we set "center objects" to true
Vector3 modelPos(0, 0, 0);

if (options.centerObjects)
{
modelPos = -exporter.getCenterTransform().tCol().getVector3();
}

scene::INodePtr modelNode = GlobalEntityModule().createEntityFromSelection("func_static", modelPos);

Node_getEntity(modelNode)->setKeyValue("model", relativeModelPath);

// It's possible that the export overwrote a model we're already using in this map, refresh it
GlobalModelCache().refreshSelectedModels(false);
}
catch (cmd::ExecutionFailure& ex)
{
throw std::runtime_error(fmt::format(_("Unable to create model: {0}"), ex.what()));
}
std::string relativeModelPath = os::getRelativePath(absOutputPath, rootPath);

if (options.replaceSelectionWithModel)
{
UndoableCommand command("replaceModel");

// Remove the selection
selection::algorithm::deleteSelection();

// Create a func_static in its place
try
{
// Place the model in the world origin, unless we set "center objects" to true
Vector3 modelPos(0, 0, 0);

if (options.centerObjects)
{
modelPos = -exporter.getCenterTransform().tCol().getVector3();
}

scene::INodePtr modelNode = GlobalEntityModule().createEntityFromSelection("func_static", modelPos);

Node_getEntity(modelNode)->setKeyValue("model", relativeModelPath);
}
catch (cmd::ExecutionFailure& ex)
{
throw std::runtime_error(fmt::format(_("Unable to create model: {0}"), ex.what()));
}
}

// It's possible that the export overwrote a model we're already using in this map, refresh it
refreshModelsByPath(relativeModelPath);
}

void exportSelectedAsModelCmd(const cmd::ArgumentList& args)
Expand Down
25 changes: 25 additions & 0 deletions radiantcore/map/algorithm/Models.cpp
Expand Up @@ -140,6 +140,31 @@ void refreshSelectedModels(bool blockScreenUpdates)
}
}

// Reloads all entities with their model spawnarg referencing the given model path.
// The given model path denotes a VFS path, i.e. it is mod/game-relative
void refreshModelsByPath(const std::string& relativeModelPath)
{
std::size_t refreshedEntityCount = 0;

GlobalModelCache().removeModel(relativeModelPath);

GlobalMapModule().getRoot()->foreachNode([&](const scene::INodePtr& node)
{
auto entity = std::dynamic_pointer_cast<IEntityNode>(node);

if (entity && entity->getEntity().getKeyValue("model") == relativeModelPath)
{
entity->refreshModel();
++refreshedEntityCount;
return false;
}

return true;
});

rMessage() << "Refreshed " << refreshedEntityCount << " entities using the model " << relativeModelPath << std::endl;
}

}

}
4 changes: 4 additions & 0 deletions radiantcore/map/algorithm/Models.h
Expand Up @@ -12,6 +12,10 @@ void refreshModels(bool blockScreenUpdates);
// This reloads all selected models in the map
void refreshSelectedModels(bool blockScreenUpdates);

// Reloads all entities with their model spawnarg referencing the given model path.
// The given model path denotes a VFS path, i.e. it is mod/game-relative
void refreshModelsByPath(const std::string& relativeModelPath);

}

}
Expand Down
1 change: 0 additions & 1 deletion tools/msvc/libs.vcxproj
Expand Up @@ -187,7 +187,6 @@
<ClInclude Include="..\..\libs\parser\ParseException.h" />
<ClInclude Include="..\..\libs\parser\Tokeniser.h" />
<ClInclude Include="..\..\libs\patch\PatchIterators.h" />
<ClInclude Include="..\..\libs\picomodel.h" />
<ClInclude Include="..\..\libs\pivot.h" />
<ClInclude Include="..\..\libs\RandomOrigin.h" />
<ClInclude Include="..\..\libs\Rectangle.h" />
Expand Down
1 change: 0 additions & 1 deletion tools/msvc/libs.vcxproj.filters
Expand Up @@ -2,7 +2,6 @@
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ClInclude Include="..\..\libs\EventRateLimiter.h" />
<ClInclude Include="..\..\libs\picomodel.h" />
<ClInclude Include="..\..\libs\pivot.h" />
<ClInclude Include="..\..\libs\RandomOrigin.h" />
<ClInclude Include="..\..\libs\render.h" />
Expand Down

0 comments on commit 6153f08

Please sign in to comment.