mirror of
https://github.com/bdunderscore/modular-avatar.git
synced 2025-03-10 07:44:57 +08:00
chore: extract column logic for future inspectors
This commit is contained in:
parent
d8cab10a3d
commit
1175e43bfd
@ -0,0 +1,130 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEditor;
|
||||
using UnityEditorInternal;
|
||||
using UnityEngine;
|
||||
using static nadena.dev.modular_avatar.core.editor.Localization;
|
||||
|
||||
namespace nadena.dev.modular_avatar.core.editor
|
||||
{
|
||||
public class ColumnReorderableList : ReorderableList
|
||||
{
|
||||
public ColumnReorderableList(IList elements, Type elementType) : base(elements, elementType)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public ColumnReorderableList(IList elements, Type elementType, bool draggable, bool displayHeader,
|
||||
bool displayAddButton, bool displayRemoveButton) : base(elements, elementType, draggable, displayHeader,
|
||||
displayAddButton, displayRemoveButton)
|
||||
{
|
||||
throw new NotSupportedException();
|
||||
}
|
||||
|
||||
public ColumnReorderableList(SerializedObject serializedObject, SerializedProperty elements) : base(
|
||||
serializedObject, elements)
|
||||
{
|
||||
Init();
|
||||
_elements = elements;
|
||||
}
|
||||
|
||||
public ColumnReorderableList(SerializedObject serializedObject, SerializedProperty elements, bool draggable,
|
||||
bool displayHeader, bool displayAddButton, bool displayRemoveButton) : base(serializedObject, elements,
|
||||
draggable, displayHeader, displayAddButton, displayRemoveButton)
|
||||
{
|
||||
Init();
|
||||
_elements = elements;
|
||||
}
|
||||
|
||||
|
||||
private struct Column
|
||||
{
|
||||
public string headerKey;
|
||||
public Action<Rect, SerializedProperty> drawField;
|
||||
public float offsetX, width;
|
||||
}
|
||||
|
||||
private SerializedProperty _elements;
|
||||
private float _elementWidth, _remainingWidth;
|
||||
private readonly List<Column> _columns = new List<Column>();
|
||||
private float _columnPos;
|
||||
public float margin = 4;
|
||||
|
||||
public Action<float> OnGenerateColumns = (_) => { };
|
||||
public Action Repaint;
|
||||
|
||||
public float AddColumn(float width, string headerKey, Action<Rect, SerializedProperty> drawField)
|
||||
{
|
||||
_columns.Add(new Column
|
||||
{
|
||||
headerKey = headerKey,
|
||||
drawField = drawField,
|
||||
offsetX = _columnPos,
|
||||
width = width
|
||||
});
|
||||
|
||||
width += margin;
|
||||
|
||||
_columnPos += width;
|
||||
_remainingWidth -= width;
|
||||
|
||||
return _remainingWidth;
|
||||
}
|
||||
|
||||
void Init()
|
||||
{
|
||||
drawHeaderCallback = DrawListHeader;
|
||||
drawElementCallback = DrawElement;
|
||||
}
|
||||
|
||||
private void DrawElement(Rect rect, int i, bool isactive, bool isfocused)
|
||||
{
|
||||
if (Mathf.Abs(_elementWidth - rect.width) > 0.01f && rect.width > 1)
|
||||
{
|
||||
_elementWidth = rect.width;
|
||||
_columns.Clear();
|
||||
Repaint();
|
||||
}
|
||||
|
||||
var element = _elements.GetArrayElementAtIndex(i);
|
||||
|
||||
using (new ZeroIndentScope())
|
||||
{
|
||||
foreach (var column in _columns)
|
||||
{
|
||||
var elemRect = rect;
|
||||
elemRect.xMin += column.offsetX;
|
||||
elemRect.width = column.width;
|
||||
|
||||
column.drawField(elemRect, element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DrawListHeader(Rect rect)
|
||||
{
|
||||
if (_elementWidth > 1 && _elementWidth < rect.width)
|
||||
{
|
||||
rect.x += rect.width - _elementWidth;
|
||||
rect.width = _elementWidth;
|
||||
}
|
||||
|
||||
if (_columns.Count == 0)
|
||||
{
|
||||
_columnPos = 0;
|
||||
_remainingWidth = _elementWidth;
|
||||
OnGenerateColumns(_elementWidth);
|
||||
}
|
||||
|
||||
foreach (var column in _columns)
|
||||
{
|
||||
var elemRect = rect;
|
||||
elemRect.xMin += column.offsetX;
|
||||
elemRect.width = column.width;
|
||||
|
||||
EditorGUI.LabelField(elemRect, G(column.headerKey));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: cd909bad61d942a992e73100b927a102
|
||||
timeCreated: 1683271567
|
@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BestHTTP;
|
||||
using UnityEditor;
|
||||
using UnityEditorInternal;
|
||||
using UnityEngine;
|
||||
@ -12,13 +13,13 @@ namespace nadena.dev.modular_avatar.core.editor
|
||||
[CustomEditor(typeof(ActionToggleObject))]
|
||||
internal class ActionToggleObjectInspector : MAEditorBase
|
||||
{
|
||||
private ReorderableList _list;
|
||||
private ColumnReorderableList _list;
|
||||
private SerializedProperty _listProp;
|
||||
|
||||
private void OnEnable()
|
||||
{
|
||||
_listProp = serializedObject.FindProperty(nameof(ActionToggleObject.Objects));
|
||||
_list = new ReorderableList(
|
||||
_list = new ColumnReorderableList(
|
||||
serializedObject,
|
||||
_listProp,
|
||||
true,
|
||||
@ -27,8 +28,8 @@ namespace nadena.dev.modular_avatar.core.editor
|
||||
true
|
||||
);
|
||||
|
||||
_list.drawHeaderCallback += DrawListHeader;
|
||||
_list.drawElementCallback += DrawElement;
|
||||
_list.Repaint = Repaint;
|
||||
_list.OnGenerateColumns = BuildColumns;
|
||||
_list.onRemoveCallback += OnRemoveElement;
|
||||
_list.onAddCallback += OnAddElement;
|
||||
|
||||
@ -48,86 +49,42 @@ namespace nadena.dev.modular_avatar.core.editor
|
||||
}
|
||||
}
|
||||
|
||||
private float elementWidth;
|
||||
private GUIContent _rectHeader, _objectHeader;
|
||||
private Rect _checkRect, _objectRect;
|
||||
|
||||
private void DrawListHeader(Rect rect)
|
||||
private void BuildColumns(float width)
|
||||
{
|
||||
if (elementWidth > 1 && elementWidth < rect.width)
|
||||
{
|
||||
rect.x += rect.width - elementWidth;
|
||||
rect.width = elementWidth;
|
||||
}
|
||||
|
||||
var margin = 4;
|
||||
|
||||
var t = EditorStyles.toggle;
|
||||
var toggleSize = t.CalcSize(GUIContent.none);
|
||||
_checkRect = new Rect(0, 0, toggleSize.x, toggleSize.y);
|
||||
|
||||
_rectHeader = G("action.toggle_object.header.show");
|
||||
_objectHeader = G("action.toggle_object.header.object");
|
||||
var checkHeaderContent = G("action.toggle_object.header.show");
|
||||
var rectHeaderSize = EditorStyles.label.CalcSize(checkHeaderContent);
|
||||
|
||||
var checkLabelRect = _checkRect;
|
||||
width = _list.AddColumn(rectHeaderSize.x, "action.toggle_object.header.show", (rect, prop) =>
|
||||
{
|
||||
rect = CenterElement(rect, toggleSize);
|
||||
|
||||
var rectHeaderSize = EditorStyles.label.CalcSize(_rectHeader);
|
||||
checkLabelRect.width = Mathf.Max(checkLabelRect.width, rectHeaderSize.x);
|
||||
_checkRect.x += (checkLabelRect.width - _checkRect.width) / 2;
|
||||
_checkRect.width = checkLabelRect.width;
|
||||
var activeProp = prop.FindPropertyRelative(nameof(ActionToggleObject.ObjectEntry.Active));
|
||||
EditorGUI.PropertyField(rect, activeProp, GUIContent.none);
|
||||
});
|
||||
|
||||
_checkRect.y += (rect.height - _checkRect.height);
|
||||
|
||||
_objectRect = new Rect(checkLabelRect.xMax + margin, 0, rect.width - checkLabelRect.width - margin,
|
||||
_list.elementHeight);
|
||||
|
||||
//EditorGUI.LabelField(rect, G("action.toggle_object.header"));
|
||||
var headerCheckRect = checkLabelRect;
|
||||
headerCheckRect.x += rect.x;
|
||||
headerCheckRect.height = rectHeaderSize.y;
|
||||
// why does *2 work well here???
|
||||
headerCheckRect.y += (rect.height - headerCheckRect.height) * 2;
|
||||
|
||||
var headerObjectRect = _objectRect;
|
||||
headerObjectRect.x += rect.x;
|
||||
headerObjectRect.height = rectHeaderSize.y;
|
||||
headerObjectRect.y += (rect.height - headerObjectRect.height) * 2;
|
||||
|
||||
EditorGUI.LabelField(headerCheckRect, _rectHeader);
|
||||
EditorGUI.LabelField(headerObjectRect, _objectHeader);
|
||||
_list.AddColumn(width, "action.toggle_object.header.object", ((rect, elem) =>
|
||||
{
|
||||
var targetProp = elem.FindPropertyRelative(nameof(ActionToggleObject.ObjectEntry.target));
|
||||
EditorGUI.PropertyField(rect, targetProp, GUIContent.none);
|
||||
}));
|
||||
}
|
||||
|
||||
private void DrawElement(Rect rect, int index, bool isactive, bool isfocused)
|
||||
private static Rect CenterElement(Rect rect, Vector2 toggleSize)
|
||||
{
|
||||
if (Mathf.Abs(elementWidth - rect.width) > 0.01f && rect.width > 1)
|
||||
{
|
||||
elementWidth = rect.width;
|
||||
Repaint();
|
||||
}
|
||||
float adjust = rect.height - toggleSize.y;
|
||||
rect.yMin += adjust / 2;
|
||||
rect.yMax -= adjust / 2;
|
||||
|
||||
var element = _listProp.GetArrayElementAtIndex(index);
|
||||
var activeProp = element.FindPropertyRelative(nameof(ActionToggleObject.ObjectEntry.Active));
|
||||
var targetProp = element.FindPropertyRelative(nameof(ActionToggleObject.ObjectEntry.target));
|
||||
adjust = rect.width - toggleSize.x;
|
||||
rect.xMin += adjust / 2;
|
||||
rect.xMax -= adjust / 2;
|
||||
|
||||
var checkRect = _checkRect;
|
||||
checkRect.x += rect.x;
|
||||
checkRect.y += rect.y;
|
||||
|
||||
var objectRect = _objectRect;
|
||||
objectRect.x += rect.x;
|
||||
objectRect.y += rect.y;
|
||||
objectRect.xMax = rect.xMax;
|
||||
objectRect.yMin += 1;
|
||||
objectRect.yMax -= 1;
|
||||
|
||||
using (new ZeroIndentScope())
|
||||
{
|
||||
EditorGUI.PropertyField(checkRect, activeProp, GUIContent.none);
|
||||
EditorGUI.PropertyField(objectRect, targetProp, GUIContent.none);
|
||||
}
|
||||
return rect;
|
||||
}
|
||||
|
||||
|
||||
protected override void OnInnerInspectorGUI()
|
||||
{
|
||||
serializedObject.Update();
|
||||
|
@ -69,6 +69,7 @@ GameObject:
|
||||
m_Component:
|
||||
- component: {fileID: 4243820047656436306}
|
||||
- component: {fileID: 4243820047656436307}
|
||||
- component: {fileID: 4919827624912715597}
|
||||
m_Layer: 0
|
||||
m_Name: ClothingMenu
|
||||
m_TagString: Untagged
|
||||
@ -105,6 +106,20 @@ MonoBehaviour:
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
targetObject: {fileID: 4243820047656436300}
|
||||
--- !u!114 &4919827624912715597
|
||||
MonoBehaviour:
|
||||
m_ObjectHideFlags: 0
|
||||
m_CorrespondingSourceObject: {fileID: 0}
|
||||
m_PrefabInstance: {fileID: 0}
|
||||
m_PrefabAsset: {fileID: 0}
|
||||
m_GameObject: {fileID: 4243820047656436300}
|
||||
m_Enabled: 1
|
||||
m_EditorHideFlags: 0
|
||||
m_Script: {fileID: 11500000, guid: 7ef83cb0c23d4d7c9d41021e544a1978, type: 3}
|
||||
m_Name:
|
||||
m_EditorClassIdentifier:
|
||||
menuToAppend: {fileID: 0}
|
||||
installTargetMenu: {fileID: 0}
|
||||
--- !u!1001 &1450650863557829439
|
||||
PrefabInstance:
|
||||
m_ObjectHideFlags: 0
|
||||
|
Loading…
Reference in New Issue
Block a user