|
| 1 | +using Live2D.Cubism.Core; |
| 2 | +using UnityEditor; |
| 3 | +using UnityEditorInternal; |
| 4 | +using UnityEngine; |
| 5 | + |
| 6 | +namespace Live2D.Cubism.Editor.Inspectors |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Inspector for <see cref="CubismRenderer"/>s. |
| 10 | + /// </summary> |
| 11 | + [CustomEditor(typeof(CubismPart)), CanEditMultipleObjects] |
| 12 | + internal sealed class CubismPartInspector : UnityEditor.Editor |
| 13 | + { |
| 14 | + /// <summary> |
| 15 | + /// Parent <see cref="CubismPart"/> cache. |
| 16 | + /// </summary> |
| 17 | + private CubismPart _parent; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Child <see cref="CubismPart"/>s foldout state. |
| 21 | + /// </summary> |
| 22 | + private static bool ChildPartsFoldout { get; set; } = true; |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Child <see cref="CubismPart"/>s ReorderableList. |
| 26 | + /// </summary> |
| 27 | + private ReorderableList ChildParts { get; set; } |
| 28 | + |
| 29 | + /// <summary> |
| 30 | + /// Child <see cref="CubismPart"/>'s child <see cref="CubismDrawable"/>s. |
| 31 | + /// </summary> |
| 32 | + private InternalTreeInfo ChildPartDrawables { get; set; } |
| 33 | + |
| 34 | + private int DrawablesTotalCount { get; set; } |
| 35 | + |
| 36 | + #region Editor |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Draws inspector. |
| 40 | + /// </summary> |
| 41 | + public override void OnInspectorGUI() |
| 42 | + { |
| 43 | + if (!IsInitialized) |
| 44 | + { |
| 45 | + Initialize(); |
| 46 | + } |
| 47 | + |
| 48 | + EditorGUILayout.ObjectField("Parent Part", _parent, typeof(CubismPart), false); |
| 49 | + |
| 50 | + { |
| 51 | + var rect = EditorGUILayout.GetControlRect(); |
| 52 | + var foldoutRect = new Rect(rect.x, rect.y, rect.width, rect.height); |
| 53 | + var fieldRect = new Rect(rect.x + rect.width - 50f, rect.y, 50f, rect.height); |
| 54 | + |
| 55 | + ChildPartsFoldout = EditorGUI.Foldout(foldoutRect, ChildPartsFoldout, "Child Parts"); |
| 56 | + EditorGUI.IntField(fieldRect, ChildParts.count); |
| 57 | + } |
| 58 | + if (ChildPartsFoldout) |
| 59 | + { |
| 60 | + ChildParts.DoLayoutList(); |
| 61 | + } |
| 62 | + |
| 63 | + { |
| 64 | + var rect = EditorGUILayout.GetControlRect(); |
| 65 | + var foldoutRect = new Rect(rect.x, rect.y, rect.width, rect.height); |
| 66 | + var fieldRect = new Rect(rect.x + rect.width - 50f, rect.y, 50f, rect.height); |
| 67 | + |
| 68 | + ChildPartDrawables.Foldout = EditorGUI.Foldout(foldoutRect, ChildPartDrawables.Foldout, "Child Drawables"); |
| 69 | + EditorGUI.IntField(fieldRect, DrawablesTotalCount); |
| 70 | + } |
| 71 | + if (ChildPartDrawables.Foldout) |
| 72 | + { |
| 73 | + for (var i = 0; i < ChildPartDrawables.Drawables.Length; i++) |
| 74 | + { |
| 75 | + EditorGUILayout.ObjectField(ChildPartDrawables.Drawables[i], typeof(CubismDrawable), true); |
| 76 | + } |
| 77 | + EditorGUI.indentLevel++; |
| 78 | + for (var i = 0; i < ChildPartDrawables.Childs.Length; i++) |
| 79 | + { |
| 80 | + DrawTree(ChildPartDrawables.Childs[i]); |
| 81 | + } |
| 82 | + EditorGUI.indentLevel--; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void DrawTree(InternalTreeInfo item) |
| 87 | + { |
| 88 | + item.Foldout = EditorGUILayout.Foldout(item.Foldout, item.Part.Id); |
| 89 | + if (item.Foldout) |
| 90 | + { |
| 91 | + for (var i = 0; i < item.Drawables.Length; i++) |
| 92 | + { |
| 93 | + EditorGUILayout.ObjectField(item.Drawables[i], typeof(CubismDrawable), true); |
| 94 | + } |
| 95 | + for (var i = 0; i < item.Childs.Length; i++) |
| 96 | + { |
| 97 | + DrawTree(item.Childs[i]); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + #endregion |
| 103 | + |
| 104 | + /// <summary> |
| 105 | + /// Gets whether <see langword="this"/> is initialized. |
| 106 | + /// </summary> |
| 107 | + private bool IsInitialized |
| 108 | + { |
| 109 | + get |
| 110 | + { |
| 111 | + return ChildParts != null && ChildPartDrawables != null; |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Initializes <see langword="this"/>. |
| 117 | + /// </summary> |
| 118 | + private void Initialize() |
| 119 | + { |
| 120 | + var part = target as CubismPart; |
| 121 | + |
| 122 | + var model = part.FindCubismModel(true); |
| 123 | + var parts = model.Parts; |
| 124 | + var drawables = model.Drawables; |
| 125 | + |
| 126 | + _parent = part.UnmanagedParentIndex < 0 ? null : parts[part.UnmanagedParentIndex]; |
| 127 | + |
| 128 | + var childParts = new CubismPart[0]; |
| 129 | + for (var i = 0; i < parts.Length; i++) |
| 130 | + { |
| 131 | + if (parts[i].UnmanagedParentIndex == part.UnmanagedIndex) |
| 132 | + { |
| 133 | + System.Array.Resize(ref childParts, childParts.Length + 1); |
| 134 | + childParts[childParts.Length - 1] = parts[i]; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + ChildParts = new ReorderableList(childParts, typeof(CubismPart), false, false, false, false) |
| 139 | + { |
| 140 | + drawElementCallback = (rect, index, isActive, isFocused) => |
| 141 | + { |
| 142 | + var element = childParts[index]; |
| 143 | + rect.height = EditorGUIUtility.singleLineHeight; |
| 144 | + var labelRect = new Rect(rect.x, rect.y, rect.width * 0.2f, rect.height); |
| 145 | + var objRect = new Rect(rect.x + labelRect.width, rect.y, rect.width * 0.8f, rect.height); |
| 146 | + EditorGUI.LabelField(labelRect, $"{index}"); |
| 147 | + EditorGUI.ObjectField(objRect, childParts[index], typeof(CubismPart), true); |
| 148 | + } |
| 149 | + }; |
| 150 | + |
| 151 | + int count = 0; |
| 152 | + ChildPartDrawables = new InternalTreeInfo(part, drawables, parts, ref count); |
| 153 | + DrawablesTotalCount = count; |
| 154 | + } |
| 155 | + |
| 156 | + private class InternalTreeInfo |
| 157 | + { |
| 158 | + public bool Foldout = true; |
| 159 | + public readonly CubismPart Part; |
| 160 | + public readonly CubismDrawable[] Drawables; |
| 161 | + |
| 162 | + public readonly InternalTreeInfo[] Childs; |
| 163 | + |
| 164 | + |
| 165 | + public InternalTreeInfo(CubismPart part, CubismDrawable[] srcDrawables, CubismPart[] srcParts, ref int drawableCount) |
| 166 | + { |
| 167 | + this.Part = part; |
| 168 | + Drawables = new CubismDrawable[0]; |
| 169 | + for (var i = 0; i < srcDrawables.Length; i++) |
| 170 | + { |
| 171 | + if (srcDrawables[i].UnmanagedParentIndex == part.UnmanagedIndex) |
| 172 | + { |
| 173 | + System.Array.Resize(ref Drawables, Drawables.Length + 1); |
| 174 | + Drawables[Drawables.Length - 1] = srcDrawables[i]; |
| 175 | + drawableCount++; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + Childs = new InternalTreeInfo[0]; |
| 180 | + for (var i = 0; i < srcParts.Length; i++) |
| 181 | + { |
| 182 | + if (srcParts[i].UnmanagedParentIndex == part.UnmanagedIndex) |
| 183 | + { |
| 184 | + System.Array.Resize(ref Childs, Childs.Length + 1); |
| 185 | + Childs[Childs.Length - 1] = new InternalTreeInfo(srcParts[i], srcDrawables, srcParts, ref drawableCount); |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
0 commit comments