summaryrefslogtreecommitdiff
path: root/Assets/Plugins/AdvancedInspector/Attributes/Descriptor.cs
blob: a452e72bc73ed790aadd1524b637aacfda048d08 (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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections.Generic;
using System.Reflection;

using UnityEngine;

namespace AdvancedInspector
{
    /// <summary>
    /// A description is the information about "something".
    /// It contains an optional name, description, icon, color.
    /// It can be used both as a attributes or a normal object, container of information.
    /// Ex.: The Toolbox is using it as object.
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method | AttributeTargets.Class | 
        AttributeTargets.Interface | AttributeTargets.Struct | AttributeTargets.Enum, Inherited = false)]
    public class DescriptorAttribute : Attribute, IRuntimeAttribute<DescriptorAttribute>
    {
        public delegate DescriptorAttribute DescriptorDelegate();
        public delegate DescriptorAttribute DescriptorStaticDelegate(DescriptorAttribute descriptor, object instance, object value);

        private static Color TRANSPARENT = new Color(0, 0, 0, 0);

        private string name = "";

        /// <summary>
        /// Give this item a name.
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        private string description = "";

        /// <summary>
        /// Give this item a description. 
        /// Usually used for tooltip.
        /// </summary>
        public string Description
        {
            get { return description; }
            set { description = value; }
        }

        private string url = "";

        /// <summary>
        /// Give this item an help URL.
        /// </summary>
        public string URL
        {
            get { return url; }
            set { url = value; }
        }

        private Texture icon = null;

        /// <summary>
        /// Give this item an icon.
        /// Useful in a list of items.
        /// </summary>
        public Texture Icon
        {
            get { return icon; }
            set { icon = value; }
        }

        private Color color = Color.clear;

        /// <summary>
        /// Give this item a color.
        /// Default is transparent
        /// </summary>
        public Color Color
        {
            get { return color; }
            set { color = value; }
        }

        #region IRuntime Implementation
        private string methodName = "";

        public string MethodName
        {
            get { return methodName; }
        }

        public Type Template
        {
            get { return typeof(DescriptorDelegate); }
        }

        public Type TemplateStatic
        {
            get { return typeof(DescriptorStaticDelegate); }
        }

        private List<Delegate> delegates = new List<Delegate>();

        public List<Delegate> Delegates
        {
            get { return delegates; }
            set { delegates = value; }
        }

        public DescriptorAttribute Invoke(int index, object instance, object value)
        {
            if (delegates.Count == 0 || index >= delegates.Count)
                return this;

            try
            {
                if (delegates[index].Target == null)
                {
                    return delegates[0].DynamicInvoke(this, instance, value) as DescriptorAttribute;
                }
                else
                {
                    return delegates[0].DynamicInvoke() as DescriptorAttribute;
                }
            }
            catch (Exception e)
            {
                if (e is TargetInvocationException)
                    e = ((TargetInvocationException)e).InnerException;

                Debug.LogError(string.Format("Invoking a method to retrieve a Destriptor attribute failed. The exception was \"{0}\".", e.Message));
                return null;
            }
        }
        #endregion

        public DescriptorAttribute() { }

        #region Attributes Constructor
        public DescriptorAttribute(string methodName)
        {
            this.methodName = methodName;
        }

        public DescriptorAttribute(float r, float g, float b)
            : this("", "", "", r, g, b, 1) { }

        public DescriptorAttribute(string name, string description)
            : this(name, description, "", 0, 0, 0, 0) { }

        public DescriptorAttribute(string name, string description, string url)
            : this(name, description, url, 0, 0, 0, 0) { }

        public DescriptorAttribute(string name, string description, string url, float r, float g, float b)
            : this(name, description, url, r, g, b, 1) { }

        private DescriptorAttribute(string name, string description, string url, float r, float g, float b, float a)
        {
            this.name = name;
            this.description = description;
            this.url = url;
            color = new Color(r, g, b, a);
        }
        #endregion

        #region Object Constructor
        public DescriptorAttribute(string name, string description, Texture icon)
            : this(name, description, icon, TRANSPARENT) { }

        public DescriptorAttribute(string name, string description, Texture icon, Color color)
        {
            this.name = name;
            this.description = description;
            this.icon = icon;
            this.color = color;
        }
        #endregion

        public static DescriptorAttribute GetDescriptor(Type type)
        {
            object[] obj = type.GetCustomAttributes(typeof(DescriptorAttribute), true);

            if (obj.Length == 0)
                return null;
            else
                return (obj[0] as DescriptorAttribute);
        }

        public static List<DescriptorAttribute> GetDescriptors(List<Type> types)
        {
            List<DescriptorAttribute> descriptors = new List<DescriptorAttribute>();

            foreach (Type type in types)
                descriptors.Add(GetDescriptor(type));

            return descriptors;
        }
    }

    /// <summary>
    /// Pairs an object with a descriptor.
    /// Used by the Toolbox and the Advanced Inspector.
    /// </summary>
    public class DescriptorPair
    {
        private object value;

        public object Value
        {
            get { return value; }
        }

        private DescriptorAttribute descriptor;

        public DescriptorAttribute Descriptor
        {
            get { return descriptor; }
        }

        public DescriptorPair(object value, DescriptorAttribute descriptor)
        {
            this.value = value;
            this.descriptor = descriptor;
        }

        public DescriptorPair(object value, string name)
            : this(value, new DescriptorAttribute(name, "")) { }

        public DescriptorPair(object value, string name, string description)
            : this(value, new DescriptorAttribute(name, description)) { }

        public static bool operator ==(DescriptorPair a, DescriptorPair b)
        {
            // If both are null, or both are same instance, return true.
            if (System.Object.ReferenceEquals(a, b))
                return true;

            // If one is null, but not both, return false.
            if (((object)a == null) || ((object)b == null))
                return false;

            return a.Equals(b);
        }

        public static bool operator !=(DescriptorPair a, DescriptorPair b)
        {
            return !(a == b);
        }

        public override bool Equals(object obj)
        {
            DescriptorPair other = obj as DescriptorPair;
            if (other == null)
                return false;

            return this.Value.Equals(other.Value);
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}