Skip to content

Commit

Permalink
#5907: EntityClass::getVisibility() returns value from spawnargs
Browse files Browse the repository at this point in the history
Instead of a constant value, getVisibility() now returns the correct
value based on the presence of an "editor_visibility" spawnarg. The
value is initialised lazily by way of a new Lazy class template which
provides a generic means of lazily initialising a value using a callback
function.
  • Loading branch information
Matthew Mott committed Mar 30, 2022
1 parent 6bb4c83 commit 0722857
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 11 deletions.
30 changes: 30 additions & 0 deletions libs/generic/Lazy.h
@@ -0,0 +1,30 @@
#pragma once

#include <optional>
#include <functional>

/// Wrapper for an arbitrary object which is initialised lazily via a functor
template<typename T> class Lazy
{
// Contained value (may or may not be initialised)
mutable std::optional<T> _value;

// Functor to initialise _value when required
using InitialiseFunc = std::function<T()>;
InitialiseFunc _initFunc;

public:

/// Construct the Lazy wrapper with a functor to be called when initialisation is required
Lazy(InitialiseFunc func): _initFunc(func)
{}

/// Return the contained value, initialising via the functor if necessary
T get() const
{
if (!_value)
_value = _initFunc();

return *_value;
}
};
15 changes: 6 additions & 9 deletions radiantcore/eclass/EClassManager.cpp
Expand Up @@ -129,18 +129,15 @@ void EClassManager::resolveModelInheritance(const std::string& name, const Doom3

void EClassManager::parseDefFiles()
{
rMessage() << "searching vfs directory 'def' for *.def\n";
// Increase the parse stamp for this run
_curParseStamp++;

// Increase the parse stamp for this run
_curParseStamp++;

{
ScopedDebugTimer timer("EntityDefs parsed: ");
{
ScopedDebugTimer timer("EntityDefs parsed: ");
GlobalFileSystem().forEachFile(
"def/", "def",
[&](const vfs::FileInfo& fileInfo) { parseFile(fileInfo); }
"def/", "def", [&](const vfs::FileInfo& fileInfo) { parseFile(fileInfo); }
);
}
}
}

void EClassManager::resolveInheritance()
Expand Down
6 changes: 5 additions & 1 deletion radiantcore/eclass/EntityClass.cpp
Expand Up @@ -19,6 +19,10 @@ namespace

EntityClass::EntityClass(const std::string& name, bool fixedSize)
: _name(name),
_visibility([this]() {
return (getAttributeValue("editor_visibility") == "hidden" ? vfs::Visibility::HIDDEN
: vfs::Visibility::NORMAL);
}),
_colour(UndefinedColour),
_fixedSize(fixedSize)
{}
Expand All @@ -41,7 +45,7 @@ const IEntityClass* EntityClass::getParent() const

vfs::Visibility EntityClass::getVisibility() const
{
return _visibility;
return _visibility.get();
}

sigc::signal<void>& EntityClass::changedSignal()
Expand Down
3 changes: 2 additions & 1 deletion radiantcore/eclass/EntityClass.h
Expand Up @@ -7,6 +7,7 @@
#include "math/Vector3.h"
#include "math/AABB.h"
#include "string/string.h"
#include "generic/Lazy.h"

#include "parser/DefTokeniser.h"

Expand Down Expand Up @@ -43,7 +44,7 @@ class EntityClass
EntityClass* _parent = nullptr;

// UI visibility of this entity class
vfs::Visibility _visibility = vfs::Visibility::NORMAL;
Lazy<vfs::Visibility> _visibility;

// Should this entity type be treated as a light?
bool _isLight = false;
Expand Down
1 change: 1 addition & 0 deletions test/Entity.cpp
Expand Up @@ -1999,6 +1999,7 @@ TEST_F(EntityTest, GetEClassVisibility)
// Hidden entity
auto entityBase = GlobalEntityClassManager().findClass("atdm:entity_base");
ASSERT_TRUE(entityBase);
EXPECT_EQ(entityBase->getAttributeValue("editor_visibility"), "hidden");
EXPECT_EQ(entityBase->getVisibility(), vfs::Visibility::HIDDEN);
}

Expand Down

0 comments on commit 0722857

Please sign in to comment.