Browse Source

Added prefab importer so that prefabs are recognized by the project library
Creating prefab by drag and drop in project window works
Added return/escape keyboard shortcuts to dialog box
Fixed an issue with generating a unique path for project assets
Fixed a crash when trying to reference unrecognized project library assets

Marko Pintera 10 years ago
parent
commit
d195e06084

+ 2 - 0
BansheeCore/BansheeCore.vcxproj

@@ -287,6 +287,7 @@
     <ClInclude Include="Include\BsPrefab.h" />
     <ClInclude Include="Include\BsPrefabDiff.h" />
     <ClInclude Include="Include\BsPrefabDiffRTTI.h" />
+    <ClInclude Include="Include\BsPrefabImporter.h" />
     <ClInclude Include="Include\BsPrefabRTTI.h" />
     <ClInclude Include="Include\BsPrefabUtility.h" />
     <ClInclude Include="Include\BsRendererMeshData.h" />
@@ -449,6 +450,7 @@
     <ClCompile Include="Source\BsMeshUtility.cpp" />
     <ClCompile Include="Source\BsPrefab.cpp" />
     <ClCompile Include="Source\BsPrefabDiff.cpp" />
+    <ClCompile Include="Source\BsPrefabImporter.cpp" />
     <ClCompile Include="Source\BsPrefabUtility.cpp" />
     <ClCompile Include="Source\BsProfilerCPU.cpp" />
     <ClCompile Include="Source\BsDeferredCallManager.cpp" />

+ 6 - 0
BansheeCore/BansheeCore.vcxproj.filters

@@ -569,6 +569,9 @@
     <ClInclude Include="Include\BsStringTableRTTI.h">
       <Filter>Header Files\RTTI</Filter>
     </ClInclude>
+    <ClInclude Include="Include\BsPrefabImporter.h">
+      <Filter>Header Files\Importer</Filter>
+    </ClInclude>
   </ItemGroup>
   <ItemGroup>
     <ClCompile Include="Source\BsCoreApplication.cpp">
@@ -901,5 +904,8 @@
     <ClCompile Include="Source\BsStringTableManager.cpp">
       <Filter>Source Files\Localization</Filter>
     </ClCompile>
+    <ClCompile Include="Source\BsPrefabImporter.cpp">
+      <Filter>Source Files\Importer</Filter>
+    </ClCompile>
   </ItemGroup>
 </Project>

+ 1 - 1
BansheeCore/Include/BsImportOptions.h

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

+ 1 - 1
BansheeCore/Include/BsMeshImportOptions.h

@@ -98,7 +98,7 @@ namespace BansheeEngine
 	public:
 		friend class MeshImportOptionsRTTI;
 		static RTTITypeBase* getRTTIStatic();
-		virtual RTTITypeBase* getRTTI() const;
+		virtual RTTITypeBase* getRTTI() const override;
 
 	private:
 		bool mCPUReadable;

+ 27 - 0
BansheeCore/Include/BsPrefabImporter.h

@@ -0,0 +1,27 @@
+#pragma once
+
+#include "BsCorePrerequisites.h"
+#include "BsSpecificImporter.h"
+
+namespace BansheeEngine
+{
+	/**
+	 * @brief	Importer using for importing Prefab resources.
+	 * 			Prefab resources are serialized engine objects ending in ".prefab" extension.
+	 */
+	class BS_CORE_EXPORT PrefabImporter : public SpecificImporter
+	{
+	public:
+		PrefabImporter();
+		virtual ~PrefabImporter();
+
+		/** @copydoc SpecificImporter::isExtensionSupported */
+		virtual bool isExtensionSupported(const WString& ext) const override;
+
+		/** @copydoc SpecificImporter::isMagicNumberSupported */
+		virtual bool isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const override;
+
+		/** @copydoc SpecificImporter::import */
+		virtual ResourcePtr import(const Path& filePath, ConstImportOptionsPtr importOptions) override;
+	};
+}

+ 3 - 4
BansheeCore/Include/BsShaderIncludeImporter.h

@@ -16,13 +16,12 @@ namespace BansheeEngine
 		virtual ~ShaderIncludeImporter();
 
 		/** @copydoc SpecificImporter::isExtensionSupported */
-		virtual bool isExtensionSupported(const WString& ext) const;
+		virtual bool isExtensionSupported(const WString& ext) const override;
 
 		/** @copydoc SpecificImporter::isMagicNumberSupported */
-		virtual bool isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const; 
+		virtual bool isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const override;
 
 		/** @copydoc SpecificImporter::import */
