summaryrefslogtreecommitdiff
path: root/Assets/Plugins/AdvancedInspector/Attributes/Collection.cs
blob: 2e9f870a270b7741eff7e397aa494874bf0992e7 (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
using System;
using System.Collections.Generic;
using System.Reflection;

using UnityEngine;

namespace AdvancedInspector
{
    /// <summary>
    /// When affixes to a collection, prevent this collection's size to be modified by the inspector.
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
    public class CollectionAttribute : Attribute, IListAttribute
    {
        private int size = -1;

        /// <summary>
        /// Size of this collection.
        /// Default -1; size is not controlled by code.
        /// 0 means the collection's size will be handled internally.
        /// > 0 indicate the same of the collection.
        /// </summary>
        public int Size
        {
            get { return size; }
            set { size = value; }
        }

        private bool sortable = true;

        /// <summary>
        /// If true, the list can be sorted by hand.
        /// </summary>
        public bool Sortable
        {
            get { return sortable; }
            set { sortable = value; }
        }

        private CollectionDisplay display = CollectionDisplay.List;

        /// <summary>
        /// If not default, removes the collection list and only display one item at a time.
        /// </summary>
        public CollectionDisplay Display
        {
            get { return display; }
            set { display = value; }
        }

        private int maxDisplayedItems = 25;

        /// <summary>
        /// When a collection is very large, it get up/down arrows to scrolls in items instead of displaying them all.
        /// This property controls how many items are displayed before the scrolls appears. 
        /// </summary>
        public int MaxDisplayedItems
        {
            get { return maxDisplayedItems; }
            set { maxDisplayedItems = value; }
        }

        private int maxItemsPerRow = 6;

        /// <summary>
        /// When display is using Button, this is the maximum number of button displayed per rows before creating a new one.
        /// </summary>
        public int MaxItemsPerRow
        {
            get { return maxItemsPerRow; }
            set { maxItemsPerRow = value; }
        }

        private Type enumType = null;

        /// <summary>
        /// Bind the size of a collection to the values of an enum.
        /// The name of the indexed are displayed using the enum values' names. 
        /// </summary>
        public Type EnumType
        {
            get { return enumType; }
            set
            {
                if (!value.IsEnum)
                    return;

                int index = 0;
                foreach (object i in Enum.GetValues(value))
                {
                    if ((int)i != index)
                        return;

                    index++;
                }

                enumType = value;
            }
        }

        public CollectionAttribute() { }

        public CollectionAttribute(int size)
            : this(size, true) { }

        public CollectionAttribute(Type enumType)
            : this(enumType, true) { }

        public CollectionAttribute(bool sortable)
            : this(-1, sortable) { }

        public CollectionAttribute(CollectionDisplay display)
            : this(-1, true, display) { }

        public CollectionAttribute(int size, bool sortable)
            : this(size, sortable, CollectionDisplay.List) { }

        public CollectionAttribute(Type enumType, bool sortable)
            : this(enumType, sortable, CollectionDisplay.List) { }

        public CollectionAttribute(int size, CollectionDisplay display)
            : this(size, true, display) { }

        public CollectionAttribute(Type enumType, CollectionDisplay display)
            : this(enumType, true, display) { }

        public CollectionAttribute(int size, bool sortable, CollectionDisplay display)
        {
            this.size = size;
            this.sortable = sortable;
            this.display = display;
        }

        public CollectionAttribute(Type enumType, bool sortable, CollectionDisplay display)
        {
            this.EnumType = enumType;
            this.sortable = sortable;
            this.display = display;
        }
    }

    /// <summary>
    /// None default display should only be used on collection that contain expandable objects.
    /// </summary>
    public enum CollectionDisplay
    {
        List,
        DropDown,
        Button
    }
}