Sirikata
liboh/plugins/js/JSVisibleManager.hpp
Go to the documentation of this file.
00001 #ifndef __JS_VISIBLE_MANAGER_HPP__
00002 #define __JS_VISIBLE_MANAGER_HPP__
00003 
00004 #include <sirikata/oh/Platform.hpp>
00005 #include <sirikata/core/util/SpaceObjectReference.hpp>
00006 #include <sirikata/proxyobject/ProxyObject.hpp>
00007 #include <sirikata/proxyobject/ProxyCreationListener.hpp>
00008 #include "JSUtil.hpp"
00009 #include "JSCtx.hpp"
00010 #include "JSVisibleData.hpp"
00011 
00012 namespace Sirikata{
00013 namespace JS{
00014 
00015 class EmersonScript;
00016 class JSVisibleStruct;
00017 
00040 class JSVisibleManager :
00041         public ProxyCreationListener,
00042         public JSVisibleDataListener,
00043         public PositionListener,
00044         public MeshListener
00045 {
00046 public:
00047     JSVisibleManager(JSCtx* ctx);
00048     virtual ~JSVisibleManager();
00049 
00050     typedef boost::recursive_mutex RMutex;
00051     RMutex vmMtx;
00052 
00053 
00062     JSVisibleStruct* createVisStruct(EmersonScript* parent, const SpaceObjectReference& whatsVisible, JSVisibleDataPtr addParams = JSVisibleDataPtr());
00063 
00064     // Looks up or creates the data for a visible, but leaves it
00065     // uninitialized. You should only call this if you know data will be
00066     // available or if you follow it up by initializing the data (e.g. with a
00067     // ProxyObject).
00068     JSAggregateVisibleDataPtr getOrCreateVisible(const SpaceObjectReference& whatsVisible);
00069 
00070     // ProxyCreationListener Interface
00071     //  - Creates or increments and decrements refcount of
00072     //    JSVisibleData. Updates data.
00073     virtual void onCreateProxy(ProxyObjectPtr p);
00074     virtual void onDestroyProxy(ProxyObjectPtr p);
00075 
00076     // PositionListener
00077     //  - Updates JSProxyData state
00078     //  - Destruction ignored, handled by onDestroyProxy
00079     virtual void updateLocation (ProxyObjectPtr proxy, const TimedMotionVector3f &newLocation, const TimedMotionQuaternion& newOrient, const AggregateBoundingInfo& newBounds,const SpaceObjectReference& sporef);
00080 
00081     // MeshListener Interface
00082     //  - Updates JSProxyData state
00083     virtual void onSetMesh (ProxyObjectPtr proxy, Transfer::URI const& newMesh,const SpaceObjectReference& sporef);
00084     virtual void onSetScale (ProxyObjectPtr proxy, float32 newScale ,const SpaceObjectReference& sporef);
00085     virtual void onSetPhysics (ProxyObjectPtr proxy, const String& newphy,const SpaceObjectReference& sporef);
00086     virtual void onSetIsAggregate(ProxyObjectPtr proxy, bool isAggregate, const SpaceObjectReference& sporef);
00087 
00088     // Indicate whether this object is still visible to on of your presences or
00089     // not.
00090     bool isVisible(const SpaceObjectReference& sporef);
00091     v8::Handle<v8::Value> isVisibleV8(const SpaceObjectReference& sporef);
00092 
00093 
00094 
00095 protected:
00096     void iOnDestroyProxy(ProxyObjectPtr p);
00097 
00098 
00099     // Invoked when we received an update on a Proxy, making it the most up-to-date.
00100     void iUpdatedProxy(ProxyObjectPtr p);
00101 
00102 private:
00103 
00104     void iOnCreateProxy(ProxyObjectPtr p);
00105     void clearVisibles();
00106 
00107 
00108 
00109     // Clean up our references to a JSVisibleData. Only ever invoked by
00110     // JSVisibleData's destructor when there are no more references to
00111     // it.
00112     virtual void removeVisibleData(JSVisibleData* data);
00113 
00114     JSCtx* mCtx;
00115 
00116     typedef std::tr1::unordered_map<SpaceObjectReference, JSAggregateVisibleDataWPtr, SpaceObjectReference::Hasher > SporefProxyMap;
00117     typedef SporefProxyMap::iterator SporefProxyMapIter;
00118     SporefProxyMap mProxies;
00119 
00120     typedef std::tr1::unordered_set<ProxyObjectPtr, ProxyObject::Hasher> TrackedObjectsMap;
00121     TrackedObjectsMap mTrackedObjects;
00122 
00123 
00124     friend class EmersonScript;
00125 };
00126 
00127 } //end namespace js
00128 } //end namespace sirikata
00129 
00130 #endif