Ocean
JSLibrary.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_JS_LIBRARY_H
9 #define META_OCEAN_INTERACTION_JS_LIBRARY_H
10 
13 
15 
16 #include <vector>
17 
18 namespace Ocean
19 {
20 
21 namespace Interaction
22 {
23 
24 namespace JavaScript
25 {
26 
27 /**
28  * This class implements the java script interaction library object.
29  * @ingroup interactionjs
30  */
31 class OCEAN_INTERACTION_JS_EXPORT JSLibrary : public Library
32 {
33  friend class JSContext;
34 
35  protected:
36 
37  /**
38  * Definition of a vector holding JavaScript contexts.
39  */
40  typedef std::vector<std::shared_ptr<JSContext>> JSContexts;
41 
42  public:
43 
44  /**
45  * Creates this library and registeres it at the global interaction manager.<br>
46  * Do not register this library if using it as plugin, because it's done by the plugin itself.<br>
47  * However, if you are not using the plugin mechanism you have to initialize this library once at program initialization.<br>
48  * If the library is not used anymore unregister it using the unregister function.<br>
49  * Beware: This registration must not be done more than once!<br>
50  * @see Manager, unregisterLibrary()
51  */
52  static void registerLibrary();
53 
54  /**
55  * Unregisters this library at the global interaction manager.
56  * Do not unregister this library if using it as plugin, because it's done by the plugin itself when the plugin is unloaded.<br>
57  * Beware: This registration must not be done more than once and must not be done without previous registration!<br>
58  * @return True, if succeeded
59  * @see Manager, registerLibrary()
60  */
61  static bool unregisterLibrary();
62 
63  protected:
64 
65  /**
66  * Creates a new java script library object.
67  */
69 
70  /**
71  * Destructs a java script library object.
72  */
73  ~JSLibrary() override;
74 
75  /**
76  * Loads a new interaction file.
77  * @see Library::load().
78  */
79  bool load(const UserInterface& userInterface, const Rendering::EngineRef& engine, const Timestamp timestamp, const std::string& filename) override;
80 
81  /**
82  * Unloads one specific loaded interaction files.
83  * @see Library::unload().
84  */
85  bool unload(const UserInterface& userInterface, const Rendering::EngineRef& engine, const Timestamp timestamp, const std::string& filename) override;
86 
87  /**
88  * Unloads all loaded scripts.
89  * @see Library::unload
90  */
91  void unload(const UserInterface& userInterface, const Rendering::EngineRef& engine, const Timestamp timestamp) override;
92 
93  /**
94  * Pre file load interaction function.
95  * @see Library::preFileLoad().
96  */
97  void preFileLoad(const UserInterface& userInterface, const std::string& filename) override;
98 
99  /**
100  * Post file load interaction function.
101  * @see Library::postFileLoad().
102  */
103  void postFileLoad(const UserInterface& userInterface, const std::string& filename, const bool succeeded) override;
104 
105  /**
106  * Pre update interaction function.
107  * If a JavaScript interaction file returns a different timestmap this timestamp is used for the next interaction file and so on.<br>
108  * Therefore, the last registered JavaScript interaction file is able to force the preferred timestamp.<br>
109  * However, different interaction libraries / modules can change the timestamp if they are executed after later.<br>
110  * @see Library::preUpdate().
111  */
112  Timestamp preUpdate(const UserInterface& userInterface, const Rendering::EngineRef& engine, const Rendering::ViewRef& view, const Timestamp timestamp) override;
113 
114  /**
115  * Post update interaction function.
116  * @see Library::postUpdate().
117  */
118  void postUpdate(const UserInterface& userInterface, const Rendering::EngineRef& engine, const Rendering::ViewRef& view, const Timestamp timestamp) override;
119 
120  /**
121  * Mouse press event function.
122  * @see Library::onMousePress().
123  */
124  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;
125 
126  /**
127  * Mouse move event function.
128  * @see Library::onMouseMove().
129  */
130  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;
131 
132  /**
133  * Mouse release event function.
134  * @see Library::onMouseRelease().
135  */
136  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;
137 
138  /**
139  * Key press function.
140  * @see Library::onKeyPress().
141  */
142  void onKeyPress(const UserInterface& userInterface, const Rendering::EngineRef& engine, const std::string& key, const Timestamp timestamp) override;
143 
144  /**
145  * Key release function.
146  * @see Library::onKeyRelease().
147  */
148  void onKeyRelease(const UserInterface& userInterface, const Rendering::EngineRef& engine, const std::string& key, const Timestamp timestamp) override;
149 
150  /**
151  * Returns the global template object holding definitions of all global custom JavaScript object templates (e.g. constructors of custom JavaScript object).
152  * @return The global template object
153  */
154  v8::Local<v8::ObjectTemplate> globalTemplate();
155 
156  /**
157  * Releases the global template object.
158  */
160 
161  /**
162  * Load function for additional java script file.
163  * @param info The function callback info
164  */
165  static void functionLoad(const v8::FunctionCallbackInfo<v8::Value>& info);
166 
167  /**
168  * Translates a given rendering object id into a named picking object.
169  * @param engine Rendering engine
170  * @param objectId Id of the rendering object
171  * @return Resulting name of the picking object
172  */
173  std::string translatePickingObject(const Rendering::EngineRef& engine, const Rendering::ObjectId objectId);
174 
175  protected:
176 
177  /// The platform for all contexts.
178  std::unique_ptr<v8::Platform> platform_;
179 
180  /// The create parameters for the isolate.
181  v8::Isolate::CreateParams createParams_;
182 
183  /// The isolate for all contexts.
184  v8::Isolate* isolate_ = nullptr;
185 
186  /// Global template object holding definitions of all global custom JavaScript object templates (e.g. constructors of custom JavaScript object).
187  v8::Persistent<v8::ObjectTemplate> globalTemplate_;
188 
189  /// All JavaScript context objects.
191 
192  /// The library's lock.
194 };
195 
196 }
197 
198 }
199 
200 }
201 
202 #endif // META_OCEAN_INTERACTION_JS_LIBRARY_H
This class implements a wrapper for a JavaScript context.
Definition: JSContext.h:37
This class implements the java script interaction library object.
Definition: JSLibrary.h:32
v8::Local< v8::ObjectTemplate > globalTemplate()
Returns the global template object holding definitions of all global custom JavaScript object templat...
~JSLibrary() override
Destructs a java script 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.
void unload(const UserInterface &userInterface, const Rendering::EngineRef &engine, const Timestamp timestamp) override
Unloads all loaded scripts.
static bool unregisterLibrary()
Unregisters this library at the global interaction manager.
static void registerLibrary()
Creates this library and registeres it at the global interaction manager.
void onKeyPress(const UserInterface &userInterface, const Rendering::EngineRef &engine, const std::string &key, const Timestamp timestamp) override
Key press function.
void postFileLoad(const UserInterface &userInterface, const std::string &filename, const bool succeeded) override
Post file load interaction function.
std::vector< std::shared_ptr< JSContext > > JSContexts
Definition of a vector holding JavaScript contexts.
Definition: JSLibrary.h:40
JSLibrary()
Creates a new java script library object.
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.
std::unique_ptr< v8::Platform > platform_
The platform for all contexts.
Definition: JSLibrary.h:178
JSContexts jsContexts_
All JavaScript context objects.
Definition: JSLibrary.h:190
std::string translatePickingObject(const Rendering::EngineRef &engine, const Rendering::ObjectId objectId)
Translates a given rendering object id into a named picking object.
void preFileLoad(const UserInterface &userInterface, const std::string &filename) override
Pre file load interaction function.
void releaseGlobalTemplate()
Releases the global template object.
v8::Isolate::CreateParams createParams_
The create parameters for the isolate.
Definition: JSLibrary.h:181
void postUpdate(const UserInterface &userInterface, const Rendering::EngineRef &engine, const Rendering::ViewRef &view, const Timestamp timestamp) override
Post update interaction function.
v8::Persistent< v8::ObjectTemplate > globalTemplate_
Global template object holding definitions of all global custom JavaScript object templates (e....
Definition: JSLibrary.h:187
bool load(const UserInterface &userInterface, const Rendering::EngineRef &engine, const Timestamp timestamp, const std::string &filename) override
Loads a new interaction file.
static void functionLoad(const v8::FunctionCallbackInfo< v8::Value > &info)
Load function for additional java script file.
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.
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.
Timestamp preUpdate(const UserInterface &userInterface, const Rendering::EngineRef &engine, const Rendering::ViewRef &view, const Timestamp timestamp) override
Pre update interaction function.
Lock lock_
The library's lock.
Definition: JSLibrary.h:193
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 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