Example #1
0
    /**
     * Builds a neat button given its iteration count.
     */
    private void BuildButton(DialogueNode node, int i, string decision)
    {
        // figure out of the button is enabled or not.
        var isEnabled     = node.CheckConstraintAt(i);
        var isEnabledText = isEnabled ? "" : " [too dumb]";
        var buttonPanel   = Instantiate(choiceField) as GameObject; // build a button

        buttons.Add(buttonPanel);                                   // register the button
        var rectTransform = buttonPanel.transform as RectTransform;

        buttonPanel.transform.SetParent(baseCanvas.transform);                                             // nest it properly in the object hirachy
        // ReSharper disable once PossibleNullReferenceException
        rectTransform.anchoredPosition = Vector2.down * (i % 3) * (rectTransform.rect.height + 10) * 0.5f; // move it around a bit

        var rows = (node.Outputs.Count - 1) / 3;
        var pos  = i / 3;

        if (rows > 0)   // if we have more than one single row, we have to move it (move it!)
        {
            rectTransform.anchoredPosition += Vector2.left * 340;
            rectTransform.anchoredPosition += Vector2.right * pos * 340;
        }

        var buttonText = buttonPanel.GetComponentInChildren <Text>();

        if (buttonText == null)
        {
            Debug.LogError("FATAL: Nodegraph ButtonText is null");
        }
        else
        {
            buttonText.text = decision + isEnabledText;
        }
        var button = buttonPanel.GetComponent <Button>();

        button.onClick.AddListener(() => { // And register an EventListener when the button is clicked.
            state = InterfaceState.Dirty;
            if (isEnabled)
            {
                GetNext(i);
            }
        });
    }