summaryrefslogtreecommitdiff
path: root/Other/NodeEditorExamples/Assets/UNEB/NodeConnection.cs
blob: 40f4d6de6b4b8a6e7c3045af62e2bc678c44ea40 (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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace UNEB
{
    public abstract class NodeConnection : ScriptableObject
    {
        public static readonly Vector2 kMinSize = new Vector2(12f, 12f);
        public static readonly Vector2 kMinHalfSize = kMinSize / 2f;

        [HideInInspector]
        public Rect bodyRect = new Rect(Vector2.zero, kMinSize);
        
        public System.Func<object> getValue;

        public static System.Action<NodeConnection> OnConnectionCreated;

        [SerializeField, HideInInspector]
        protected Node parentNode;

        void OnEnable()
        {
            hideFlags = HideFlags.HideInHierarchy;
        }

        public virtual void Init(Node parent)
        {
            name = "connection";
            parentNode = parent;
        }

        /// <summary>
        /// The node associated with this knob.
        /// </summary>
        public Node ParentNode
        {
            get { return parentNode; }
        }

        public abstract GUIStyle GetStyle();

        public virtual void OnConnectionGUI(int order)
        {
            OnNameGUI();

            // Position the knobs properly for the draw pass of the knobs in the editor.
            float yPos = parentNode.HeaderTop + order * GetStyle().fixedHeight + Node.kKnobOffset;
            bodyRect.center = new Vector2(GetNodeAnchor(), 0f);
            bodyRect.y = yPos;
        }

        public virtual void OnNameGUI()
        {
            GUILayout.Label(Content, GetStyle());
        }

        private GUIContent _content;
        private GUIContent Content
        {
            get
            {
                if (_content == null) {
                    _content = new GUIContent(name);
                }

                return _content;
            }
        }

        /// <summary>
        /// What side of the node should the knob anchor to.
        /// </summary>
        /// <returns></returns>
        public abstract float GetNodeAnchor();

        /// <summary>
        /// Attempts to get the value of the specified type.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public T GetValue<T>()
        {
            if (getValue != null) {
                return (T)getValue();
            }

            return default(T);
        }
    }
}