Skip to content

Commit

Permalink
#5813: Add new method IEntityClass::getAttributeType() which should b…
Browse files Browse the repository at this point in the history
…e used to determine the key type, respecting the ancestor defs.
  • Loading branch information
codereader committed Nov 21, 2021
1 parent 9359f16 commit 316bfda
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 deletions.
17 changes: 11 additions & 6 deletions include/ieclass.h
Expand Up @@ -138,10 +138,11 @@ typedef std::shared_ptr<const IEntityClass> IEntityClassConstPtr;
*
* \ingroup eclass
*/
class IEntityClass
: public ModResource
class IEntityClass :
public ModResource
{
public:
virtual ~IEntityClass() {}

/// Signal emitted when entity class contents are changed or reloaded
virtual sigc::signal<void>& changedSignal() = 0;
Expand Down Expand Up @@ -201,14 +202,18 @@ class IEntityClass
* A reference to the named EntityClassAttribute. If the named attribute is
* not found, an empty EntityClassAttribute is returned.
*/
virtual EntityClassAttribute&
getAttribute(const std::string& name, bool includeInherited = true) = 0;
virtual EntityClassAttribute& getAttribute(const std::string& name,
bool includeInherited = true) = 0;

/// Get a const EntityClassAttribute reference by name
virtual const EntityClassAttribute&
getAttribute(const std::string& name,
virtual const EntityClassAttribute& getAttribute(const std::string& name,
bool includeInherited = true) const = 0;

// Returns the attribute type string for the given name.
// This method will walk up the inheritance hierarchy until it encounters a type definition.
// If no type is found, an empty string will be returned.
virtual const std::string& getAttributeType(const std::string& name) const = 0;

/**
* Function that will be invoked by forEachAttribute.
*
Expand Down
14 changes: 11 additions & 3 deletions radiantcore/eclass/EntityClass.cpp
Expand Up @@ -323,9 +323,7 @@ EntityClassAttribute& EntityClass::getAttribute(const std::string& name,
}

// Find a single attribute
const EntityClassAttribute&
EntityClass::getAttribute(const std::string& name,
bool includeInherited) const
const EntityClassAttribute& EntityClass::getAttribute(const std::string& name, bool includeInherited) const
{
// First look up the attribute on this class; if found, we can simply return it
auto f = _attributes.find(name);
Expand All @@ -342,6 +340,11 @@ EntityClass::getAttribute(const std::string& name,
return _parent->getAttribute(name);
}

const std::string& EntityClass::getAttributeType(const std::string& name) const
{
return _emptyAttribute.getType();
}

void EntityClass::clear()
{
// Don't clear the name
Expand Down Expand Up @@ -403,6 +406,11 @@ void EntityClass::parseFromTokens(parser::DefTokeniser& tokeniser)
// Required open brace (the name has already been parsed by the EClassManager)
tokeniser.assertNextToken("{");

if (getName() == "atdm:security_camera01")
{
int i = 6;
}

// Loop over all of the keys in this entitydef
std::string key;
while ((key = tokeniser.nextToken()) != "}")
Expand Down
8 changes: 3 additions & 5 deletions radiantcore/eclass/EntityClass.h
Expand Up @@ -149,11 +149,9 @@ class EntityClass
void resetColour();
const std::string& getWireShader() const override;
const std::string& getFillShader() const override;
EntityClassAttribute& getAttribute(const std::string&,
bool includeInherited = true) override;
const EntityClassAttribute&
getAttribute(const std::string&,
bool includeInherited = true) const override;
EntityClassAttribute& getAttribute(const std::string&, bool includeInherited = true) override;
const EntityClassAttribute& getAttribute(const std::string&, bool includeInherited = true) const override;
const std::string& getAttributeType(const std::string& name) const override;
void forEachAttribute(AttributeVisitor, bool) const override;

const std::string& getModelPath() const override { return _model; }
Expand Down
17 changes: 17 additions & 0 deletions test/Entity.cpp
Expand Up @@ -1614,4 +1614,21 @@ TEST_F(EntityTest, MovePlayerStart)
EXPECT_EQ(Node_getEntity(playerStart)->getKeyValue("origin"), originalPosition) << "Origin change didn't get undone";
}

TEST_F(EntityTest, GetDefaultAttributeType)
{
auto eclass = GlobalEntityClassManager().findClass("attribute_type_test");
EXPECT_EQ(eclass->getAttributeType("ordinary_key"), "text");
}

TEST_F(EntityTest, GetNonInheritedAttributeType)
{
auto eclass = GlobalEntityClassManager().findClass("attribute_type_test");

// The "defined_bool" is defined on the eclass, next to its editor_bool descriptor
EXPECT_EQ(eclass->getAttributeType("defined_bool"), "bool");

// The "undefined_bool" is not directly set on the eclass
EXPECT_EQ(eclass->getAttributeType("undefined_bool"), "bool");
}

}
20 changes: 20 additions & 0 deletions test/resources/tdm/def/attribute_types.def
@@ -0,0 +1,20 @@

entityDef attribute_type_base
{

}

entityDef attribute_type_test
{
"inherit" "attribute_type_base"

// Just an ordinary key without any editor_ declarations
"ordinary_key" "Test"

// An editor_bool key without the actual key being defined on this class
"editor_bool undefined_bool" "Some bool description"

// An editor bool key with the key defined on this same class
"editor_bool defined_bool" "Some bool description"
"defined_bool" "1"
}

0 comments on commit 316bfda

Please sign in to comment.