summaryrefslogtreecommitdiff
path: root/Assets/Plugins/Editor/AdvancedInspector/FieldEditors/RectEditor.cs
blob: ee3e61e4bfafe24139e1d3540b8ebfc6daf12a1e (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;

namespace AdvancedInspector
{
    public class RectEditor : FieldEditor
    {
        public override bool Expandable
        {
            get { return false; }
        }

        public override Type[] EditedTypes
        {
            get { return new Type[] { typeof(Rect), typeof(RectOffset) }; }
        }

        public override void Draw(InspectorField field, GUIStyle style)
        {
            Type type = field.BaseType;

            float labelWidth = EditorGUIUtility.labelWidth;
            EditorGUIUtility.labelWidth = VECTOR_FIELD_WIDTH;

            if (type == typeof(Rect))
            {
                Rect[] values = field.GetValues<Rect>();

                float[] x = new float[values.Length];
                float[] y = new float[values.Length];
                float[] height = new float[values.Length];
                float[] width = new float[values.Length];

                for (int i = 0; i < values.Length; i++)
                {
                    x[i] = values[i].x;
                    y[i] = values[i].y;
                    height[i] = values[i].height;
                    width[i] = values[i].width;
                }

                GUILayout.BeginHorizontal();
                float result;
                if (FloatEditor.DrawFloat("X", x, style, out result))
                {
                    Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " X");

                    for (int i = 0; i < field.Instances.Length; i++)
                    {
                        values[i].x = result;
                        field.SetValue(field.Instances[i], values[i]);
                    }
                }

                if (FloatEditor.DrawFloat("Y", y, style, out result))
                {
                    Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " Y");

                    for (int i = 0; i < field.Instances.Length; i++)
                    {
                        values[i].y = result;
                        field.SetValue(field.Instances[i], values[i]);
                    }
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                if (FloatEditor.DrawFloat("W", width, style, out result))
                {
                    Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " Width");

                    for (int i = 0; i < field.Instances.Length; i++)
                    {
                        values[i].width = result;
                        field.SetValue(field.Instances[i], values[i]);
                    }
                }

                if (FloatEditor.DrawFloat("H", height, style, out result))
                {
                    Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " Height");

                    for (int i = 0; i < field.Instances.Length; i++)
                    {
                        values[i].height = result;
                        field.SetValue(field.Instances[i], values[i]);
                    }
                }
                GUILayout.EndHorizontal();
            }
            else if (type == typeof(RectOffset))
            {
                RectOffset[] values = field.GetValues<RectOffset>();

                int[] left = new int[values.Length];
                int[] right = new int[values.Length];
                int[] top = new int[values.Length];
                int[] bottom = new int[values.Length];

                for (int i = 0; i < values.Length; i++)
                {
                    left[i] = values[i].left;
                    right[i] = values[i].right;
                    top[i] = values[i].top;
                    bottom[i] = values[i].bottom;
                }

                GUILayout.BeginHorizontal();
                int result;
                if (IntegerEditor.DrawInt("L", left, style, out result))
                {
                    Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " Left");

                    for (int i = 0; i < field.Instances.Length; i++)
                    {
                        values[i].left = result;
                        field.SetValue(field.Instances[i], values[i]);
                    }
                }

                if (IntegerEditor.DrawInt("R", right, style, out result))
                {
                    Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " Right");

                    for (int i = 0; i < field.Instances.Length; i++)
                    {
                        values[i].right = result;
                        field.SetValue(field.Instances[i], values[i]);
                    }
                }
                GUILayout.EndHorizontal();

                GUILayout.BeginHorizontal();
                if (IntegerEditor.DrawInt("T", top, style, out result))
                {
                    Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " Top");

                    for (int i = 0; i < field.Instances.Length; i++)
                    {
                        values[i].top = result;
                        field.SetValue(field.Instances[i], values[i]);
                    }
                }

                if (IntegerEditor.DrawInt("B", bottom, style, out result))
                {
                    Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " Bottom");

                    for (int i = 0; i < field.Instances.Length; i++)
                    {
                        values[i].bottom = result;
                        field.SetValue(field.Instances[i], values[i]);
                    }
                }
                GUILayout.EndHorizontal();
            }

            EditorGUILayout.Space();

            EditorGUIUtility.labelWidth = labelWidth;
        }
    }
}