protected internal static IEnumerable <IGrouping <Type, Control> > LocalPropertyList(object obj, IViewVariablesManagerInternal vvm,
                                                                                             IRobustSerializer robustSerializer)
        {
            var styleOther = false;
            var type       = obj.GetType();

            var members = new List <(MemberInfo, VVAccess, object?value, Action <object?, bool> onValueChanged, Type)>();

            foreach (var fieldInfo in type.GetAllFields())
            {
                if (!ViewVariablesUtility.TryGetViewVariablesAccess(fieldInfo, out var access))
                {
                    continue;
                }

                members.Add((fieldInfo, (VVAccess)access, fieldInfo.GetValue(obj), (v, _) => fieldInfo.SetValue(obj, v),
                             fieldInfo.FieldType));
            }

            foreach (var propertyInfo in type.GetAllProperties())
            {
                if (!ViewVariablesUtility.TryGetViewVariablesAccess(propertyInfo, out var access))
                {
                    continue;
                }

                if (!propertyInfo.IsBasePropertyDefinition())
                {
                    continue;
                }

                members.Add((propertyInfo, (VVAccess)access, propertyInfo.GetValue(obj),
                             (v, _) => propertyInfo.GetSetMethod(true) !.Invoke(obj, new[] { v }), propertyInfo.PropertyType));
            }

            var groupedSorted = members
                                .OrderBy(p => p.Item1.Name)
                                .GroupBy(p => p.Item1.DeclaringType !, tuple =>
            {
                var(memberInfo, access, value, onValueChanged, memberType) = tuple;
                var data = new ViewVariablesBlobMembers.MemberData
                {
                    Editable   = access == VVAccess.ReadWrite,
                    Name       = memberInfo.Name,
                    Type       = memberType.AssemblyQualifiedName,
                    TypePretty = TypeAbbreviation.Abbreviate(memberType),
                    Value      = value
                };

                var propertyEdit = new ViewVariablesPropertyControl(vvm, robustSerializer);
                propertyEdit.SetStyle(styleOther = !styleOther);
                var editor             = propertyEdit.SetProperty(data);
                editor.OnValueChanged += onValueChanged;
                return(propertyEdit);
            })
                                .OrderByDescending(p => p.Key, TypeHelpers.TypeInheritanceComparer);

            return(groupedSorted);
        }
        protected internal static IEnumerable <Control> LocalPropertyList(object obj, IViewVariablesManagerInternal vvm,
                                                                          IResourceCache resCache)
        {
            var styleOther = false;
            var type       = obj.GetType();

            var members = new List <(MemberInfo, VVAccess, object value, Action <object> onValueChanged, Type)>();

            foreach (var fieldInfo in type.GetAllFields())
            {
                var attr = fieldInfo.GetCustomAttribute <ViewVariablesAttribute>();
                if (attr == null)
                {
                    continue;
                }

                members.Add((fieldInfo, attr.Access, fieldInfo.GetValue(obj), v => fieldInfo.SetValue(obj, v),
                             fieldInfo.FieldType));
            }

            foreach (var propertyInfo in type.GetAllProperties())
            {
                var attr = propertyInfo.GetCustomAttribute <ViewVariablesAttribute>();
                if (attr == null)
                {
                    continue;
                }

                members.Add((propertyInfo, attr.Access, propertyInfo.GetValue(obj),
                             v => propertyInfo.GetSetMethod(true).Invoke(obj, new[] { v }), propertyInfo.PropertyType));
            }

            members.Sort((a, b) => string.Compare(a.Item1.Name, b.Item1.Name, StringComparison.Ordinal));

            foreach (var(memberInfo, access, value, onValueChanged, memberType) in members)
            {
                var data = new ViewVariablesBlobMembers.MemberData
                {
                    Editable   = access == VVAccess.ReadWrite,
                    Name       = memberInfo.Name,
                    Type       = memberType.AssemblyQualifiedName,
                    TypePretty = memberType.ToString(),
                    Value      = value
                };

                var propertyEdit = new ViewVariablesPropertyControl(vvm, resCache);
                propertyEdit.SetStyle(styleOther = !styleOther);
                var editor = propertyEdit.SetProperty(data);
                editor.OnValueChanged += onValueChanged;
                // TODO: should this maybe not be hardcoded?
                if (editor is ViewVariablesPropertyEditorReference refEditor)
                {
                    refEditor.OnPressed += () => vvm.OpenVV(data.Value);
                }

                yield return(propertyEdit);
            }
        }
Esempio n. 3
0
        public ViewVariablesPropertyEditor SetProperty(ViewVariablesBlobMembers.MemberData member)
        {
            NameLabel.Text = member.Name;
            var type = Type.GetType(member.Type);

            _bottomLabel.Text = $"Type: {member.TypePretty}";
            ViewVariablesPropertyEditor editor;

            if (type == null)
            {
                // Type is server-side only.
                // Info whether it's reference or value type can be figured out from the sent value.
                if (member.Value is ViewVariablesBlobMembers.ServerValueTypeToken)
                {
                    // Value type, just display it stringified read-only.
                    editor = new ViewVariablesPropertyEditorDummy();
                }
                else
                {
                    // Has to be a reference type at this point.
                    DebugTools.Assert(member.Value is ViewVariablesBlobMembers.ReferenceToken || member.Value == null);
                    editor = _viewVariablesManager.PropertyFor(typeof(object));
                }
            }
            else
            {
                editor = _viewVariablesManager.PropertyFor(type);
            }

            var view = editor.Initialize(member.Value, !member.Editable);

            if (view.SizeFlagsHorizontal != SizeFlags.FillExpand)
            {
                NameLabel.SizeFlagsHorizontal = SizeFlags.FillExpand;
            }

            NameLabel.CustomMinimumSize = new Vector2(150, 0);
            TopContainer.AddChild(view);

            /*
             * _beingEdited = obj;
             * _editedProperty = propertyInfo;
             * DebugTools.Assert(propertyInfo.DeclaringType != null);
             * DebugTools.Assert(propertyInfo.DeclaringType.IsInstanceOfType(obj));
             *
             * var attr = propertyInfo.GetCustomAttribute<ViewVariablesAttribute>();
             * DebugTools.Assert(attr != null);
             * NameLabel.Text = propertyInfo.Name;
             *
             * _bottomLabel.Text = $"Type: {propertyInfo.PropertyType.FullName}";
             *
             * var editor = vvm.PropertyFor(propertyInfo.PropertyType);
             * var value = propertyInfo.GetValue(obj);
             *
             * var view = editor.Initialize(value, attr.Access != VVAccess.ReadWrite);
             * if (view.SizeFlagsHorizontal != SizeFlags.FillExpand)
             * {
             *  NameLabel.SizeFlagsHorizontal = SizeFlags.FillExpand;
             * }
             * NameLabel.CustomMinimumSize = new Vector2(150, 0);
             * TopContainer.AddChild(view);
             * editor.OnValueChanged += v => { propertyInfo.SetValue(obj, v); };
             */
            return(editor);
        }