예제 #1
0
        public static Tuple <UIElement, UIElement> WrapIt(UIElement parent, ref int top, PropertyFieldWrapper memberInfo, object item, int order, object list = null, Type arrayType = null, int index = -1)
        {
            int  elementHeight = 0;
            Type type          = memberInfo.Type;

            if (arrayType != null)
            {
                type = arrayType;
            }
            UIElement e;

            // TODO: Other common structs? -- Rectangle, Point
            CustomModConfigItemAttribute customUI = ConfigManager.GetCustomAttribute <CustomModConfigItemAttribute>(memberInfo, null, null);

            if (customUI != null)
            {
                Type customUIType = customUI.t;
                if (typeof(ConfigElement).IsAssignableFrom(customUIType))
                {
                    ConstructorInfo ctor = customUIType.GetConstructor(new Type[0]);
                    if (ctor != null)
                    {
                        object instance = ctor.Invoke(new object[0]);
                        e = instance as UIElement;
                    }
                    else
                    {
                        e = new UIText($"{customUIType.Name} specified via CustomModConfigItem for {memberInfo.Name} does not have an empty constructor.");
                    }
                }
                else
                {
                    e = new UIText($"{customUIType.Name} specified via CustomModConfigItem for {memberInfo.Name} does not inherit from ConfigElement.");
                }
            }
            else if (item.GetType() == typeof(HeaderAttribute))
            {
                e = new HeaderElement((string)memberInfo.GetValue(item));
            }
            else if (type == typeof(ItemDefinition))
            {
                e = new ItemDefinitionElement();
            }
            else if (type == typeof(ProjectileDefinition))
            {
                e = new ProjectileDefinitionElement();
            }
            else if (type == typeof(NPCDefinition))
            {
                e = new NPCDefinitionElement();
            }
            else if (type == typeof(PrefixDefinition))
            {
                e = new PrefixDefinitionElement();
            }
            else if (type == typeof(Color))
            {
                e = new ColorElement();
            }
            else if (type == typeof(Vector2))
            {
                e = new Vector2Element();
            }
            else if (type == typeof(bool))             // isassignedfrom?
            {
                e = new BooleanElement();
            }
            else if (type == typeof(float))
            {
                e = new FloatElement();
            }
            else if (type == typeof(byte))
            {
                e = new ByteElement();
            }
            else if (type == typeof(uint))
            {
                e = new UIntElement();
            }
            else if (type == typeof(int))
            {
                SliderAttribute sliderAttribute = ConfigManager.GetCustomAttribute <SliderAttribute>(memberInfo, item, list);
                if (sliderAttribute != null)
                {
                    e = new IntRangeElement();
                }
                else
                {
                    e = new IntInputElement();
                }
            }
            else if (type == typeof(string))
            {
                OptionStringsAttribute ost = ConfigManager.GetCustomAttribute <OptionStringsAttribute>(memberInfo, item, list);
                if (ost != null)
                {
                    e = new StringOptionElement();
                }
                else
                {
                    e = new StringInputElement();
                }
            }
            else if (type.IsEnum)
            {
                if (list != null)
                {
                    e = new UIText($"{memberInfo.Name} not handled yet ({type.Name}).");
                }
                else
                {
                    e = new EnumElement();
                }
            }
            else if (type.IsArray)
            {
                e = new ArrayElement();
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>))
            {
                e = new ListElement();
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(HashSet <>))
            {
                e = new SetElement();
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary <,>))
            {
                e = new DictionaryElement();
            }
            else if (type.IsClass)
            {
                e = new ObjectElement(/*, ignoreSeparatePage: ignoreSeparatePage*/);
            }
            else if (type.IsValueType && !type.IsPrimitive)
            {
                e = new UIText($"{memberInfo.Name} not handled yet ({type.Name}) Structs need special UI.");
                //e.Top.Pixels += 6;
                e.Height.Pixels += 6;
                e.Left.Pixels   += 4;

                object subitem = memberInfo.GetValue(item);
            }
            else
            {
                e              = new UIText($"{memberInfo.Name} not handled yet ({type.Name})");
                e.Top.Pixels  += 6;
                e.Left.Pixels += 4;
            }
            if (e != null)
            {
                if (e is ConfigElement configElement)
                {
                    configElement.Bind(memberInfo, item, (IList)list, index);
                    configElement.OnBind();
                }
                e.Recalculate();
                elementHeight = (int)e.GetOuterDimensions().Height;

                var container = GetContainer(e, index == -1 ? order : index);
                container.Height.Pixels = elementHeight;
                UIList uiList = parent as UIList;
                if (uiList != null)
                {
                    uiList.Add(container);
                    float p = uiList.GetTotalHeight();
                }
                else
                {
                    // Only Vector2 and Color use this I think, but modders can use the non-UIList approach for custom UI and layout.
                    container.Top.Pixels   = top;
                    container.Width.Pixels = -20;
                    container.Left.Pixels  = 20;
                    top += elementHeight + 4;
                    parent.Append(container);
                    parent.Height.Set(top, 0);
                }
                var tuple = new Tuple <UIElement, UIElement>(container, e);
                if (parent == Interface.modConfig.mainConfigList)
                {
                    Interface.modConfig.mainConfigItems.Add(tuple);
                }

                return(tuple);
            }
            return(null);
        }
