public override void OnGUI()
        {
            if (!(foldout = gui.Foldout(dictionaryName, foldout, Layout.sExpandWidth())))
            {
                return;
            }

            if (memberValue == null)
            {
                                #if DBG
                Log("Dictionary null " + dictionaryName);
                                #endif
                memberValue = new Dictionary <TKey, TValue>();
            }

            shouldRead |= (kvpList == null || memberValue.Count != kvpList.Count);

            if (shouldRead)
            {
                                #if DBG
                Log("Reading " + dictionaryName);
                                #endif
                kvpList    = memberValue.ToKVPList();
                shouldRead = false;
            }

            if (!Readonly)
            {
                                #if PROFILE
                Profiler.BeginSample("DictionaryDrawer Header");
                                #endif
                using (gui.Indent())
                {
                    var pStr   = FormatPair(addInfo.key, addInfo.value);
                    var addKey = id + "add";

                    using (gui.Horizontal())
                    {
                        foldouts[addKey] = gui.Foldout("Add pair:", foldouts[addKey], Layout.sWidth(65f));

                        gui.TextLabel(pStr);

                        using (gui.State(kvpList.Count > 0))
                        {
                            if (gui.ClearButton("entries"))
                            {
                                kvpList.Clear();
                                shouldWrite = true;
                            }

                            if (gui.RemoveButton("Last dictionary pair"))
                            {
                                kvpList.RemoveLast();
                                shouldWrite = true;
                            }
                        }

                        if (gui.AddButton("pair", MiniButtonStyle.ModRight))
                        {
                            AddPair(addInfo.key, addInfo.value);
                            shouldWrite = true;
                        }
                    }

                    if (foldouts[addKey])
                    {
                                                #if PROFILE
                        Profiler.BeginSample("DictionaryDrawer AddingPair");
                                                #endif
                        using (gui.Indent())
                        {
                            gui.Member(addKeyInfo, attributes, ignoreAddArea || !perKeyDrawing);
                            gui.Member(addValueInfo, attributes, ignoreAddArea || !perValueDrawing);
                        }
                                                #if PROFILE
                        Profiler.EndSample();
                                                #endif
                    }
                }
                                #if PROFILE
                Profiler.EndSample();
                                #endif
            }

            if (kvpList.Count == 0)
            {
                gui.HelpBox("Dictionary is empty");
            }
            else
            {
                                #if PROFILE
                Profiler.BeginSample("DictionaryDrawer Pairs");
                                #endif
                using (gui.Indent())
                {
                    for (int i = 0; i < kvpList.Count; i++)
                    {
                        var dKey   = kvpList.Keys[i];
                        var dValue = kvpList.Values[i];

                        TValue val;
                        if (memberValue.TryGetValue(dKey, out val))
                        {
                            shouldRead |= !dValue.GenericEqual(val);
                        }

                                                #if PROFILE
                        Profiler.BeginSample("DictionaryDrawer KVP assignments");
                                                #endif

                        var pairStr  = FormatPair(dKey, dValue);
                        var entryKey = id + i + "entry";
                        foldouts[entryKey] = gui.Foldout(pairStr, foldouts[entryKey], Layout.sExpandWidth());

                                                #if PROFILE
                        Profiler.EndSample();
                                                #endif

                        if (!foldouts[entryKey])
                        {
                            continue;
                        }

                                                #if PROFILE
                        Profiler.BeginSample("DictionaryDrawer SinglePair");
                                                #endif
                        using (gui.Indent())
                        {
                            var keyMember = GetElement(keyElements, kvpList.Keys, i, entryKey + "key");
                            shouldWrite |= gui.Member(keyMember, !perKeyDrawing);

                            var valueMember = GetElement(valueElements, kvpList.Values, i, entryKey + "value");
                            shouldWrite |= gui.Member(valueMember, !perValueDrawing);
                        }
                                                #if PROFILE
                        Profiler.EndSample();
                                                #endif
                    }
                }
                                #if PROFILE
                Profiler.EndSample();
                                #endif

                shouldWrite |= memberValue.Count > kvpList.Count;
            }

            if (shouldWrite)
            {
                                #if DBG
                Log("Writing " + dictionaryName);
                                #endif
                memberValue = kvpList.ToDictionary();
                shouldWrite = false;
            }
        }
