Răsfoiți Sursa

Camera/Renderable/Light components now properly delay creating their handles until the components are instantiated

Marko Pintera 10 ani în urmă
părinte
comite
e5c07aff9c

+ 13 - 1
BansheeEngine/Include/BsCamera.h

@@ -285,12 +285,24 @@ namespace BansheeEngine
 
 		mutable CameraHandlerPtr mInternal;
 
+		// Only valid during construction
+		RenderTargetPtr mTarget;
+		float mLeft;
+		float mTop;
+		float mWidth;
+		float mHeight;
+
 		/************************************************************************/
 		/* 						COMPONENT OVERRIDES                      		*/
 		/************************************************************************/
 	protected:
 		friend class SceneObject;
 
+		/**
+		 * @copydoc	Component::onInitialized
+		 */
+		void onInitialized() override;
+
 		/**
 		 * @copydoc	Component::onDestroyed
 		 */
@@ -308,7 +320,7 @@ namespace BansheeEngine
 	public:
 		friend class CameraRTTI;
 		static RTTITypeBase* getRTTIStatic();
-		virtual RTTITypeBase* getRTTI() const;
+		virtual RTTITypeBase* getRTTI() const override;
 
 	protected:
 		Camera() {} // Serialization only

+ 4 - 2
BansheeEngine/Include/BsCameraHandlerRTTI.h

@@ -100,8 +100,10 @@ namespace BansheeEngine
 
 		virtual void onDeserializationEnded(IReflectable* obj) override
 		{
-			CameraHandler* cameraHandler = static_cast<CameraHandler*>(obj);
-			cameraHandler->initialize();
+			// Note: Since this is a CoreObject I should call initialize() right after deserialization,
+			// but since this specific type is used in Components we delay initialization until Component
+			// itself does it. Keep this is mind in case this ever needs to be deserialized for non-Component 
+			// purposes (you'll need to call initialize manually).
 		}
 
 		virtual const String& getRTTIName() override

+ 19 - 11
BansheeEngine/Include/BsLight.h

@@ -20,16 +20,6 @@ namespace BansheeEngine
 
 		virtual ~Light();
 
-	    /**
-		 * @copydoc	Component::onInitialized
-	     */
-		void onInitialized() override;
-
-	    /**
-		 * @copydoc	Component::onDestroyed
-	     */
-		void onDestroyed() override;
-
 	    /**
 		 * @copydoc	LightInternal::getPosition
 	     */
@@ -123,13 +113,31 @@ namespace BansheeEngine
     protected:
 		mutable SPtr<LightInternal> mInternal;
 
+		// Only valid during construction
+		LightType mType;
+		Color mColor;
+		float mIntensity; 
+		float mRange; 
+		bool mCastsShadows; 
+		Degree mSpotAngle; 
+		Degree mSpotFalloffAngle;
+
 		/************************************************************************/
 		/* 						COMPONENT OVERRIDES                      		*/
 		/************************************************************************/
 	protected:
 		friend class SceneObject;
 
-	public:
+	    /**
+		 * @copydoc	Component::onInitialized
+	     */
+		void onInitialized() override;
+
+	    /**
+		 * @copydoc	Component::onDestroyed
+	     */
+		void onDestroyed() override;
+
 		/**
 		 * @copydoc	Component::update
 		 */

+ 4 - 2
BansheeEngine/Include/BsLightInternalRTTI.h

@@ -51,8 +51,10 @@ namespace BansheeEngine
 
 		virtual void onDeserializationEnded(IReflectable* obj) override
 		{
-			LightInternal* lightInternal = static_cast<LightInternal*>(obj);
-			lightInternal->initialize();
+			// Note: Since this is a CoreObject I should call initialize() right after deserialization,
+			// but since this specific type is used in Components we delay initialization until Component
+			// itself does it. Keep this is mind in case this ever needs to be deserialized for non-Component 
+			// purposes (you'll need to call initialize manually).
 		}
 
 		virtual const String& getRTTIName() override

+ 1 - 1
BansheeEngine/Include/BsRenderableHandler.h

@@ -276,6 +276,6 @@ namespace BansheeEngine
 	public:
 		friend class RenderableHandlerRTTI;
 		static RTTITypeBase* getRTTIStatic();
-		virtual RTTITypeBase* getRTTI() const;
+		virtual RTTITypeBase* getRTTI() const override;
 	};
 }

+ 8 - 6
BansheeEngine/Include/BsRenderableHandlerRTTI.h

@@ -29,24 +29,26 @@ namespace BansheeEngine
 				&RenderableHandlerRTTI::getNumMaterials, &RenderableHandlerRTTI::setMaterial, &RenderableHandlerRTTI::setNumMaterials);
 		}
 
