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); }
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)); }
/// <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); }
private static IViewItem CreateViewItem( IContainerViewItem container, PropertyInfo property, ConfigurationProvider <RealTimeConfig> configProvider, IViewItemFactory itemFactory) { object Config() { return(configProvider.Configuration); } 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); } }
private static void CreateViewItems( ConfigurationProvider <ModConfiguration> 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)) { var 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)) { var viewItem = CreateViewItem(containerItem, item.Property, configProvider, itemFactory); if (viewItem != null) { viewItems.Add(viewItem); } } } } }
/// <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); }
/// <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 <ModConfiguration> 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); return(new ConfigUI(configProvider, viewItems)); }
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)); case ConfigItemCheckBoxAttribute _ when property.PropertyType == typeof(bool): return(itemFactory.CreateCheckBox(container, property.Name, property, config)); default: return(null); } }