Sirikata
liboh/plugins/js/JSObjectStructs/JSPositionListener.hpp
Go to the documentation of this file.
00001 #ifndef __SIRIKATA_JS_POSITION_LISTENER_STRUCT_HPP__
00002 #define __SIRIKATA_JS_POSITION_LISTENER_STRUCT_HPP__
00003 
00004 #include <sirikata/oh/HostedObject.hpp>
00005 #include <v8.h>
00006 #include <sirikata/core/util/Liveness.hpp>
00007 #include <sirikata/mesh/Visual.hpp>
00008 #include "../JSVisibleManager.hpp"
00009 #include "../JSCtx.hpp"
00010 
00011 namespace Sirikata {
00012 namespace JS {
00013 
00014 class JSVisibleData;
00015 class JSContextStruct;
00016 
00017 //note: only position and isConnected will actually set the flag of the watchable
00018 struct JSPositionListener : public Liveness
00019 {
00020     friend class JSSerializer;
00021     friend class JSVisibleStruct;
00022     friend class JSPresenceStruct;
00023 
00024 public:
00025     virtual ~JSPositionListener();
00026 
00027     virtual Vector3f     getPosition();
00028     virtual Vector3f     getVelocity();
00029     virtual Quaternion   getOrientation();
00030     virtual Quaternion   getOrientationVelocity();
00031     virtual BoundingSphere3f   getBounds();
00032     virtual String getMesh();
00033     virtual String getPhysics();
00034     virtual bool getStillVisible();
00035 
00036     virtual v8::Handle<v8::Value> struct_getPosition();
00037     virtual v8::Handle<v8::Value> struct_getVelocity();
00038     virtual v8::Handle<v8::Value> struct_getOrientation();
00039     virtual v8::Handle<v8::Value> struct_getOrientationVel();
00040     virtual v8::Handle<v8::Value> struct_getScale();
00041     virtual v8::Handle<v8::Value> struct_getMesh();
00042     virtual v8::Handle<v8::Value> struct_getPhysics();
00043     virtual v8::Handle<v8::Value> struct_getTransTime();
00044     virtual v8::Handle<v8::Value> struct_getOrientTime();
00045     virtual v8::Handle<v8::Value> struct_getSporef();
00046     virtual v8::Handle<v8::Value> struct_getStillVisible();
00047 
00048     virtual v8::Handle<v8::Value> struct_getAnimationList();
00049 
00050 
00051     virtual v8::Handle<v8::Value> struct_getAllData();
00052     virtual v8::Handle<v8::Value> struct_checkEqual(JSPositionListener* jpl);
00053 
00054     virtual v8::Handle<v8::Value> struct_getDistance(const Vector3d& distTo);
00055 
00056     v8::Handle<v8::Value> loadMesh(JSContextStruct* ctx, v8::Handle<v8::Function> cb);
00057     v8::Handle<v8::Value> meshBounds();
00058     v8::Handle<v8::Value> untransformedMeshBounds();
00059     // NOTE: This the ray parameters are in *object* space.
00060     v8::Handle<v8::Value> raytrace(const Vector3f& mesh_ray_start, const Vector3f& mesh_ray_dir);
00061     v8::Handle<v8::Value> unloadMesh();
00062 
00063     //simple accessors for sporef fields
00064     SpaceObjectReference getSporef();
00065 
00066 
00067 protected:
00068     v8::Handle<v8::Value> wrapSporef(SpaceObjectReference sporef);
00069 
00070 
00071     EmersonScript* mParentScript;
00072     JSAggregateVisibleDataPtr jpp;
00073     // We don't store this in jpp because we would just have to keep track of
00074     // separate flags for whether we loaded it so we could do some refcounting.
00075     Mesh::VisualPtr mVisual;
00076     JSCtx* mCtx;
00077 
00078 private:
00079 
00080     void eLoadMesh(
00081         JSContextStruct* ctx,v8::Persistent<v8::Function>cb);
00082 
00083     void iFinishLoadMesh(
00084         Liveness::Token alive, Liveness::Token ctx_alive,
00085         JSContextStruct* ctx, v8::Persistent<v8::Function> cb,
00086         Mesh::VisualPtr data);
00087 
00088 
00089     //private constructor.  Can only be made through serializer,
00090     //JSVisibleStruct, or JSPresenceStruct.
00091     JSPositionListener(EmersonScript* parent, JSAggregateVisibleDataPtr _jpp, JSCtx* ctx);
00092     // Disabled default constructor
00093     JSPositionListener();
00094 
00095     // Invoked after loading is complete, invokes callback if all necessary
00096     // components are still alive.
00097     void finishLoadMesh(Liveness::Token alive, Liveness::Token ctx_alive, JSContextStruct* ctx, v8::Persistent<v8::Function> cb, Mesh::VisualPtr data);
00098 };
00099 
00100 
00101 //Throws an error if jpp has not yet been initialized.
00102 #define CHECK_JPP_INIT_THROW_LOG_CPP_ERROR(funcIn,alternateVal)     \
00103 {\
00104     if (!jpp)\
00105     {\
00106         JSLOG(detailed,"Error in jspositionlistener.  Position proxy was not set."); \
00107         return alternateVal;\
00108     }\
00109 }
00110 
00111 
00112 
00113 //Throws an error if jpp has not yet been initialized.
00114 #define CHECK_JPP_INIT_THROW_V8_ERROR(funcIn)\
00115 {\
00116     if (!jpp)\
00117     {\
00118         JSLOG(error,"Error in jspositionlistener.  Position proxy was not set."); \
00119         return v8::ThrowException(v8::Exception::Error(v8::String::New("Error when calling " #funcIn ".  Proxy ptr was not set.")));\
00120     }\
00121 }
00122 
00123 
00124 //Throws an error if not in context.
00125 //funcIn specifies which function is asking passErrorChecks, and gets printed in
00126 //an error message if call fails.
00127 //If in context, returns current context in con.
00128 #define JSPOSITION_CHECK_IN_CONTEXT_THROW_EXCEP(funcIn,con)\
00129     CHECK_JPP_INIT_THROW_V8_ERROR(funcIn);\
00130     if (!v8::Context::InContext())                  \
00131     {\
00132         JSLOG(error,"Error in jspositionlistener.  Was not in a context."); \
00133         return v8::ThrowException(v8::Exception::Error(v8::String::New("Error when calling " #funcIn ".  Not currently within a context.")));\
00134     }\
00135     v8::Handle<v8::Context>con = v8::Context::GetCurrent();
00136 
00137 
00138 
00139 }//end namespace js
00140 }//end namespace sirikata
00141 
00142 #endif