-		virtual ResourcePtr import(const Path& filePath, ConstImportOptionsPtr importOptions);
-	private:
+		virtual ResourcePtr import(const Path& filePath, ConstImportOptionsPtr importOptions) override;
 	};
 }

+ 1 - 1
BansheeCore/Include/BsTextureImportOptions.h

@@ -75,7 +75,7 @@ namespace BansheeEngine
 	public:
 		friend class TextureImportOptionsRTTI;
 		static RTTITypeBase* getRTTIStatic();
-		virtual RTTITypeBase* getRTTI() const;
+		virtual RTTITypeBase* getRTTI() const override;
 
 	private:
 		PixelFormat mFormat;

+ 0 - 2
BansheeCore/Source/BsPrefab.cpp

@@ -14,8 +14,6 @@ namespace BansheeEngine
 
 	HPrefab Prefab::create(const HSceneObject& sceneObject)
 	{
-		assert(sceneObject->mPrefabLinkUUID.empty());
-
 		PrefabPtr newPrefab = createEmpty();
 		newPrefab->initialize(sceneObject);
 

+ 47 - 0
BansheeCore/Source/BsPrefabImporter.cpp

@@ -0,0 +1,47 @@
+#include "BsPrefabImporter.h"
+#include "BsFileSerializer.h"
+#include "BsResource.h"
+
+namespace BansheeEngine
+{
+	PrefabImporter::PrefabImporter()
+		:SpecificImporter()
+	{
+
+	}
+
+	PrefabImporter::~PrefabImporter()
+	{
+
+	}
+
+	bool PrefabImporter::isExtensionSupported(const WString& ext) const
+	{
+		WString lowerCaseExt = ext;
+		StringUtil::toLowerCase(lowerCaseExt);
+
+		return lowerCaseExt == L"prefab";
+	}
+
+	bool PrefabImporter::isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const
+	{
+		return true; // No magic number of asset files, they must always rely on extension
+	}
+
+	ResourcePtr PrefabImporter::import(const Path& filePath, ConstImportOptionsPtr importOptions)
+	{
+		FileDecoder fs(filePath);
+		fs.skip(); // Skipped over saved resource data
+		std::shared_ptr<IReflectable> loadedData = fs.decode();
+
+		if (loadedData == nullptr)
+			BS_EXCEPT(InternalErrorException, "Unable to import resource.");
+
+		if (!loadedData->isDerivedFrom(Resource::getRTTIStatic()))
+			BS_EXCEPT(InternalErrorException, "Imported object doesn't derive from Resource.");
+
+		ResourcePtr resource = std::static_pointer_cast<Resource>(loadedData);
+
+		return resource;
+	}
+}

+ 0 - 5
BansheeCore/Source/BsResources.cpp

@@ -80,11 +80,6 @@ namespace BansheeEngine
 		if(!foundUUID)
 			uuid = UUIDGenerator::generateRandom();
 
-		
-		// TODO - Load dependencies not implemented
-
-
-
 		HResource outputResource;
 		bool alreadyLoading = false;
 		{

+ 4 - 0
BansheeEditor/Source/BsEditorApplication.cpp

@@ -17,6 +17,7 @@
 #include "BsScriptCodeImporter.h"
 #include "BsShaderIncludeHandler.h"
 #include "BsDropDownWindowManager.h"
+#include "BsPrefabImporter.h"
 
 // DEBUG ONLY
 #include "BsResources.h"
@@ -121,6 +122,9 @@ namespace BansheeEngine
 		ResourceImporter* resourceImporter = bs_new<ResourceImporter>();
 		Importer::instance()._registerAssetImporter(resourceImporter);
 
+		PrefabImporter* prefabImporter = bs_new<PrefabImporter>();
+		Importer::instance()._registerAssetImporter(prefabImporter);
+
 		ProjectLibrary::startUp(getProjectPath());
 
 		UndoRedo::startUp();

+ 4 - 4
BansheeEditor/Source/BsProjectLibrary.cpp

@@ -377,7 +377,9 @@ namespace BansheeEngine
 		Path metaPath = resource->path;
 		metaPath.setFilename(metaPath.getWFilename() + L".meta");
 
-		ext = ext.substr(1, ext.size() - 1); // Remove the .
+		if (ext.size() > 0)
+			ext = ext.substr(1, ext.size() - 1); // Remove the .
+
 		if (!Importer::instance().supportsFileType(ext))
 			return;
 
@@ -622,13 +624,11 @@ namespace BansheeEngine
 			assetPath = path.getRelative(getResourcesFolder());
 		}
 
-		assetPath.setExtension(assetPath.getWExtension() + L"." + ResourceImporter::DEFAULT_EXTENSION);
-
 		LibraryEntry* existingEntry = findEntry(assetPath);
 		if (existingEntry != nullptr)
 			BS_EXCEPT(InvalidParametersException, "Resource already exists at the specified path: " + assetPath.toString());
 
-		Resources::instance().save(resource, assetPath, false);
+		Resources::instance().save(resource, assetPath.getAbsolute(getResourcesFolder()), false);
 		checkForModifications(assetPath);
 	}
 

