Ejemplo n.º 1
0
        private bool AddNewItem(bool autoIncreaseEnum)
        {
            Type typeTK = typeof(TK);
            TK   key    = Misc.Default <TK>();

            if (autoIncreaseEnum && typeTK.IsEnum)
            {
                if (mDictionary.ContainsKey(key))
                {
                    var values = EnumUtilityNoObsoleted <TK> .GetValues();

                    foreach (var v in values)
                    {
                        if (!mDictionary.ContainsKey(v))
                        {
                            key = v;
                            break;
                        }
                    }
                }
            }

            //TK key = Misc.Default<TK>();
            if (mDictionary.ContainsKey(key))
            {
                return(false);
            }
            mDictionary.Add(key, Misc.Default <TV>());
            return(true);
        }
Ejemplo n.º 2
0
            private bool SetFieldValue(IEnumerable <IConsoleObject> objs, object value)
            {
                try
                {
                    var field = this.field;
                    if (!field.IsStatic && objs == null)
                    {
                        return(false);
                    }

                    value = (field.FieldType.IsValueType && value == null) ? Misc.Default(field.FieldType) : value;

                    object[] args = new[] { value };

                    // only call once
                    if (field.IsStatic)
                    {
                        bool result = true;
                        if (validate != null)
                        {
                            result = (bool)validate.Invoke(null, args);
                        }

                        if (result)
                        {
                            field.SetValue(null, value);
                        }
                    }
                    else
                    {
                        foreach (var obj in objs)
                        {
                            bool result = true;
                            if (validate != null)
                            {
                                result = (bool)validate.Invoke(obj, args);
                            }
                            if (result)
                            {
                                field.SetValue(obj, value);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    DebugUtility.LogException(ex);
                    return(false);
                }
                return(true);
            }
Ejemplo n.º 3
0
        private T DoField <T>(ref Rect position, ref Rect rect, Type type, T value)
        {
            if (value == null)
            {
                value = Misc.Default <T>(type);
            }

            if (LokiEditor.IsSupportedEditorGUIObjectField(type))
            {
                return(LokiEditor.EditorGUIObjectField <T>(rect, type, value));
            }

            DebugUtility.LogWarningTrace(LoggerTags.Engine, "Type is not supported: " + type);
            return(value);
        }
Ejemplo n.º 4
0
        private T DoListField <T>(ref Rect position, ref Rect rect, Type type, TK key, T value)
        {
            if (value == null)
            {
                value = Misc.Default <T>(type);
            }

            if (typeof(System.Collections.IList).IsAssignableFrom(type))
            {
                var list = (System.Collections.IList)value;

                Rect listCountRect = rect;
                listCountRect.width = kButtonWidth;
                listCountRect.x     = rect.xMax + 6 - kButtonWidth * 2 - kLabelWidth;

                EditorGUI.LabelField(listCountRect, list.Count.ToString());

                Type elementType = null;
                var  attri       = type.GetCustomAttribute <CollectionAttribute>(true);
                if (attri != null)
                {
                    elementType = attri.valueType;
                }
                else if (type.IsGenericType)
                {
                    elementType = type.GetGenericArguments()[0];
                }

                if (elementType != null)
                {
                    Rect buttonRect = rect;
                    buttonRect.width = kButtonWidth;
                    buttonRect.x     = rect.xMax + 2;
                    if (GUI.Button(buttonRect, new GUIContent("+", "Add item"), EditorStyles.miniButton))
                    {
                        list.Add(Misc.Default(elementType));
                        SetTKFoldout(key, true);
                    }
                }
                position.y += 17;

                if (IsTKFoldout(key))
                {
                    rect.width -= kButtonWidth * 2.0f;
                    int count = list.Count;
                    for (int i = 0; i < count; ++i)
                    {
                        rect.y = position.y;

                        var removeRect = rect;
                        removeRect.y     = rect.y;
                        removeRect.width = kButtonWidth;
                        removeRect.x     = rect.xMax + 2 + kButtonWidth;
                        if (GUI.Button(removeRect, new GUIContent("x", "Remove item"), EditorStyles.miniButtonRight))
                        {
                            list.RemoveAt(i);
                            break;
                        }
                        //rect.width -= kButtonWidth;
                        list[i]     = DoField(ref position, ref rect, list[i].GetType(), list[i]);
                        position.y += 17;
                    }
                }
            }

            return(value);
        }