Пример #1
0
        public CustomField(MemberData data, object owner)
        {
            _label          = new GUIContent(data.name, data.description);
            this.memberName = data.member.Name;

            var prop = data.member as PropertyInfo;

            if (prop != null)
            {
                _itemType = prop.PropertyType;
                _item     = prop.GetValue(owner, null);
                _setter   = (item) => prop.SetValue(owner, item, null);
            }
            else
            {
                var field = data.member as FieldInfo;
                if (field != null)
                {
                    _itemType = field.FieldType;
                    _item     = field.GetValue(owner);
                    _setter   = (item) => field.SetValue(owner, item);
                }
                else
                {
                    throw new ArgumentException("Invalid reflected member type, only fields and properties are supported.");
                }
            }

            if (_item != null)
            {
                _editorItem = ReflectMaster.Reflect(_item);
            }
        }
Пример #2
0
        private void AddNew(Vector2 mousePos, AIInspectorState state)
        {
            if (_dictionary == null)
            {
                var dict = (IDictionary)Activator.CreateInstance(typeof(Dictionary <,>).MakeGenericType(_keyType, _valueType));
                _setter(dict);
            }

            if (_itemCreator == null)
            {
                _itemCreator = new ItemConstructor(_dictionary, _keyType, _valueType);
            }

            var itemWrapper = _itemCreator.Conctruct();

            _editorItems.Add(ReflectMaster.Reflect(itemWrapper));

            // We do not want the button click itself to count as a change.. same as above.
            GUI.changed = false;
        }
Пример #3
0
        public DictionaryField(MemberData data, object owner)
        {
            _owner          = owner;
            _label          = new GUIContent(data.name, data.description);
            this.memberName = data.member.Name;

            var prop = data.member as PropertyInfo;

            if (prop != null)
            {
                GetKeyValueTypes(prop.PropertyType);
                _dictionary = (IDictionary)prop.GetValue(owner, null);
                if (_dictionary == null)
                {
                    _setter = (dict) =>
                    {
                        _dictionary = dict;
                        prop.SetValue(owner, dict, null);
                        if (_itemCreator != null)
                        {
                            _itemCreator.SetParent(_dictionary);
                        }
                    };
                }
            }
            else
            {
                var field = data.member as FieldInfo;
                if (field != null)
                {
                    GetKeyValueTypes(field.FieldType);
                    _dictionary = (IDictionary)field.GetValue(owner);
                    if (_dictionary == null)
                    {
                        _setter = (dict) =>
                        {
                            _dictionary = dict;
                            field.SetValue(owner, dict);
                            if (_itemCreator != null)
                            {
                                _itemCreator.SetParent(_dictionary);
                            }
                        };
                    }
                }
                else
                {
                    throw new ArgumentException("Invalid reflected member type, only fields and properties are supported.");
                }
            }

            if (_dictionary != null)
            {
                var count = _dictionary.Count;
                _editorItems = new List <EditorItem>(count);
                _itemCreator = new ItemConstructor(_dictionary, _keyType, _valueType);

                foreach (var key in _dictionary.Keys)
                {
                    var item = _itemCreator.Conctruct(key, _dictionary[key]);
                    _editorItems.Add(ReflectMaster.Reflect(item));
                }
            }
            else
            {
                _editorItems = new List <EditorItem>();
            }
        }
Пример #4
0
        public void RenderField(AIInspectorState state)
        {
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField(_label);

            if (GUILayout.Button(SharedStyles.changeSelectionTooltip, SharedStyles.BuiltIn.changeButtonSmall))
            {
                Action <Type> cb = (selectedType) =>
                {
                    if (_itemType.IsGenericType && selectedType.IsGenericType)
                    {
                        var genArgs = _itemType.GetGenericArguments();
                        selectedType = selectedType.GetGenericTypeDefinition().MakeGenericType(genArgs);
                    }

                    var old = _item;
                    _item       = Activator.CreateInstance(selectedType);
                    _editorItem = ReflectMaster.Reflect(_item);
                    _setter(_item);

                    state.currentAIUI.undoRedo.Do(new CustomEditorFieldOperation(old, _item, _setter));
                    state.MarkDirty();
                };

                //We do not want the button click itself to count as a change.. same as above.
                GUI.changed = false;

                var screenPos = EditorGUIUtility.GUIToScreenPoint(Event.current.mousePosition);
                AIEntitySelectorWindow.Get(screenPos, _itemType, cb);
            }

            EditorGUILayout.EndHorizontal();

            bool doDelete = false;

            if (_item != null)
            {
                EditorGUILayout.BeginVertical("Box");
                EditorGUILayout.BeginHorizontal(SharedStyles.BuiltIn.listItemHeader);
                EditorGUILayout.LabelField(_editorItem.name, SharedStyles.BuiltIn.normalText);
                if (GUILayout.Button(SharedStyles.deleteTooltip, SharedStyles.BuiltIn.deleteButtonSmall))
                {
                    GUI.changed = false;

                    if (DisplayHelper.ConfirmDelete("item"))
                    {
                        doDelete = true;
                    }
                }

                EditorGUILayout.EndHorizontal();

                _editorItem.Render(state);
                EditorGUILayout.EndVertical();
            }

            EditorGUILayout.Separator();

            //We do the delete outside any layout stuff to ensure we don't get weird warnings.
            if (doDelete)
            {
                state.currentAIUI.undoRedo.Do(new CustomEditorFieldOperation(_item, null, _setter));
                _setter(null);
                _item       = null;
                _editorItem = null;
                state.MarkDirty();
            }
        }
