summaryrefslogtreecommitdiffstats
path: root/Scripts/Fold/Editor/KeyframeNodeView.cs
blob: 75c7bfa16183bed035ceef006ff62693a1af626b (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
using GraphProcessor;
using UnityEngine.UIElements;
using UnityEngine;
using System.Linq;

[NodeCustomEditor(typeof(KeyframeNode))]
public class KeyframeNodeView : BaseNodeView
{
    Button generateButton;

    public override void Enable()
    {
        var node = nodeTarget as KeyframeNode;

        generateButton = new Button(() => {
            Debug.Log($"Generating Keyframe for '{node.targetObject.name}' using fold data from '{node.foldData.name}'");
        })
        {
            text = "Generate Keyframe"
        };

        controlsContainer.Add(generateButton);

        // Subscribe to connection events. This lets us disable the button when
        // the keyframe node is not in a valid state.
        onPortConnected += (p) => RefreshButtonState();
        onPortDisconnected += (p) => RefreshButtonState();

        // Initial state check.
        // We use schedule to ensure ports are fully initialized before checking.
        schedule.Execute(RefreshButtonState).ExecuteLater(0);
    }

    void RefreshButtonState()
    {
        if (generateButton == null) return;

        // Enable the keyframing button once both ports are connected.
        bool targetConnected = GetPortViewsFromFieldName(nameof(KeyframeNode.targetObject))
            .Any(pv => pv.GetEdges().Count > 0);
        bool foldConnected = GetPortViewsFromFieldName(nameof(KeyframeNode.foldData))
            .Any(pv => pv.GetEdges().Count > 0);
        generateButton.SetEnabled(targetConnected && foldConnected);
    }
}