# Code coverage report for src/compositor/Graph.js

# all files / src/compositor/ Graph.js

2% Statements 1/50
0% Branches 0/36
0% Functions 0/7
2% Lines 1/50

    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

    1×

     import Base from '../core/Base';
    import GraphNode from './CompositorNode';

    /**
     * @constructor clay.compositor.Graph
     * @extends clay.core.Base
     */
    var Graph = Base.extend(function () {
        return /** @lends clay.compositor.Graph# */ {
            /**
             * @type {Array.<clay.compositor.CompositorNode>}
             */
            nodes: []
        };
    },
    /** @lends clay.compositor.Graph.prototype */
    {

        /**
         * Mark to update
         */
        dirty: function () {
            this._dirty = true;
        },
        /**
         * @param {clay.compositor.CompositorNode} node
         */
        addNode: function (node) {

            if (this.nodes.indexOf(node) >= 0) {
                return;
            }

            this.nodes.push(node);

            this._dirty = true;
        },
        /**
         * @param  {clay.compositor.CompositorNode|string} node
         */
        removeNode: function (node) {
            if (typeof node === 'string') {
                node = this.getNodeByName(node);
            }
            var idx = this.nodes.indexOf(node);
            if (idx >= 0) {
                this.nodes.splice(idx, 1);
                this._dirty = true;
            }
        },
        /**
         * @param {string} name
         * @return {clay.compositor.CompositorNode}
         */
        getNodeByName: function (name) {
            for (var i = 0; i < this.nodes.length; i++) {
                if (this.nodes[i].name === name) {
                    return this.nodes[i];
                }
            }
        },
        /**
         * Update links of graph
         */
        update: function () {
            for (var i = 0; i < this.nodes.length; i++) {
                this.nodes[i].clear();
            }
            // Traverse all the nodes and build the graph
            for (var i = 0; i < this.nodes.length; i++) {
                var node = this.nodes[i];

                if (!node.inputs) {
                    continue;
                }
                for (var inputName in node.inputs) {
                    if (!node.inputs[inputName]) {
                        continue;
                    }
                    if (node.pass && !node.pass.material.isUniformEnabled(inputName)) {
                        console.warn('Pin '  + node.name + '.' + inputName + ' not used.');
                        continue;
                    }
                    var fromPinInfo = node.inputs[inputName];

                    var fromPin = this.findPin(fromPinInfo);
                    if (fromPin) {
                        node.link(inputName, fromPin.node, fromPin.pin);
                    }
                    else {
                        if (typeof fromPinInfo === 'string') {
                            console.warn('Node ' + fromPinInfo + ' not exist');
                        }
                        else {
                            console.warn('Pin of ' + fromPinInfo.node + '.' + fromPinInfo.pin + ' not exist');
                        }
                    }
                }
            }
        },

        findPin: function (input) {
            var node;
            // Try to take input as a directly a node
            if (typeof input === 'string' || input instanceof GraphNode) {
                input = {
                    node: input
                };
            }

            if (typeof input.node === 'string') {
                for (var i = 0; i < this.nodes.length; i++) {
                    var tmp = this.nodes[i];
                    if (tmp.name === input.node) {
                        node = tmp;
                    }
                }
            }
            else {
                node = input.node;
            }
            if (node) {
                var inputPin = input.pin;
                if (!inputPin) {
                    // Use first pin defaultly
                    if (node.outputs) {
                        inputPin = Object.keys(node.outputs)[0];
                    }
                }
                if (node.outputs[inputPin]) {
                    return {
                        node: node,
                        pin: inputPin
                    };
                }
            }
        }
    });

    export default Graph;