Skip to content

Commit

Permalink
#6035: Add unit test covering the regression introduced in #5504
Browse files Browse the repository at this point in the history
  • Loading branch information
codereader committed Jul 31, 2022
1 parent fae7d83 commit 9c712b6
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 3 deletions.
34 changes: 34 additions & 0 deletions test/Models.cpp
Expand Up @@ -664,4 +664,38 @@ TEST_F(ModelTest, ModelKeyMonitorsDefAfterUndo)
EXPECT_EQ(model->getIModel().getPolyCount(), 96) << "Polycount is incorrect after the def change";
}

// #6035: ReloadDecls causes the ModelKey to refresh the model, resetting its transform to identity
TEST_F(ModelTest, ModelOriginIsPreservedAfterDefChange)
{
auto modelDef = GlobalEntityClassManager().findModel("reload_decl_test_model");
EXPECT_TRUE(modelDef) << "ModelDef not found, cannot continue test";
EXPECT_FALSE(os::fileOrDirExists(_context.getTestProjectPath() + modelDef->getMesh())) << "ModelDef shouldn't exist on disk";

auto funcStatic = algorithm::createEntityByClassName("func_static");
funcStatic->getEntity().setKeyValue("model", modelDef->getDeclName());
funcStatic->getEntity().setKeyValue("origin", "300 100 50");

scene::addNodeToContainer(funcStatic, GlobalMapModule().getRoot());
auto modelNode = algorithm::findChildModelNode(funcStatic);
EXPECT_TRUE(modelNode) << "No ModelNode found after assigning the key";

// Check and remember this translation
auto translation = modelNode->localToWorld().tCol();
EXPECT_TRUE(math::isNear(translation, { 300, 100, 50 }, 0.001));
EXPECT_TRUE(math::isNear(translation, funcStatic->localToWorld().tCol(), 0.001));

// Change the syntax block of the def
auto newMd5Mesh = "models/md5/flag01.md5mesh";
auto syntax = modelDef->getBlockSyntax();
string::replace_all(syntax.contents, modelDef->getMesh(), newMd5Mesh);
modelDef->setBlockSyntax(syntax);

modelNode = algorithm::findChildModelNode(funcStatic);

EXPECT_TRUE(modelNode) << "No ModelNode found on the entity after changing the def";
auto newTranslation = modelNode->localToWorld().tCol();
EXPECT_TRUE(math::isNear(newTranslation, translation, 0.001))
<< "Translation changed after reloading the def, was " << translation << ", it changed to " << newTranslation;
}

}
11 changes: 8 additions & 3 deletions test/algorithm/Scene.h
Expand Up @@ -151,17 +151,17 @@ inline void setWorldspawnKeyValue(const std::string& key, const std::string& val
Node_getEntity(entity)->setKeyValue(key, value);
}

inline model::ModelNodePtr findChildModel(const scene::INodePtr& parent)
inline scene::INodePtr findChildModelNode(const scene::INodePtr& parent)
{
model::ModelNodePtr candidate;
scene::INodePtr candidate;

parent->foreachNode([&](const scene::INodePtr& node)
{
auto model = Node_getModel(node);

if (model)
{
candidate = model;
candidate = node;
return false;
}

Expand All @@ -171,6 +171,11 @@ inline model::ModelNodePtr findChildModel(const scene::INodePtr& parent)
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)
Expand Down

0 comments on commit 9c712b6

Please sign in to comment.