Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
JLChnToZ committed Dec 13, 2023
1 parent f53446d commit 389551a
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 159 deletions.
139 changes: 0 additions & 139 deletions Packages/idv.jlchntoz.vvmw/Editor/Common/ReorderableListUtils.cs

This file was deleted.

139 changes: 139 additions & 0 deletions Packages/idv.jlchntoz.vvmw/Editor/Common/SerializedReorderableList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#if !UNITY_2022_1_OR_NEWER
using System.Reflection;
#endif
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using UnityEditorInternal;

namespace JLChnToZ.VRC.VVMW.Editors {
public class SerializedReorderableList : ReorderableList {
static GUIContent headerContent;
#if !UNITY_2022_1_OR_NEWER
// SerializedProperty.gradientValue is internal before Unity 2022.1
static readonly PropertyInfo gradientValueField = typeof(SerializedProperty)
.GetProperty("gradientValue", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);
#endif
readonly HashSet<int> expandedIndices = new HashSet<int>();

public SerializedReorderableList(
SerializedProperty elements
) : this(elements.serializedObject, elements, true, true, true, true) { }

public SerializedReorderableList(
SerializedObject serializedObject, SerializedProperty elements
) : this(serializedObject, elements, true, true, true, true) { }

public SerializedReorderableList(
SerializedObject serializedObject, SerializedProperty elements,
bool draggable, bool displayHeader, bool displayAddButton, bool displayRemoveButton
) : base(serializedObject, elements, draggable, displayHeader, displayAddButton, displayRemoveButton) {
drawHeaderCallback = DrawHeader;
drawElementCallback = DrawElement;
elementHeightCallback = GetElementHeight;
onAddCallback = OnAdd;
onRemoveCallback = OnRemove;
onReorderCallbackWithDetails = OnReorder;
}

void DrawHeader(Rect rect) {
if (headerContent == null) headerContent = new GUIContent();
headerContent.text = serializedProperty.displayName;
headerContent.tooltip = serializedProperty.tooltip;
EditorGUI.LabelField(rect, headerContent, EditorStyles.boldLabel);
}

float GetElementHeight(int index) =>
EditorGUI.GetPropertyHeight(serializedProperty.GetArrayElementAtIndex(index), GUIContent.none, expandedIndices.Contains(index));

void DrawElement(Rect rect, int index, bool isActive, bool isFocused) {
if (EditorGUI.PropertyField(rect, serializedProperty.GetArrayElementAtIndex(index), GUIContent.none, expandedIndices.Contains(index)) &&
!expandedIndices.Add(index)) expandedIndices.Remove(index);
}

static void OnAdd(ReorderableList list) {
var property = list.serializedProperty;
int index = list.index + 1, count = property.arraySize;
if (index < 0 || index > count) index = count;
property.InsertArrayElementAtIndex(index);
var element = property.GetArrayElementAtIndex(index);
int depth = element.depth;
do {
if (element.isArray) {
element.ClearArray();
continue;
}
switch (element.propertyType) {
case SerializedPropertyType.Integer:
case SerializedPropertyType.LayerMask:
case SerializedPropertyType.ArraySize:
case SerializedPropertyType.Character: element.intValue = default; break;
case SerializedPropertyType.Boolean: element.boolValue = default; break;
case SerializedPropertyType.Float: element.floatValue = default; break;
case SerializedPropertyType.String: element.stringValue = ""; break;
case SerializedPropertyType.Color: element.colorValue = Color.black; break;
case SerializedPropertyType.ObjectReference: element.objectReferenceValue = default; break;
case SerializedPropertyType.Enum: element.enumValueIndex = default; break;
case SerializedPropertyType.Vector2: element.vector2Value = default; break;
case SerializedPropertyType.Vector3: element.vector3Value = default; break;
case SerializedPropertyType.Vector4: element.vector4Value = default; break;
case SerializedPropertyType.Rect: element.rectValue = default; break;
case SerializedPropertyType.AnimationCurve: element.animationCurveValue = default; break;
case SerializedPropertyType.Bounds: element.boundsValue = default; break;
case SerializedPropertyType.Gradient:
#if UNITY_2022_1_OR_NEWER
element.gradientValue = default;
#else
gradientValueField.SetValue(element, default);
#endif
break;
case SerializedPropertyType.Quaternion: element.quaternionValue = Quaternion.identity; break;
case SerializedPropertyType.ExposedReference: element.exposedReferenceValue = default; break;
case SerializedPropertyType.Vector2Int: element.vector2IntValue = default; break;
case SerializedPropertyType.Vector3Int: element.vector3IntValue = default; break;
case SerializedPropertyType.RectInt: element.rectIntValue = default; break;
case SerializedPropertyType.BoundsInt: element.boundsIntValue = default; break;
#if UNITY_2019_3_OR_NEWER
case SerializedPropertyType.ManagedReference: element.managedReferenceValue = default; break;
#endif
#if UNITY_2021_1_OR_NEWER
case SerializedPropertyType.Hash128: element.hash128Value = default; break;
#endif
}
} while (element.Next(element.propertyType == SerializedPropertyType.Generic) && element.depth > depth);
list.index = index;
}

static void OnRemove(ReorderableList list) {
var property = list.serializedProperty;
int index = list.index, count = property.arraySize;
if (index < 0 || index >= count) index = count - 1;
property.DeleteArrayElementAtIndex(index);
if (property.arraySize == count) property.DeleteArrayElementAtIndex(index);
if (list.index == index) list.index--;
if (list is SerializedReorderableList srl) {
var expandedIndices = srl.expandedIndices;
expandedIndices.Remove(index);
for (int i = index; i < count; i++)
if (expandedIndices.Remove(i + 1))
expandedIndices.Add(i);
}
}

static void OnReorder(ReorderableList list, int oldIndex, int newIndex) {
if (!(list is SerializedReorderableList srl)) return;
var expandedIndices = srl.expandedIndices;
if (oldIndex < newIndex) {
for (int i = oldIndex; i < newIndex; i++)
if (expandedIndices.Remove(i))
expandedIndices.Add(i + 1);
} else if (oldIndex > newIndex) {
for (int i = newIndex + 1; i <= oldIndex; i++)
if (expandedIndices.Remove(i))
expandedIndices.Add(i - 1);
} else return;
if (expandedIndices.Remove(newIndex))
expandedIndices.Add(oldIndex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class LanguageManagerEditor : VVMWEditorBase {
static GUIContent textContent;
SerializedProperty languageJsonFiles, languageJson;
LanguageEditorWindow openedWindow;
ReorderableListUtils languageJsonFilesList;
SerializedReorderableList languageJsonFilesList;
GUIStyle wrappedTextAreaStyle;
bool showJson = false;
[NonSerialized] bool hasInit;
Expand All @@ -24,7 +24,7 @@ protected override void OnEnable() {
};
languageJsonFiles = serializedObject.FindProperty("languageJsonFiles");
languageJson = serializedObject.FindProperty("languageJson");
languageJsonFilesList = new ReorderableListUtils(languageJsonFiles);
languageJsonFilesList = new SerializedReorderableList(languageJsonFiles);
hasInit = true;
}

Expand All @@ -39,7 +39,7 @@ public override void OnInspectorGUI() {
EditorGUILayout.Space();
serializedObject.Update();
using (var changed = new EditorGUI.ChangeCheckScope()) {
languageJsonFilesList.Draw();
languageJsonFilesList.DoLayoutList();
if (changed.changed && openedWindow != null) openedWindow.RefreshJsonLists();
}
if (openedWindow == null || openedWindow.LanguageManager != target) openedWindow = null;
Expand Down
Loading

0 comments on commit 389551a

Please sign in to comment.