Пример #5
0
        protected ListFieldBase(MemberData data, object owner, bool requiresSetter)
        {
            _owner          = owner;
            _label          = new GUIContent(data.name, data.description);
            this.memberName = data.member.Name;

            var prop = data.member as PropertyInfo;

            if (prop != null)
            {
                _reorderable = !prop.IsDefined <NotReorderableAttribute>(true);
                _itemType    = GetItemType(prop.PropertyType);
                _list        = (IList)prop.GetValue(owner, null);
                if (_list == null || requiresSetter)
                {
                    _setter = (list) =>
                    {
                        _list = list;
                        prop.SetValue(owner, list, null);
                        if (_simpleItemCreator != null)
                        {
                            _simpleItemCreator.SetParent(_list);
                        }
                    };
                }
            }
            else
            {
                var field = data.member as FieldInfo;
                if (field != null)
                {
                    _reorderable = !field.IsDefined <NotReorderableAttribute>(true);
                    _itemType    = GetItemType(field.FieldType);
                    _list        = (IList)field.GetValue(owner);
                    if (_list == null || requiresSetter)
                    {
                        _setter = (list) =>
                        {
                            _list = list;
                            field.SetValue(owner, list);
                            if (_simpleItemCreator != null)
                            {
                                _simpleItemCreator.SetParent(_list);
                            }
                        };
                    }
                }
                else
                {
                    throw new ArgumentException("Invalid reflected member type, only fields and properties are supported.");
                }
            }

            _isSimpleType = SerializationMaster.ConverterExists(_itemType) || SerializationMaster.StagerExists(_itemType);

            if (_list != null)
            {
                var count = _list.Count;
                _editorItems = new List <EditorItem>(count);

                if (_isSimpleType)
                {
                    _simpleItemCreator = new SimpleItemConstructor(_list, _itemType);

                    for (int i = 0; i < count; i++)
                    {
                        var item = _simpleItemCreator.Conctruct(i, _list[i]);
                        _editorItems.Add(ReflectMaster.Reflect(item));
                    }
                }
                else
                {
                    for (int i = 0; i < count; i++)
                    {
                        _editorItems.Add(ReflectMaster.Reflect(_list[i]));
                    }
                }
            }
            else
            {
                _editorItems = new List <EditorItem>();
            }
        }
Пример #6
0
        private void AddNew(Vector2 mousePos, AIInspectorState state)
        {
            if (_isSimpleType)
            {
                if (_list == null)
                {
                    _setter(CreateList());
                }

                if (_simpleItemCreator == null)
                {
                    _simpleItemCreator = new SimpleItemConstructor(_list, _itemType);
                }

                var item = Activator.CreateInstance(_itemType);
                DoAdd(item, state);

                var itemWrapper = _simpleItemCreator.Conctruct(_list.Count - 1, item);
                _editorItems.Add(ReflectMaster.Reflect(itemWrapper));
                return;
            }

            Action <Type[]> cb = (selectedTypes) =>
            {
                if (_list == null)
                {
                    _setter(CreateList());
                }

                using (state.currentAIUI.undoRedo.bulkOperation)
                {
                    for (int i = 0; i < selectedTypes.Length; i++)
                    {
                        var selectedType = selectedTypes[i];
                        if (_itemType.IsGenericType && selectedType.IsGenericType)
                        {
                            var genArgs = _itemType.GetGenericArguments();
                            selectedType = selectedType.GetGenericTypeDefinition().MakeGenericType(genArgs);
                        }

                        var item = Activator.CreateInstance(selectedType);
                        DoAdd(item, state);
                        _editorItems.Add(ReflectMaster.Reflect(item));

                        if (Application.isPlaying)
                        {
                            HandleVisualizerAdd(state, item);
                        }
                    }

                    state.MarkDirty();
                }
            };

            // We do not want the button click itself to count as a change.. same as above.
            GUI.changed = false;

            var screenPos = EditorGUIUtility.GUIToScreenPoint(mousePos);

            AIEntitySelectorWindow.Get(screenPos, _itemType, cb);
        }