Debian Codegraph @

include/ientity.h

raw
1 #pragma once
2
3 #include "inode.h"
4 #include "imodule.h"
5 #include "irender.h"
6 #include "inameobserver.h"
7 #include "iscenegraph.h"
8 #include "itransformnode.h"
9 #include <functional>
10
11 #include "string/predicate.h"
12
13 class IEntityClass;
14 typedef std::shared_ptr<IEntityClass> IEntityClassPtr;
15 typedef std::shared_ptr<const IEntityClass> IEntityClassConstPtr;
16
17 // Observes a single entity key value and gets notified on change
18 class KeyObserver: public sigc::trackable
19 {
20 public:
21 using Ptr = std::shared_ptr<KeyObserver>;
22
23 virtual ~KeyObserver() {}
24
25 /**
26 * This event gets called when the observed keyvalue changes.
27 * The new value is passed as argument, which can be an empty string.
28 */
29 virtual void onKeyValueChanged(const std::string& newValue) = 0;
30 };
31
32 /**
33 * @brief Object representing a single keyvalue (spawnarg) on an entity.
34 *
35 * This class exists so that each spawnarg can have its own independent set of
36 * KeyObservers responding to changes in its value. For most purposes it is
37 * simpler to use Entity::Observer::onKeyChange, Entity::setKeyValue and
38 * Entity::getKeyValue to interact with key values.
39 */
40 class EntityKeyValue: public NameObserver
41 {
42 public:
43 virtual ~EntityKeyValue() {}
44
45 /// Retrieves the actual value of this key
46 virtual const std::string& get() const = 0;
47
48 /// Sets the value of this key
49 virtual void assign(const std::string& other) = 0;
50
51 /// Attaches a callback to get notified about the key change.
52 virtual void attach(KeyObserver& observer) = 0;
53
54 /**
55 * @brief Detach the given observer from this key value.
56 *
57 * @param observer
58 * Observer to detach. No action will be taken if this observer is not
59 * already attached.
60 *
61 * @param sendEmptyValue
62 * If true (the default), the observer will be invoked with an empty value
63 * before being detached. If false, no final value will be sent.
64 */
65 virtual void detach(KeyObserver& observer, bool sendEmptyValue = true) = 0;
66 };
67 typedef std::shared_ptr<EntityKeyValue> EntityKeyValuePtr;
68
69 /**
70 * Interface for a map entity. The Entity is the main building block of a
71 * map, and the uppermost layer in the scenegraph under the root node. Each
72 * entity contains a arbitrary dictionary of strings ("properties" or
73 * "spawnargs") containing information about this entity which is used by the
74 * game engine to modify its behaviour, and may additionally contain child
75 * primitives (brushes and patches) depending on its type.
76 *
77 * At the minimum, each Entity must contain three properties: "name" which
78 * contains a map-unique string identifier, "classname" which identifies the
79 * entity class to the game, and "origin" which stores the location of the
80 * entity in 3-dimensional world space.
81 *
82 * A valid <b>Id Tech 4</b> map must contain at least one entity: the
83 * "worldspawn" which is the parent of all map geometry primitives.
84 *
85 * greebo: Note that keys are treated case-insensitively in Doom 3, so
86 * the Entity class will return the same result for "MYKeY" as for "mykey".
87 */
88 class Entity
89 {
90 public:
91 // A container with key => value pairs
92 typedef std::vector< std::pair<std::string, std::string> > KeyValuePairs;
93
94 /**
95 * \brief
96 * Abstract base class for entity observers.
97 *
98 * An entity observer receives notifications when keyvalues are inserted or
99 * deleted on the entity it is observing.
100 */
101 class Observer
102 {
103 public:
104 virtual ~Observer() {}
105
106 /**
107 * \brief
108 * Notification that a new key value has been inserted on the entity.
109 */
110 virtual void onKeyInsert(const std::string& key, EntityKeyValue& value)
111 { }
112
113 /**
114 * \brief
115 * Notification that a key value has changed on the entity.
116 */
117 virtual void onKeyChange(const std::string& key, const std::string& val)
118 { }
119
120 /**
121 * \brief
122 * Notification that a key value has been removed from the entity.
123 */
124 virtual void onKeyErase(const std::string& key, EntityKeyValue& value)
125 { }
126 };
127
128 // Function typedef to visit actual EntityKeyValue objects, not just the string values
129 typedef std::function<void(const std::string& key, EntityKeyValue& value)> EntityKeyValueVisitFunctor;
130
131 virtual ~Entity() {}
132
133 /**
134 * Return the entity class object for this entity.
135 */
136 virtual IEntityClassPtr getEntityClass() const = 0;
137
138 /// Functor to receive keys and values as strings
139 using KeyValueVisitFunc = std::function<
140 void(const std::string&, const std::string&)
141 >;
142
143 /**
144 * \brief Enumerate all keys and values on this entity, optionally including
145 * inherited spawnargs.
146 *
147 * \param func
148 * Functor to receive each key and its associated value.
149 *
150 * \param includeInherited
151 * true if the functor should be invoked for each inherited spawnarg (from
152 * the entity class), false if only explicit spawnargs on this particular
153 * entity should be visited.
154 */
155 virtual void forEachKeyValue(KeyValueVisitFunc func,
156 bool includeInherited = false) const = 0;
157
158 // Similar to above, visiting the EntityKeyValue objects itself, not just the string value.
159 virtual void forEachEntityKeyValue(const EntityKeyValueVisitFunctor& visitor) = 0;
160
161 /** Set a key value on this entity. Setting the value to "" will
162 * remove the key.
163 *
164 * @param key
165 * The key to set.
166 *
167 * @param value
168 * Value to give the key, or the empty string to remove the key.
169 */
170 virtual void setKeyValue(const std::string& key,
171 const std::string& value) = 0;
172
173 /* Retrieve a key value from the entity.
174 *
175 * @param key
176 * The key to retrieve.
177 *
178 * @returns
179 * The current value for this key, or the empty string if it does not
180 * exist.
181 */
182 virtual std::string getKeyValue(const std::string& key) const = 0;
183
184 /**
185 * greebo: Checks whether the given key is inherited or not.
186 *
187 * @returns: TRUE if the value is inherited,
188 * FALSE when it is not or when the key doesn't exist at all.
189 */
190 virtual bool isInherited(const std::string& key) const = 0;
191
192 /**
193 * \brief Return the list of keyvalues matching the given prefix.
194 *
195 * This method performs a search for all spawnargs whose key matches the
196 * given prefix, with a suffix consisting of zero or more arbitrary
197 * characters. For example, if "target" were specified as the prefix, the
198 * list would include "target", "target0", "target127" etc.
199 *
200 * This operation may not have high performance, due to the need to scan
201 * for matching names, therefore should not be used in performance-critical
202 * code.
203 *
204 * @param prefix
205 * The prefix to search for, interpreted case-insensitively.
206 *
207 * @return
208 * A list of KeyValue pairs matching the provided prefix. This list will be
209 * empty if there were no matches.
210 */
211 KeyValuePairs getKeyValuePairs(const std::string& prefix) const
212 {
213 KeyValuePairs list;
214
215 forEachKeyValue([&](const std::string& k, const std::string& v) {
216 if (string::istarts_with(k, prefix))
217 list.push_back(std::make_pair(k, v));
218 });
219
220 return list;
221 }
222
223 /** greebo: Returns true if the entity is a model. For Doom3, this is
224 * usually true when the classname == "func_static" and
225 * the non-empty spawnarg "model" != "name".
226 */
227 virtual bool isModel() const = 0;
228
229 /**
230 * Returns true if this entity is the worldspawn, which can be game-specific,
231 * but is usually true if this entity's classname equals "worldspawn"
232 */
233 virtual bool isWorldspawn() const = 0;
234
235 virtual bool isContainer() const = 0;
236
237 /**
238 * \brief
239 * Attach an Entity::Observer to this Entity.
240 */
241 virtual void attachObserver(Observer* observer) = 0;
242
243 /**
244 * \brief
245 * Detach an Entity::Observer from this Entity.
246 */
247 virtual void detachObserver(Observer* observer) = 0;
248
249 /**
250 * Returns true if this entity is of type or inherits from the
251 * given entity class name. className is treated case-sensitively.
252 */
253 virtual bool isOfType(const std::string& className) = 0;
254
255 /* ENTITY ATTACHMENTS */
256
257 /// Details of an attached entity
258 struct Attachment
259 {
260 /// Entity class of the attached entity
261 std::string eclass;
262
263 // Optional attachment name (arbitrarily chosen by the user)
264 std::string name;
265
266 /// Vector offset where the attached entity should appear
267 Vector3 offset;
268
269 /// Optional model joint to use as origin
270 std::string joint;
271 };
272
273 /// A functor which can receive Attachment objects
274 using AttachmentFunc = std::function<void(const Attachment&)>;
275
276 /**
277 * \brief Iterate over attached entities, if any.
278 *
279 * Each entity can define one or more attached entities, which should
280 * appear at specific offsets relative to the parent entity. Such attached
281 * entities are for visualisation only, and should not be saved into the
282 * map as genuine map entities.
283 *
284 * \param func
285 * Functor to receive attachment information.
286 */
287 virtual void forEachAttachment(AttachmentFunc func) const = 0;
288 };
289
290 /// Callback for an entity key value change
291 using KeyObserverFunc = sigc::slot<void(const std::string&)>;
292
293 /**
294 * \brief Interface for a node which represents an entity.
295 *
296 * As well as providing access to the entity data with getEntity(), every
297 * IEntityNode can clone itself and apply a transformation matrix to its
298 * children (which might be brushes, patches or other entities).
299 */
300 class IEntityNode : public IRenderEntity,
301 public virtual scene::INode,
302 public scene::Cloneable,
303 public IMatrixTransform
304 {
305 public:
306 virtual ~IEntityNode() {}
307
308 /// Get a modifiable reference to the contained Entity
309 virtual Entity& getEntity() = 0;
310
311 /**
312 * @brief Observe key value changes using a callback function.
313 *
314 * This method provides a simpler interface for observing key value changes
315 * via the use of a callback function, rather than requiring a full
316 * KeyObserver object to be constructed and maintained by the calling code.
317 *
318 * @param key
319 * The key to observe.
320 *
321 * @param func
322 * Function to call when the key value changes.
323 */
324 virtual void observeKey(const std::string& key, KeyObserverFunc func) = 0;
325
326 /**
327 * greebo: Tells the entity to reload the child model. This usually
328 * includes removal of the child model node and triggering
329 * a "skin changed" event.
330 */
331 virtual void refreshModel() = 0;
332
333 /**
334 * Invokes the given function object for each attached entity.
335 * At this point attachment entities are not accessible through the node's children,
336 * they have to be accessed through this method instead.
337 */
338 virtual void foreachAttachment(const std::function<void(const IEntityNodePtr&)>& functor) = 0;
339 };
340 typedef std::shared_ptr<IEntityNode> IEntityNodePtr;
341
342 inline Entity* Node_getEntity(const scene::INodePtr& node)
343 {
344 IEntityNodePtr entityNode = std::dynamic_pointer_cast<IEntityNode>(node);
345
346 if (entityNode != NULL) {
347 return &(entityNode->getEntity());
348 }
349
350 return NULL;
351 }
352
353 inline bool Node_isEntity(const scene::INodePtr& node)
354 {
355 //assert(!((std::dynamic_pointer_cast<IEntityNode>(node) != nullptr) ^ (node->getNodeType() == scene::INode::Type::Entity)));
356 return node->getNodeType() == scene::INode::Type::Entity;
357 }
358
359 // Represents a set of selected entities
360 class IEntitySelection
361 {
362 public:
363 virtual ~IEntitySelection() {}
364
365 // True if there's no selected entity available
366 virtual bool empty() const = 0;
367
368 // Returns the number of selected entities
369 virtual std::size_t size() const = 0;
370
371 // Iterates over each selected entity node, invoking the given functor
372 virtual void foreachEntity(const std::function<void(const IEntityNodePtr&)>& functor) = 0;
373
374 // Returns the key value shared by all entities in this set, or an empty string
375 // if there is no such value.
376 virtual std::string getSharedKeyValue(const std::string& key, bool includeInherited) = 0;
377 };
378
379 /**
380 * greebo: This is an abstract representation of a target.
381 * In Doom3 maps, a Target can be any entity node, that's
382 * why this object encapsulates a reference to an actual
383 * scene::INode.
384 *
385 * Note: Such a Target object can be empty. That's the case for
386 * entities referring to non-existing entities in their
387 * "target" spawnarg.
388 *
389 * All ITargetableObjects are owned by the TargetManager class.
390 */
391 class ITargetableObject
392 {
393 public:
394 virtual ~ITargetableObject() {}
395
396 // Returns the scene node behind this target. If the named target
397 // cannot be resolved in the current scene, an empty pointer is returned.
398 virtual const scene::INode* getNode() const = 0;
399
400 // Use this method to check whether the node can be resolved
401 virtual bool isEmpty() const = 0;
402 };
403 typedef std::shared_ptr<ITargetableObject> ITargetableObjectPtr;
404
405 /**
406 * greebo: The TargetManager keeps track of all ITargetableObjects
407 * in the current scene/map. A TargetManager instance is owned
408 * by the RootNode. TargetManager instances can be acquired through
409 * the EntityCreator interface.
410 *
411 * Clients acquire a named ITargetableObjectPtr by calling getTarget(). This
412 * always succeeds - if the named ITargetableObject is not found,
413 * a new, empty one is created.
414 *
415 * ITargetableObject object (can be empty)
416 * ________
417 * / \
418 * Entity | |
419 * TargetKey ----->>| -------->> holds scene::INodePtr (==NULL, if empty)
420 * | |
421 * \________/
422 */
423 class ITargetManager
424 {
425 public:
426 /**
427 * Returns the Target with the given name.
428 * This never returns NULL, an ITargetableObject is created if it doesn't exist yet.
429 */
430 virtual ITargetableObjectPtr getTarget(const std::string& name) = 0;
431
432 /**
433 * greebo: Associates the named Target with the given scene::INode.
434 * The Target will be created if it doesn't exist yet.
435 */
436 virtual void associateTarget(const std::string& name, const scene::INode& node) = 0;
437
438 // Will be called by a TargetableNode to notify about visibility changes
439 virtual void onTargetVisibilityChanged(const std::string& name, const scene::INode& node) = 0;
440
441 // Will be called by a TargetableNode to notify about a position change
442 virtual void onTargetPositionChanged(const std::string& name, const scene::INode& node) = 0;
443
444 /**
445 * greebo: Disassociates the Target from the given name. The node
446 * must also be passed to allow the manager to check the request.
447 * Otherwise it would be possible for cloned nodes to dissociate
448 * the target from their source node.
449 */
450 virtual void clearTarget(const std::string& name, const scene::INode& node) = 0;
451 };
452 typedef std::shared_ptr<ITargetManager> ITargetManagerPtr;
453
454 enum class LightEditVertexType : std::size_t
455 {
456 StartEndDeselected,
457 StartEndSelected,
458 Inactive,
459 Deselected,
460 Selected,
461 NumberOfVertexTypes,
462 };
463
464 constexpr const char* const RKEY_SHOW_ENTITY_NAMES("user/ui/xyview/showEntityNames");
465 constexpr const char* const RKEY_SHOW_ALL_SPEAKER_RADII = "user/ui/showAllSpeakerRadii";
466 constexpr const char* const RKEY_SHOW_ALL_LIGHT_RADII = "user/ui/showAllLightRadii";
467 constexpr const char* const RKEY_DRAG_RESIZE_SYMMETRICALLY = "user/ui/dragResizeEntitiesSymmetrically";
468 constexpr const char* const RKEY_ALWAYS_SHOW_LIGHT_VERTICES = "user/ui/alwaysShowLightVertices";
469 constexpr const char* const RKEY_FREE_OBJECT_ROTATION = "user/ui/rotateObjectsIndependently";
470 constexpr const char* const RKEY_SHOW_ENTITY_ANGLES = "user/ui/xyview/showEntityAngles";
471
472 /**
473 * Global entity settings affecting appearance, render options, etc.
474 */
475 class IEntitySettings
476 {
477 public:
478 virtual ~IEntitySettings() {}
479
480 virtual const Vector3& getLightVertexColour(LightEditVertexType type) const = 0;
481 virtual void setLightVertexColour(LightEditVertexType type, const Vector3& value) = 0;
482
483 virtual bool getRenderEntityNames() const = 0;
484 virtual void setRenderEntityNames(bool value) = 0;
485
486 virtual bool getShowAllSpeakerRadii() const = 0;
487 virtual void setShowAllSpeakerRadii(bool value) = 0;
488
489 virtual bool getShowAllLightRadii() const = 0;
490 virtual void setShowAllLightRadii(bool value) = 0;
491
492 virtual bool getDragResizeEntitiesSymmetrically() const = 0;
493 virtual void setDragResizeEntitiesSymmetrically(bool value) = 0;
494
495 virtual bool getAlwaysShowLightVertices() const = 0;
496 virtual void setAlwaysShowLightVertices(bool value) = 0;
497
498 virtual bool getFreeObjectRotation() const = 0;
499 virtual void setFreeObjectRotation(bool value) = 0;
500
501 virtual bool getShowEntityAngles() const = 0;
502 virtual void setShowEntityAngles(bool value) = 0;
503
504 virtual sigc::signal<void>& signal_settingsChanged() = 0;
505 };
506
507 constexpr const char* const MODULE_ENTITY("EntityModule");
508
509 /**
510 * \brief
511 * Interface for the entity module.
512 */
513 class IEntityModule :
514 public RegisterableModule
515 {
516 public:
517 virtual ~IEntityModule() {}
518
519 /// Create an entity node with the given entity class.
520 virtual IEntityNodePtr createEntity(const IEntityClassPtr& eclass) = 0;
521
522 // Constructs a new targetmanager instance (used by root nodes)
523 virtual ITargetManagerPtr createTargetManager() = 0;
524
525 // Access to the settings manager
526 virtual IEntitySettings& getSettings() = 0;
527
528 /**
529 * Create an instance of the given entity at the given position, and return
530 * the Node containing the new entity.
531 *
532 * @returns: the scene::IEntityNodePtr referring to the new entity.
533 * @throws: cmd::ExecutionFailure if anything goes wrong or the selection is not suitable.
534 */
535 virtual IEntityNodePtr createEntityFromSelection(const std::string& name, const Vector3& origin) = 0;
536 };
537
538 inline IEntityModule& GlobalEntityModule()
539 {
540 static module::InstanceReference<IEntityModule> _reference(MODULE_ENTITY);
541 return _reference;
542 }