-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ComboBoxes now use proper displayed values instead of using raw Enum …
…values
- Loading branch information
Showing
8 changed files
with
184 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
namespace HsrGraphicsTool.Models; | ||
using System.ComponentModel; | ||
|
||
namespace HsrGraphicsTool.Models; | ||
|
||
/// <summary> | ||
/// Enum representing the options for the in-game "Anti-Aliasing" setting | ||
/// </summary> | ||
public enum AntiAliasingMode | ||
{ | ||
[Description("Off")] | ||
Off, | ||
|
||
[Description("TAA")] | ||
Taa = 1, | ||
|
||
[Description("FXAA")] | ||
Fxaa = 2, | ||
} |
46 changes: 46 additions & 0 deletions
46
HsrGraphicsTool/Models/EnumUtils/EnumCollectionConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Globalization; | ||
using Avalonia.Data.Converters; | ||
using Avalonia.Markup.Xaml; | ||
|
||
namespace HsrGraphicsTool.Models.EnumUtils; | ||
|
||
/// <summary> | ||
/// Converter class to convert an enum to a collection of its values and descriptions | ||
/// </summary> | ||
public class EnumCollectionConverter : MarkupExtension, IValueConverter | ||
{ | ||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
return EnumUtils.GetAllValuesAndDescriptions(value!.GetType()); | ||
} | ||
|
||
public object ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
return null; | ||
} | ||
|
||
public override object ProvideValue(IServiceProvider serviceProvider) | ||
{ | ||
return this; | ||
} | ||
} | ||
|
||
public class EnumValueConverter : MarkupExtension, IValueConverter | ||
{ | ||
|
||
public object Convert(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
return new ValueDescription(value!, EnumUtils.GetEnumDescription((Enum)value!)); | ||
} | ||
|
||
public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture) | ||
{ | ||
return value == null ? null : EnumUtils.GetEnumValue(targetType, (string)((ValueDescription) value).Description); | ||
} | ||
|
||
public override object ProvideValue(IServiceProvider serviceProvider) | ||
{ | ||
return this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Globalization; | ||
using System.Linq; | ||
|
||
namespace HsrGraphicsTool.Models.EnumUtils; | ||
|
||
public static class EnumUtils | ||
{ | ||
public static object? GetEnumValue(Type targetType, string description) | ||
{ | ||
// From the type, get all the enum Value/Description pair | ||
var enumValueDescriptions = GetAllValuesAndDescriptions(targetType); | ||
|
||
// Return the first enum value that matches the description | ||
return enumValueDescriptions | ||
.Where(x => (string)x.Description == description) | ||
.Select(x => x.Value) | ||
.FirstOrDefault(); | ||
} | ||
|
||
/// <summary> | ||
/// Given an enum value, identify its description assigned using Description attribute | ||
/// </summary> | ||
/// <param name="value">Enum value</param> | ||
/// <returns>Description string of the enum</returns> | ||
public static string GetEnumDescription(Enum value) | ||
{ | ||
string? description = null; | ||
|
||
// Get attributes associated with the enum value | ||
var attributes = value.GetType().GetField(value.ToString())? | ||
.GetCustomAttributes(typeof(DescriptionAttribute), false); | ||
if (attributes != null && attributes.Length != 0) | ||
{ | ||
description = (attributes.First() as DescriptionAttribute)?.Description; | ||
} | ||
|
||
if (description != null) | ||
{ | ||
return description; | ||
} | ||
|
||
// If no DescriptionAttribute is found, default to the enum value name | ||
var ti = CultureInfo.CurrentCulture.TextInfo; | ||
description = ti.ToTitleCase(ti.ToLower(value.ToString().Replace("_", " "))); | ||
|
||
return description; | ||
} | ||
|
||
public static IEnumerable<ValueDescription> GetAllValuesAndDescriptions(Type type) | ||
{ | ||
var actualType = type; | ||
if (type.IsArray) | ||
{ | ||
actualType = type.GetElementType(); | ||
} | ||
if (actualType == null || !actualType.IsEnum) | ||
{ | ||
throw new ArgumentException("Type must be an or enum array."); | ||
} | ||
|
||
return Enum.GetValues(actualType) | ||
.Cast<Enum>() | ||
.Select(value => new ValueDescription(value, GetEnumDescription(value))) | ||
.ToList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace HsrGraphicsTool.Models.EnumUtils; | ||
|
||
public record ValueDescription(object Value, object Description) | ||
{ | ||
public override string ToString() | ||
{ | ||
return (string)Description; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,27 @@ | ||
namespace HsrGraphicsTool.Models; | ||
using System.ComponentModel; | ||
|
||
namespace HsrGraphicsTool.Models; | ||
|
||
/// <summary> | ||
/// Enum representing the presets for the in-game "Graphics Quality" setting | ||
/// </summary> | ||
public enum GraphicsPresetOption | ||
{ | ||
[Description("Custom")] | ||
Custom = 0, | ||
|
||
[Description("Very Low")] | ||
VeryLow = 1, | ||
|
||
[Description("Low")] | ||
Low = 2, | ||
|
||
[Description("Medium")] | ||
Medium = 3, | ||
|
||
[Description("High")] | ||
High = 4, | ||
|
||
[Description("Very High")] | ||
VeryHigh = 5, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,28 @@ | ||
namespace HsrGraphicsTool.Models; | ||
using System.ComponentModel; | ||
|
||
namespace HsrGraphicsTool.Models; | ||
|
||
/// <summary> | ||
/// Enum representing the options for various graphics settings in Honkai: Star Rail. | ||
/// Each setting have different sets of valid values. | ||
/// </summary> | ||
public enum GraphicsQualityOption | ||
{ | ||
[Description("Off")] | ||
Off = 0, | ||
|
||
[Description("Very Low")] | ||
VeryLow = 1, | ||
|
||
[Description("Low")] | ||
Low = 2, | ||
|
||
[Description("Medium")] | ||
Medium = 3, | ||
|
||
[Description("High")] | ||
High = 4, | ||
|
||
[Description("Very High")] | ||
VeryHigh = 5, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters