Skip to content

Commit

Permalink
Merge pull request #95 from anatawa12/remove-prefab-safe-list
Browse files Browse the repository at this point in the history
Remove prefab safe list
  • Loading branch information
anatawa12 authored Apr 17, 2023
2 parents 7d3edde + ac01c06 commit f59b1c0
Show file tree
Hide file tree
Showing 41 changed files with 162 additions and 1,536 deletions.
1 change: 1 addition & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog].
### Added

### Changed
- **BREAKING** Removed Prefab Safe List `#95`

### Deprecated

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ The format is based on [Keep a Changelog].
### Added

### Changed
- **BREAKING** Removed Prefab Safe List `#95`

### Deprecated

Expand Down
116 changes: 84 additions & 32 deletions Editor/Migration/Migration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,23 @@ private static bool MigrateComponent(AvatarTagComponent component, int forceVers
{
#pragma warning disable CS0618
case 1:
if (serialized.targetObject.GetType() == typeof(RemoveMeshInBox))
{
// format for RemoveMeshInBox is reverted to v1 in v3 so skip.
versionProp.intValue = 3;
modified = true;
goto case 3;
}
MigrateV1ToV2(nestCount, serialized);
versionProp.intValue = 2;
modified = true;
goto case 2;
case 2:
MigrateV2ToV3(nestCount, serialized);
versionProp.intValue = 2;
modified = true;
goto case 3;
case 3:
#pragma warning restore CS0618
// it's current version: save
serialized.ApplyModifiedProperties();
Expand Down Expand Up @@ -362,9 +374,78 @@ private static void MigrateV1ToV2(int nestCount, SerializedObject serialized)
case 4:
{
// RemoveMeshInBox
MigrateList(serialized.FindProperty(nameof(RemoveMeshInBox.boxes)),
serialized.FindProperty(nameof(RemoveMeshInBox.boxList)),
nestCount);
throw new Exception("V1 to V2 migration for RemoveMeshInBox has been removed");
}
}
}

private static readonly TypeId MigrateV2ToV3Types = new TypeId(new []
{
typeof(RemoveMeshInBox),
});

[Obsolete("migration process")]
private static void MigrateV2ToV3(int nestCount, SerializedObject serialized)
{
switch (MigrateV2ToV3Types.GetType(serialized.targetObject.GetType()))
{
case 1:
{
// RemoveMeshInBox
SerializedProperty[] GetContainers()
{
var psList = serialized.FindProperty(nameof(RemoveMeshInBox.boxList));
// Container[]
var firstLayer =
psList.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBoxList.firstLayer));
// Layer[]
var prefabLayers =
psList.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBoxList.prefabLayers));
// Container[][]
var layerArrays = new SerializedProperty[prefabLayers.arraySize + 1];

layerArrays[0] = firstLayer;

for (var i = 0; i < prefabLayers.arraySize; ++i)
layerArrays[i + 1] = prefabLayers.GetArrayElementAtIndex(i)
.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBoxList.Layer.elements));

var count = layerArrays.Sum(x => x.arraySize);
var result = new SerializedProperty[count];

var k = 0;
foreach (var ary in layerArrays)
for (var j = 0; j < ary.arraySize; ++j)
result[k++] = ary.GetArrayElementAtIndex(j);

return result;
}

var containers = GetContainers();
var array = serialized.FindProperty(nameof(RemoveMeshInBox.boxes));
if (array.arraySize != containers.Length)
array.arraySize = containers.Length;

for (var i = 0; i < containers.Length; i++)
{
var container = containers[i];
var removed =
container.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBoxList.Container.removed));
var value = container.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBoxList.Container
.value));
var field = array.GetArrayElementAtIndex(i);

field.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.size)).vector3Value =
removed.boolValue
? Vector3.zero
: value.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.size)).vector3Value;

field.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.center)).vector3Value =
field.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.center)).vector3Value;

field.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.rotation)).quaternionValue =
field.FindPropertyRelative(nameof(RemoveMeshInBox.BoundingBox.rotation)).quaternionValue;
}
break;
}
}
Expand Down Expand Up @@ -410,35 +491,6 @@ private static void MigrateSet<T>(
Assert.IsTrue(valuesSet.SetEquals(renderersSet.Values));
}

private static void MigrateList(SerializedProperty arrayProperty, SerializedProperty listProperty,
int nestCount)
{
var list = PrefabSafeList.EditorUtil.Create(listProperty, nestCount);
int index = 0;
foreach (var element in list.Elements)
{
if (!element.Contains) continue;
if (index == arrayProperty.arraySize)
element.RemovedProperty.boolValue = true;
else
{
CopySerializedPropertyValue.Copy(
arrayProperty.GetArrayElementAtIndex(index),
element.ValueProperty);
index++;
}
}

while (index < arrayProperty.arraySize)
{
var element = list.AddElement();
CopySerializedPropertyValue.Copy(
arrayProperty.GetArrayElementAtIndex(index),
element.ValueProperty);
index++;
}
}

