Skip to content

Commit

Permalink
Fix convention to follow project, Fix ReadOnly errors, Fix OnEnabled …
Browse files Browse the repository at this point in the history
…never been called
  • Loading branch information
Prastiwar committed Feb 25, 2021
1 parent 7707f3b commit 0f22dc1
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 47 deletions.
1 change: 0 additions & 1 deletion CommonUDictionaries.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using UnityEngine;

namespace UnityEngine.Collections.Generic
{
Expand Down
63 changes: 28 additions & 35 deletions Editor/UDictionaryDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using UnityEditor;
using Unity.Collections;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.Collections;
using UnityEngine.Collections.Generic;

namespace UnityEditor.Collections.Generic
{
[CustomPropertyDrawer(typeof(UDictionaryBase), true)]
public class UDictionaryDrawer : PropertyDrawer
{
const string DuplicatedKeyErrorMessage = "You have duplicated keys, some changes can be lost!";
const string KeyTypeErrorMessage = "The key type does not support serialization";
const string ValueTypeErrorMessage = "The value type does not support serialization";
private const string DuplicatedKeyErrorMessage = "You have duplicated keys, some changes can be lost!";
private const string KeyTypeErrorMessage = "The key type does not support serialization";
private const string ValueTypeErrorMessage = "The value type does not support serialization";

private bool isEnabled = false;
private readonly float space = 17;
Expand All @@ -43,7 +42,7 @@ public class UDictionaryDrawer : PropertyDrawer
protected SerializedProperty KeysProperty { get; private set; }
protected SerializedProperty ValuesProperty { get; private set; }

protected bool IsDragging { get { return (bool)RList.GetType().GetField("m_Dragging", privateInstanceFlags).GetValue(RList); } }
protected bool IsDragging => (bool)RList.GetType().GetField("m_Dragging", privateInstanceFlags).GetValue(RList);

public sealed override bool CanCacheInspectorGUI(SerializedProperty property)
{
Expand All @@ -55,10 +54,7 @@ public sealed override bool CanCacheInspectorGUI(SerializedProperty property)
return base.CanCacheInspectorGUI(property);
}

public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return foldoutRList ? (RList != null ? RList.GetHeight() : 0) + space : space;
}
public override float GetPropertyHeight(SerializedProperty property, GUIContent label) => foldoutRList ? (RList != null ? RList.GetHeight() : 0) + space : space;

private void OnEnable(SerializedProperty property)
{
Expand Down Expand Up @@ -86,14 +82,26 @@ protected virtual void InitializeRedBoxVariables()

public override void OnGUI(Rect rect, SerializedProperty property, GUIContent label)
{
if (!isEnabled)
{
// this shouldn't be here, but since CanCacheInspectorGUI is apparently not called
// there is not currently way to call this just on enabled
OnEnable(property);
}
foldoutRList = EditorGUI.Foldout(new Rect(rect.position, new Vector2(rect.size.x, space)), foldoutRList, label, true);
if(KeysProperty == null)
if (KeysProperty == null)
{
DrawErrorMessage(rect, property.name.Length, KeyTypeErrorMessage);
else if(ValuesProperty == null)
}
else if (ValuesProperty == null)
{
DrawErrorMessage(rect, property.name.Length, ValueTypeErrorMessage);
}
else if (hasDuplicatedKey)
{
DrawErrorMessage(rect, property.name.Length, DuplicatedKeyErrorMessage);

}

if (foldoutRList && RList != null)
{
hasDuplicatedKey = false;
Expand All @@ -117,23 +125,17 @@ protected void InitializeList(SerializedProperty prop)
}
}

protected virtual ReorderableList CreateList(SerializedObject serializedObj, SerializedProperty elements)
{
return new ReorderableList(serializedObj, elements, !isReadOnly, true, !isReadOnly, !isReadOnly)
{
protected virtual ReorderableList CreateList(SerializedObject serializedObj, SerializedProperty elements) =>
new ReorderableList(serializedObj, elements, !isReadOnly, true, !isReadOnly, !isReadOnly) {
drawHeaderCallback = DrawHeader,
onAddCallback = OnAdd,
onRemoveCallback = OnRemove,
onReorderCallback = OnReorder,
elementHeightCallback = GetReorderableElementHeight,
drawElementCallback = DrawElement,
};
}

protected void DrawHeader(Rect rect)
{
EditorGUI.LabelField(rect, fieldInfo.Name);
}
protected void DrawHeader(Rect rect) => EditorGUI.LabelField(rect, fieldInfo.Name);

protected void OnAdd(ReorderableList list)
{
Expand All @@ -148,15 +150,9 @@ protected void OnRemove(ReorderableList list)
ValuesProperty.DeleteArrayElementAtIndex(SelectedIndex);
}

protected void OnReorder(ReorderableList list)
{
ValuesProperty.MoveArrayElement(SelectedIndex, list.index);
}
protected void OnReorder(ReorderableList list) => ValuesProperty.MoveArrayElement(SelectedIndex, list.index);

private void OnSelect(ReorderableList list)
{
SelectedIndex = list.index;
}
private void OnSelect(ReorderableList list) => SelectedIndex = list.index;

protected float GetReorderableElementHeight(int index)
{
Expand Down Expand Up @@ -229,15 +225,12 @@ protected GUIContent GUIContent(string text)


private bool wasEnabled;
protected virtual void OnBeforeDrawProperties()
protected virtual void OnBeforeDrawProperties()
{
wasEnabled = GUI.enabled;
GUI.enabled = !isReadOnly;
}

protected virtual void OnAfterDrawProperties()
{
GUI.enabled = wasEnabled;
}
protected virtual void OnAfterDrawProperties() => GUI.enabled = wasEnabled;
}
}
1 change: 0 additions & 1 deletion Editor/UDictionaryExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using System;
using System.Collections;
using System.Reflection;
using UnityEditor;

namespace UnityEditor.Collections.Generic
{
Expand Down
1 change: 0 additions & 1 deletion Editor/UDictionaryReadKeyDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* Repository: https://github.com/Prastiwar/UnitySerializedDictionary
*/

using UnityEditor;
using UnityEngine;

namespace UnityEditor.Collections.Generic
Expand Down
22 changes: 13 additions & 9 deletions HashHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Runtime.ConstrainedExecution;


namespace Unity.Collections
{
public static class HashHelpers
{
public const Int32 HashPrime = 101;
public const int HashPrime = 101;

// Table of prime numbers to use as hash table sizes.
// A typical resize algorithm would pick the smallest prime number in this array
Expand Down Expand Up @@ -36,7 +35,9 @@ public static bool IsPrime(int candidate)
for (int divisor = 3; divisor <= limit; divisor += 2)
{
if ((candidate % divisor) == 0)
{
return false;
}
}
return true;
}
Expand All @@ -47,28 +48,32 @@ public static bool IsPrime(int candidate)
public static int GetPrime(int min)
{
if (min < 0)
{
throw new ArgumentException();
}

for (int i = 0; i < primes.Length; i++)
{
int prime = primes[i];
if (prime >= min) return prime;
if (prime >= min)
{
return prime;
}
}

//outside of our predefined table.
//compute the hard way.
for (int i = (min | 1); i < Int32.MaxValue; i += 2)
for (int i = (min | 1); i < int.MaxValue; i += 2)
{
if (IsPrime(i) && ((i - 1) % HashPrime != 0))
{
return i;
}
}
return min;
}

public static int GetMinPrime()
{
return primes[0];
}
public static int GetMinPrime() => primes[0];

// Returns size of hashtable to grow to.
public static int ExpandPrime(int oldSize)
Expand All @@ -81,7 +86,6 @@ public static int ExpandPrime(int oldSize)
{
return MaxPrimeArrayLength;
}

return GetPrime(newSize);
}

Expand Down
Binary file modified UnityDictionaryExamples.unitypackage
Binary file not shown.

0 comments on commit 0f22dc1

Please sign in to comment.