Преглед изворни кода

Various GUI dictionary fixes (WIP)

BearishSun пре 10 година
родитељ
комит
a609ff7f4c

+ 38 - 5
MBansheeEditor/GUI/GUIDictionaryField.cs

@@ -410,6 +410,12 @@ namespace BansheeEditor
         /// <returns>True if the key exists in the dictionary, false otherwise.</returns>
         protected internal abstract bool Contains(object key);
 
+        /// <summary>
+        /// Clones the specified dictionary element.
+        /// </summary>
+        /// <param name="index">Sequential index of the element in the dictionary to clone.</param>
+        protected internal abstract KeyValuePair<object, object> CloneElement(int index);
+
         /// <summary>
         /// Creates a brand new dictionary with zero elements in the place of the current dictionary.
         /// </summary>
@@ -511,14 +517,21 @@ namespace BansheeEditor
                 int oldNumRows = GetNumRows();
                 Dictionary<object, int> oldKeys = new Dictionary<object, int>();
                 for (int i = 0; i < oldNumRows; i++)
+                {
+                    Debug.Log("OLD KEYS: " + i + " - " + ((SerializableProperty)GetKey(i)).GetValue<object>() + " -- " +
+                        ((SerializableProperty)GetValue(GetKey(i))).GetValue<object>());
                     oldKeys.Add(GetKey(i), i);
+                }
 
                 RemoveEntry(GetKey(rowIdx));
 
                 int newNumRows = GetNumRows();
                 Dictionary<object, int> newKeys = new Dictionary<object, int>();
                 for (int i = 0; i < newNumRows; i++)
+                {
+                    Debug.Log("NEW KEYS: " + i + " - " + ((SerializableProperty)GetKey(i)).GetValue<object>());
                     newKeys.Add(GetKey(i), i);
+                }
 
                 foreach (var KVP in oldKeys)
                 {
@@ -528,6 +541,7 @@ namespace BansheeEditor
                         if (KVP.Value != newRowIdx)
                         {
                             GUIDictionaryFieldRow temp = rows[KVP.Value];
+                            Debug.Log("Swapping: " + KVP.Value + " - " + newRowIdx);
 
                             temp.SetIndex(newRowIdx);
                             rows[newRowIdx].SetIndex(KVP.Value);
@@ -538,6 +552,10 @@ namespace BansheeEditor
                     }
                 }
 
