Files
MPF/MPF.Core/UI/ComboBoxItems/RedumpSystemComboBoxItem.cs

102 lines
3.1 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2023-09-05 00:08:09 -04:00
using SabreTools.RedumpLib.Data;
2018-07-05 13:33:08 -07:00
2023-10-07 01:30:11 -04:00
namespace MPF.Core.UI.ComboBoxItems
2018-07-05 13:33:08 -07:00
{
/// <summary>
/// Represents a single item in the System combo box
/// </summary>
2023-10-07 22:40:51 -04:00
public class RedumpSystemComboBoxItem : IEquatable<RedumpSystemComboBoxItem>, IElement
2018-07-05 13:33:08 -07:00
{
2023-10-07 01:30:11 -04:00
private readonly object? Data;
2018-07-05 13:33:08 -07:00
2021-08-18 22:13:38 -07:00
public RedumpSystemComboBoxItem(RedumpSystem? system) => Data = system;
public RedumpSystemComboBoxItem(SystemCategory? category) => Data = category;
2018-07-05 13:33:08 -07:00
2021-08-18 22:13:38 -07:00
public static implicit operator RedumpSystem?(RedumpSystemComboBoxItem item) => item.Data as RedumpSystem?;
2018-07-05 13:33:08 -07:00
/// <inheritdoc/>
2018-07-05 13:33:08 -07:00
public string Name
{
get
{
if (IsHeader)
2021-08-18 22:13:38 -07:00
return "---------- " + (Data as SystemCategory?).LongName() + " ----------";
2018-07-05 13:33:08 -07:00
else
2021-08-18 22:13:38 -07:00
return (Data as RedumpSystem?).LongName() ?? "No system selected";
2018-07-05 13:33:08 -07:00
}
}
2021-03-09 16:38:15 -08:00
public override string ToString() => Name;
/// <summary>
/// Internal enum value
/// </summary>
2021-08-18 22:13:38 -07:00
public RedumpSystem? Value => Data as RedumpSystem?;
/// <summary>
/// Determines if the item is a header value
/// </summary>
2021-08-18 22:13:38 -07:00
public bool IsHeader => Data is SystemCategory?;
/// <summary>
/// Determines if the item is a standard system value
/// </summary>
2021-08-18 22:13:38 -07:00
public bool IsSystem => Data is RedumpSystem?;
/// <summary>
/// Generate all elements for the known system combo box
/// </summary>
/// <returns></returns>
2021-08-18 22:13:38 -07:00
public static IEnumerable<RedumpSystemComboBoxItem> GenerateElements()
{
2021-08-18 22:13:38 -07:00
var knownSystems = Enum.GetValues(typeof(RedumpSystem))
.OfType<RedumpSystem?>()
.Where(s => !s.IsMarker() && s.GetCategory() != SystemCategory.NONE)
.ToList();
2021-08-18 22:13:38 -07:00
Dictionary<SystemCategory, List<RedumpSystem?>> mapping = knownSystems
.GroupBy(s => s.GetCategory())
.ToDictionary(
k => k.Key,
v => v
.OrderBy(s => s.LongName())
.ToList()
);
2021-08-18 22:13:38 -07:00
var systemsValues = new List<RedumpSystemComboBoxItem>
{
2021-08-18 22:13:38 -07:00
new RedumpSystemComboBoxItem((RedumpSystem?)null),
};
foreach (var group in mapping)
{
2021-08-18 22:13:38 -07:00
systemsValues.Add(new RedumpSystemComboBoxItem(group.Key));
group.Value.ForEach(system => systemsValues.Add(new RedumpSystemComboBoxItem(system)));
}
return systemsValues;
}
2023-10-07 22:40:51 -04:00
2023-11-06 23:06:11 -05:00
/// <inheritdoc/>
public override bool Equals(object? obj)
{
return Equals(obj as RedumpSystemComboBoxItem);
}
2023-10-07 22:40:51 -04:00
/// <inheritdoc/>
public bool Equals(RedumpSystemComboBoxItem? other)
{
if (other == null)
return false;
return Value == other.Value;
}
2023-11-06 23:06:11 -05:00
/// <inheritdoc/>
public override int GetHashCode() => base.GetHashCode();
2018-07-05 13:33:08 -07:00
}
}