Browse Source

Refactored DropDownWindow so that creating GUI in constructor doesn't cause a crash

BearishSun 9 years ago
parent
commit
85a25e08cf

+ 1 - 1
Source/BansheeUtility/Source/BsMatrix4.cpp

@@ -124,7 +124,7 @@ namespace BansheeEngine
         float d13 = + (v5 * m00 - v2 * m02 + v1 * m03) * invDet;
         float d13 = + (v5 * m00 - v2 * m02 + v1 * m03) * invDet;
         float d23 = - (v4 * m00 - v2 * m01 + v0 * m03) * invDet;
         float d23 = - (v4 * m00 - v2 * m01 + v0 * m03) * invDet;
         float d33 = + (v3 * m00 - v1 * m01 + v0 * m02) * invDet;
         float d33 = + (v3 * m00 - v1 * m01 + v0 * m02) * invDet;
-
+		
         return Matrix4(
         return Matrix4(
             d00, d01, d02, d03,
             d00, d01, d02, d03,
             d10, d11, d12, d13,
             d10, d11, d12, d13,

+ 18 - 32
Source/MBansheeEditor/Window/DropDownWindow.cs

@@ -17,16 +17,13 @@ namespace BansheeEditor
     /// </summary>
     /// </summary>
     public class DropDownWindow : ScriptObject
     public class DropDownWindow : ScriptObject
     {
     {
-        private int width;
-        private int height;
-
         /// <summary>
         /// <summary>
         /// Width of the window in pixels.
         /// Width of the window in pixels.
         /// </summary>
         /// </summary>
         public int Width
         public int Width
         {
         {
-            get { return width; }
-            set { Internal_SetWidth(mCachedPtr, value); width = value; }
+            get { return Internal_GetWidth(mCachedPtr); }
+            set { Internal_SetWidth(mCachedPtr, value); }
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -34,8 +31,8 @@ namespace BansheeEditor
         /// </summary>
         /// </summary>
         public int Height
         public int Height
         {
         {
-            get { return height; }
-            set { Internal_SetHeight(mCachedPtr, value); height = value; }
+            get { return Internal_GetHeight(mCachedPtr); }
+            set { Internal_SetHeight(mCachedPtr, value); }
         }
         }
 
 
         protected GUIPanel GUI;
         protected GUIPanel GUI;
@@ -50,8 +47,11 @@ namespace BansheeEditor
         /// <returns>Instance of the opened drop down window.</returns>
         /// <returns>Instance of the opened drop down window.</returns>
         public static T Open<T>(EditorWindow parent, Vector2I position) where T : DropDownWindow, new()
         public static T Open<T>(EditorWindow parent, Vector2I position) where T : DropDownWindow, new()
         {
         {
-            T window = new T();
-            window.Initialize(parent, position);
+            IntPtr parentPtr = IntPtr.Zero;
+            if (parent != null)
+                parentPtr = parent.GetCachedPtr();
+
+            T window = (T)Internal_CreateInstance(typeof(T).Namespace, typeof(T).Name, parentPtr, ref position);
 
 
             return window;
             return window;
         }
         }
@@ -59,28 +59,8 @@ namespace BansheeEditor
         /// <summary>
         /// <summary>
         /// Constructs a new drop down window.
         /// Constructs a new drop down window.
         /// </summary>
         /// </summary>
-        /// <param name="width">Width of the window in pixels.</param>
-        /// <param name="height">Height of the window in pixels.</param>
-        protected DropDownWindow(int width = 200, int height = 200)
-        {
-            this.width = width;
-            this.height = height;
-        }
-
-        /// <summary>
-        /// Initializes the drop down window after construction. Must be called before use.
-        /// </summary>
-        /// <param name="parent">Parent editor window to open the drop down window in.</param>
-        /// <param name="position">Position relative to the parent editor window at which to open the drop down window.
-        ///                        </param>
-        private void Initialize(EditorWindow parent, Vector2I position)
-        {
-            IntPtr parentPtr = IntPtr.Zero;
-            if (parent != null)
-                parentPtr = parent.GetCachedPtr();
-
-            Internal_CreateInstance(this, parentPtr, ref position, width, height);
-        }
+        protected DropDownWindow()
+        { }
 
 
         /// <summary>
         /// <summary>
         /// Converts coordinates in screen space to coordinates relative to the drop down window.
         /// Converts coordinates in screen space to coordinates relative to the drop down window.
@@ -115,14 +95,20 @@ namespace BansheeEditor
         }
         }
 
 
         [MethodImpl(MethodImplOptions.InternalCall)]
         [MethodImpl(MethodImplOptions.InternalCall)]
-        private static extern void Internal_CreateInstance(DropDownWindow instance, IntPtr parentWindow, ref Vector2I position, int width, int height);
+        private static extern DropDownWindow Internal_CreateInstance(string ns, string typeName, IntPtr parentWindow, ref Vector2I position);
 
 
         [MethodImpl(MethodImplOptions.InternalCall)]
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern void Internal_Close(IntPtr nativeInstance);
         private static extern void Internal_Close(IntPtr nativeInstance);
 
 
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern int Internal_GetWidth(IntPtr nativeInstance);
+
         [MethodImpl(MethodImplOptions.InternalCall)]
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern void Internal_SetWidth(IntPtr nativeInstance, int value);
         private static extern void Internal_SetWidth(IntPtr nativeInstance, int value);
 
 
+        [MethodImpl(MethodImplOptions.InternalCall)]
+        private static extern int Internal_GetHeight(IntPtr nativeInstance);
+
         [MethodImpl(MethodImplOptions.InternalCall)]
         [MethodImpl(MethodImplOptions.InternalCall)]
         private static extern void Internal_SetHeight(IntPtr nativeInstance, int value);
         private static extern void Internal_SetHeight(IntPtr nativeInstance, int value);
 
 

+ 1 - 7
Source/MBansheeEditor/Windows/Library/LibraryDropDown.cs

@@ -11,17 +11,11 @@ namespace BansheeEditor
     /// <summary>
     /// <summary>
     /// Drop down window that displays options used by the library window.
     /// Drop down window that displays options used by the library window.
     /// </summary>
     /// </summary>
+    [DefaultSize(150, 30)]
     internal class LibraryDropDown : DropDownWindow
     internal class LibraryDropDown : DropDownWindow
     {
     {
         private LibraryWindow parent;
         private LibraryWindow parent;
 
 
-        /// <summary>
-        /// Constructs the drop down window.
-        /// </summary>
-        public LibraryDropDown()
-            : base(150, 30)
-        { }
-
         /// <summary>
         /// <summary>
         /// Initializes the drop down window by creating the necessary GUI. Must be called after construction and before
         /// Initializes the drop down window by creating the necessary GUI. Must be called after construction and before
         /// use.
         /// use.

+ 3 - 1
Source/SBansheeEditor/Include/BsScriptDropDownWindow.h

@@ -42,9 +42,11 @@ namespace BansheeEngine
 		/************************************************************************/
 		/************************************************************************/
 		/* 								CLR HOOKS						   		*/
 		/* 								CLR HOOKS						   		*/
 		/************************************************************************/
 		/************************************************************************/
-		static void internal_CreateInstance(MonoObject* instance, ScriptEditorWindow* parentWindow, Vector2I* position, int width, int height);
+		static MonoObject* internal_CreateInstance(MonoString* ns, MonoString* typeName, ScriptEditorWindow* parentWindow, Vector2I* position);
 		static void internal_Close(ScriptDropDownWindow* nativeInstance);
 		static void internal_Close(ScriptDropDownWindow* nativeInstance);
+		static UINT32 internal_GetWidth(ScriptDropDownWindow* nativeInstance);
 		static void internal_SetWidth(ScriptDropDownWindow* nativeInstance, UINT32 value);
 		static void internal_SetWidth(ScriptDropDownWindow* nativeInstance, UINT32 value);
+		static UINT32 internal_GetHeight(ScriptDropDownWindow* nativeInstance);
 		static void internal_SetHeight(ScriptDropDownWindow* nativeInstance, UINT32 value);
 		static void internal_SetHeight(ScriptDropDownWindow* nativeInstance, UINT32 value);
 		static void internal_ScreenToWindowPos(ScriptDropDownWindow* nativeInstance, Vector2I* position, Vector2I* windowPos);
 		static void internal_ScreenToWindowPos(ScriptDropDownWindow* nativeInstance, Vector2I* position, Vector2I* windowPos);
 		static void internal_WindowToScreenPos(ScriptDropDownWindow* nativeInstance, Vector2I* position, Vector2I* screenPos);
 		static void internal_WindowToScreenPos(ScriptDropDownWindow* nativeInstance, Vector2I* position, Vector2I* screenPos);

+ 53 - 2
Source/SBansheeEditor/Source/BsScriptDropDownWindow.cpp

@@ -5,6 +5,7 @@
 #include "BsMonoField.h"
 #include "BsMonoField.h"
 #include "BsMonoClass.h"
 #include "BsMonoClass.h"
 #include "BsMonoMethod.h"
 #include "BsMonoMethod.h"
+#include "BsMonoAssembly.h"
 #include "BsMonoManager.h"
 #include "BsMonoManager.h"
 #include "BsMonoUtil.h"
 #include "BsMonoUtil.h"
 #include "BsScriptGUILayout.h"
 #include "BsScriptGUILayout.h"
@@ -40,7 +41,9 @@ namespace BansheeEngine
 	{
 	{
 		metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptDropDownWindow::internal_CreateInstance);
 		metaData.scriptClass->addInternalCall("Internal_CreateInstance", &ScriptDropDownWindow::internal_CreateInstance);
 		metaData.scriptClass->addInternalCall("Internal_Close", &ScriptDropDownWindow::internal_Close);
 		metaData.scriptClass->addInternalCall("Internal_Close", &ScriptDropDownWindow::internal_Close);
+		metaData.scriptClass->addInternalCall("Internal_GetWidth", &ScriptDropDownWindow::internal_GetWidth);
 		metaData.scriptClass->addInternalCall("Internal_SetWidth", &ScriptDropDownWindow::internal_SetWidth);
 		metaData.scriptClass->addInternalCall("Internal_SetWidth", &ScriptDropDownWindow::internal_SetWidth);
+		metaData.scriptClass->addInternalCall("Internal_GetHeight", &ScriptDropDownWindow::internal_GetHeight);
 		metaData.scriptClass->addInternalCall("Internal_SetHeight", &ScriptDropDownWindow::internal_SetHeight);
 		metaData.scriptClass->addInternalCall("Internal_SetHeight", &ScriptDropDownWindow::internal_SetHeight);
 		metaData.scriptClass->addInternalCall("Internal_ScreenToWindowPos", &ScriptDropDownWindow::internal_ScreenToWindowPos);
 		metaData.scriptClass->addInternalCall("Internal_ScreenToWindowPos", &ScriptDropDownWindow::internal_ScreenToWindowPos);
 		metaData.scriptClass->addInternalCall("Internal_WindowToScreenPos", &ScriptDropDownWindow::internal_WindowToScreenPos);
 		metaData.scriptClass->addInternalCall("Internal_WindowToScreenPos", &ScriptDropDownWindow::internal_WindowToScreenPos);
@@ -48,9 +51,38 @@ namespace BansheeEngine
 		guiPanelField = metaData.scriptClass->getField("GUI");
 		guiPanelField = metaData.scriptClass->getField("GUI");
 	}
 	}
 
 
-	void ScriptDropDownWindow::internal_CreateInstance(MonoObject* instance, ScriptEditorWindow* parentWindow, 
-		Vector2I* position, int width, int height)
+	MonoObject* ScriptDropDownWindow::internal_CreateInstance(MonoString* ns, MonoString* typeName, 
+		ScriptEditorWindow* parentWindow, Vector2I* position)
 	{
 	{
+		String strTypeName = toString(MonoUtil::monoToWString(typeName));
+		String strNamespace = toString(MonoUtil::monoToWString(ns));
+		String fullName = strNamespace + "." + strTypeName;
+
+		MonoClass* windowClass = MonoManager::instance().findClass(strNamespace, strTypeName);
+		if (windowClass == nullptr)
+			return nullptr;
+
+		MonoAssembly* assembly = MonoManager::instance().getAssembly(EDITOR_ASSEMBLY);
+
+		MonoClass* defaultSizeAttrib = assembly->getClass("BansheeEditor", "DefaultSize");
+		if (defaultSizeAttrib == nullptr)
+			BS_EXCEPT(InternalErrorException, "Cannot find DefaultSize managed class.");
+
+		MonoField* defaultWidthField = defaultSizeAttrib->getField("width");
+		MonoField* defaultHeightField = defaultSizeAttrib->getField("height");
+
+		int width = 200;
+		int height = 200;
+
+		MonoObject* defaultSizeObj = windowClass->getAttribute(defaultSizeAttrib);
+		if (defaultSizeObj != nullptr)
+		{
+			defaultWidthField->getValue(defaultSizeObj, &width);
+			defaultHeightField->getValue(defaultSizeObj, &height);
+		}
+
+		MonoObject* instance = windowClass->createInstance(false);
+
 		ManagedDropDownWindow* dropDownWindow = nullptr;
 		ManagedDropDownWindow* dropDownWindow = nullptr;
 		if (parentWindow != nullptr && !parentWindow->isDestroyed())
 		if (parentWindow != nullptr && !parentWindow->isDestroyed())
 		{
 		{
@@ -73,6 +105,9 @@ namespace BansheeEngine
 
 
 		if (dropDownWindow != nullptr)
 		if (dropDownWindow != nullptr)
 			dropDownWindow->initialize(nativeInstance);
 			dropDownWindow->initialize(nativeInstance);
+
+		windowClass->construct(instance);
+		return instance;
 	}
 	}
 
 
 	void ScriptDropDownWindow::internal_Close(ScriptDropDownWindow* thisPtr)
 	void ScriptDropDownWindow::internal_Close(ScriptDropDownWindow* thisPtr)
@@ -92,12 +127,28 @@ namespace BansheeEngine
 		mDropDownWindow = nullptr;
 		mDropDownWindow = nullptr;
 	}
 	}
 
 
+	UINT32 ScriptDropDownWindow::internal_GetWidth(ScriptDropDownWindow* thisPtr)
+	{
+		if (thisPtr->mDropDownWindow != nullptr)
+			return thisPtr->mDropDownWindow->getWidth();
+
+		return 0;
+	}
+
 	void ScriptDropDownWindow::internal_SetWidth(ScriptDropDownWindow* thisPtr, UINT32 value)
 	void ScriptDropDownWindow::internal_SetWidth(ScriptDropDownWindow* thisPtr, UINT32 value)
 	{
 	{
 		if (thisPtr->mDropDownWindow != nullptr)
 		if (thisPtr->mDropDownWindow != nullptr)
 			thisPtr->mDropDownWindow->setSize(value, thisPtr->mDropDownWindow->getHeight());
 			thisPtr->mDropDownWindow->setSize(value, thisPtr->mDropDownWindow->getHeight());
 	}
 	}
 
 
+	UINT32 ScriptDropDownWindow::internal_GetHeight(ScriptDropDownWindow* thisPtr)
+	{
+		if (thisPtr->mDropDownWindow != nullptr)
+			return thisPtr->mDropDownWindow->getHeight();
+
+		return 0;
+	}
+
 	void ScriptDropDownWindow::internal_SetHeight(ScriptDropDownWindow* thisPtr, UINT32 value)
 	void ScriptDropDownWindow::internal_SetHeight(ScriptDropDownWindow* thisPtr, UINT32 value)
 	{
 	{
 		if (thisPtr->mDropDownWindow != nullptr)
 		if (thisPtr->mDropDownWindow != nullptr)