예제 #2
0
        public override void OnGUI()
        {
            if (_invalidKeyType)
            {
                gui.HelpBox("Unsuported key type: {0}. Only Value-types and strings are, sorry!"
                            .FormatWith(typeof(TK)), MessageType.Error);
                return;
            }

            if (memberValue == null)
            {
                memberValue = new TD();
            }

            if (_kvpList == null)
            {
                _kvpList = new KVPList <TK, TV>();
            }
            else
            {
                _kvpList.Clear();
            }

            // Read
            {
                var iter = memberValue.GetEnumerator();
                while (iter.MoveNext())
                {
                    var key   = iter.Current.Key;
                    var value = iter.Current.Value;
                    _kvpList[key] = value;
                }
            }

            #if PROFILE
            Profiler.BeginSample("DictionaryDrawer Header");
            #endif

            using (gui.Horizontal())
            {
                foldout = gui.Foldout(displayText, foldout, Layout.sExpandWidth());

                if (!_isReadonly)
                {
                    gui.FlexibleSpace();

                    using (gui.State(_kvpList.Count > 0))
                    {
                        if (gui.ClearButton("dictionary"))
                        {
                            _kvpList.Clear();
                        }

                        if (gui.RemoveButton("last dictionary pair"))
                        {
                            _kvpList.RemoveFirst();
                        }
                    }

                    if (gui.AddButton("pair", MiniButtonStyle.ModRight))
                    {
                        AddNewPair();
                    }
                }
            }

            #if PROFILE
            Profiler.EndSample();
            #endif

            if (!foldout)
            {
                return;
            }

            if (memberValue.Count == 0)
            {
                using (gui.Indent())
                    gui.HelpBox("Dictionary is empty");
            }
            else
            {
                #if PROFILE
                Profiler.BeginSample("DictionaryDrawer Pairs");
                #endif
                using (gui.Indent())
                {
                    for (int i = 0; i < _kvpList.Count; i++)
                    {
                        var dKey   = _kvpList.Keys[i];
                        var dValue = _kvpList.Values[i];

                        #if PROFILE
                        Profiler.BeginSample("DictionaryDrawer KVP assignments");
                        #endif

                        var pairStr  = FormatPair(dKey, dValue);
                        var entryKey = RuntimeHelper.CombineHashCodes(id, i, "entry");
                        foldouts[entryKey] = gui.Foldout(pairStr, foldouts[entryKey], Layout.sExpandWidth());

                        #if PROFILE
                        Profiler.EndSample();
                        #endif

                        if (!foldouts[entryKey])
                        {
                            continue;
                        }

                        #if PROFILE
                        Profiler.BeginSample("DictionaryDrawer SinglePair");
                        #endif
                        using (gui.Indent())
                        {
                            var keyMember = GetElement(_keyElements, _kvpList.Keys, i, entryKey + 1);
                            gui.Member(keyMember, !_perKeyDrawing);

                            var valueMember = GetElement(_valueElements, _kvpList.Values, i, entryKey + 2);
                            gui.Member(valueMember, !_perValueDrawing);
                        }
                        #if PROFILE
                        Profiler.EndSample();
                        #endif
                    }
                }
                #if PROFILE
                Profiler.EndSample();
                #endif

                // Write
                {
                    memberValue.Clear();
                    for (int i = 0; i < _kvpList.Count; i++)
                    {
                        var key   = _kvpList.Keys[i];
                        var value = _kvpList.Values[i];
                        try
                        {
                            memberValue.Add(key, value);
                        }
                        catch (ArgumentException)
                        {
                            Log("Key already exists: " + key);
                        }
                    }
                }
            }
        }