+ 1 - 0
BansheeEditor/Source/BsResourceImporter.cpp

@@ -33,6 +33,7 @@ namespace BansheeEngine
 	ResourcePtr ResourceImporter::import(const Path& filePath, ConstImportOptionsPtr importOptions)
 	{
 		FileDecoder fs(filePath);
+		fs.skip(); // Skipped over saved resource data
 		std::shared_ptr<IReflectable> loadedData = fs.decode();
 
 		if (loadedData == nullptr)

+ 3 - 3
BansheeEngine/Include/BsPlainTextImporter.h

@@ -15,13 +15,13 @@ namespace BansheeEngine
 		virtual ~PlainTextImporter();
 
 		/** @copydoc SpecificImporter::isExtensionSupported */
-		virtual bool isExtensionSupported(const WString& ext) const;
+		virtual bool isExtensionSupported(const WString& ext) const override;
 
 		/** @copydoc SpecificImporter::isMagicNumberSupported */
-		virtual bool isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const;
+		virtual bool isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const override;
 
 		/** @copydoc SpecificImporter::import */
-		virtual ResourcePtr import(const Path& filePath, ConstImportOptionsPtr importOptions);
+		virtual ResourcePtr import(const Path& filePath, ConstImportOptionsPtr importOptions) override;
 
 		static const WString DEFAULT_EXTENSION;
 	};

+ 4 - 4
BansheeEngine/Include/BsScriptCodeImporter.h

@@ -15,16 +15,16 @@ namespace BansheeEngine
 		virtual ~ScriptCodeImporter();
 
 		/** @copydoc SpecificImporter::isExtensionSupported */
-		virtual bool isExtensionSupported(const WString& ext) const;
+		virtual bool isExtensionSupported(const WString& ext) const override;
 
 		/** @copydoc SpecificImporter::isMagicNumberSupported */
-		virtual bool isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const;
+		virtual bool isMagicNumberSupported(const UINT8* magicNumPtr, UINT32 numBytes) const override;
 
 		/** @copydoc SpecificImporter::import */
-		virtual ResourcePtr import(const Path& filePath, ConstImportOptionsPtr importOptions);
+		virtual ResourcePtr import(const Path& filePath, ConstImportOptionsPtr importOptions) override;
 
 		/** @copydoc SpecificImporter::createImportOptions */
-		virtual ImportOptionsPtr createImportOptions() const;
+		virtual ImportOptionsPtr createImportOptions() const override;
 
 		static const WString DEFAULT_EXTENSION;
 	};

+ 47 - 0
MBansheeEditor/DialogBox.cs

@@ -69,6 +69,53 @@ namespace BansheeEditor
                 SetupGUI();
         }
 
+        private void OnEditorUpdate()
+        {
+            if (Input.IsButtonDown(ButtonCode.Return))
+            {
+                switch (type)
+                {
+                    case Type.OK:
+                    case Type.OKCancel:
+                        ButtonClicked(ResultType.OK);
+                        break;
+                    case Type.RetryAbortIgnore:
+                    case Type.RetryCancel:
+                        ButtonClicked(ResultType.Retry);
+                        break;
+                    case Type.TryCancelContinue:
+                        ButtonClicked(ResultType.Try);
+                        break;
+                    case Type.YesNo:
+                    case Type.YesNoCancel:
+                        ButtonClicked(ResultType.Yes); 
+                        break;
+                }
+            }
+
+            if (Input.IsButtonDown(ButtonCode.Escape))
+            {
+                switch (type)
+                {
+                    case Type.OK:
+                        ButtonClicked(ResultType.OK);
+                        break;
+                    case Type.RetryAbortIgnore:
+                        ButtonClicked(ResultType.Ignore);
+                        break;
+                    case Type.OKCancel:
+                    case Type.RetryCancel:
+                    case Type.YesNoCancel:
+                    case Type.TryCancelContinue:
+                        ButtonClicked(ResultType.Cancel);
+                        break;
+                    case Type.YesNo:
+                        ButtonClicked(ResultType.No);
+                        break;
+                }
+            }
+        }
+
         private void SetupGUI()
         {
             messageLabel = new GUILabel("", EditorStyles.MultiLineLabel,

+ 2 - 2
MBansheeEditor/EditorApplication.cs

@@ -135,7 +135,7 @@ namespace BansheeEditor
         private static void SavePrefabAs()
         {
             string scenePath = "";
-            BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "", out scenePath);
+            BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.prefab", out scenePath);
 
             if (!PathEx.IsPartOf(scenePath, ProjectLibrary.ResourceFolder))
                 DialogBox.Open("Error", "The location must be inside the Resources folder of the project.", DialogBox.Type.OK);
@@ -144,7 +144,7 @@ namespace BansheeEditor
                 // TODO - If path points to an existing non-scene asset or folder I should delete it otherwise
                 //        Internal_SaveScene will silently fail.
 
-                Scene.ActiveSceneUUID = Internal_SaveScene(scenePath);
+                Scene.ActiveSceneUUID = Internal_SaveScene(scenePath + ".prefab");
             }
         }
 