예제 #2
0
        public static Tuple <UIElement, UIElement> WrapIt(UIElement parent, ref int top, PropertyFieldWrapper memberInfo, object item, int order, object array = null, Type arrayType = null, int index = -1)
        {
            int  elementHeight = 0;
            Type type          = memberInfo.Type;

            if (arrayType != null)
            {
                type = arrayType;
            }
            UIElement e = null;

            // TODO: Other common structs? -- Rectangle, Point
            CustomModConfigItemAttribute customUI = ConfigManager.GetCustomAttribute <CustomModConfigItemAttribute>(memberInfo, null, null);

            if (customUI != null)
            {
                Type            customUIType = customUI.t;
                ConstructorInfo ctor         = customUIType.GetConstructor(new[] { typeof(PropertyFieldWrapper), typeof(object), typeof(int), typeof(IList), typeof(int) });
                if (ctor != null)
                {
                    object[] arguments = new object[] { memberInfo, item, 0, array, index };
                    object   instance  = ctor.Invoke(arguments);
                    e = instance as UIElement;
                    if (e != null)
                    {
                        //e.Recalculate();
                        //elementHeight = (int)e.GetOuterDimensions().Height;
                        //elementHeight = 400; //e.GetHeight();
                    }
                    else
                    {
                        e = new UIText($"CustomUI for {memberInfo.Name} does not inherit from UIElement.");
                    }
                }
                else
                {
                    e = new UIText($"CustomUI for {memberInfo.Name} does not have the correct constructor.");
                }
            }
            else if (item.GetType() == typeof(HeaderAttribute))
            {
                //e = new UIText($"{memberInfo.GetValue(item)}", .4f, true);
                //e.SetPadding(6);
                e = new HeaderElement((string)memberInfo.GetValue(item));
            }
            else if (type == typeof(ItemDefinition))
            {
                e = new ItemDefinitionElement(memberInfo, item, (IList <ItemDefinition>)array, index);
            }
            else if (type == typeof(Color))
            {
                e = new ColorElement(memberInfo, item, (IList <Color>)array, index);
                //elementHeight = (int)(e as UIModConfigColorItem).GetHeight();
            }
            else if (type == typeof(Vector2))
            {
                e = new Vector2Element(memberInfo, item, (IList <Vector2>)array, index);
            }
            else if (type == typeof(bool))             // isassignedfrom?
            {
                e = new BooleanElement(memberInfo, item, (IList <bool>)array, index);
            }
            else if (type == typeof(float))
            {
                e = new FloatElement(memberInfo, item, (IList <float>)array, index);
            }
            else if (type == typeof(byte))
            {
                e = new ByteElement(memberInfo, item, (IList <byte>)array, index);
            }
            else if (type == typeof(uint))
            {
                e = new UIntElement(memberInfo, item, (IList <uint>)array, index);
            }
            else if (type == typeof(int))
            {
                RangeAttribute rangeAttribute = ConfigManager.GetCustomAttribute <RangeAttribute>(memberInfo, item, array);
                if (rangeAttribute != null)
                {
                    e = new IntRangeElement(memberInfo, item, (IList <int>)array, index);
                }
                else
                {
                    e = new IntInputElement(memberInfo, item, (IList <int>)array, index);
                }
            }
            else if (type == typeof(string))
            {
                OptionStringsAttribute ost = ConfigManager.GetCustomAttribute <OptionStringsAttribute>(memberInfo, item, array);
                if (ost != null)
                {
                    e = new StringOptionElement(memberInfo, item, (IList <string>)array, index);
                }
                else
                {
                    e = new StringInputElement(memberInfo, item, (IList <string>)array, index);
                }
            }
            else if (type.IsEnum)
            {
                if (array != null)
                {
                    e = new UIText($"{memberInfo.Name} not handled yet ({type.Name}).");
                }
                else
                {
                    e = new EnumElement(memberInfo, item);
                }
            }
            else if (type.IsArray)
            {
                e = new ArrayElement(memberInfo, item);
                //elementHeight = 225;
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>))
            {
                e = new ListElement(memberInfo, item);
                //elementHeight = 225;
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(HashSet <>))
            {
                e = new SetElement(memberInfo, item);
            }
            else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary <,>))
            {
                e = new DictionaryElement(memberInfo, item);
                //elementHeight = 300;
            }
            else if (type.IsClass)
            {
                e = new ObjectElement(memberInfo, item, (IList)array, index /*, ignoreSeparatePage: ignoreSeparatePage*/);
            }
            else if (type.IsValueType && !type.IsPrimitive)
            {
                e = new UIText($"{memberInfo.Name} not handled yet ({type.Name}) Structs need special UI.");
                //e.Top.Pixels += 6;
                e.Height.Pixels += 6;
                e.Left.Pixels   += 4;

                object subitem = memberInfo.GetValue(item);
            }
            else
            {
                e              = new UIText($"{memberInfo.Name} not handled yet ({type.Name})");
                e.Top.Pixels  += 6;
                e.Left.Pixels += 4;
            }
            if (e != null)
            {
                e.Recalculate();
                elementHeight = (int)e.GetOuterDimensions().Height;

                var container = GetContainer(e, index == -1 ? order : index);
                container.Height.Pixels = elementHeight;
                UIList list = parent as UIList;
                if (list != null)
                {
                    list.Add(container);
                    float p = list.GetTotalHeight();
                }
                else
                {
                    // Only Vector2 and Color use this I think, but modders can use the non-UIList approach for custom UI and layout.
                    container.Top.Pixels   = top;
                    container.Width.Pixels = -20;
                    container.Left.Pixels  = 20;
                    top += elementHeight + 4;
                    parent.Append(container);
                    parent.Height.Set(top, 0);
                }

                return(new Tuple <UIElement, UIElement>(container, e));
            }
            return(null);
        }