-		virtual void onDeserializationEnded(IReflectable* obj)
+		virtual void onDeserializationEnded(IReflectable* obj) override
 		{
-			RenderableHandler* renderable = static_cast<RenderableHandler*>(obj);
-			renderable->initialize();
+			// Note: Since this is a CoreObject I should call initialize() right after deserialization,
+			// but since this specific type is used in Components we delay initialization until Component
+			// itself does it. Keep this is mind in case this ever needs to be deserialized for non-Component 
+			// purposes (you'll need to call initialize manually).
 		}
 
-		virtual const String& getRTTIName()
+		virtual const String& getRTTIName() override
 		{
 			static String name = "RenderableHandler";
 			return name;
 		}
 
-		virtual UINT32 getRTTIId()
+		virtual UINT32 getRTTIId() override
 		{
 			return TID_RenderableHandler;
 		}
 
-		virtual std::shared_ptr<IReflectable> newRTTIObject()
+		virtual std::shared_ptr<IReflectable> newRTTIObject() override
 		{
 			return RenderableHandler::createEmpty();
 		}

+ 16 - 4
BansheeEngine/Source/BsCamera.cpp

@@ -17,11 +17,8 @@
 namespace BansheeEngine 
 {
 	Camera::Camera(const HSceneObject& parent, RenderTargetPtr target, float left, float top, float width, float height)
-		: Component(parent)
+		: Component(parent), mTarget(target), mLeft(left), mTop(top), mWidth(width), mHeight(height)
     {
-		mInternal = CameraHandler::create(target, left, top, width, height);
-		gSceneManager()._registerCamera(mInternal, parent);
-
 		setName("Camera");
     }
 
@@ -63,6 +60,21 @@ namespace BansheeEngine
 
 	}
 
+	void Camera::onInitialized()
+	{
+		// If mInternal already exists this means this object was deserialized,
+		// so all we need to do is initialize it.
+		if (mInternal != nullptr)
+			mInternal->initialize();
+		else
+		{
+			mInternal = CameraHandler::create(mTarget, mLeft, mTop, mWidth, mHeight);
+			mTarget = nullptr;
+		}
+
+		gSceneManager()._registerCamera(mInternal, SO());
+	}
+
 	void Camera::onDestroyed()
 	{
 		gSceneManager()._unregisterCamera(mInternal);

+ 12 - 4
BansheeEngine/Source/BsLight.cpp

@@ -6,11 +6,9 @@ namespace BansheeEngine
 {
 	Light::Light(const HSceneObject& parent, LightType type, Color color,
 		float intensity, float range, bool castsShadows, Degree spotAngle, Degree spotFalloffAngle)
-		: Component(parent)
+		: Component(parent), mType(type), mColor(color), mIntensity(intensity), mRange(range),
+		mCastsShadows(castsShadows), mSpotAngle(spotAngle), mSpotFalloffAngle(spotFalloffAngle)
 	{
-		mInternal = LightInternal::create(type, color, intensity, 
-			range, castsShadows, spotAngle, spotFalloffAngle);
-
 		setName("Light");
 	}
 
@@ -28,6 +26,16 @@ namespace BansheeEngine
 
 	void Light::onInitialized()
 	{
+		// If mInternal already exists this means this object was deserialized,
+		// so all we need to do is initialize it.
+		if (mInternal != nullptr)
+			mInternal->initialize();
+		else
+		{
+			mInternal = LightInternal::create(mType, mColor, mIntensity,
+				mRange, mCastsShadows, mSpotAngle, mSpotFalloffAngle);
+		}
+
 		gSceneManager()._registerLight(mInternal, sceneObject());
 	}
 

+ 7 - 1
BansheeEngine/Source/BsRenderable.cpp

@@ -14,11 +14,17 @@ namespace BansheeEngine
 		:Component(parent)
 	{
 		setName("Renderable");
-		mInternal = RenderableHandler::create();
 	}
 
 	void Renderable::onInitialized()
 	{
+		// If mInternal already exists this means this object was deserialized,
+		// so all we need to do is initialize it.
+		if (mInternal != nullptr)
+			mInternal->initialize();
+		else
+			mInternal = RenderableHandler::create();
+
 		gSceneManager()._registerRenderable(mInternal, sceneObject());
 	}
 

+ 0 - 1
TODO.txt

@@ -63,7 +63,6 @@ Test:
 
 Ribek use:
  - Hook up color picker to guicolor field
- - Move forward in scene view doesn't work because the W tool handle shortcut messes with it
  - When starting drag from hierarchy tree view it tends to select another object (can't repro)
  - Camera, Renderable, Material, Texture inspector
  - Project create/open window