+ 1 - 1
MBansheeEditor/ProjectWindow.cs

@@ -668,7 +668,7 @@ namespace BansheeEditor
                 pathNoExtension = path.Remove(path.Length - extension.Length);
 
             int idx = 0;
-            string destination = pathNoExtension;
+            string destination = path;
             while (ProjectLibrary.Exists(destination))
             {
                 destination = pathNoExtension + "_" + idx;

+ 7 - 7
SBansheeEditor/Include/BsScriptModalWindow.h

@@ -38,10 +38,10 @@ namespace BansheeEngine
 
 		void onAssemblyRefreshStarted();
 
-		void _onManagedInstanceDeleted();
-		ScriptObjectBackup beginRefresh();
-		void endRefresh(const ScriptObjectBackup& backupData);
-		MonoObject* _createManagedInstance(bool construct);
+		void _onManagedInstanceDeleted() override;
+		ScriptObjectBackup beginRefresh() override;
+		void endRefresh(const ScriptObjectBackup& backupData) override;
+		MonoObject* _createManagedInstance(bool construct) override;
 		void notifyWindowDestroyed();
 
 		ManagedModalWindow* mModalWindow;
@@ -61,15 +61,15 @@ namespace BansheeEngine
 		void releaseManagedInstance();
 
 		void setParent(ScriptModalWindow* parent);
-		void update();
+		void update() override;
 		void reloadMonoTypes(MonoClass* windowClass);
 		void triggerOnInitialize();
 		void triggerOnDestroy();
 
 		MonoObject* getManagedInstance() const { return mManagedInstance; }
 	protected:
-		virtual void resized();
-		virtual void close();
+		virtual void resized() override;
+		virtual void close() override;
 
 	private:
 		friend class ScriptModalWindow;

+ 5 - 3
SBansheeEditor/Source/BsScriptEditorApplication.cpp

@@ -118,15 +118,17 @@ namespace BansheeEngine
 
 			scene = static_resource_cast<Prefab>(ProjectLibrary::instance().load(nativePath));
 			scene->update(sceneRoot);
+
+			PrefabUtility::recordPrefabDiff(sceneRoot);
+			ProjectLibrary::instance().saveEntry(scene);
 		}
 		else
 		{
 			scene = Prefab::create(sceneRoot);
+			PrefabUtility::recordPrefabDiff(sceneRoot);
+			ProjectLibrary::instance().createEntry(scene, nativePath);
 		}
 
-		PrefabUtility::recordPrefabDiff(sceneRoot);
-		ProjectLibrary::instance().saveEntry(scene);
-
 		return MonoUtil::stringToMono(MonoManager::instance().getDomain(), scene.getUUID());
 	}
 }

+ 5 - 1
SBansheeEditor/Source/BsScriptProjectLibrary.cpp

@@ -412,6 +412,10 @@ namespace BansheeEngine
 			return ScriptResourceType::Undefined;
 
 		ProjectLibrary::ResourceEntry* fileEntry = static_cast<ProjectLibrary::ResourceEntry*>(entry);
-		return ScriptResource::getTypeFromTypeId(fileEntry->meta->getTypeID());
+
+		if (fileEntry->meta != nullptr)
+			return ScriptResource::getTypeFromTypeId(fileEntry->meta->getTypeID());
+
+		return ScriptResourceType::Undefined;
 	}
 }