private async Task <DependencyObject> GetEditorForType(object originalObject, object value, Type memberType, string propertyPath, string memberName, Type declaringTypeToIgnore)
        {
            DependencyObject result = null;

            if (memberType.IsEnum)
            {
                var editor = new EnumEditor {
                    EnumType     = memberType,
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = value
                };

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <object> >(e => this.UpdateProperty(e.PropertyName, value, e.Value, originalObject, editor), true);
                result = editor;
            }
            else if (memberType == typeof(string))
            {
                var editor = new StringEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = (string)value
                };

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <string> >(e => this.UpdateProperty(e.PropertyName, (string)value, e.Value, originalObject, editor), true);
                result = editor;
            }
            else if (memberType == typeof(int))
            {
                var editor = new IntEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = (int)value
                };

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <int> >(e => this.UpdateProperty(e.PropertyName, (int)value, e.Value, originalObject, editor), true);
                result = editor;
            }
            else if (memberType == typeof(float))
            {
                var editor = new FloatEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = (float)value
                };

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <float> >(e => this.UpdateProperty(e.PropertyName, (float)value, e.Value, originalObject, editor), true);
                result = editor;
            }
            else if (memberType == typeof(bool))
            {
                var editor = new BoolEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = (bool)value
                };

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <bool> >(e => this.UpdateProperty(e.PropertyName, (bool)value, e.Value, originalObject, editor), true);
                result = editor;
            }
            else if (memberType == typeof(Vector2))
            {
                var editor = new VectorEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = (Vector2)value
                };

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <Vector2> >(e => this.UpdateProperty(e.PropertyName, (Vector2)value, e.Value, originalObject, editor), true);
                result = editor;
            }
            else if (memberType == typeof(Microsoft.Xna.Framework.Point))
            {
                var editor = new PointEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = (Microsoft.Xna.Framework.Point)value
                };

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <Microsoft.Xna.Framework.Point> >(e => this.UpdateProperty(e.PropertyName, (Microsoft.Xna.Framework.Point)value, e.Value, originalObject, editor), true);
                result = editor;
            }
            else if (memberType == typeof(Microsoft.Xna.Framework.Color))
            {
                var editor = new ColorEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = (Microsoft.Xna.Framework.Color)value
                };

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <Microsoft.Xna.Framework.Color> >(e => this.UpdateProperty(e.PropertyName, (Microsoft.Xna.Framework.Color)value, e.Value, originalObject, editor), true);
                result = editor;
            }
            else if (memberType == typeof(Sprite))
            {
                var editor = new SpriteEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = value as Sprite
                };

                if (value is Sprite sprite)
                {
                    var spriteWrappers = this._projectService.CurrentProject.AssetFolder.GetAssetsOfType <SpriteWrapper>();
                    editor.SpriteWrapper = spriteWrappers.FirstOrDefault(x => x.Sprite.Id == sprite.Id);
                }

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <Sprite> >(e => this.UpdateProperty(e.PropertyName, value as Sprite, e.Value, originalObject, editor), true);
                editor.SelectSpriteCommand = new RelayCommand(() => {
                    var asset = this._dialogService.ShowSelectAssetDialog(this._projectService.CurrentProject, AssetType.Image | AssetType.Sprite, AssetType.Sprite);
                    if (asset is SpriteWrapper spriteWrapper)
                    {
                        editor.SpriteWrapper = spriteWrapper;
                    }
                }, true);

                result = editor;
            }
            else if (memberType == typeof(AudioClip))
            {
                var editor = new AudioClipEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = value as AudioClip
                };

                editor.ValueChangedCommand    = new RelayCommand <EditableValueChangedEventArgs <AudioClip> >(e => this.UpdateProperty(e.PropertyName, value as AudioClip, e.Value, originalObject, editor), true);
                editor.SelectAudioClipCommand = new RelayCommand(() => {
                    var asset = this._dialogService.ShowSelectAssetDialog(this._projectService.CurrentProject, AssetType.Audio, AssetType.Audio);
                    if (asset is AudioAsset audioAsset)
                    {
                        editor.Value = audioAsset.AudioClip;
                    }
                }, true);

                result = editor;
            }
            else if (memberType == typeof(SpriteAnimation))
            {
                var editor = new SpriteAnimationEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = value as SpriteAnimation
                };

                if (value is SpriteAnimation animation)
                {
                    var spriteAnimationAssets = this._projectService.CurrentProject.AssetFolder.GetAssetsOfType <SpriteAnimationAsset>();
                    editor.Asset = spriteAnimationAssets.FirstOrDefault(x => x.SavableValue.Id == animation.Id);
                }

                editor.ValueChangedCommand          = new RelayCommand <EditableValueChangedEventArgs <SpriteAnimation> >(e => this.UpdateProperty(e.PropertyName, value as SpriteAnimation, e.Value, originalObject, editor), true);
                editor.SelectSpriteAnimationCommand = new RelayCommand(() => {
                    var asset = this._dialogService.ShowSelectAssetDialog(this._projectService.CurrentProject, AssetType.SpriteAnimation, AssetType.SpriteAnimation);
                    if (asset is SpriteAnimationAsset spriteAnimationAsset)
                    {
                        editor.Asset = spriteAnimationAsset;
                        editor.Value = spriteAnimationAsset.SavableValue;
                    }
                }, true);

                result = editor;
            }
            else if (memberType == typeof(Font))
            {
                var editor = new FontEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = value as Font
                };

                if (value is Font font)
                {
                    var fontAssets = this._projectService.CurrentProject.AssetFolder.GetAssetsOfType <FontAsset>();
                    editor.Asset = fontAssets.FirstOrDefault(x => x.SavableValue.Id == font.Id);
                }

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <Font> >(e => this.UpdateProperty(e.PropertyName, value as Font, e.Value, originalObject, editor), true);
                editor.SelectFontCommand   = new RelayCommand(() => {
                    var asset = this._dialogService.ShowSelectAssetDialog(this._projectService.CurrentProject, AssetType.Font, AssetType.Font);
                    if (asset is FontAsset fontAsset)
                    {
                        editor.Asset = fontAsset;
                        editor.Value = fontAsset.SavableValue;
                    }
                }, true);

                result = editor;
            }
            else if (memberType == typeof(Collider))
            {
                var colliderTypes = await this._assemblyService.LoadTypes(typeof(Collider));

                colliderTypes.Remove(typeof(PolygonCollider)); // TODO: Eventually allow PolygonCollider.
                var editor = new ColliderEditor {
                    ColliderTypes = colliderTypes,
                    PropertyName  = propertyPath,
                    Title         = memberName,
                    Value         = (Collider)value
                };

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <Collider> >(e => this.UpdateProperty(e.PropertyName, (Collider)value, e.Value, originalObject, editor), true);
                result = editor;
            }
            else if (memberType == typeof(LineCollider))
            {
                var colliderTypes = await this._assemblyService.LoadTypes(typeof(Collider));

                var editor = new LineColliderEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = (LineCollider)value
                };

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <LineCollider> >(e => this.UpdateProperty(e.PropertyName, (LineCollider)value, e.Value, originalObject, editor), true);
                result = editor;
            }
            else if (memberType == typeof(RectangleCollider))
            {
                var colliderTypes = await this._assemblyService.LoadTypes(typeof(Collider));

                var editor = new RectangleColliderEditor {
                    PropertyName = propertyPath,
                    Title        = memberName,
                    Value        = (RectangleCollider)value
                };

                editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <RectangleCollider> >(e => this.UpdateProperty(e.PropertyName, (RectangleCollider)value, e.Value, originalObject, editor), true);
                result = editor;
            }
            else if (memberType.IsSubclassOf(typeof(BaseComponent)))
            {
                // TODO: Allow user to select a component.
            }
            else
            {
                // TODO: I don't know, this should probably work when value is null
                if (value != null)
                {
                    var editor = new GenericValueEditor {
                        DeclaringType = declaringTypeToIgnore,
                        PropertyName  = propertyPath,
                        Title         = memberName,
                        Value         = value
                    };

                    editor.ValueChangedCommand = new RelayCommand <EditableValueChangedEventArgs <object> >(e => this.UpdateProperty(e.PropertyName, value, e.Value, originalObject, editor), true);
                    result = editor;
                }
            }

            return(result);
        }
