Ejemplo n.º 1
0
        public EnumPropertyEditor(IPropertyEditorParams editorParams) : base(editorParams)
        {
            Selector            = editorParams.DropDownListFactory();
            Selector.LayoutCell = new LayoutCell(Alignment.Center);
            EditorContainer.AddNode(Selector);
            var propType      = editorParams.PropertyInfo.PropertyType;
            var fields        = propType.GetFields(BindingFlags.Public | BindingFlags.Static);
            var allowedFields = fields.Where(f => !Attribute.IsDefined((MemberInfo)f, typeof(TangerineIgnoreAttribute)));

            foreach (var field in allowedFields)
            {
                Selector.Items.Add(new CommonDropDownList.Item(field.Name, field.GetValue(null)));
            }
            Selector.Changed += a => {
                if (a.ChangedByUser)
                {
                    SetProperty((T)Selector.Items[a.Index].Value);
                }
            };
            Selector.AddChangeWatcher(CoalescedPropertyValue(), v => Selector.Value = v);
        }
Ejemplo n.º 2
0
        public FontPropertyEditor(IPropertyEditorParams editorParams) : base(editorParams)
        {
            selector            = editorParams.DropDownListFactory();
            selector.LayoutCell = new LayoutCell(Alignment.Center);
            ContainerWidget.AddNode(selector);
            var propType = editorParams.PropertyInfo.PropertyType;
            var items    = AssetBundle.Current.EnumerateFiles("Fonts").
                           Where(i => i.EndsWith(".fnt") || i.EndsWith(".tft")).
                           Select(i => new DropDownList.Item(Path.ChangeExtension(Path.GetFileName(i), null)));

            foreach (var i in items)
            {
                selector.Items.Add(i);
            }
            selector.Text     = GetFontName(CoalescedPropertyValue().GetValue());
            selector.Changed += a => {
                SetProperty(new SerializableFont((string)a.Value));
            };
            selector.AddChangeWatcher(CoalescedPropertyValue(), i => {
                selector.Text = GetFontName(i);
            });
        }
Ejemplo n.º 3
0
        public InstancePropertyEditor(IPropertyEditorParams editorParams, Action <Widget> OnValueChanged) : base(editorParams)
        {
            Selector            = editorParams.DropDownListFactory();
            Selector.LayoutCell = new LayoutCell(Alignment.Center);
            EditorContainer.AddNode(Selector);
            var propertyType     = typeof(T);
            var meta             = Yuzu.Metadata.Meta.Get(editorParams.Type, Serialization.YuzuCommonOptions);
            var propertyMetaItem = meta.Items.Where(i => i.Name == editorParams.PropertyName).FirstOrDefault();

            if (propertyMetaItem != null)
            {
                var defaultValue         = propertyMetaItem.GetValue(meta.Default);
                var resetToDefaultButton = new ToolbarButton(IconPool.GetTexture("Tools.Revert"))
                {
                    Clicked = () => { SetProperty(defaultValue); }
                };
                EditorContainer.AddNode(resetToDefaultButton);
                Selector.AddChangeWatcher(CoalescedPropertyValue(), v => {
                    resetToDefaultButton.Visible = !Equals(v, defaultValue);
                });
            }
            if (!propertyType.IsInterface)
            {
                Selector.Items.Add(new CommonDropDownList.Item(propertyType.Name, propertyType));
            }
            // TODO: invalidate cache on loading new assemblies at runtime
            if (!derivedTypesCache.TryGetValue(propertyType, out List <Type> derivedTypes))
            {
                derivedTypes = new List <Type>();
                foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                {
                    var assignables = assembly
                                      .GetTypes()
                                      .Where(t =>
                                             !t.IsInterface &&
                                             t.GetCustomAttribute <TangerineIgnoreAttribute>(false) == null &&
                                             t != propertyType &&
                                             propertyType.IsAssignableFrom(t));
                    foreach (var type in assignables)
                    {
                        derivedTypes.Add(type);
                    }
                }
                derivedTypesCache.Add(propertyType, derivedTypes);
            }
            foreach (var t in derivedTypes)
            {
                Selector.Items.Add(new CommonDropDownList.Item(t.Name, t));
            }
            Selector.Changed += a => {
                if (a.ChangedByUser)
                {
                    Type type = (Type)Selector.Items[a.Index].Value;
                    SetProperty <object>((_) => type != null ? Activator.CreateInstance(type) : null);
                }
            };
            Selector.AddChangeWatcher(CoalescedPropertyValue(), v => {
                OnValueChanged?.Invoke(ExpandableContent);
                Selector.Value = v?.GetType();
            });
        }