summaryrefslogtreecommitdiff
path: root/UnityCollection/Assets/Tools/EditorGUIHelper/MinMaxSliderAttribute/Editor/MinMaxSliderDrawer.cs
blob: b5fc57c1a1f4f44155e96144d8ae329a836c6584 (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
using UnityEngine;
using UnityEditor;

[CustomPropertyDrawer(typeof(MinMaxSliderAttribute))]
public class MinMaxSliderDrawer : PropertyDrawer
{
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        var minMaxAttribute = (MinMaxSliderAttribute)attribute;
        var propertyType = property.propertyType;

        label.tooltip = minMaxAttribute.min.ToString("F2") + " to " + minMaxAttribute.max.ToString("F2");

        Rect controlRect = EditorGUI.PrefixLabel(position, label);

        Rect[] splittedRect = SplitRect(controlRect, 3);

        if (propertyType == SerializedPropertyType.Vector2)
        {

            EditorGUI.BeginChangeCheck();

            Vector2 vector = property.vector2Value;
            float minVal = vector.x;
            float maxVal = vector.y;

            minVal = float.Parse(GUI.TextField(splittedRect[0], minVal.ToString("F2")));
            maxVal = float.Parse(GUI.TextField(splittedRect[2], maxVal.ToString("F2")));

            Rect sliderRect = new Rect(splittedRect[0].x + 10, splittedRect[0].y, splittedRect[2].x - splittedRect[0].x - 15, splittedRect[0].height);

            EditorGUI.MinMaxSlider(sliderRect, ref minVal, ref maxVal,
            minMaxAttribute.min, minMaxAttribute.max);

            if (minVal < minMaxAttribute.min)
            {
                minVal = minMaxAttribute.min;
            }

            if (maxVal > minMaxAttribute.max)
            {
                maxVal = minMaxAttribute.max;
            }

            vector = new Vector2(minVal > maxVal ? maxVal : minVal, maxVal);

            if (EditorGUI.EndChangeCheck())
            {
                property.vector2Value = vector;
            }

        }
        else if (propertyType == SerializedPropertyType.Vector2Int)
        {

            EditorGUI.BeginChangeCheck();

            Vector2Int vector = property.vector2IntValue;
            float minVal = vector.x;
            float maxVal = vector.y;

            minVal = EditorGUI.FloatField(splittedRect[0], minVal);
            maxVal = EditorGUI.FloatField(splittedRect[2], maxVal);

            EditorGUI.MinMaxSlider(splittedRect[1], ref minVal, ref maxVal,
            minMaxAttribute.min, minMaxAttribute.max);

            if (minVal < minMaxAttribute.min)
            {
                maxVal = minMaxAttribute.min;
            }

            if (minVal > minMaxAttribute.max)
            {
                maxVal = minMaxAttribute.max;
            }

            vector = new Vector2Int(Mathf.FloorToInt(minVal > maxVal ? maxVal : minVal), Mathf.FloorToInt(maxVal));

            if (EditorGUI.EndChangeCheck())
            {
                property.vector2IntValue = vector;
            }

        }

    }

    Rect[] SplitRect(Rect rectToSplit, int n)
    {


        Rect[] rects = new Rect[n];

        for (int i = 0; i < n; i++)
        {

            rects[i] = new Rect(rectToSplit.position.x + (i * rectToSplit.width / n), rectToSplit.position.y, rectToSplit.width / n, rectToSplit.height);

        }

        int padding = (int)rects[0].width - 40;
        int space = 5;

        rects[0].width -= padding + space;
        rects[2].width -= padding + space;

        rects[1].x -= padding;
        rects[1].width += padding * 2;

        rects[2].x += padding + space;


        return rects;

    }

}