Beispiel #2
0
        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            string Property = e.Link.LinkData as string;

            PropertyInfo info = action.GetType().GetProperty(Property);

            if (info == null)
            {
                MessageBox.Show("Property " + Property + " Does not exist for " + action.GetType().Name);
                return;
            }

            DialogResult result = DialogResult.Cancel;

            string valueResult = "";

            if (info.PropertyType == typeof(NumberValue))
            {
                VariableEditor editor = new VariableEditor();
                if (Variables != null)
                {
                    editor.SetVariable(Variables.Select(x => x.Name));
                }
                else
                {
                    editor.SetVariable(new string[] { });
                }
                NumberValue val = info.GetMethod.Invoke(action, new object[] { }) as NumberValue;
                editor.SetDefault(val == null ? "" : val.ToString());
                editor.Text = "Variable Editor - " + Property;

                result = editor.ShowDialog();

                if (result == DialogResult.OK)
                {
                    info.SetMethod.Invoke(action, new object[] { new NumberValue(valueResult) });
                }
            }
            if (info.PropertyType == typeof(TargetKey))
            {
                TargetKeyEditor editor = new TargetKeyEditor();
                editor.SetPresets(ActionContext);
                editor.VariableList = Variables;

                TargetKey t = info.GetMethod.Invoke(action, new object[] { }) as TargetKey;
                if (t == null)
                {
                    t = new TargetKey();
                }
                editor.Target = t;

                result = editor.ShowDialog();

                if (result == DialogResult.OK)
                {
                    TargetKey target = editor.Target;
                    info.SetMethod.Invoke(action, new object[] { target });
                }
            }
            //It's an Enum!
            if (typeof(Enum).IsAssignableFrom(info.PropertyType))
            {
                Enum enumValue = info.GetMethod.Invoke(action, new object[] { }) as Enum;


                //Find out if it's a flag and open the flag editor
                if (info.PropertyType.GetCustomAttribute <FlagsAttribute>() != null)
                {
                    FlagCheckBoxEditor editor = new FlagCheckBoxEditor();
                    editor.EnumValue = enumValue;

                    result = editor.ShowDialog();

                    enumValue = editor.EnumValue;
                }
                else
                {
                    EnumEditor editor = new EnumEditor();
                    editor.EnumValue = enumValue;

                    result = editor.ShowDialog();

                    enumValue = editor.EnumValue;
                }

                if (result == DialogResult.OK)
                {
                    info.SetMethod.Invoke(action, new object[] { enumValue });

                    valueResult = enumValue.ToString();
                }
            }

            if (typeof(bool) == info.PropertyType)
            {
                bool val = (bool)info.GetMethod.Invoke(action, new object[] { });

                BoolEditor editor = new BoolEditor();
                editor.Value = val;

                result = editor.ShowDialog();

                if (result == DialogResult.OK)
                {
                    info.SetMethod.Invoke(action, new object[] { editor.Value });
                }
            }
            if (typeof(string) == info.PropertyType)
            {
                string val = (string)info.GetMethod.Invoke(action, new object[] { });

                TextPrompt editor = new TextPrompt();

                editor.Text       = Property;
                editor.PromptText = val;

                result = editor.ShowDialog();
                if (result == DialogResult.OK)
                {
                    info.SetMethod.Invoke(action, new object[] { editor.PromptText });
                }
            }
            if (typeof(ActionCollection) == info.PropertyType)
            {
                ActionCollection actions = info.GetMethod.Invoke(action, new object[] { }) as ActionCollection;

                ActionListEditor editor = new ActionListEditor();
                if (actions == null)
                {
                    actions = new ActionCollection(Property);
                }

                editor.ActionContext = ActionContext;
                editor.Variables     = Variables;
                editor.Actions       = actions;

                result = editor.ShowDialog();

                if (result == DialogResult.OK)
                {
                    info.SetMethod.Invoke(action, new object[] { editor.Actions });
                }
            }


            if (result == DialogResult.OK)
            {
                UpdateLinkTexts();
            }

            if (LinkClicked != null)
            {
                LinkClicked.Invoke(this, new LinkClickedEventArgs(e.Link.LinkData as string));
            }
        }
