summaryrefslogtreecommitdiff
path: root/Assets/Plugins/Editor/AdvancedInspector/FieldEditors/LayerMaskEditor.cs
blob: a007f1b62f7c76dfb9050a24e358560d367a2769 (plain)
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 UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;

namespace AdvancedInspector
{
    public class LayerMaskEditor : FieldEditor
    {
        private static List<string> names;
        private static List<int> masks;

        public override Type[] EditedTypes
        {
            get { return new Type[] { typeof(LayerMask) }; }
        }

        public override void Draw(InspectorField field, GUIStyle style)
        {
            names = new List<string>();
            masks = new List<int>();

            for (int i = 0; i < 32; i++)
            {
                string name = LayerMask.LayerToName(i);

                if (!string.IsNullOrEmpty(name))
                {
                    names.Add(name);
                    masks.Add(1 << i);
                }
            }

            object value = field.GetValue();

            bool isMask = value is LayerMask;

            int mask;
            if (isMask)
                mask = (int)(LayerMask)value;
            else
                mask = (int)value;

            int result;
            if (DrawLayerMaskField(mask, style, out result))
            {
                if (isMask)
                    field.SetValue((LayerMask)result);
                else
                    field.SetValue(result);
            }
        }

        public static bool DrawLayerMaskField(int aMask, GUIStyle style, out int result)
        {
            int val = aMask;
            int maskVal = 0;
            for (int i = 0; i < names.Count; i++)
            {
                if (masks[i] != 0)
                {
                    if ((val & masks[i]) == masks[i])
                        maskVal |= 1 << i;
                }
                else if (val == 0)
                    maskVal |= 1 << i;
            }

            EditorGUI.BeginChangeCheck();

            int newMaskVal;
            if (style != null)
                newMaskVal = EditorGUILayout.MaskField(maskVal, names.ToArray(), style);
            else
                newMaskVal = EditorGUILayout.MaskField(maskVal, names.ToArray());

            int changes = maskVal ^ newMaskVal;

            for (int i = 0; i < masks.Count; i++)
            {
                if ((changes & (1 << i)) != 0)
                {
                    if ((newMaskVal & (1 << i)) != 0)
                    {
                        if (masks[i] == 0)
                        {
                            val = 0;
                            break;
                        }
                        else
                            val |= masks[i];
                    }
                    else
                        val &= ~masks[i];
                }
            }

            result = val;

            return EditorGUI.EndChangeCheck();
        }
    }
}