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

namespace AdvancedInspector
{
    public class TimeSpanEditor : FieldEditor
    {
        private InspectorField field;

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

        public override void Draw(InspectorField field, GUIStyle style)
        {
            int[] days = new int[field.Instances.Length]; 
            int[] hours = new int[field.Instances.Length]; 
            int[] mins = new int[field.Instances.Length]; 
            int[] secs = new int[field.Instances.Length];
            for (int i = 0; i < field.Instances.Length; i++)
            {
                TimeSpan span = field.GetValue<TimeSpan>(field.Instances[i]);
                days[i] = span.Days;
                hours[i] = span.Hours;
                mins[i] = span.Minutes;
                secs[i] = span.Seconds;
            }

            float labelWidth = EditorGUIUtility.labelWidth;
            EditorGUIUtility.labelWidth = 42;
            GUILayout.BeginHorizontal();

            int result;
            if (IntegerEditor.DrawInt("Days", days, style, out result))
            {
                result = Mathf.Clamp(result, 0, int.MaxValue);
                Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " Days");

                for (int i = 0; i < field.Instances.Length; i++)
                {
                    TimeSpan span = field.GetValue<TimeSpan>(field.Instances[i]);
                    span = new TimeSpan(result, span.Hours, span.Minutes, span.Seconds);
                    field.SetValue(field.Instances[i], span);
                }
            }

            if (IntegerEditor.DrawInt("Hours", hours, style, out result))
            {
                result = Mathf.Clamp(result, 0, int.MaxValue);
                Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " Hours");

                for (int i = 0; i < field.Instances.Length; i++)
                {
                    TimeSpan span = field.GetValue<TimeSpan>(field.Instances[i]);
                    span = new TimeSpan(span.Days, result, span.Minutes, span.Seconds);
                    field.SetValue(field.Instances[i], span);
                }
            }

            GUILayout.EndHorizontal();
            GUILayout.BeginHorizontal();

            if (IntegerEditor.DrawInt("Mins", mins, style, out result))
            {
                result = Mathf.Clamp(result, 0, int.MaxValue);
                Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " Minutes");

                for (int i = 0; i < field.Instances.Length; i++)
                {
                    TimeSpan span = field.GetValue<TimeSpan>(field.Instances[i]);
                    span = new TimeSpan(span.Days, span.Hours, result, span.Seconds);
                    field.SetValue(field.Instances[i], span);
                }
            }

            if (IntegerEditor.DrawInt("Secs", secs, style, out result))
            {
                result = Mathf.Clamp(result, 0, int.MaxValue);
                Undo.RecordObjects(field.SerializedInstances, "Edit " + field.Name + " Seconds");

                for (int i = 0; i < field.Instances.Length; i++)
                {
                    TimeSpan span = field.GetValue<TimeSpan>(field.Instances[i]);
                    span = new TimeSpan(span.Days, span.Hours, span.Minutes, result);
                    field.SetValue(field.Instances[i], span);
                }
            }

            GUILayout.EndHorizontal();
            EditorGUILayout.Space();

            EditorGUIUtility.labelWidth = labelWidth;
        }
    }
}