aboutsummaryrefslogtreecommitdiff
path: root/Assets/Cinemachine/Base/Editor/Editors/CinemachineMixingCameraEditor.cs
blob: 0f99c95425a716df8e289d36ad1a7da1fc37d4c3 (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
95
96
97
98
99
100
101
102
103
104
using UnityEditor;
using UnityEngine;
using Cinemachine.Utility;
using System.Collections.Generic;

namespace Cinemachine.Editor
{
    [CustomEditor(typeof(CinemachineMixingCamera))]
    internal sealed class CinemachineMixingCameraEditor 
        : CinemachineVirtualCameraBaseEditor<CinemachineMixingCamera>
    {
        protected override List<string> GetExcludedPropertiesInInspector()
        {
            List<string> excluded = base.GetExcludedPropertiesInInspector();
            for (int i = 0; i < CinemachineMixingCamera.MaxCameras; ++i)
                excluded.Add(WeightPropertyName(i));
            return excluded;
        }

        static string WeightPropertyName(int i) { return "m_Weight" + i; }

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

            float totalWeight = 0;
            CinemachineVirtualCameraBase[] children = Target.ChildCameras;
            int numCameras = Mathf.Min(CinemachineMixingCamera.MaxCameras, children.Length);
            for (int i = 0; i < numCameras; ++i)
                if (children[i].isActiveAndEnabled)
                    totalWeight += Target.GetWeight(i);

            if (numCameras == 0)
                EditorGUILayout.HelpBox("There are no Virtual Camera children", MessageType.Warning);
            else 
            {
                EditorGUILayout.Separator();
                EditorGUILayout.LabelField("Child Camera Weights", EditorStyles.boldLabel);
                for (int i = 0; i < numCameras; ++i)
                {
                    SerializedProperty prop = serializedObject.FindProperty(WeightPropertyName(i));
                    if (prop != null)
                        EditorGUILayout.PropertyField(prop, new GUIContent(children[i].Name));
                }
                serializedObject.ApplyModifiedProperties();

                if (totalWeight <= UnityVectorExtensions.Epsilon)
                    EditorGUILayout.HelpBox("No input channels are active", MessageType.Warning);

                if (children.Length > numCameras)
                    EditorGUILayout.HelpBox(
                        "There are " + children.Length 
                        + " child cameras.  A maximum of " + numCameras + " is supported.", 
                        MessageType.Warning);

                // Camera proportion indicator
                EditorGUILayout.Separator();
                EditorGUILayout.LabelField("Mix Result", EditorStyles.boldLabel);
                DrawProportionIndicator(children, numCameras, totalWeight);
            }

            // Extensions
            DrawExtensionsWidgetInInspector();
        }

        void DrawProportionIndicator(
            CinemachineVirtualCameraBase[] children, int numCameras, float totalWeight)
        {
            GUIStyle style = EditorStyles.centeredGreyMiniLabel;
            Color bkg = new Color(0.27f, 0.27f, 0.27f); // ack! no better way than this?
            Color fg = Color.Lerp(CinemachineBrain.GetSoloGUIColor(), bkg, 0.8f);
            float totalHeight = (style.lineHeight + style.margin.vertical) * numCameras;
            Rect r = EditorGUILayout.GetControlRect(true, totalHeight);
            r.height /= numCameras; r.height -= 1;
            float fullWidth = r.width;
            for (int i = 0; i < numCameras; ++i)
            {
                float p = 0;
                string label = children[i].Name;
                if (totalWeight > UnityVectorExtensions.Epsilon)
                {
                    if (children[i].isActiveAndEnabled)
                        p = Target.GetWeight(i) / totalWeight;
                    else
                        label += " (disabled)";
                }
                r.width = fullWidth * p;
                EditorGUI.DrawRect(r, fg);

                Rect r2 = r;
                r2.x += r.width;
                r2.width = fullWidth - r.width;
                EditorGUI.DrawRect(r2, bkg);

                r.width = fullWidth;
                EditorGUI.LabelField(r, label, style);

                r.y += r.height + 1;
            }
        }
    }
}