Esempio n. 1
0
    public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
    {
        CheckInitialize(property, label);
        if (!_Foldout || _Dictionary.Count == 0)
        {
            return(17f);
        }
        float height = 17f;
        Type  type   = typeof(TV);

        if (type == typeof(DInt))
        {
            foreach (var dInt in _Dictionary.Values)
            {
                height += DIntDrawer.GetPropertyHeight(dInt as DInt);
            }
        }
        else if (type == typeof(DLong))
        {
            foreach (var dLong in _Dictionary.Values)
            {
                height += DLongDrawer.GetPropertyHeight(dLong as DLong);
            }
        }
        else if (type == typeof(DFloat))
        {
            foreach (var dFloat in _Dictionary.Values)
            {
                height += DFloatDrawer.GetPropertyHeight(dFloat as DFloat);
            }
        }
        else
        {
            height = _Dictionary.Count * 17f;
        }

        return(height);
    }
Esempio n. 2
0
    public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
    {
        CheckInitialize(property, label);

        EditorGUI.DrawRect(position, new Color(.85f, .85f, .85f));

        position.height = 17f;

        var foldoutRect = position;

        foldoutRect.width -= 2 * kButtonWidth;
        EditorGUI.BeginChangeCheck();
        _Foldout = EditorGUI.Foldout(foldoutRect, _Foldout, label, true);
        if (EditorGUI.EndChangeCheck())
        {
            EditorPrefs.SetBool(label.text, _Foldout);
            EditorUtility.SetDirty(property.serializedObject.targetObject);
        }

        var buttonRect = position;

        buttonRect.x     = position.width - kButtonWidth + position.x;
        buttonRect.width = kButtonWidth + 2;

        if (GUI.Button(buttonRect, new GUIContent("+", "Add item"), EditorStyles.miniButton))
        {
            AddNewItem();
        }
        buttonRect.x -= kButtonWidth;

        if (GUI.Button(buttonRect, new GUIContent("X", "Clear dictionary"), EditorStyles.miniButtonRight))
        {
            ClearDictionary();
        }
        if (!_Foldout)
        {
            return;
        }

        float extra = 19f;

        foreach (var item in _Dictionary)
        {
            var key   = item.Key;
            var value = item.Value;

            position.y += extra;

            var keyRect = position;
            keyRect.width /= 3f;
            keyRect.width -= 4f;
            EditorGUI.BeginChangeCheck();
            var newKey = DoField(keyRect, typeof(TK), key, property);
            if (EditorGUI.EndChangeCheck())
            {
                try {
                    _Dictionary.Remove(key);
                    _Dictionary.Add(newKey, value);
                } catch (Exception e) {
                    Debug.Log(e.Message);
                }
                break;
            }

            var valueRect = position;
            // valueRect.x = position.width / 2f + 15f;
            valueRect.x     = keyRect.xMax + 15f;
            valueRect.width = position.xMax - valueRect.x - kButtonWidth;
            EditorGUI.BeginChangeCheck();
            if (typeof(TV) == typeof(DInt))
            {
                extra = DIntDrawer.GetPropertyHeight(value as DInt);
            }
            else if (typeof(TV) == typeof(DLong))
            {
                extra = DLongDrawer.GetPropertyHeight(value as DLong);
            }
            else if (typeof(TV) == typeof(DFloat))
            {
                extra = DFloatDrawer.GetPropertyHeight(value as DFloat);
            }
            else
            {
                extra = 17f;
            }

            value = DoField(valueRect, typeof(TV), value, property);
            if (EditorGUI.EndChangeCheck())
            {
                _Dictionary[key] = value;
                break;
            }

            var removeRect = valueRect;
            removeRect.x     = valueRect.xMax + 2;
            removeRect.width = kButtonWidth;
            if (GUI.Button(removeRect, new GUIContent("x", "Remove item"), EditorStyles.miniButtonRight))
            {
                RemoveItem(key);
                break;
            }
        }
    }