Created
September 14, 2016 23:29
-
-
Save terryjsmith/c39cb289c5c8ec9f5b7241c3406058b7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ScriptVector3::ScriptVector3() { | |
m_vector.x = m_vector.y = m_vector.z = 0.0f; | |
} | |
ScriptVector3::ScriptVector3(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
float x, y, z; | |
x = y = z = 0.0f; | |
if(info.Length() == 1) { | |
assert(info[0]->IsNumber()); | |
x = y = z = (float)info[0]->NumberValue(); | |
} | |
else if(info.Length() == 3) { | |
assert(info[0]->IsNumber()); | |
assert(info[1]->IsNumber()); | |
assert(info[2]->IsNumber()); | |
x = (float)info[0]->ToNumber()->Value(); | |
y = (float)info[1]->ToNumber()->Value(); | |
z = (float)info[2]->ToNumber()->Value(); | |
} | |
m_vector.x = x; | |
m_vector.y = y; | |
m_vector.z = z; | |
} | |
void ScriptVector3::Initialize(v8::Isolate* isolate, v8::Local<v8::Context> context) { | |
StartTemplate(isolate); | |
SetTypeName("Vector3"); | |
AddVariable("x", GetX, SetX); | |
AddVariable("y", GetY, SetY); | |
AddVariable("z", GetZ, SetZ); | |
AddFunction("Normalize", Normalize); | |
EndTemplate(context); | |
} | |
v8::Local<v8::Value> ScriptVector3::GetX(v8::Isolate* isolate, void* obj) { | |
ScriptVector3* object = (ScriptVector3*)obj; | |
return(v8::Number::New(isolate, object->m_vector.x)); | |
} | |
v8::Local<v8::Value> ScriptVector3::GetY(v8::Isolate* isolate, void* obj) { | |
ScriptVector3* object = (ScriptVector3*)obj; | |
return(v8::Number::New(isolate, object->m_vector.y)); | |
} | |
v8::Local<v8::Value> ScriptVector3::GetZ(v8::Isolate* isolate, void* obj) { | |
ScriptVector3* object = (ScriptVector3*)obj; | |
return(v8::Number::New(isolate, object->m_vector.z)); | |
} | |
void ScriptVector3::SetX(void* obj, v8::Local<v8::Value> value) { | |
ScriptVector3* object = (ScriptVector3*)obj; | |
object->m_vector.x = (float)value->ToNumber()->Value(); | |
} | |
void ScriptVector3::SetY(void* obj, v8::Local<v8::Value> value) { | |
ScriptVector3* object = (ScriptVector3*)obj; | |
object->m_vector.y = (float)value->ToNumber()->Value(); | |
} | |
void ScriptVector3::SetZ(void* obj, v8::Local<v8::Value> value) { | |
ScriptVector3* object = (ScriptVector3*)obj; | |
object->m_vector.z = (float)value->ToNumber()->Value(); | |
} | |
// Prototype functions | |
void ScriptVector3::Normalize(const v8::FunctionCallbackInfo<v8::Value>& info) { | |
ScriptVector3* obj = ScriptObject::Unwrap<ScriptVector3>(info.This()); | |
obj->m_vector = glm::normalize(obj->m_vector); | |
return info.GetReturnValue().Set(info.This()); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A "vector3" class exposed to JS | |
*/ | |
class ScriptVector3 : public ScriptType<ScriptVector3> { | |
public: | |
ScriptVector3(); | |
ScriptVector3(const v8::FunctionCallbackInfo<v8::Value>& info); | |
~ScriptVector3(); | |
/** | |
* Initialize this type into V8's scope | |
*/ | |
static void Initialize(v8::Isolate* isolate, v8::Local<v8::Context> context); | |
/** | |
* Accessor functions | |
*/ | |
static v8::Local<v8::Value> GetX(v8::Isolate* isolate, void* obj); | |
static v8::Local<v8::Value> GetY(v8::Isolate* isolate, void* obj); | |
static v8::Local<v8::Value> GetZ(v8::Isolate* isolate, void* obj); | |
static void SetX(void* obj, v8::Local<v8::Value> value); | |
static void SetY(void* obj, v8::Local<v8::Value> value); | |
static void SetZ(void* obj, v8::Local<v8::Value> value); | |
/** | |
* Prototype functions | |
*/ | |
static void Normalize(const v8::FunctionCallbackInfo<v8::Value>& info); | |
// Get our vector | |
vector3& GetVector() { return m_vector; } | |
void SetVector(vector3 vector) { m_vector = vector; } | |
protected: | |
// Our internal vector type | |
vector3 m_vector; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment