Пример #1
0
        private void DrawBody(Rect position, IEditorDictionary dict)
        {
            foreach (KeyValuePair <object, object> entry in dict.GetEntries().ToList())
            {
                position = position.ColumnNext();

                float buttonWidth = ButtonDrawers.ButtonSize.x;
                float columnSize  = (position.width - buttonWidth) / 2;

                Rect[] row = position.SplitRow(columnSize, columnSize, buttonWidth);

                object newKey   = Drawers.Draw(row[0], dict.KeyType, entry.Key);
                object newValue = Drawers.Draw(row[1], dict.ValueType, entry.Value);

                if (newKey != entry.Key || newValue != entry.Value)
                {
                    dict.RemoveEntry(entry.Key);
                    dict.AddEntry(newKey, newValue);
                }

                if (ButtonDrawers.Minus(row[2]))
                {
                    dict.RemoveEntry(newKey);
                }
            }
        }
Пример #2
0
        private bool DrawFoldout(Rect position, GUIContent label, IEditorDictionary dict,
                                 object defaultKey, object defaultValue)
        {
            int fieldToken = fieldInfo.MetadataToken;

            if (!Foldouts.ContainsKey(fieldToken))
            {
                Foldouts[fieldToken] = false;
            }

            float buttonWidth = ButtonDrawers.ButtonSize.x;

            Rect[] rects = position.SplitRow(position.width - buttonWidth, buttonWidth);

            bool foldout = EditorGUI.Foldout(rects[0], Foldouts[fieldToken], label);

            if (ButtonDrawers.Plus(rects[1]))
            {
                defaultKey ??= CreateInstance(dict.KeyType);
                defaultValue ??= CreateInstance(dict.ValueType);
                dict.AddEntry(defaultKey, defaultValue);
            }

            return(Foldouts[fieldToken] = foldout);
        }
Пример #3
0
        private bool ValidateDefaultConstructors(SerializedProperty property, out string message)
        {
            DictionaryViewAttribute viewAttr = GetViewAttr();
            IEditorDictionary       dict     = GetDict(property);

            if (viewAttr.DefaultKey == null)
            {
                try
                {
                    CreateInstance(dict !.KeyType);
                }
                catch (MissingMethodException)
                {
                    message = "Invalid key constructor. Use DictionaryView(defaultKey: ...)";
                    return(false);
                }
            }

            if (viewAttr.DefaultValue == null)
            {
                try
                {
                    CreateInstance(dict !.ValueType);
                }
                catch (MissingMethodException)
                {
                    message = "Invalid value constructor. Use DictionaryView(defaultValue: ...)";
                    return(false);
                }
            }

            message = null;
            return(true);
        }
Пример #4
0
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            DictionaryViewAttribute viewAttr = GetViewAttr();

            if (viewAttr == null)
            {
                EditorGUI.PropertyField(position, property, label, true);
                return;
            }

            position.height = 18;

            if (!TryValidate(property, out string message))
            {
                if (message != null && viewAttr.ShowUnsupportedBox)
                {
                    EditorGUI.HelpBox(position, message, MessageType.Error);
                }
                return;
            }

            int fieldToken = fieldInfo.MetadataToken;

            if (!Foldouts.ContainsKey(fieldToken))
            {
                Foldouts[fieldToken] = false;
            }

            IEditorDictionary dict = GetDict(property);

            if (DrawFoldout(position, label, dict, viewAttr.DefaultKey, viewAttr.DefaultValue))
            {
                DrawBody(position, dict);
            }
        }
Пример #5
0
 private bool CanBeDrawn(IEditorDictionary dict)
 => Drawers.CanDraw(dict.KeyType) && Drawers.CanDraw(dict.ValueType);