예제 #3
0
        public override void OnGUI()
        {
            if (invalidKeyType)
            {
                gui.HelpBox("Key type {0} must either be a ValueType or a 'new'able ReferenceType (not an abstract type/a UnityEngine.Object and has an empty or implicit/compiler-generated constructor)".FormatWith(typeof(TK).Name),
                            MessageType.Error);
                return;
            }

            if (memberValue == null)
            {
                #if DBG
                Log("Dictionary null " + dictionaryName);
                #endif
                memberValue = new TD();
            }

            // if the member is a kvpList, we can read immediately because we don't have to worry about allocation or anything it's just an assignment
            if (isKvpList)
            {
                kvpList = memberValue as KVPList <TK, TV>;
            }
            else
            {
                shouldRead |= (kvpList == null || (!shouldWrite && memberValue.Count != kvpList.Count));

                if (shouldRead)
                {
                    #if DBG
                    Log("Reading " + dictionaryName);
                    #endif
                    kvpList    = memberValue.ToKVPList();
                    shouldRead = false;
                }
            }


            #if PROFILE
            Profiler.BeginSample("DictionaryDrawer Header");
            #endif

            using (gui.Horizontal())
            {
                foldout = gui.Foldout(dictionaryName, foldout, Layout.sExpandWidth());

                if (!Readonly)
                {
                    gui.FlexibleSpace();

                    using (gui.State(kvpList.Count > 0))
                    {
                        if (gui.ClearButton("dictionary"))
                        {
                            kvpList.Clear();
                            shouldWrite = true;
                        }

                        if (gui.RemoveButton("last dictionary pair"))
                        {
                            kvpList.RemoveFirst();
                            shouldWrite = true;
                        }
                    }

                    if (gui.AddButton("pair"))
                    {
                        AddNewPair();
                        shouldWrite = true;
                    }

                    Color col;
                    if (!kvpList.Keys.IsUnique())
                    {
                        col = dupKeyColor;
                    }
                    else if (shouldWrite)
                    {
                        col = shouldWriteColor;
                    }
                    else
                    {
                        col = Color.white;
                    }

                    using (gui.ColorBlock(col))
                        if (gui.MiniButton("w", "Write dictionary (Orange means you modified the dictionary and should write, Red means you have a duplicate key and must address it before writing)", MiniButtonStyle.ModRight))
                        {
                        #if DBG
                            Log("Writing " + dictionaryName);
                        #endif
                            if (isKvpList)
                            {
                                memberValue = kvpList as TD;
                            }
                            else
                            {
                                try
                                {
                                    var newDict = new TD();
                                    for (int i = 0; i < kvpList.Count; i++)
                                    {
                                        var k = kvpList.Keys[i];
                                        var v = kvpList.Values[i];
                                        newDict.Add(k, v);
                                    }
                                    memberValue = newDict;
                                }
                                catch (ArgumentException e)
                                {
                                    Log(e.Message);
                                }
                            }

                            shouldWrite = false;
                        }
                }
            }

            #if PROFILE
            Profiler.EndSample();
            #endif

            if (!foldout)
            {
                return;
            }

            if (kvpList.Count == 0)
            {
                gui.HelpBox("Dictionary is empty");
            }
            else
            {
                #if PROFILE
                Profiler.BeginSample("DictionaryDrawer Pairs");
                #endif
                using (gui.Indent())
                {
                    for (int i = 0; i < kvpList.Count; i++)
                    {
                        var dKey   = kvpList.Keys[i];
                        var dValue = kvpList.Values[i];

                        #if PROFILE
                        Profiler.BeginSample("DictionaryDrawer KVP assignments");
                        #endif

                        var pairStr  = FormatPair(dKey, dValue);
                        var entryKey = RuntimeHelper.CombineHashCodes(id, i, "entry");
                        foldouts[entryKey] = gui.Foldout(pairStr, foldouts[entryKey], Layout.sExpandWidth());

                        #if PROFILE
                        Profiler.EndSample();
                        #endif

                        if (!foldouts[entryKey])
                        {
                            continue;
                        }

                        #if PROFILE
                        Profiler.BeginSample("DictionaryDrawer SinglePair");
                        #endif
                        using (gui.Indent())
                        {
                            var keyMember = GetElement(keyElements, kvpList.Keys, i, entryKey + 1);
                            shouldWrite |= gui.Member(keyMember, !perKeyDrawing);

                            var valueMember = GetElement(valueElements, kvpList.Values, i, entryKey + 2);
                            shouldWrite |= gui.Member(valueMember, !perValueDrawing);
                        }
                        #if PROFILE
                        Profiler.EndSample();
                        #endif
                    }
                }
                #if PROFILE
                Profiler.EndSample();
                #endif

                shouldWrite |= memberValue.Count > kvpList.Count;
            }
        }