private readonly struct TypeId
{
[CanBeNull] private readonly Dictionary<Type, int> _map;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public override void Process(OptimizerSession session, MeshInfo2 target, MeshInf
foreach (var vertex in target.Vertices)
{
var actualPosition = vertex.ComputeActualPosition(target, Target.transform.worldToLocalMatrix);
if (Component.boxList.GetAsList().Any(x => x.ContainsVertex(actualPosition)))
if (Component.boxes.Any(x => x.ContainsVertex(actualPosition)))
inBoxVertices.Add(vertex);
}

Expand Down
120 changes: 59 additions & 61 deletions Editor/RemoveMeshInBoxEditor.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using Anatawa12.AvatarOptimizer.PrefabSafeList;
using JetBrains.Annotations;
using UnityEditor;
using UnityEngine;

Expand All @@ -8,34 +9,48 @@ namespace Anatawa12.AvatarOptimizer
[CustomEditor(typeof(RemoveMeshInBox))]
internal class RemoveMeshInBoxEditor : AvatarTagComponentEditorBase
{
private ListEditor _boxList;
private SerializedProperty _boxes;
private string _editingBoxPropPath;
private readonly Dictionary<string, (Quaternion value, Vector3 euler)> _eulerAngles =
new Dictionary<string, (Quaternion value, Vector3 euler)>();

private void OnEnable()
{
var nestCount = PrefabSafeListUtil.PrefabNestCount(serializedObject.targetObject);
_boxList = new ListEditor(serializedObject.FindProperty("boxList"), nestCount);
_boxes = serializedObject.FindProperty(nameof(RemoveMeshInBox.boxes));
}

protected override void OnInspectorGUIInner()
{
var rect = EditorGUILayout.GetControlRect(true, _boxList.GetPropertyHeight());
_boxList.OnGUI(rect);
// size prop
_boxes.isExpanded = true;
using (new BoundingBoxEditor.EditorScope(this))
EditorGUILayout.PropertyField(_boxes);

serializedObject.ApplyModifiedProperties();
}

private class ListEditor : EditorBase
[CustomPropertyDrawer(typeof(RemoveMeshInBox.BoundingBox))]
class BoundingBoxEditor : PropertyDrawer
{
public string EditingBoxPropPath;
[CanBeNull] private static RemoveMeshInBoxEditor _upstreamEditor;

private readonly Dictionary<string, (Quaternion value, Vector3 euler)> _eulerAngle =
new Dictionary<string, (Quaternion value, Vector3 euler)>();

public ListEditor(SerializedProperty property, int nestCount) : base(property, nestCount)
public readonly struct EditorScope : IDisposable
{
private readonly RemoveMeshInBoxEditor _oldEditor;

public EditorScope(RemoveMeshInBoxEditor editor)
{
_oldEditor = _upstreamEditor;
_upstreamEditor = editor;
}

public void Dispose()
{
_upstreamEditor = _oldEditor;
}
}

protected override float FieldHeight(SerializedProperty serializedProperty)
public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
{
return EditorGUIUtility.singleLineHeight // header
+ EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight // center
Expand All @@ -45,52 +60,50 @@ protected override float FieldHeight(SerializedProperty serializedProperty)
;
}

protected override bool Field(Rect position, GUIContent label, SerializedProperty boxProp)
public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
{
var removeElement = false;

position.height = EditorGUIUtility.singleLineHeight;
var prevWidth = position.width;
var prevX = position.x;
position.width -= EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight;
EditorGUI.LabelField(position, label);
position.x += position.width + EditorGUIUtility.standardVerticalSpacing;
position.width = EditorGUIUtility.singleLineHeight;

if (GUI.Button(position, EditorStatics.RemoveButton))
removeElement = true;

position.width = prevWidth;
position.x = prevX;

position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight;

var centerProp = boxProp.FindPropertyRelative("center");
var sizeProp = boxProp.FindPropertyRelative("size");
var rotationProp = boxProp.FindPropertyRelative("rotation");
var centerProp = property.FindPropertyRelative("center");
var sizeProp = property.FindPropertyRelative("size");
var rotationProp = property.FindPropertyRelative("rotation");

using (new EditorGUI.IndentLevelScope())
{
using (new GUILayout.HorizontalScope())
{
var editingCurrent = EditingBoxPropPath == boxProp.propertyPath;
if (GUI.Button(position, editingCurrent ? "Finish Editing Box" : "Edit This Box"))
if (_upstreamEditor)
{
var editingCurrent = _upstreamEditor._editingBoxPropPath == property.propertyPath;
if (GUI.Button(position, editingCurrent ? "Finish Editing Box" : "Edit This Box"))
{
_upstreamEditor._editingBoxPropPath = editingCurrent ? null : property.propertyPath;
SceneView.RepaintAll(); // to show/hide gizmo
}
}
else
{
EditingBoxPropPath = editingCurrent ? null : boxProp.propertyPath;
SceneView.RepaintAll(); // to show/hide gizmo
using (new EditorGUI.DisabledScope(true))
GUI.Button(position, "Cannot edit in this context");
}

position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight;
}

EditorGUI.PropertyField(position, centerProp);
position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight;
EditorGUI.PropertyField(position, sizeProp);
position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight;
if (!_eulerAngle.TryGetValue(boxProp.propertyPath, out var eulerCache)
|| eulerCache.value != rotationProp.quaternionValue)

if (!_upstreamEditor ||
!_upstreamEditor._eulerAngles.TryGetValue(property.propertyPath, out var eulerCache) ||
eulerCache.value != rotationProp.quaternionValue)
{
_eulerAngle[boxProp.propertyPath] = eulerCache = (rotationProp.quaternionValue,
rotationProp.quaternionValue.eulerAngles);
eulerCache = (value: rotationProp.quaternionValue,
euler: rotationProp.quaternionValue.eulerAngles);
}

// rotation in euler
Expand All @@ -102,37 +115,22 @@ protected override bool Field(Rect position, GUIContent label, SerializedPropert
{
var quot = Quaternion.Euler(euler);
rotationProp.quaternionValue = quot;
_eulerAngle[boxProp.propertyPath] = (quot, euler);
eulerCache = (quot, euler);
}

if (_upstreamEditor)
_upstreamEditor._eulerAngles[property.propertyPath] = eulerCache;

EditorGUI.EndProperty();
position.y += EditorGUIUtility.standardVerticalSpacing + EditorGUIUtility.singleLineHeight;
}

return removeElement;
}

protected override void InitializeNewElement(IElement element)
{
var box = element.ValueProperty;
box.FindPropertyRelative("center").vector3Value = Vector3.zero;
box.FindPropertyRelative("size").vector3Value = Vector3.one;
box.FindPropertyRelative("rotation").quaternionValue = Quaternion.identity;
EditingBoxPropPath = element.ValueProperty.propertyPath;
}

static class EditorStatics
{
public static readonly GUIContent RemoveButton = new GUIContent("x")
{
tooltip = "Remove Element from the list."
};
}
}

private void OnSceneGUI()
{
if (_boxList.EditingBoxPropPath == null) return;
var box = serializedObject.FindProperty(_boxList.EditingBoxPropPath);
if (_editingBoxPropPath == null) return;
var box = serializedObject.FindProperty(_editingBoxPropPath);
if (box == null) return;

var centerProp = box.FindPropertyRelative("center");
Expand Down Expand Up @@ -195,7 +193,7 @@ public static void DrawGizmoActive(RemoveMeshInBox script, GizmoType gizmoType)
Handles.matrix = script.transform.localToWorldMatrix;
Handles.color = Color.red;

foreach (var boundingBox in script.boxList.GetAsList())
foreach (var boundingBox in script.boxes)
{
var halfSize = boundingBox.size / 2;
var x = boundingBox.rotation * new Vector3(halfSize.x, 0, 0);
Expand Down
4 changes: 1 addition & 3 deletions Editor/com.anatawa12.avatar-optimizer.editor.asmdef
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"GUID:f69eeb3e25674f4a9bd20e6d7e69e0e6",
"GUID:2633ab9fa94544a69517fc9a1bc143c9",
"GUID:b9880ca0b6584453a2627bd3c038759f",
"GUID:8542dbf824204440a818dbc2377cb4d6",
"GUID:e3dc35e4a606c438485f5fb532cb875d",
"GUID:c85616d79bd2a41109c251ab53d14c20"
"GUID:8542dbf824204440a818dbc2377cb4d6"
],
"includePlatforms": [
"Editor"
Expand Down
8 changes: 0 additions & 8 deletions Internal/PrefabSafeList.meta

This file was deleted.

3 changes: 0 additions & 3 deletions Internal/PrefabSafeList/Editor.meta

This file was deleted.

Loading

0 comments on commit f59b1c0

Please sign in to comment.