summaryrefslogtreecommitdiff
path: root/Other/NodeEditorExamples/Assets/UNEB/Editor/Actions/CreateConnection.cs
blob: 09d3e521a798d3ecf3579d49bed7539a2f457585 (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

using System.Linq;
using System.Collections.Generic;

using UnityEngine;
using UnityEditor;

namespace UNEB
{
    public class CreateConnection : MultiStageAction
    {
        private NodeInput _input;
        private NodeOutput _output;

        // The output of the old node it was connected to.
        private NodeOutput _oldConnectedOutput;

        // Old inputs of the node.
        private List<NodeInput> _oldConnectedInputs;

        public override void Do()
        {
            manager.window.state.selectedOutput = _output;
        }

        public override void Undo()
        {
            _output.Remove(_input);
            reconnectOldConnections();
        }

        public override void Redo()
        {
            disconnectOldConnections();
            _output.Add(_input);
        }

        private void reconnectOldConnections()
        {
            //  Re-connect old connections
            if (_oldConnectedOutput != null) {
                _oldConnectedOutput.Add(_input);
            }

            if (_oldConnectedInputs != null) {
                foreach (var input in _oldConnectedInputs) {
                    _output.Add(input);
                }
            }
        }

        private void disconnectOldConnections()
        {
            // Remove old connections
            if (_oldConnectedOutput != null) {
                _oldConnectedOutput.Remove(_input);
            }

            if (_oldConnectedInputs != null) {
                _output.RemoveAll();
            }
        }

        public override void OnActionStart()
        {
            _output = manager.window.state.selectedOutput;
        }

        public override bool OnActionEnd()
        {
            manager.window.state.selectedOutput = null;
            manager.window.editor.OnMouseOverInput((input) => { _input = input; });

            // Make the connection.
            if (_input != null && _output.CanConnectInput(_input)) {

                if (!_output.bCanHaveMultipleConnections)
                {
                    _output.RemoveAll();
                }

                if (!_input.bCanHaveMultipleConnections) {
                    cacheOldConnections();
                    disconnectOldConnections();
                }

                return _output.Add(_input);
            }

            return false;
        }

        private void cacheOldConnections()
        {
            // Check if the receiving node was already connected.
            if (_input != null && _input.HasOutputConnected()) {
                _oldConnectedOutput = _input.Outputs[0];
            }

            // Check if the origin node already had inputs
            if (!_output.bCanHaveMultipleConnections && _output.InputCount > 0) {
                _oldConnectedInputs = _output.Inputs.ToList();
            }
        }
    }
}