Beispiel #1
0
        private static IViewItem CreateViewItem(IContainerViewItem container, PropertyInfo property, object config, IViewItemFactory itemFactory)
        {
            switch (GetCustomItemAttribute <ConfigItemUIBaseAttribute>(property))
            {
            case ConfigItemSliderAttribute slider when property.PropertyType.IsPrimitive:
                if (property.PropertyType == typeof(bool) || property.PropertyType == typeof(IntPtr) ||
                    property.PropertyType == typeof(UIntPtr) || property.PropertyType == typeof(char))
                {
                    goto default;
                }

                return(itemFactory.CreateSlider(
                           container,
                           property.Name,
                           property,
                           config,
                           slider.Min,
                           slider.Max,
                           slider.Step,
                           slider.ValueType,
                           slider.DisplayMultiplier));

            case ConfigItemCheckBoxAttribute _ when property.PropertyType == typeof(bool):
                return(itemFactory.CreateCheckBox(container, property.Name, property, config));

            case ConfigItemComboBoxAttribute _ when property.PropertyType.IsEnum:
                return(itemFactory.CreateComboBox(container, property.Name, property, config, Enum.GetNames(property.PropertyType)));

            default:
                return(null);
            }
        }
Beispiel #2
0
        private static void CreateViewItems(ConfigurationProvider <RealTimeConfig> configProvider, IViewItemFactory itemFactory, ICollection <IViewItem> viewItems)
        {
            var properties = configProvider.Configuration.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                             .Select(p => new { Property = p, Attribute = GetCustomItemAttribute <ConfigItemAttribute>(p) })
                             .Where(v => v.Attribute != null);

            foreach (var tab in properties.GroupBy(p => p.Attribute.TabId).OrderBy(p => p.Key))
            {
                IContainerViewItem tabItem = itemFactory.CreateTabItem(tab.Key);
                viewItems.Add(tabItem);

                foreach (var group in tab.GroupBy(p => p.Attribute.GroupId).OrderBy(p => p.Key))
                {
                    IContainerViewItem containerItem;
                    if (string.IsNullOrEmpty(group.Key))
                    {
                        containerItem = tabItem;
                    }
                    else
                    {
                        containerItem = itemFactory.CreateGroup(tabItem, group.Key);
                        viewItems.Add(containerItem);
                    }

                    foreach (var item in group.OrderBy(i => i.Attribute.Order))
                    {
                        IViewItem viewItem = CreateViewItem(containerItem, item.Property, configProvider, itemFactory);
                        if (viewItem != null)
                        {
                            viewItems.Add(viewItem);
                        }
                    }
                }
            }
        }
Beispiel #3
0
        private static IViewItem CreateViewItem(
            IContainerViewItem container,
            PropertyInfo property,
            ConfigurationProvider <ModConfiguration> configProvider,
            IViewItemFactory itemFactory)
        {
            object Config() => configProvider.Configuration;

            if (property.PropertyType == typeof(int) &&
                GetCustomItemAttribute <ConfigItemUIBaseAttribute>(property) is ConfigItemSliderAttribute slider)
            {
                return(itemFactory.CreateSlider(
                           container,
                           property.Name,
                           property,
                           Config,
                           slider.Min,
                           slider.Max,
                           slider.Step,
                           slider.ValueType,
                           slider.DisplayMultiplier));
            }

            return(null);
        }
Beispiel #4
0
        public static ConfigUI Create(object config, IViewItemFactory itemFactory)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (itemFactory == null)
            {
                throw new ArgumentNullException(nameof(itemFactory));
            }

            var properties = config.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                             .Select(p => new { Property = p, Attribute = GetCustomItemAttribute <ConfigItemAttribute>(p) })
                             .Where(v => v.Attribute != null);

            var viewItems = new List <IViewItem>();

            foreach (var group in properties.GroupBy(p => p.Attribute.GroupId).OrderBy(p => p.Key))
            {
                IContainerViewItem groupItem = itemFactory.CreateGroup(group.Key);
                viewItems.Add(groupItem);

                foreach (var item in group.OrderBy(i => i.Attribute.Order))
                {
                    IViewItem viewItem = CreateViewItem(groupItem, item.Property, config, itemFactory);
                    if (viewItem != null)
                    {
                        viewItems.Add(viewItem);
                    }
                }
            }

            return(new ConfigUI(viewItems));
        }
