/// <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 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); }