Overte C++ Documentation
EntityTypes.h
1 //
2 // EntityTypes.h
3 // libraries/entities/src
4 //
5 // Created by Brad Hefta-Gaub on December 4th, 2013.
6 // Copyright 2013 High Fidelity, Inc.
7 // Copyright 2023-2025 Overte e.V.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 //
12 
13 #ifndef hifi_EntityTypes_h
14 #define hifi_EntityTypes_h
15 
16 #include <memory>
17 
18 #include <QHash>
19 #include <QString>
20 
21 #include "EntitiesLogging.h"
22 
23 class EntityItem;
24 using EntityItemPointer = std::shared_ptr<EntityItem>;
25 using EntityItemWeakPointer = std::weak_ptr<EntityItem>;
26 
27 inline uint qHash(const EntityItemPointer& a, uint seed) {
28  return qHash(a.get(), seed);
29 }
30 
31 class EntityItemID;
32 class EntityItemProperties;
33 class ReadBitstreamToTreeParams;
34 
35 typedef EntityItemPointer (*EntityTypeFactory)(const EntityItemID& entityID, const EntityItemProperties& properties);
36 
37 class EntityTypes {
38 public:
39  /*@jsdoc
40  * <p>An entity may be one of the following types:</p>
41  * <table>
42  * <thead>
43  * <tr><th>Value</th><th>Description</th><th>Properties</th></tr>
44  * </thead>
45  * <tbody>
46  * <tr><td><code>"Shape"</code></td><td>A basic entity such as a cube.
47  * See also, the <code>"Box"</code> and <code>"Sphere"</code> entity types.</td>
48  * <td>{@link Entities.EntityProperties-Shape|EntityProperties-Shape}</td></tr>
49  * <tr><td><code>"Box"</code></td><td>A rectangular prism. This is a synonym of <code>"Shape"</code> for the case
50  * where the entity's <code>shape</code> property value is <code>"Cube"</code>.
51  * <p>If an entity is created with its <code>type</code>
52  * set to <code>"Box"</code> it will always be created with a <code>shape</code> property value of
53  * <code>"Cube"</code>. If an entity of type <code>Shape</code> or <code>Sphere</code> has its <code>shape</code> set
54  * to <code>"Cube"</code> then its <code>type</code> will be reported as <code>"Box"</code>.</p></td>
55  * <td>{@link Entities.EntityProperties-Box|EntityProperties-Box}</td></tr>
56  * <tr><td><code>"Sphere"</code></td><td>A sphere. This is a synonym of <code>"Shape"</code> for the case
57  * where the entity's <code>shape</code> property value is <code>"Sphere"</code>.
58  * <p>If an entity is created with its <code>type</code>
59  * set to <code>"Sphere"</code> it will always be created with a <code>shape</code> property value of
60  * <code>"Sphere"</code>. If an entity of type <code>Box</code> or <code>Shape</code> has its <code>shape</code> set
61  * to <code>"Sphere"</code> then its <code>type</code> will be reported as <code>"Sphere"</code>.</td>
62  * <td>{@link Entities.EntityProperties-Sphere|EntityProperties-Sphere}</td></tr>
63  * <tr><td><code>"Model"</code></td><td>A mesh model from a glTF, VRM, FBX, or OBJ file.</td>
64  * <td>{@link Entities.EntityProperties-Model|EntityProperties-Model}</td></tr>
65  * <tr><td><code>"Text"</code></td><td>A pane of text oriented in space.</td>
66  * <td>{@link Entities.EntityProperties-Text|EntityProperties-Text}</td></tr>
67  * <tr><td><code>"Image"</code></td><td>An image oriented in space.</td>
68  * <td>{@link Entities.EntityProperties-Image|EntityProperties-Image}</td></tr>
69  * <tr><td><code>"Web"</code></td><td>A browsable web page.</td>
70  * <td>{@link Entities.EntityProperties-Web|EntityProperties-Web}</td></tr>
71  * <tr><td><code>"ParticleEffect"</code></td><td>A particle system that can be used to simulate things such as fire,
72  * smoke, snow, magic spells, etc.</td>
73  * <td>{@link Entities.EntityProperties-ParticleEffect|EntityProperties-ParticleEffect}</td></tr>
74  * <tr><td><code>"ProceduralParticleEffect"</code></td><td>A GPU particle system with custom update and rendering that can
75  * be used to simulate things such as fire, smoke, snow, magic spells, etc.</td>
76  * <td>{@link Entities.EntityProperties-ProceduralParticleEffect|EntityProperties-ProceduralParticleEffect}</td></tr>
77  * <tr><td><code>"Line"</code></td><td>A sequence of one or more simple straight lines.</td>
78  * <td>{@link Entities.EntityProperties-Line|EntityProperties-Line}</td></tr>
79  * <tr><td><code>"PolyLine"</code></td><td>A sequence of one or more textured straight lines.</td>
80  * <td>{@link Entities.EntityProperties-PolyLine|EntityProperties-PolyLine}</td></tr>
81  * <tr><td><code>"PolyVox"</code></td><td>A set of textured voxels.</td>
82  * <td>{@link Entities.EntityProperties-PolyVox|EntityProperties-PolyVox}</td></tr>
83  * <tr><td><code>"Grid"</code></td><td>A grid of lines in a plane.</td>
84  * <td>{@link Entities.EntityProperties-Grid|EntityProperties-Grid}</td></tr>
85  * <tr><td><code>"Gizmo"</code></td><td>A gizmo intended for UI.</td>
86  * <td>{@link Entities.EntityProperties-Gizmo|EntityProperties-Gizmo}</td></tr>
87  * <tr><td><code>"Light"</code></td><td>A local lighting effect.</td>
88  * <td>{@link Entities.EntityProperties-Light|EntityProperties-Light}</td></tr>
89  * <tr><td><code>"Zone"</code></td><td>A volume of lighting effects and avatar permissions.</td>
90  * <td>{@link Entities.EntityProperties-Zone|EntityProperties-Zone}</td></tr>
91  * <tr><td><code>"Material"</code></td><td>Modifies the existing materials on entities and avatars.</td>
92  * <td>{@link Entities.EntityProperties-Material|EntityProperties-Material}</td></tr>
93  * <tr><td><code>"Sound"</code></td><td>Plays a sound.</td>
94  * <td>{@link Entities.EntityProperties-Sound|EntityProperties-Sound}</td></tr>
95  * <tr><td><code>"Canvas"</code></td><td>A runtime scriptable image.</td>
96  * <td>{@link Entities.EntityProperties-Canvas|EntityProperties-Canvas}</td></tr>
97  * <tr><td><code>"Empty"</code></td><td>Invisible, intangible entity that has no special behavior.</td>
98  * <td>{@link Entities.EntityProperties-Empty|EntityProperties-Empty}</td></tr>
99  * <tr><td><code>"Script"</code></td><td>Starts an entity script on its parent.</td>
100  * <td>{@link Entities.EntityProperties-Script|EntityProperties-Script}</td></tr>
101  * </tbody>
102  * </table>
103  * @typedef {string} Entities.EntityType
104  */
105  typedef enum EntityType_t {
106  Unknown,
107  Box,
108  Sphere,
109  Shape,
110  Model,
111  Text,
112  Image,
113  Web,
114  ParticleEffect,
115  ProceduralParticleEffect,
116  Line,
117  PolyLine,
118  PolyVox,
119  Grid,
120  Gizmo,
121  Light,
122  Zone,
123  Material,
124  Sound,
125  Canvas,
126  Empty,
127  Script,
128  NUM_TYPES
129  } EntityType;
130 
131  static bool typeIsValid(EntityType type);
132  static const QString& getEntityTypeName(EntityType entityType);
133  static EntityTypes::EntityType getEntityTypeFromName(const QString& name);
134  static bool registerEntityType(EntityType entityType, const char* name, EntityTypeFactory factoryMethod);
135  static void extractEntityTypeAndID(const unsigned char* data, int dataLength, EntityTypes::EntityType& typeOut, QUuid& idOut);
136  static EntityItemPointer constructEntityItem(EntityType entityType, const EntityItemID& entityID, const EntityItemProperties& properties);
137  static EntityItemPointer constructEntityItem(const unsigned char* data, int bytesToRead);
138  static EntityItemPointer constructEntityItem(const QUuid& id, const EntityItemProperties& properties);
139 
140 private:
141  static QMap<EntityType, QString> _typeToNameMap;
142  static QMap<QString, EntityTypes::EntityType> _nameToTypeMap;
143  static EntityTypeFactory _factories[NUM_TYPES];
144  static bool _factoriesInitialized;
145 };
146 
147 
151 // static EntityItemPointer factory(const EntityItemID& entityID, const EntityItemProperties& properties);
152 #define REGISTER_ENTITY_TYPE(x) bool x##Registration = \
153  EntityTypes::registerEntityType(EntityTypes::x, #x, x##EntityItem::factory);
154 
155 
156 struct EntityRegistrationChecker {
157  EntityRegistrationChecker(bool result, const char* debugMessage) {
158  if (!result) {
159  qCDebug(entities) << debugMessage;
160  }
161  }
162 };
163 
168 // static EntityItemPointer factory(const EntityItemID& entityID, const EntityItemProperties& properties);
169 #define REGISTER_ENTITY_TYPE_WITH_FACTORY(x,y) static bool x##Registration = \
170  EntityTypes::registerEntityType(EntityTypes::x, #x, y); \
171  EntityRegistrationChecker x##RegistrationChecker( \
172  x##Registration, \
173  "UNEXPECTED: REGISTER_ENTITY_TYPE_WITH_FACTORY(" #x "," #y ") FAILED.!");
174 
175 
176 #endif // hifi_EntityTypes_h
Abstract ID for editing model items. Used in EntityItem JS API.
Definition: EntityItemID.h:28
A generic 3D model displaying geometry loaded from a URL.
Definition: Model.h:85
@ Unknown
Socket type unknown or not set.