+                for (int i = 0; i < newNumRows; i++)
+                    Debug.Log("NEW VALUES: " + i + " - " + ((SerializableProperty)GetKey(i)).GetValue<object>() + " -- " 
+                        + ((SerializableProperty)GetValue(GetKey(i))).GetValue<object>());
+
                 for (int i = oldNumRows - 1; i >= newNumRows; i--)
                 {
                     rows[i].Destroy();
@@ -628,9 +646,10 @@ namespace BansheeEditor
         private void StartEdit(int rowIdx)
         {
             object key = GetKey(rowIdx);
+            KeyValuePair<object, object> clone = CloneElement(rowIdx);
 
-            editKey = SerializableUtility.Clone(key);
-            editValue = SerializableUtility.Clone(GetValue(key));
+            editKey = clone.Key;
+            editValue = clone.Value;
             editOriginalKey = key;
             editRowIdx = rowIdx;
 
@@ -649,6 +668,7 @@ namespace BansheeEditor
             editOriginalKey = null;
             editRowIdx = rows.Count;
 
+            editRow.BuildGUI();
             editRow.Enabled = true;
             editRow.EditMode = true;
 
@@ -663,13 +683,14 @@ namespace BansheeEditor
         /// <param name="rowIdx">Sequential row index of the entry to clone.</param>
         private void StartClone(int rowIdx)
         {
-            object key = GetKey(rowIdx);
+            KeyValuePair<object, object> clone = CloneElement(rowIdx);
 
-            editKey = SerializableUtility.Clone(key);
-            editValue = SerializableUtility.Clone(GetValue(key));
+            editKey = clone.Key;
+            editValue = clone.Value;
             editOriginalKey = null;
             editRowIdx = rows.Count;
 
+            editRow.BuildGUI();
             editRow.Enabled = true;
             editRow.EditMode = true;
 
@@ -1003,6 +1024,18 @@ namespace BansheeEditor
             return SerializableUtility.Create<Value>();
         }
 
+        /// <inheritdoc/>
+        protected internal override KeyValuePair<object, object> CloneElement(int index)
+        {
+            object key = GetKey(index);
+            object value = GetValue(key);
+
+            KeyValuePair<object, object> clone = new KeyValuePair<object, object>(
+                SerializableUtility.Clone(key), SerializableUtility.Clone(value));
+
+            return clone;
+        }
+
         /// <inheritdoc/>
         protected override void CreateDictionary()
         {

+ 41 - 14
MBansheeEditor/Inspector/InspectableDictionary.cs

@@ -141,9 +141,9 @@ namespace BansheeEditor
             {
                 orderedKeys.Clear();
 
-                SerializableDictionary dict = property.GetDictionary();
-                if (dict != null)
+                if (dictionary != null)
                 {
+                    SerializableDictionary dict = property.GetDictionary();
                     foreach (var key in dictionary.Keys)
                         orderedKeys.Add(dict.GetProperty(key).Key);
                 }
@@ -266,6 +266,31 @@ namespace BansheeEditor
                 return valueProperty;
             }
 
+            /// <inheritdoc/>
+            protected internal override KeyValuePair<object, object> CloneElement(int index)
+            {
+                SerializableProperty keyProperty = (SerializableProperty)GetKey(index);
+                SerializableProperty valueProperty = (SerializableProperty)GetValue(keyProperty);
+
+                SerializableDictionary dictionary = property.GetDictionary();
+
+                DictionaryDataWrapper keyData = new DictionaryDataWrapper();
+                keyData.value = SerializableUtility.Clone(keyProperty.GetValue<object>());
+
+                SerializableProperty clonedKeyProperty = new SerializableProperty(dictionary.KeyPropertyType,
+                    dictionary.KeyType,
+                    () => keyData.value, (x) => keyData.value = x);
+
+                DictionaryDataWrapper valueData = new DictionaryDataWrapper();
+                valueData.value = SerializableUtility.Clone(valueProperty.GetValue<object>());
+
+                SerializableProperty clonedValueProperty = new SerializableProperty(dictionary.ValuePropertyType,
+                    dictionary.ValueType,
+                    () => valueData.value, (x) => valueData.value = x);
+
+                return new KeyValuePair<object,object>(clonedKeyProperty, clonedValueProperty);
+            }
+
             /// <inheritdoc/>
             protected override void CreateDictionary()
             {
@@ -300,19 +325,18 @@ namespace BansheeEditor
         /// </summary>
         private class InspectableDictionaryGUIRow : GUIDictionaryFieldRow
         {
+            private GUILayoutY keyLayout;
             private InspectableField fieldKey;
             private InspectableField fieldValue;
 
             /// <inheritdoc/>
             protected override GUILayoutX CreateKeyGUI(GUILayoutY layout)
             {
-                if (fieldKey == null)
-                {
-                    SerializableProperty property = GetKey<SerializableProperty>();
+                keyLayout = layout;
+                SerializableProperty property = GetKey<SerializableProperty>();
 
-                    fieldKey = CreateInspectable("Key", 0, Depth + 1,
-                        new InspectableFieldLayout(layout), property);
-                }
+                fieldKey = CreateInspectable("Key", 0, Depth + 1,
+                    new InspectableFieldLayout(layout), property);
 
                 return fieldKey.GetTitleLayout();
             }
@@ -320,13 +344,16 @@ namespace BansheeEditor
             /// <inheritdoc/>
             protected override void CreateValueGUI(GUILayoutY layout)
             {
-                if (fieldValue == null)
-                {
-                    SerializableProperty property = GetValue<SerializableProperty>();
+                SerializableProperty property = GetValue<SerializableProperty>();
 
-                    fieldValue = CreateInspectable("Value", 0, Depth + 1,
-                        new InspectableFieldLayout(layout), property);
-                }
+                fieldValue = CreateInspectable("Value", 0, Depth + 1,
+                    new InspectableFieldLayout(layout), property);
+            }
+
+            /// <inheritdoc/>
+            protected override void OnEditModeChanged(bool editMode)
+            {
+                keyLayout.Disabled = !editMode;
             }
 
             /// <inheritdoc/>

+ 13 - 1
SBansheeEditor/Source/BsScriptGUITextField.cpp

@@ -69,24 +69,36 @@ namespace BansheeEngine
 
 	void ScriptGUITextField::internal_getValue(ScriptGUITextField* nativeInstance, MonoString** output)
 	{
+		if (nativeInstance->isDestroyed())
+			*output = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), StringUtil::WBLANK);
+
 		GUITextField* field = static_cast<GUITextField*>(nativeInstance->getGUIElement());
 		*output = MonoUtil::wstringToMono(MonoManager::instance().getDomain(), field->getValue());
 	}
 
 	void ScriptGUITextField::internal_setValue(ScriptGUITextField* nativeInstance, MonoString* value)
 	{
+		if (nativeInstance->isDestroyed())
+			return;
+
 		GUITextField* field = static_cast<GUITextField*>(nativeInstance->getGUIElement());
-		return field->setValue(MonoUtil::monoToWString(value));
+		field->setValue(MonoUtil::monoToWString(value));
 	}
 
 	void ScriptGUITextField::internal_hasInputFocus(ScriptGUITextField* nativeInstance, bool* output)
 	{
+		if (nativeInstance->isDestroyed())
+			*output = false;
+
 		GUITextField* field = static_cast<GUITextField*>(nativeInstance->getGUIElement());
 		*output = field->hasInputFocus();
 	}
 
 	void ScriptGUITextField::internal_setTint(ScriptGUITextField* nativeInstance, Color color)
 	{
+		if (nativeInstance->isDestroyed())
+			return;
+
 		GUITextField* field = (GUITextField*)nativeInstance->getGUIElement();
 		field->setTint(color);
 	}