Ejemplo n.º 1
0
    private ActionUiLabelButtonWithHeight SpawnOneLable(
        Vector2 pos,
        string text,
        Transform parent,
        bool button,
        out GameObject buttonLabel,
        bool monoName)
    {
        var result = new ActionUiLabelButtonWithHeight();

        var label     = Instantiate(ReferenceBuffer.Instance.EditorInput.labelPreFab, parent);
        var labelRT   = label.GetComponent <RectTransform>();
        var labelText = label.GetComponent <Text>();

        labelText.text = text;

        if (monoName)
        {
            labelRT.sizeDelta   = new Vector2(this.labelX, this.labelY);
            labelText.fontSize  = 20;
            labelText.alignment = TextAnchor.MiddleCenter;
        }
        else
        {
            labelRT.sizeDelta = new Vector2(this.buttonX, this.labelY);
        }

        var localX = labelRT.sizeDelta.x;
        var localY = labelRT.sizeDelta.y;

        var position = new Vector2(pos.x + localX / 2, pos.y - localY / 2);

        label.GetComponent <RectTransform>().anchoredPosition = position;


        if (button)
        {
            label.AddComponent <Button>();
            var script = label.AddComponent <LabelButtonMonoName>();
            result.labelButtonScript = script;
            buttonLabel = label;
        }
        else
        {
            result.labelButtonScript = null;
            buttonLabel = null;
        }

        result.Height = this.labelY + this.marginLableToItsButtons;

        return(result);
    }
Ejemplo n.º 2
0
    /// <summary>
    /// Creates the UI to visualise given mono's methods as buttons and their paramaters as inputs
    /// </summary>
    /// <param name="monoName">The name of the mono script</param>
    /// <param name="methods">Information abouth the methods in the script</param>
    /// <returns>The generated UI as well as meta data for it</returns>
    private CreateMonUIResult CreateScriptUI(string monoName, UiMethodNameWithParameters[] methods)
    {
        /// localGrandParent hold the whole UI
        /// Directly howds only the buttonLabel(the mono name) and the local parent
        /// the local parent holds everything else (method buttons and parameter inputs)
        /// clicking on the buttonLabel collapses the local parent
        GameObject localGrandParent = new GameObject(monoName + "GrandParent");

        localGrandParent.transform.SetParent(this.parentTransform, false);
        localGrandParent.transform.position = this.parentTransform.position;

        GameObject localParent = new GameObject(monoName);

        localParent.transform.SetParent(localGrandParent.transform, false);
        localParent.transform.position = localGrandParent.transform.position;
        Transform localTransform = localParent.transform;

        float localHeight = 0f;

        ///assigning the label to the grandparent not the parent
        ActionUiLabelButtonWithHeight labelInfo         = this.SpawnOneLable(new Vector2(this.marginX, localHeight), monoName, localGrandParent.transform, true, out GameObject intButtonLabel, true);
        LabelButtonMonoName           lableButtonScript = labelInfo.labelButtonScript;

        localHeight -= labelInfo.Height;

        /// Collectiong all the method button behaviours so that we can extract
        /// all colorButton scripts from them.
        List <MethodButtonBehaviour> methodButtonBehs = new List <MethodButtonBehaviour>();

        /// All others are assigned to the parent so we can deactivate them seperately.
        /// We are doing to buttons on the same time, putting them side by side
        for (int i = 0; i < methods.Length; i += 2)
        {
            /// If we have unevent mothod count, we place the last button individualy to the left
            if (i == methods.Length - 1)
            {
                UiMethodNameWithParameters method = methods[i];
                Vector2 pos = new Vector2(marginX, localHeight);
                MethodButtonBehaviour beh = this.SpawnOneButton(pos, method.Name, monoName, localTransform);
                pos.y -= this.buttonY;
                float height = this.SpawnAllInputs(pos, beh, method, localTransform);
                localHeight -= (height + this.buttonY + this.marginY);
                methodButtonBehs.Add(beh);
            }
            /// Placing two buttons side by side.
            else
            {
                UiMethodNameWithParameters method1 = methods[i];
                UiMethodNameWithParameters method2 = methods[i + 1];

                Vector2 pos1 = new Vector2(marginX, localHeight);
                MethodButtonBehaviour beh1 = this.SpawnOneButton(pos1, method1.Name, monoName, localTransform);
                pos1.y -= this.buttonY;
                float height1 = this.SpawnAllInputs(pos1, beh1, method1, localTransform);

                Vector2 pos2 = new Vector2(marginX * 2 + this.buttonX, localHeight);
                MethodButtonBehaviour beh2 = this.SpawnOneButton(pos2, method2.Name, monoName, localTransform);
                pos2.y -= this.buttonY;
                float height2 = this.SpawnAllInputs(pos2, beh2, method2, localTransform);

                localHeight -= (Mathf.Max(height1, height2) + buttonY + marginY);

                methodButtonBehs.Add(beh1);
                methodButtonBehs.Add(beh2);
            }
        }

        ///Collecting all the color scripts so we can close Color Picker on colapse
        foreach (MethodButtonBehaviour methodScript in methodButtonBehs)
        {
            foreach (ParameterNameWithColorButtonScript colorButtonScript in methodScript.ColorParamaters)
            {
                lableButtonScript.ColorSelectionButtons.Add(colorButtonScript.colorScript);
            }
        }

        /// newly created item whould be activated later;
        localParent.SetActive(false);
        intButtonLabel.SetActive(false);

        var result = new CreateMonUIResult
        {
            ButtonLabel       = intButtonLabel,
            CollapsedHeight   = labelInfo.Height,
            FinalHeight       = -localHeight,
            GrandParent       = localGrandParent,
            LabelButtonScritp = labelInfo.labelButtonScript,
            Parent            = localParent,
        };

        return(result);
    }