Esempio n. 1
0
        public InstancePropertyEditor(IPropertyEditorParams editorParams, Action <Widget> onValueChanged) : base(editorParams)
        {
            Selector            = editorParams.DropDownListFactory();
            Selector.LayoutCell = new LayoutCell(Alignment.Center);
            var propertyType = typeof(T);
            var meta         = Yuzu.Metadata.Meta.Get(editorParams.Type, InternalPersistence.Instance.YuzuCommonOptions);

            if (!propertyType.IsInterface && !propertyType.IsAbstract)
            {
                Selector.Items.Add(new CommonDropDownList.Item(propertyType.Name, propertyType));
            }
            foreach (var t in DerivedTypesCache.GetDerivedTypesFor(propertyType))
            {
                Selector.Items.Add(new CommonDropDownList.Item(t.Name, t));
            }
            EditorContainer.AddChangeLateWatcher(CoalescedPropertyValue(
                                                     comparator: (t1, t2) => t1 == null && t2 == null || t1 != null && t2 != null && t1.GetType() == t2.GetType()),
                                                 v => {
                onValueChanged?.Invoke(ExpandableContent);
                if (v.IsDefined)
                {
                    Selector.Value = v.Value?.GetType();
                }
                else
                {
                    Selector.Text = ManyValuesText;
                }
            }
                                                 );
            Selector.Changed += a => {
                if (a.ChangedByUser)
                {
                    var type = (Type)Selector.Items[a.Index].Value;
                    SetProperty <object>((_) => type != null ? Activator.CreateInstance(type) : null);
                }
            };
            var propertyMetaItem     = meta.Items.FirstOrDefault(i => i.Name == editorParams.PropertyName);
            var defaultValue         = propertyMetaItem?.GetValue(meta.Default);
            var resetToDefaultButton = new ToolbarButton(IconPool.GetTexture("Tools.Revert"))
            {
                Clicked = () => SetProperty(Cloner.Clone(defaultValue))
            };

            if (Selector.Items.Count == 1)
            {
                var t = Selector.Items[0].Value as Type;
                var b = new ToolbarButton("Create")
                {
                    TabTravesable = new TabTraversable(),
                    LayoutCell    = new LayoutCell(Alignment.LeftCenter),
                    Padding       = new Thickness(left: 5.0f),
                    HitTestTarget = true,
                    MinWidth      = 0,
                    MaxWidth      = float.PositiveInfinity
                };
                b.Clicked = () => {
                    b.Visible = false;
                    SetProperty <object>(_ => t != null ? Activator.CreateInstance(t) : null);
                    onValueChanged?.Invoke(ExpandableContent);
                    Expanded = true;
                };
                var value = CoalescedPropertyValue().GetValue();
                b.Visible = Equals(value.Value, defaultValue);
                resetToDefaultButton.Clicked = () => {
                    b.Visible = true;
                    SetProperty(defaultValue);
                    onValueChanged?.Invoke(ExpandableContent);
                };
                EditorContainer.AddNode(b);
                EditorContainer.AddNode(Spacer.HStretch());
                onValueChanged?.Invoke(ExpandableContent);
            }
            else
            {
                EditorContainer.Nodes.Insert(0, Selector);
            }
            EditorContainer.AddNode(resetToDefaultButton);
            EditorContainer.AddChangeLateWatcher(CoalescedPropertyValue(), v => {
                resetToDefaultButton.Visible = !Equals(v.Value, defaultValue);
            });
        }