Ocean
ExperiencesLibrary.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_INTERACTION_EXPERIENCES_EXPERIENCES_LIBRARY_H
9 #define META_OCEAN_INTERACTION_EXPERIENCES_EXPERIENCES_LIBRARY_H
10 
13 
14 #include "ocean/base/Singleton.h"
15 
17 
18 #include <functional>
19 
20 namespace Ocean
21 {
22 
23 namespace Interaction
24 {
25 
26 namespace Experiences
27 {
28 
29 /**
30  * This class implements the experiences interaction library object.
31  * @ingroup interactionexperiences
32  */
33 class OCEAN_INTERACTION_EXPERIENCES_EXPORT ExperiencesLibrary : public Library
34 {
35  public:
36 
37  /**
38  * Definition of a unique pointer to an Experience object.
39  */
40  typedef std::unique_ptr<Experience> UniqueExperience;
41 
42  /**
43  * Definition of a callback function allowing to create a new experience.
44  */
45  typedef std::function<UniqueExperience()> CreateExperienceFunction;
46 
47  protected:
48 
49  /**
50  * Internal helper class holding the instance of this library.
51  */
52  class Instance : public Singleton<Instance>
53  {
54  friend class Singleton<Instance>;
55 
56  public:
57 
58  /**
59  * Returns the pointer to the instance of the experience library.
60  * @return The experience library, nullptr if not yet set
61  */
63 
64  /**
65  * Returns the lock.
66  * @return The lock
67  */
68  Lock& lock();
69 
70  protected:
71 
72  /**
73  * Creates a new object with invalid instance.
74  */
76 
77  protected:
78 
79  /// The pointer to the instance of the experience library.
80  ExperiencesLibrary* experiencesLibrary_ = nullptr;
81 
82  /// Lock to make this class thread safe
84  };
85 
86  /**
87  * Definition of a vector holding experiences.
88  */
89  typedef std::unordered_map<std::string, UniqueExperience> NameToExperienceMap;
90 
91  /**
92  * Definition of an unordered map mapping experience names to experience create functions.
93  */
94  typedef std::unordered_map<std::string, CreateExperienceFunction> NameToFunctionMap;
95 
96  public:
97 
98  /**
99  * Creates this library and registers it at the global interaction manager.
100  * Do not register this library if using it as plugin, because it's done by the plugin itself.<br>
101  * However, if you are not using the plugin mechanism you have to initialize this library once at program initialization.<br>
102  * If the library is not used anymore unregister it using the unregister function.<br>
103  * Beware: This registration must not be done more than once!<br>
104  * @see Manager, unregisterLibrary()
105  */
106  static void registerLibrary();
107 
108  /**
109  * Unregisters this library at the global interaction manager.
110  * Do not unregister this library if using it as plugin, because it's done by the plugin itself when the plugin is unloaded.<br>
111  * Beware: This registration must not be done more than once and must not be done without previous registration!<br>
112  * @return True, if succeeded
113  * @see Manager, registerLibrary()
114  */
115  static bool unregisterLibrary();
116 
117  /**
118  * Registers a new experience without actually creating the experience.
119  * @param experienceName The name of the experience to register, must be valid
120  * @param createExperienceFunction The create function of the new experience, must be valid
121  * @return True, if succeeded
122  */
123  static bool registerNewExperience(std::string experienceName, CreateExperienceFunction createExperienceFunction);
124 
125  protected:
126 
127  /**
128  * Creates a new experiences library object.
129  */
131 
132  /**
133  * Destructs a experiences library object.
134  */
136 
137  /**
138  * Loads a new interaction file.
139  * @see Library::load().
140  */
141  bool load(const UserInterface& userInterface, const Rendering::EngineRef& engine, const Timestamp timestamp, const std::string& filename) override;
142 
143  /**
144  * Unloads one specific loaded interaction files.
145  * @see Library::unload().
146  */
147  bool unload(const UserInterface& userInterface, const Rendering::EngineRef& engine, const Timestamp timestamp, const std::string& filename) override;
148 
149  /**
150  * Unloads all loaded scripts.
151  * @see Library::unload
152  */
153  void unload(const UserInterface& userInterface, const Rendering::EngineRef& engine, const Timestamp timestamp) override;
154 
155  /**
156  * Pre file load interaction function.
157  * @see Library::preFileLoad().
158  */
159  void preFileLoad(const UserInterface& userInterface, const std::string& filename) override;
160 
161  /**
162  * Post file load interaction function.
163  * @see Library::postFileLoad().
164  */
165  void postFileLoad(const UserInterface& userInterface, const std::string& filename, const bool succeeded) override;
166 
167  /**
168  * Pre update interaction function.
169  * @see Library::preUpdate().
170  */
171  Timestamp preUpdate(const UserInterface& userInterface, const Rendering::EngineRef& engine, const Rendering::ViewRef& view, const Timestamp timestamp) override;
172 
173  /**
174  * Post update interaction function.
175  * @see Library::postUpdate().
176  */
177  void postUpdate(const UserInterface& userInterface, const Rendering::EngineRef& engine, const Rendering::ViewRef& view, const Timestamp timestamp) override;
178 
179  /**
180  * Mouse press event function.
181  * @see Library::onMousePress().
182  */
183  void onMousePress(const UserInterface& userInterface, const Rendering::EngineRef& engine, const std::string& button, const Vector2& screenPosition, const Line3& ray, const Rendering::ObjectId pickedObject, const Vector3& pickedPosition, const Timestamp timestamp) override;
184 
185  /**
186  * Mouse move event function.
187  * @see Library::onMouseMove().
188  */
189  void onMouseMove(const UserInterface& userInterface, const Rendering::EngineRef& engine, const std::string& button, const Vector2& screenPosition, const Line3& ray, const Rendering::ObjectId pickedObject, const Vector3& pickedPosition, const Timestamp timestamp) override;
190 
191  /**
192  * Mouse release event function.
193  * @see Library::onMouseRelease().
194  */
195  void onMouseRelease(const UserInterface& userInterface, const Rendering::EngineRef& engine, const std::string& button, const Vector2& screenPosition, const Line3& ray, const Rendering::ObjectId pickedObject, const Vector3& pickedPosition, const Timestamp timestamp) override;
196 
197  /**
198  * Key press function.
199  * @see Library::onKeyPress().
200  */
201  void onKeyPress(const UserInterface& userInterface, const Rendering::EngineRef& engine, const std::string& key, const Timestamp timestamp) override;
202 
203  /**
204  * Key release function.
205  * @see Library::onKeyRelease().
206  */
207  void onKeyRelease(const UserInterface& userInterface, const Rendering::EngineRef& engine, const std::string& key, const Timestamp timestamp) override;
208 
209  /**
210  * Releases this library.
211  */
212  void release() override;
213 
214  protected:
215 
216  /// The map mapping names of experiences to create functions.
218 
219  /// The map mapping names of experiences to actual instances of experiences.
221 
222  /// The library's lock.
224 };
225 
226 }
227 
228 }
229 
230 }
231 
232 #endif // META_OCEAN_INTERACTION_EXPERIENCES_EXPERIENCES_LIBRARY_H
Internal helper class holding the instance of this library.
Definition: ExperiencesLibrary.h:53
Instance()
Creates a new object with invalid instance.
ExperiencesLibrary *& experiencesLibrary()
Returns the pointer to the instance of the experience library.
Lock lock_
Lock to make this class thread safe.
Definition: ExperiencesLibrary.h:83
This class implements the experiences interaction library object.
Definition: ExperiencesLibrary.h:34
std::function< UniqueExperience()> CreateExperienceFunction
Definition of a callback function allowing to create a new experience.
Definition: ExperiencesLibrary.h:45
static bool registerNewExperience(std::string experienceName, CreateExperienceFunction createExperienceFunction)
Registers a new experience without actually creating the experience.
bool load(const UserInterface &userInterface, const Rendering::EngineRef &engine, const Timestamp timestamp, const std::string &filename) override
Loads a new interaction file.
static bool unregisterLibrary()
Unregisters this library at the global interaction manager.
~ExperiencesLibrary() override
Destructs a experiences library object.
void onMouseMove(const UserInterface &userInterface, const Rendering::EngineRef &engine, const std::string &button, const Vector2 &screenPosition, const Line3 &ray, const Rendering::ObjectId pickedObject, const Vector3 &pickedPosition, const Timestamp timestamp) override
Mouse move event function.
std::unique_ptr< Experience > UniqueExperience
Definition of a unique pointer to an Experience object.
Definition: ExperiencesLibrary.h:40
void unload(const UserInterface &userInterface, const Rendering::EngineRef &engine, const Timestamp timestamp) override
Unloads all loaded scripts.
void postUpdate(const UserInterface &userInterface, const Rendering::EngineRef &engine, const Rendering::ViewRef &view, const Timestamp timestamp) override
Post update interaction function.
void release() override
Releases this library.
bool unload(const UserInterface &userInterface, const Rendering::EngineRef &engine, const Timestamp timestamp, const std::string &filename) override
Unloads one specific loaded interaction files.
void onKeyRelease(const UserInterface &userInterface, const Rendering::EngineRef &engine, const std::string &key, const Timestamp timestamp) override
Key release function.
Lock lock_
The library's lock.
Definition: ExperiencesLibrary.h:223
std::unordered_map< std::string, CreateExperienceFunction > NameToFunctionMap
Definition of an unordered map mapping experience names to experience create functions.
Definition: ExperiencesLibrary.h:94
void onMouseRelease(const UserInterface &userInterface, const Rendering::EngineRef &engine, const std::string &button, const Vector2 &screenPosition, const Line3 &ray, const Rendering::ObjectId pickedObject, const Vector3 &pickedPosition, const Timestamp timestamp) override
Mouse release event function.
void postFileLoad(const UserInterface &userInterface, const std::string &filename, const bool succeeded) override
Post file load interaction function.
void onMousePress(const UserInterface &userInterface, const Rendering::EngineRef &engine, const std::string &button, const Vector2 &screenPosition, const Line3 &ray, const Rendering::ObjectId pickedObject, const Vector3 &pickedPosition, const Timestamp timestamp) override
Mouse press event function.
ExperiencesLibrary()
Creates a new experiences library object.
std::unordered_map< std::string, UniqueExperience > NameToExperienceMap
Definition of a vector holding experiences.
Definition: ExperiencesLibrary.h:89
static void registerLibrary()
Creates this library and registers it at the global interaction manager.
Timestamp preUpdate(const UserInterface &userInterface, const Rendering::EngineRef &engine, const Rendering::ViewRef &view, const Timestamp timestamp) override
Pre update interaction function.
NameToFunctionMap nameToFunctionMap_
The map mapping names of experiences to create functions.
Definition: ExperiencesLibrary.h:217
void onKeyPress(const UserInterface &userInterface, const Rendering::EngineRef &engine, const std::string &key, const Timestamp timestamp) override
Key press function.
void preFileLoad(const UserInterface &userInterface, const std::string &filename) override
Pre file load interaction function.
NameToExperienceMap nameToExperienceMap_
The map mapping names of experiences to actual instances of experiences.
Definition: ExperiencesLibrary.h:220
This class implements the base class for all interaction libraries.
Definition: interaction/Library.h:29
This class holds UI elements of the application from which the interaction is executed.
Definition: UserInterface.h:28
This class implements an infinite line in 3D space.
Definition: Line3.h:70
This class implements a recursive lock object.
Definition: Lock.h:31
This template class is the base class for all singleton objects.
Definition: Singleton.h:71
This class implements a timestamp.
Definition: Timestamp.h:36
size_t ObjectId
Definition of a object id.
Definition: rendering/Rendering.h:59
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15