1 /*
2  Copyright (C) 2010-2014 Kristian Duske
3 
4  This file is part of TrenchBroom.
5 
6  TrenchBroom is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  TrenchBroom is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with TrenchBroom. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef TrenchBroom_EntityProperties
21 #define TrenchBroom_EntityProperties
22 
23 #include "StringUtils.h"
24 #include "StringMap.h"
25 #include "Model/EntityAttributeSnapshot.h"
26 #include "Model/ModelTypes.h"
27 
28 #include <map>
29 #include <list>
30 
31 namespace TrenchBroom {
32     namespace Assets {
33         class EntityDefinition;
34         class AttributeDefinition;
35     }
36 
37     namespace Model {
38         extern const String AttributeEscapeChars;
39 
40         namespace AttributeNames {
41             extern const AttributeName Classname;
42             extern const AttributeName Origin;
43             extern const AttributeName Wad;
44             extern const AttributeName Textures;
45             extern const AttributeName Mods;
46             extern const AttributeName Spawnflags;
47             extern const AttributeName EntityDefinitions;
48             extern const AttributeName Angle;
49             extern const AttributeName Angles;
50             extern const AttributeName Mangle;
51             extern const AttributeName Target;
52             extern const AttributeName Targetname;
53             extern const AttributeName Killtarget;
54             extern const AttributeName GroupType;
55             extern const AttributeName LayerId;
56             extern const AttributeName LayerName;
57             extern const AttributeName Layer;
58             extern const AttributeName GroupId;
59             extern const AttributeName GroupName;
60             extern const AttributeName Group;
61             extern const AttributeName Message;
62         }
63 
64         namespace AttributeValues {
65             extern const AttributeValue WorldspawnClassname;
66             extern const AttributeValue NoClassname;
67             extern const AttributeValue LayerClassname;
68             extern const AttributeValue GroupClassname;
69             extern const AttributeValue GroupTypeLayer;
70             extern const AttributeValue GroupTypeGroup;
71         }
72 
73         String numberedAttributePrefix(const String& name);
74         bool isNumberedAttribute(const String& prefix, const AttributeName& name);
75 
76         class EntityAttribute {
77         public:
78             typedef std::map<AttributableNode*, EntityAttribute> Map;
79             typedef std::list<EntityAttribute> List;
80             static const List EmptyList;
81         private:
82             AttributeName m_name;
83             AttributeValue m_value;
84             const Assets::AttributeDefinition* m_definition;
85         public:
86             EntityAttribute();
87             EntityAttribute(const AttributeName& name, const AttributeValue& value, const Assets::AttributeDefinition* definition = NULL);
88             bool operator<(const EntityAttribute& rhs) const;
89             int compare(const EntityAttribute& rhs) const;
90 
91             const AttributeName& name() const;
92             const AttributeValue& value() const;
93             const Assets::AttributeDefinition* definition() const;
94 
95             void setName(const AttributeName& name, const Assets::AttributeDefinition* definition);
96             void setValue(const AttributeValue& value);
97         };
98 
99         bool isLayer(const String& classname, const EntityAttribute::List& attributes);
100         bool isGroup(const String& classname, const EntityAttribute::List& attributes);
101         bool isWorldspawn(const String& classname, const EntityAttribute::List& attributes);
102         const AttributeValue& findAttribute(const EntityAttribute::List& attributes, const AttributeName& name, const AttributeValue& defaultValue = EmptyString);
103 
104         class EntityAttributes {
105         private:
106             EntityAttribute::List m_attributes;
107 
108             typedef EntityAttribute::List::iterator IndexValue;
109             typedef StringMapValueContainer<IndexValue> IndexValueContainer;
110             typedef StringMap<IndexValue, IndexValueContainer> AttributeIndex;
111             AttributeIndex m_index;
112         public:
113             const EntityAttribute::List& attributes() const;
114             void setAttributes(const EntityAttribute::List& attributes);
115 
116             const EntityAttribute& addOrUpdateAttribute(const AttributeName& name, const AttributeValue& value, const Assets::AttributeDefinition* definition);
117             void renameAttribute(const AttributeName& name, const AttributeName& newName, const Assets::AttributeDefinition* newDefinition);
118             void removeAttribute(const AttributeName& name);
119             void updateDefinitions(const Assets::EntityDefinition* entityDefinition);
120 
121             bool hasAttribute(const AttributeName& name) const;
122             bool hasAttribute(const AttributeName& name, const AttributeValue& value) const;
123             bool hasAttributeWithPrefix(const AttributeName& prefix, const AttributeValue& value) const;
124             bool hasNumberedAttribute(const AttributeName& prefix, const AttributeValue& value) const;
125 
126             EntityAttributeSnapshot snapshot(const AttributeName& name) const;
127         private:
128             bool containsValue(const AttributeIndex::QueryResult& matches, const AttributeValue& value) const;
129         public:
130             const AttributeValue* attribute(const AttributeName& name) const;
131             const AttributeValue& safeAttribute(const AttributeName& name, const AttributeValue& defaultValue) const;
132 
133             EntityAttribute::List numberedAttributes(const String& prefix) const;
134         private:
135             EntityAttribute::List::const_iterator findAttribute(const AttributeName& name) const;
136             EntityAttribute::List::iterator findAttribute(const AttributeName& name);
137 
138             void rebuildIndex();
139         };
140     }
141 }
142 
143 #endif /* defined(TrenchBroom_EntityProperties) */
144