8 #ifndef META_OCEAN_INTERACTION_JS_CONTEXT_H
9 #define META_OCEAN_INTERACTION_JS_CONTEXT_H
43 typedef std::vector<std::unique_ptr<JSScript>>
Scripts;
48 typedef std::vector<v8::Handle<v8::Value>>
Values;
60 typedef std::pair<std::string, FunctionParameterCaller>
FunctionPair;
86 void initialize(
const std::shared_ptr<JSContext>& thisContext,
const v8::Local<v8::ObjectTemplate>& globalTemplate);
92 inline v8::Local<v8::Context> context()
const;
98 inline const std::vector<std::string>& filenames()
const;
136 bool callFunction(v8::Local<v8::Function>
function, v8::Local<v8::Value>& returnValue,
Values& parameters, std::string& errorMessage);
146 bool callFunction(
const std::string&
function, v8::Local<v8::Value>& returnValue,
Values& parameters, std::string& errorMessage);
229 static inline const v8::Local<v8::Context>& currentContext();
235 static inline std::shared_ptr<JSContext> currentJSContext();
286 bool hasPreUpdateFunction_ =
false;
289 bool hasPostUpdateFunction_ =
false;
292 bool hasMousePressEventFunction_ =
false;
295 bool hasMouseMoveEventFunction_ =
false;
298 bool hasMouseReleaseEventFunction_ =
false;
301 bool hasKeyPressFunction_ =
false;
304 bool hasKeyReleaseFunction_ =
false;
324 return context_.Get(v8::Isolate::GetCurrent());
This class implements a callback function container using defined function parameters.
Definition: Caller.h:1565
This class holds a file.
Definition: File.h:36
This class implements a wrapper for a JavaScript context.
Definition: JSContext.h:37
void onMouseMove(const Rendering::EngineRef &engine, const std::string &button, const Vector2 &screenPosition, const Line3 &ray, const std::string &pickedObject, const Vector3 &pickedPosition, const Timestamp timestamp)
Mouse move event function.
IO::Files resolveFile(const IO::File &file)
Resolves the entire path of a given file.
void postUpdate(const Rendering::EngineRef &engine, const Timestamp timestamp)
Post update interaction function.
bool hasFunction(const std::string &function) const
Returns whether the script contains a specific function.
void onInitialize(const Rendering::EngineRef &engine, const Timestamp timestamp)
Initialize function.
Lock lock_
The context's lock.
Definition: JSContext.h:310
bool addScriptCode(const std::string &code)
Adds a new script to this context.
Scripts scripts_
Script code objects.
Definition: JSContext.h:274
void initialize(const std::shared_ptr< JSContext > &thisContext, const v8::Local< v8::ObjectTemplate > &globalTemplate)
Initializes this context.
Caller< Values > FunctionParameterCaller
Definition of a caller object returning JavaScript parameters.
Definition: JSContext.h:53
static const v8::Local< v8::Context > & currentContext()
Returns the current JavaScript context object.
Definition: JSContext.h:337
std::vector< FunctionPair > FunctionPairs
Definition of a vector holding function pairs.
Definition: JSContext.h:65
static v8::Local< v8::Context > currentContext_
The current JavaScript context.
Definition: JSContext.h:313
JSContext()=default
Creates a new context object.
void preFileLoad(const std::string &filename)
Pre file load interaction function.
JSContext(const JSContext &context)=delete
Disabled copy constructor.
Timestamp preUpdate(const Rendering::EngineRef &engine, const Timestamp timestamp)
Pre update interaction function.
std::vector< std::unique_ptr< JSScript > > Scripts
Definition of a vector holding JavaScript code objects.
Definition: JSContext.h:43
void makeCurrent()
Makes this context the current context.
v8::UniquePersistent< v8::Context > context_
JavaScript context object handle.
Definition: JSContext.h:277
bool hasObject(const std::string &object) const
Returns whether the script contains a specific global object.
bool callFunction(v8::Local< v8::Function > function, v8::Local< v8::Value > &returnValue, Values ¶meters, std::string &errorMessage)
Calls a given JavaScript function.
bool addScriptFile(const std::string &filename)
Adds a new script to this context.
static std::weak_ptr< JSContext > currentJSContext_
The current context object.
Definition: JSContext.h:316
static unsigned int mouseEventCounter_
Global counter for context objects interested in mouse events.
Definition: JSContext.h:319
v8::Local< v8::Context > context() const
Returns the JavaScript context object.
Definition: JSContext.h:322
void addExplicitFunctionCall(const std::string &function, const FunctionParameterCaller ¶meterCreator)
Adds a new function to the queue of explicit functions.
std::weak_ptr< JSContext > thisJSContext_
The reference to this object.
Definition: JSContext.h:280
static std::shared_ptr< JSContext > currentJSContext()
Returns the current JSContext object.
Definition: JSContext.h:327
std::pair< std::string, FunctionParameterCaller > FunctionPair
Defines a pair holding function names and function parameters.
Definition: JSContext.h:60
FunctionPairs explicitFunctions_
Functions to be called in the next update step explicitly.
Definition: JSContext.h:307
bool resolveFile(const IO::File &file, std::string &resolved)
Resolves the entire path of a given file to the first unique existing file.
void onKeyRelease(const Rendering::EngineRef &engine, const std::string &key, const Timestamp timestamp)
Key release function.
void onRelease(const Rendering::EngineRef &engine, const Timestamp timestamp)
Release function.
std::vector< v8::Handle< v8::Value > > Values
Definition of a vector holding value handles.
Definition: JSContext.h:48
bool callFunction(const std::string &function, v8::Local< v8::Value > &returnValue, Values ¶meters, std::string &errorMessage)
Executes a spcific script function.
void onMousePress(const Rendering::EngineRef &engine, const std::string &button, const Vector2 &screenPosition, const Line3 &ray, const std::string &pickedObject, const Vector3 &pickedPosition, const Timestamp timestamp)
Mouse press event function.
~JSContext()
Destructs a context object.
void onKeyPress(const Rendering::EngineRef &engine, const std::string &key, const Timestamp timestamp)
Key press function.
void onMouseRelease(const Rendering::EngineRef &engine, const std::string &button, const Vector2 &screenPosition, const Line3 &ray, const std::string &pickedObject, const Vector3 &pickedPosition, const Timestamp timestamp)
Mouse release event function.
const std::vector< std::string > & filenames() const
Returns a corresponding filenames of the scripts managed by this context object.
Definition: JSContext.h:332
JSContext & operator=(const JSContext &context)=delete
Disabled copy operator.
std::vector< std::string > filenames_
Name of the file the initial script is defined inside.
Definition: JSContext.h:283
void postFileLoad(const std::string &filename, const bool succeeded)
Post file load interaction function.
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
std::vector< File > Files
Definition of a vector holding files.
Definition: File.h:23
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15