Esempio n. 1
0
    protected override void SetupControls(string name, System.Func <object> getterFunc, System.Action <object> setterAction, IEnumerable <object> attributes = null)
    {
        // Set name
        nameText.text = name;

        // List all the enum values and populate the dropdown
        var initialValue    = getterFunc();
        int initialValueInt = System.Convert.ToInt32(initialValue);
        var enumType        = initialValue.GetType();

        var vals        = System.Enum.GetValues(enumType);
        var validValues = new List <System.Enum>();

        foreach (var val in vals)
        {
            if (!UIParameterEnum.ShouldSkipValue(val))
            {
                validValues.Add(val as System.Enum);
            }
        }

        for (int i = 0; i < validValues.Count; ++i)
        {
            var bitui           = GameObject.Instantiate <UIParameterBitfieldBit>(bitPrefab, buttonRoot);
            int bit             = System.Convert.ToInt32(validValues[i]);
            int backgroundIndex = 1;
            if (i == 0)
            {
                backgroundIndex = 0;
            }
            else if (i == validValues.Count - 1)
            {
                backgroundIndex = 2;
            }

            bitui.Setup(UIParameterEnum.GetNameAttribute(validValues[i], validValues[i].ToString()), (initialValueInt & bit) != 0, backgrounds[backgroundIndex], bitColor, bitColorSelected);
            bitui.onValueChanged.AddListener((val) =>
            {
                if (val)
                {
                    setterAction(System.Enum.Parse(enumType, (System.Convert.ToInt32(getterFunc()) | bit).ToString(), true));
                }
                else
                {
                    setterAction(System.Enum.Parse(enumType, (System.Convert.ToInt32(getterFunc()) & ~bit).ToString(), true));
                }
            });
            bits.Add(bitui);
        }
    }
Esempio n. 2
0
    /// <summary>
    /// Invoke the die picker
    /// </sumary>
    public void Show(string title, System.Enum previousValue, System.Action <bool, System.Enum> closeAction, List <System.Enum> validValues)
    {
        if (isShown)
        {
            Debug.LogWarning("Previous Enum picker still active");
            ForceHide();
        }

        gameObject.SetActive(true);
        currentValue     = previousValue;
        titleText.text   = title;
        this.closeAction = closeAction;

        var enumValues = new List <System.Enum>();

        if (validValues != null)
        {
            enumValues.AddRange(validValues);
        }
        else
        {
            var vals = System.Enum.GetValues(previousValue.GetType());
            foreach (var val in vals)
            {
                enumValues.Add(val as System.Enum);
            }
        }

        List <string> enumValueNames = new List <string>();

        foreach (var value in enumValues)
        {
            enumValueNames.Add(UIParameterEnum.GetNameAttribute(value, value.ToString()));
        }

        for (int i = 0; i < enumValues.Count; ++i)
        {
            var value = enumValues[i];
            var token = CreateEnumToken(enumValueNames[i], value);
            tokens.Add(token);
            token.SetSelected(value.Equals(currentValue));
        }
    }