summaryrefslogtreecommitdiff
path: root/Assets/Plugins/AdvancedInspector/Attributes/Title.cs
blob: 2330db376f86977de13eb2875f7d90516eb1163d (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
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Linq;
using System.Text;

using UnityEngine;

namespace AdvancedInspector
{
    /// <summary>
    /// Similar to Unity "Header" attribute, but can be place on any members and be set at runtime.
    /// </summary>
    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Method)]
    public class TitleAttribute : Attribute, IRuntimeAttribute<TitleAttribute>
    {
        public delegate TitleAttribute TitleDelegate();
        public delegate TitleAttribute TitleStaticDelegate(TitleAttribute title, object instance, object value);

        private string message;

        /// <summary>
        /// The title message
        /// </summary>
        public string Message
        {
            get { return message; }
            set { message = value; }
        }

        private FontStyle style = FontStyle.Bold;

        /// <summary>
        /// The font style.
        /// </summary>
        public FontStyle Style
        {
            get { return style; }
            set { style = value; }
        }

        #region IRuntime Implementation
        private string methodName = "";

        public string MethodName
        {
            get { return methodName; }
        }

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

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

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

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

        public TitleAttribute 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 TitleAttribute;
                }
                else
                {
                    return delegates[0].DynamicInvoke() as TitleAttribute;
                }
            }
            catch (Exception e)
            {
                if (e is TargetInvocationException)
                    e = ((TargetInvocationException)e).InnerException;

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

        public TitleAttribute(string methodName)
        {
            this.methodName = methodName;
        }

        public TitleAttribute(FontStyle style, string message)
        {
            this.style = style;
            this.message = message;
        }

        public TitleAttribute(Delegate method)
        {
            this.delegates.Add(method);
        }
    }
}