Beispiel #3
0
        protected virtual EditorItem GetEditorItem(CustomDataType type, string custom, PropertyInfo property)
        {
            RequiredAttribute required = property.GetCustomAttributes(typeof(RequiredAttribute), true).FirstOrDefault() as RequiredAttribute;
            EditorItem        item;

            switch (type)
            {
            case CustomDataType.Boolean:
                item = new BoolEditor(Frame);
                break;

            case CustomDataType.Currency:
                item = new CurrencyEditor(Frame);
                break;

            case CustomDataType.Date:
                item = new DateEditor(Frame);
                break;

            case CustomDataType.DateTime:
                item = new DateTimeEditor(Frame);
                break;

            case CustomDataType.Default:
                item = new DefaultEditor(Frame);
                if (required != null)
                {
                    ((DefaultEditor)item).IsAllowdEmpty = required.AllowEmptyStrings;
                }
                break;

            case CustomDataType.EmailAddress:
                item = new EmailAddressEditor(Frame);
                break;

            case CustomDataType.Html:
                item = new HtmlEditor(Frame);
                break;

            case CustomDataType.ImageUrl:
                item = new ImageUrlEditor(Frame);
                break;

            case CustomDataType.Image:
                item = new ImageEditor(Frame);
                break;

            case CustomDataType.Integer:
                item = new IntegerEditor(Frame);
                break;

            case CustomDataType.MultilineText:
                item = new MultilineTextEditor(Frame);
                if (required != null)
                {
                    ((MultilineTextEditor)item).IsAllowdEmpty = required.AllowEmptyStrings;
                }
                break;

            case CustomDataType.Number:
                item = new NumberEditor(Frame);
                break;

            case CustomDataType.Password:
                item = new PasswordEditor(Frame);
                break;

            case CustomDataType.PhoneNumber:
                item = new PhoneNumberEditor(Frame);
                break;

            case CustomDataType.Sex:
                item = new SexEditor(Frame);
                break;

            case CustomDataType.Text:
                item = new DefaultEditor(Frame);
                break;

            case CustomDataType.Time:
                item = new TimeEditor(Frame);
                break;

            case CustomDataType.Url:
                item = new UrlEditor(Frame);
                break;

            default:
                switch (custom)
                {
                case "Enum":
                    item = GetEnumEditorItem(property.PropertyType);
                    break;

                case "Entity":
                    item = new EntityEditor(Frame, property.PropertyType);
                    break;

                case "Collection":
                    item = new CollectionEditor(Frame, property.PropertyType.GetGenericArguments()[0]);
                    break;

                default:
                    throw new NotSupportedException("不支持自定义类型编辑器。");
                }
                break;
            }
            if (required != null)
            {
                item.IsRequired = true;
            }
            return(item);
        }