Beispiel #5
0
        /// <summary>
        /// Creates the mod's configuration page using the specified object as data source.
        /// </summary>
        /// <param name="config">The configuration object to use as data source.</param>
        /// <param name="itemFactory">The view item factory to use for creating the UI elements.</param>
        /// <returns>A configured instance of the <see cref="ConfigUI"/> class.</returns>
        /// <exception cref="ArgumentNullException">Thrown when any argument is null.</exception>
        public static ConfigUI Create(RealTimeConfig config, IViewItemFactory itemFactory)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            if (itemFactory == null)
            {
                throw new ArgumentNullException(nameof(itemFactory));
            }

            var properties = config.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance)
                             .Select(p => new { Property = p, Attribute = GetCustomItemAttribute <ConfigItemAttribute>(p) })
                             .Where(v => v.Attribute != null);

            var viewItems = new List <IViewItem>();

            foreach (var tab in properties.GroupBy(p => p.Attribute.TabId).OrderBy(p => p.Key))
            {
                IContainerViewItem tabItem = itemFactory.CreateTabItem(tab.Key);
                viewItems.Add(tabItem);

                foreach (var group in tab.GroupBy(p => p.Attribute.GroupId).OrderBy(p => p.Key))
                {
                    IContainerViewItem containerItem;
                    if (string.IsNullOrEmpty(group.Key))
                    {
                        containerItem = tabItem;
                    }
                    else
                    {
                        containerItem = itemFactory.CreateGroup(tabItem, group.Key);
                        viewItems.Add(containerItem);
                    }

                    foreach (var item in group.OrderBy(i => i.Attribute.Order))
                    {
                        IViewItem viewItem = CreateViewItem(containerItem, item.Property, config, itemFactory);
                        if (viewItem != null)
                        {
                            viewItems.Add(viewItem);
                        }
                    }
                }
            }

            var result = new ConfigUI(config, viewItems);

            IContainerViewItem toolsTab = itemFactory.CreateTabItem(ToolsId);

            viewItems.Add(toolsTab);
            IViewItem resetButton = itemFactory.CreateButton(toolsTab, ResetToDefaultsId, result.ResetToDefaults);

            viewItems.Add(resetButton);

            return(result);
        }
        /// <summary>Creates a new button. If <paramref name="container"/> is not specified, the button is placed into the root element.</summary>
        /// <param name="container">The container.</param>
        /// <param name="id">The ID of the button to create.</param>
        /// <param name="clickHandler">A method that will be called when the button is clicked.</param>
        /// <returns>A newly created <see cref="IViewItem"/> instance representing a button item.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="clickHandler"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="id"/> is null or empty string.</exception>
        public IViewItem CreateButton(IContainerViewItem container, string id, Action clickHandler)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("The button ID cannot be null or empty string", nameof(id));
            }

            return(new CitiesButtonViewItem(container?.Container ?? uiHelper, id, clickHandler));
        }
        /// <summary>Creates a new check box view item.</summary>
        /// <param name="container">The parent container for the created item.</param>
        /// <param name="id">The ID of the item to create.</param>
        /// <param name="property">The property description that specifies the target property where to store the value.</param>
        /// <param name="configProvider">A method that provides the configuration storage object for the value.</param>
        /// <returns>A newly created <see cref="IViewItem"/> instance representing a check box.</returns>
        /// <exception cref="ArgumentNullException">Thrown when any argument is null.</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="id"/> is an empty string.</exception>
        public IViewItem CreateCheckBox(IContainerViewItem container, string id, PropertyInfo property, Func <object> configProvider)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            return(new CitiesCheckBoxItem(container.Container, id, property, configProvider));
        }
