2021-03-02 09:08:56 -08:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2021-09-29 11:48:37 -07:00
|
|
|
|
using MPF.Core.Converters;
|
2021-03-02 09:08:56 -08:00
|
|
|
|
|
2022-04-11 10:32:03 -07:00
|
|
|
|
namespace MPF.UI.Core.ComboBoxItems
|
2021-03-02 09:08:56 -08:00
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// A generic combo box element
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">Enum type representing the possible values</typeparam>
|
|
|
|
|
|
public class Element<T> : IElement where T : struct, Enum
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly T Data;
|
|
|
|
|
|
|
2021-03-02 09:48:36 -08:00
|
|
|
|
public Element(T data) => Data = data;
|
2021-03-02 09:08:56 -08:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Allow elements to be used as their internal enum type
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="item"></param>
|
2021-03-06 21:49:33 -08:00
|
|
|
|
public static implicit operator T? (Element<T> item) => item?.Data;
|
2021-03-02 09:08:56 -08:00
|
|
|
|
|
2021-03-02 09:48:36 -08:00
|
|
|
|
/// <inheritdoc/>
|
2021-08-18 15:32:45 -07:00
|
|
|
|
public string Name => EnumConverter.GetLongName(Data);
|
2021-03-02 09:08:56 -08:00
|
|
|
|
|
2021-03-09 16:38:15 -08:00
|
|
|
|
public override string ToString() => Name;
|
|
|
|
|
|
|
2021-03-02 09:08:56 -08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Internal enum value
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public T Value => Data;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Determine if the item is selected or not
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>Only applies to CheckBox type</remarks>
|
|
|
|
|
|
public bool IsChecked { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2021-03-02 09:48:36 -08:00
|
|
|
|
/// Generate all elements associated with the data enum type
|
2021-03-02 09:08:56 -08:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public static IEnumerable<Element<T>> GenerateElements()
|
|
|
|
|
|
{
|
|
|
|
|
|
return Enum.GetValues(typeof(T))
|
|
|
|
|
|
.OfType<T>()
|
|
|
|
|
|
.Select(e => new Element<T>(e));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|