aboutsummaryrefslogtreecommitdiff
path: root/Assets/Cinemachine/Base/Editor/Editors/CinemachineTargetGroupEditor.cs
blob: a10aa726d2e91c086c6f02f31499867162cf7e22 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using UnityEditor;
using UnityEngine;
using System.Collections.Generic;

namespace Cinemachine.Editor
{
    [CustomEditor(typeof(CinemachineTargetGroup))]
    internal sealed class CinemachineTargetGroupEditor : BaseEditor<CinemachineTargetGroup>
    {
        private UnityEditorInternal.ReorderableList mTargetList;

        protected override List<string> GetExcludedPropertiesInInspector()
        {
            List<string> excluded = base.GetExcludedPropertiesInInspector();
            excluded.Add(FieldPath(x => x.m_Targets));
            return excluded;
        }

        void OnEnable()
        {
            mTargetList = null;
        }

        public override void OnInspectorGUI()
        {
            BeginInspector();
            DrawRemainingPropertiesInInspector();

            if (mTargetList == null)
                SetupTargetList();
            EditorGUI.BeginChangeCheck();
            mTargetList.DoLayoutList();
            if (EditorGUI.EndChangeCheck())
                serializedObject.ApplyModifiedProperties();
        }

        void SetupTargetList()
        {
            float vSpace = 2;
            float floatFieldWidth = EditorGUIUtility.singleLineHeight * 3f;
            float hBigSpace = EditorGUIUtility.singleLineHeight * 2 / 3;

            mTargetList = new UnityEditorInternal.ReorderableList(
                    serializedObject, FindProperty(x => x.m_Targets),
                    true, true, true, true);

            // Needed for accessing field names as strings
            CinemachineTargetGroup.Target def = new CinemachineTargetGroup.Target();

            mTargetList.drawHeaderCallback = (Rect rect) =>
                {
                    rect.width -= EditorGUIUtility.singleLineHeight + 2 * (floatFieldWidth + hBigSpace);
                    Vector2 pos = rect.position; pos.x += EditorGUIUtility.singleLineHeight;
                    rect.position = pos;
                    EditorGUI.LabelField(rect, "Target");

                    pos.x += rect.width + hBigSpace; rect.width = floatFieldWidth; rect.position = pos;
                    EditorGUI.LabelField(rect, "Weight");

                    pos.x += rect.width + hBigSpace; rect.position = pos;
                    EditorGUI.LabelField(rect, "Radius");
                };

            mTargetList.drawElementCallback
                = (Rect rect, int index, bool isActive, bool isFocused) =>
                {
                    SerializedProperty elemProp = mTargetList.serializedProperty.GetArrayElementAtIndex(index);

                    rect.y += vSpace;
                    rect.height = EditorGUIUtility.singleLineHeight;
                    Vector2 pos = rect.position;
                    //rect.width -= hSpace + 2 * EditorGUIUtility.singleLineHeight;
                    rect.width -= 2 * (floatFieldWidth + hBigSpace);
                    EditorGUI.PropertyField(rect, elemProp.FindPropertyRelative(() => def.target), GUIContent.none);

                    float oldWidth = EditorGUIUtility.labelWidth;
                    EditorGUIUtility.labelWidth = EditorGUIUtility.singleLineHeight; 
                    pos.x += rect.width; rect.width = floatFieldWidth + hBigSpace; rect.position = pos;
                    EditorGUI.PropertyField(rect, elemProp.FindPropertyRelative(() => def.weight), new GUIContent(" "));
                    pos.x += rect.width; rect.position = pos;
                    EditorGUI.PropertyField(rect, elemProp.FindPropertyRelative(() => def.radius), new GUIContent(" "));
                    EditorGUIUtility.labelWidth = oldWidth; 
                };

            mTargetList.onAddCallback = (UnityEditorInternal.ReorderableList l) =>
                {
                    var index = l.serializedProperty.arraySize;
                    ++l.serializedProperty.arraySize;
                    SerializedProperty elemProp = mTargetList.serializedProperty.GetArrayElementAtIndex(index);
                    elemProp.FindPropertyRelative(() => def.weight).floatValue = 1;
                };
        }
    }
}