Пример #1
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();
            }
        }
Пример #2
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);
        }