Debian Codegraph @

include/ieclass.h

raw
1 /**
2 * \defgroup eclass Entity class manager
3 * \file ieclass.h
4 * \brief Entity Class definition loader API.
5 * \ingroup eclass
6 */
7 #pragma once
8
9 #include "ideclmanager.h"
10 #include "igameresource.h"
11
12 #include "imodule.h"
13 #include "ifilesystem.h"
14 #include "math/Vector4.h"
15
16 #include <vector>
17 #include <list>
18 #include <map>
19 #include <memory>
20 #include <sigc++/signal.h>
21
22 /* FORWARD DECLS */
23
24 class Shader;
25 typedef std::shared_ptr<Shader> ShaderPtr;
26 class AABB;
27
28 /**
29 * Data structure representing a single attribute on an entity class.
30 *
31 * \ingroup eclass
32 */
33 class EntityClassAttribute
34 {
35 private:
36
37 // Attribute type
38 std::string _type;
39
40 // Attribute name
41 std::string _name;
42
43 // Value
44 std::string _value;
45
46 // User-friendly description
47 std::string _desc;
48
49 public:
50 /**
51 * The key type (string, bool etc.).
52 */
53 const std::string& getType() const
54 {
55 return _type;
56 }
57
58 void setType(const std::string& type)
59 {
60 _type = type;
61 }
62
63 /// The attribute key name, e.g. "model", "editor_displayFolder" etc
64 const std::string& getName() const
65 {
66 return _name;
67 }
68
69 /// Get attribute value
70 const std::string& getValue() const
71 {
72 return _value;
73 }
74
75 /// Set attribute value
76 void setValue(const std::string& value)
77 {
78 _value = value;
79 }
80
81 /// The help text associated with the key (in the DEF file).
82 const std::string& getDescription() const
83 {
84 return _desc;
85 }
86
87 void setDescription(const std::string& desc)
88 {
89 _desc = desc;
90 }
91
92 /// Construct an EntityClassAttribute
93 EntityClassAttribute(const std::string& type_,
94 const std::string& name_,
95 const std::string& value_,
96 const std::string& description_ = "")
97 : _type(type_),
98 _name(name_),
99 _value(value_),
100 _desc(description_)
101 {}
102 };
103
104 /**
105 * IEntityClass shared pointer.
106 */
107 class IEntityClass;
108 typedef std::shared_ptr<IEntityClass> IEntityClassPtr;
109 typedef std::shared_ptr<const IEntityClass> IEntityClassConstPtr;
110
111 /**
112 * \brief Entity class interface.
113 *
114 * An entity class represents a single type of entity that can be created by
115 * the EntityCreator. Entity classes are parsed from .DEF files during startup.
116 *
117 * Entity class attribute names are compared case-insensitively, as in the
118 * Entity class.
119 *
120 * \ingroup eclass
121 */
122 class IEntityClass :
123 public decl::IDeclaration
124 {
125 public:
126 virtual ~IEntityClass() {}
127
128 // Enumeration of types DarkRadiant is capable of distinguishing when creating entities
129 enum class Type
130 {
131 Generic, // fixed-size, coloured boxes with and without arrow
132 StaticGeometry, // func_* entities supporting primitives (like worldspawn)
133 EntityClassModel, // non-fixed size entities with a non-empty "model" key set
134 Light, // all classes with editor_light/idLight or inheriting from them
135 Speaker, // special class used for "speaker" entityDefs
136 };
137
138 // Returns the type of this entity class (as determined after parsing)
139 virtual Type getClassType() = 0;
140
141 /// Signal emitted when entity class contents are changed or reloaded
142 virtual sigc::signal<void>& changedSignal() = 0;
143
144 /// Get the parent entity class or NULL if there is no parent
145 virtual IEntityClass* getParent() = 0;
146
147 /// Get the UI visibility of this entity class
148 virtual vfs::Visibility getVisibility() = 0;
149
150 /// Query whether this entity class represents a light.
151 virtual bool isLight() = 0;
152
153 /* ENTITY CLASS SIZE */
154
155 /// Query whether this entity has a fixed size.
156 virtual bool isFixedSize() = 0;
157
158 /**
159 * Return an AABB representing the declared size of this entity. This is
160 * only valid for fixed size entities.
161 *
162 * @returns
163 * AABB enclosing the "editor_mins" and "editor_maxs" points defined in the
164 * entityDef.
165 */
166 virtual AABB getBounds() = 0;
167
168 /* ENTITY CLASS COLOURS */
169
170 /// Return the display colour of this entity class
171 virtual const Vector4& getColour() = 0;
172
173 // Overrides the colour defined in the .def files
174 virtual void setColour(const Vector4& colour) = 0;
175
176 /* ENTITY CLASS ATTRIBUTES */
177
178 /**
179 * @brief Get the value of a specified attribute.
180 *
181 * @return std::string containing the attribute value, or an empty string if the attribute was
182 * not found.
183 */
184 virtual std::string getAttributeValue(const std::string& name,
185 bool includeInherited = true) = 0;
186
187 // Returns the attribute type string for the given name.
188 // This method will walk up the inheritance hierarchy until it encounters a type definition.
189 // If no type is found, an empty string will be returned.
190 virtual std::string getAttributeType(const std::string& name) = 0;
191
192 // Returns the attribute description string for the given name.
193 // This method will walk up the inheritance hierarchy until it encounters a non-empty description.
194 virtual std::string getAttributeDescription(const std::string& name) = 0;
195
196 /**
197 * Function that will be invoked by forEachAttribute.
198 *
199 * The function will be passed each EntityClassAttribute in turn, along
200 * with a bool indicating if this attribute is inherited from a parent
201 * entity class.
202 */
203 using AttributeVisitor = std::function<void(const EntityClassAttribute&, bool)>;
204
205 /**
206 * Enumerate the EntityClassAttibutes in turn, including all inherited
207 * attributes.
208 *
209 * \param visitor
210 * Function that will be invoked for each EntityClassAttibute.
211 *
212 * \param editorKeys
213 * true if editor keys (those which start with "editor_") should be passed
214 * to the visitor, false if they should be skipped.
215 */
216 virtual void forEachAttribute(AttributeVisitor visitor, bool editorKeys = false) = 0;
217
218 /**
219 * Returns true if this entity is of type or inherits from the
220 * given entity class name. className is treated case-sensitively.
221 */
222 virtual bool isOfType(const std::string& className) = 0;
223 };
224
225 /**
226 * Structure ontains the information of a model {} block as defined in a
227 * Doom3 .def file.
228 *
229 * \ingroup eclass
230 */
231 class IModelDef :
232 public decl::IDeclaration
233 {
234 public:
235 using Ptr = std::shared_ptr<IModelDef>;
236
237 // The def this model is inheriting from (empty if there's no parent)
238 virtual const Ptr& getParent() = 0;
239
240 // The MD5 mesh used by this modelDef
241 virtual const std::string& getMesh() = 0;
242
243 // The named skin
244 virtual const std::string& getSkin() = 0;
245
246 // The md5anim file name for the given anim key (e.g. "idle" or "af_pose")
247 virtual std::string getAnim(const std::string& animKey) = 0;
248
249 // Returns a dictionary of all the animations declared on this model def
250 using Anims = std::map<std::string, std::string>;
251 virtual const Anims& getAnims() = 0;
252 };
253
254 /**
255 * EntityClass visitor interface.
256 *
257 * \ingroup eclass
258 */
259 class EntityClassVisitor
260 {
261 public:
262 virtual ~EntityClassVisitor() {}
263 virtual void visit(const IEntityClassPtr& eclass) = 0;
264 };
265
266 constexpr const char* const MODULE_ECLASSMANAGER("EntityClassManager");
267
268 /**
269 * EntityClassManager interface. The entity class manager is responsible for
270 * maintaining a list of available entity classes which the EntityCreator can
271 * insert into a map.
272 *
273 * \ingroup eclass
274 */
275 class IEntityClassManager :
276 public RegisterableModule
277 {
278 public:
279 /**
280 * Return the IEntityClass corresponding to the given name, creating it if
281 * necessary. If it is created, the has_brushes parameter will be used to
282 * determine whether the new entity class should be brush-based or not.
283 *
284 * @deprecated
285 * Use findClass() instead.
286 */
287 virtual IEntityClassPtr findOrInsert(const std::string& name,
288 bool has_brushes) = 0;
289
290 /**
291 * Lookup an entity class by name. If the class is not found, a null pointer
292 * is returned.
293 *
294 * @param name
295 * Name of the entity class to look up.
296 */
297 virtual IEntityClassPtr findClass(const std::string& name) = 0;
298
299 /**
300 * Iterate over all entity defs using the given visitor.
301 */
302 virtual void forEachEntityClass(EntityClassVisitor& visitor) = 0;
303
304 // Iterate over all entityDefs using the given function object
305 virtual void forEachEntityClass(const std::function<void(const IEntityClassPtr&)>& functor) = 0;
306
307 /**
308 * greebo: This reloads the entityDefs and modelDefs from all files. Does not
309 * change the scenegraph, only the contents of the EClass objects are
310 * re-parsed. All IEntityClassPtrs remain valid, no entityDefs are removed.
311 *
312 * Note: This is NOT the same as unrealise + realise
313 */
314 virtual void reloadDefs() = 0;
315
316 /**
317 * greebo: Finds the model def with the given name. Might return NULL if not found.
318 */
319 virtual IModelDef::Ptr findModel(const std::string& name) = 0;
320
321 /**
322 * Iterate over each ModelDef using the given function object.
323 */
324 virtual void forEachModelDef(const std::function<void(const IModelDef::Ptr&)>& functor) = 0;
325 };
326
327 /**
328 * Return the global EntityClassManager to the application.
329 *
330 * \ingroup eclass
331 */
332 inline IEntityClassManager& GlobalEntityClassManager()
333 {
334 static module::InstanceReference<IEntityClassManager> _reference(MODULE_ECLASSMANAGER);
335 return _reference;
336 }