Beispiel #8
0
 /// <summary>Creates a new combo box view item.</summary>
 /// <param name="container">The parent container for the created item.</param>
 /// <param name="id">The ID of the item to create.</param>
 /// <param name="property">The property description that specifies the target property where to store the value.</param>
 /// <param name="config">The configuration storage object for the value.</param>
 /// <param name="itemIds">A collection of the item IDs for the combo box values.</param>
 /// <returns>A newly created <see cref="IViewItem"/> instance representing a combo box.</returns>
 /// <exception cref="ArgumentNullException">Thrown when any argument is null.</exception>
 /// <exception cref="ArgumentException">Thrown when <paramref name="id"/> is an empty string.</exception>
 public IViewItem CreateComboBox(
     IContainerViewItem container,
     string id,
     PropertyInfo property,
     object config,
     IEnumerable <string> itemIds)
 {
     return(new CitiesComboBoxItem(container.Container, id, property, config, itemIds));
 }
Beispiel #9
0
 public IViewItem CreateSlider(
     IContainerViewItem container,
     string id,
     PropertyInfo property,
     object config,
     float min,
     float max,
     float step,
     SliderValueType valueType)
 {
     return(new UnitySliderItem(container.Container, id, property, config, min, max, step, valueType));
 }
Beispiel #10
0
 /// <summary>Creates a new slider view item.</summary>
 /// <param name="container">The parent container for the created item.</param>
 /// <param name="id">The ID of the item to create.</param>
 /// <param name="property">The property description that specifies the target property where to store the value.</param>
 /// <param name="config">The configuration storage object for the value.</param>
 /// <param name="min">The minimum slider value.</param>
 /// <param name="max">The maximum slider value.</param>
 /// <param name="step">The slider step value.</param>
 /// <param name="valueType">Type of the value to be represented by the slider.</param>
 /// <param name="displayMultiplier">A value that will be multiplied with original values for displaying purposes.</param>
 /// <returns>A newly created <see cref="IViewItem"/> instance representing a slider.</returns>
 /// <exception cref="ArgumentNullException">Thrown when any argument is null.</exception>
 /// <exception cref="ArgumentException">Thrown when <paramref name="id"/> is an empty string.</exception>
 /// <exception cref="ArgumentException">
 /// Thrown when the <paramref name="max"/> value is less or equal to the <paramref name="min"/> value.
 /// </exception>
 /// <exception cref="ArgumentException">
 /// Thrown when the <paramref name="step"/> value is less or equal to zero.
 /// </exception>
 public IViewItem CreateSlider(
     IContainerViewItem container,
     string id,
     PropertyInfo property,
     object config,
     float min,
     float max,
     float step,
     SliderValueType valueType,
     float displayMultiplier)
 {
     return(new CitiesSliderItem(container.Container, id, property, config, min, max, step, valueType, displayMultiplier));
 }
        /// <summary>Creates a new group view item.</summary>
        /// <param name="container">The parent container for the created item.</param>
        /// <param name="id">The ID of the group to create.</param>
        /// <returns>A newly created <see cref="IContainerViewItem"/> instance representing a group.</returns>
        /// <exception cref="ArgumentNullException">Thrown when <paramref name="container"/> is null.</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="id"/> is null or an empty string.</exception>
        public IContainerViewItem CreateGroup(IContainerViewItem container, string id)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("The group ID cannot be null or empty string", nameof(id));
            }

            return(new CitiesGroupItem(container.Container.AddGroup(Constants.Placeholder), id));
        }
        /// <summary>Creates a new tab item. If it cannot be created, returns a group instead.</summary>
        /// <param name="id">The ID of the tab to create.</param>
        /// <returns>A newly created <see cref="IContainerViewItem"/> instance representing a tab item.</returns>
        /// <exception cref="ArgumentException">Thrown when <paramref name="id"/> is null or empty string.</exception>
        public IContainerViewItem CreateTabItem(string id)
        {
            if (string.IsNullOrEmpty(id))
            {
                throw new ArgumentException("The tab ID cannot be null or empty string", nameof(id));
            }

            IContainerViewItem result = null;

            if (tabStrip != null)
            {
                result = CitiesTabItem.Create(tabStrip, id);
                tabStrip.selectedIndex = tabStrip.tabCount - 1;
            }

            return(result ?? new CitiesGroupItem(uiHelper.AddGroup(Constants.Placeholder), id));
        }
        /// <summary>Creates a new slider view item.</summary>
        /// <param name="container">The parent container for the created item.</param>
        /// <param name="id">The ID of the item to create.</param>
        /// <param name="property">The property description that specifies the target property where to store the value.</param>
        /// <param name="configProvider">A method that provides the configuration storage object for the value.</param>
        /// <param name="min">The minimum slider value.</param>
        /// <param name="max">The maximum slider value.</param>
        /// <param name="step">The slider step value.</param>
        /// <param name="valueType">Type of the value to be represented by the slider.</param>
        /// <param name="displayMultiplier">A value that will be multiplied with original values for displaying purposes.</param>
        /// <returns>A newly created <see cref="IViewItem"/> instance representing a slider.</returns>
        /// <exception cref="ArgumentNullException">Thrown when any argument is null.</exception>
        /// <exception cref="ArgumentException">Thrown when <paramref name="id"/> is an empty string.</exception>
        /// <exception cref="ArgumentException">
        /// Thrown when the <paramref name="max"/> value is less or equal to the <paramref name="min"/> value.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// Thrown when the <paramref name="step"/> value is less or equal to zero.
        /// </exception>
        public IViewItem CreateSlider(
            IContainerViewItem container,
            string id,
            PropertyInfo property,
            Func <object> configProvider,
            float min,
            float max,
            float step,
            SliderValueType valueType,
            float displayMultiplier)
        {
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }

            return(new CitiesSliderItem(container.Container, id, property, configProvider, min, max, step, valueType, displayMultiplier));
        }
Beispiel #14
0
        /// <summary>
        /// Creates the mod's configuration page using the specified object as data source.
        /// </summary>
        /// <param name="configProvider">The mod's configuration provider.</param>
        /// <param name="itemFactory">The view item factory to use for creating the UI elements.</param>
        /// <returns>A configured instance of the <see cref="ConfigUI"/> class.</returns>
        /// <exception cref="ArgumentNullException">Thrown when any argument is null.</exception>
        /// <exception cref="InvalidOperationException">Thrown when the specified <see cref="ConfigurationProvider{RealTimeConfig}"/>
        /// is not initialized yet.</exception>
        public static ConfigUI Create(ConfigurationProvider <RealTimeConfig> configProvider, IViewItemFactory itemFactory)
        {
            if (configProvider == null)
            {
                throw new ArgumentNullException(nameof(configProvider));
            }

            if (itemFactory == null)
            {
                throw new ArgumentNullException(nameof(itemFactory));
            }

            if (configProvider.Configuration == null)
            {
                throw new InvalidOperationException("The configuration provider has no configuration yet");
            }

            var viewItems = new List <IViewItem>();

            CreateViewItems(configProvider, itemFactory, viewItems);

            var result = new ConfigUI(configProvider, viewItems);

            IContainerViewItem toolsTab = viewItems.OfType <IContainerViewItem>().FirstOrDefault(i => i.Id == ToolsId);

            if (toolsTab == null)
            {
                toolsTab = itemFactory.CreateTabItem(ToolsId);
                viewItems.Add(toolsTab);
            }

            IViewItem resetButton = itemFactory.CreateButton(toolsTab, ResetToDefaultsId, result.ResetToDefaults);

            viewItems.Add(resetButton);
            IViewItem newGameConfigButton = itemFactory.CreateButton(toolsTab, UseForNewGamesId, result.UseForNewGames);

            viewItems.Add(newGameConfigButton);

            return(result);
        }
Beispiel #15
0
 public IViewItem CreateCheckBox(IContainerViewItem container, string id, PropertyInfo property, object config)
 {
     return(new UnityCheckBoxItem(container.Container, id, property, config));
 }