Skip to content

Commit

Permalink
ComboBoxes now use proper displayed values instead of using raw Enum …
Browse files Browse the repository at this point in the history
…values
  • Loading branch information
gunt3001 committed Jun 16, 2024
1 parent 42fa34f commit 3481a7a
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 25 deletions.
1 change: 0 additions & 1 deletion HsrGraphicsTool/App.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="HsrGraphicsTool.App"
xmlns:local="using:HsrGraphicsTool"
xmlns:models="clr-namespace:HsrGraphicsTool.Models"
RequestedThemeVariant="Default">
<!-- "Default" ThemeVariant follows system theme variant. "Dark" or "Light" are other available options. -->

Expand Down
9 changes: 8 additions & 1 deletion HsrGraphicsTool/Models/AntiAliasingMode.cs
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 HsrGraphicsTool/Models/EnumUtils/EnumCollectionConverter.cs
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;
}
}
69 changes: 69 additions & 0 deletions HsrGraphicsTool/Models/EnumUtils/EnumUtils.cs
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();
}
}
9 changes: 9 additions & 0 deletions HsrGraphicsTool/Models/EnumUtils/ValueDescription.cs
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;
}
}
15 changes: 14 additions & 1 deletion HsrGraphicsTool/Models/GraphicsPresetOption.cs
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,
}
15 changes: 14 additions & 1 deletion HsrGraphicsTool/Models/GraphicsQualityOption.cs
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,
}
45 changes: 24 additions & 21 deletions HsrGraphicsTool/Views/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
xmlns:vm="using:HsrGraphicsTool.ViewModels"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:enumUtils="clr-namespace:HsrGraphicsTool.Models.EnumUtils"
mc:Ignorable="d"
CanResize="False"
TransparencyLevelHint="AcrylicBlur" Background="Transparent"
Expand Down Expand Up @@ -68,18 +69,20 @@
<TextBlock Classes="option-label">Resolution</TextBlock>
<StackPanel Grid.Column="1" Orientation="Horizontal" Spacing="8" HorizontalAlignment="Right">
<NumericUpDown ShowButtonSpinner="False" Minimum="1" Increment="128" FormatString="0"
Value="{Binding Width}" Width="50"/>
Value="{Binding Width}" Width="50" />
<TextBlock VerticalAlignment="Center" HorizontalAlignment="Center">x</TextBlock>
<NumericUpDown ShowButtonSpinner="False" Minimum="1" Increment="128" FormatString="0"
Value="{Binding Height}" Width="50"/>
Value="{Binding Height}" Width="50" />
<CheckBox IsChecked="{Binding IsFullscreen}">Full screen</CheckBox>
</StackPanel>
</Grid>
<Grid Classes="option" ColumnDefinitions="140, 270">
<TextBlock Classes="option-label">Graphics quality</TextBlock>
<ComboBox Grid.Column="1" SelectedIndex="0" Classes="option-value"
ItemsSource="{Binding GraphicsPresetOptions}"
SelectedItem="{Binding GraphicsPreset}" />
ItemsSource="{Binding GraphicsPresetOptions,
Converter={enumUtils:EnumCollectionConverter}}"
SelectedItem="{Binding GraphicsPreset,
Converter={enumUtils:EnumValueConverter}}" />
</Grid>
<Grid Classes="option" ColumnDefinitions="140, 270">
<TextBlock Classes="option-label">FPS</TextBlock>
Expand All @@ -103,54 +106,54 @@
<Grid Classes="option" ColumnDefinitions="140, 270">
<TextBlock Classes="option-label">Shadow quality</TextBlock>
<ComboBox Grid.Column="1" SelectedIndex="0" Classes="option-value"
ItemsSource="{Binding ShadowQualityOptions}"
SelectedItem="{Binding ShadowQuality}" />
ItemsSource="{Binding ShadowQualityOptions, Converter={enumUtils:EnumCollectionConverter}}"
SelectedItem="{Binding ShadowQuality, Converter={enumUtils:EnumValueConverter}}" />
</Grid>
<Grid Classes="option" ColumnDefinitions="140, 270">
<TextBlock Classes="option-label">Reflection quality</TextBlock>
<ComboBox Grid.Column="1" SelectedIndex="0" Classes="option-value"
ItemsSource="{Binding ReflectionQualityOptions}"
SelectedItem="{Binding ReflectionQuality}" />
ItemsSource="{Binding ReflectionQualityOptions, Converter={enumUtils:EnumCollectionConverter}}"
SelectedItem="{Binding ReflectionQuality, Converter={enumUtils:EnumValueConverter}}" />
</Grid>
<Grid Classes="option" ColumnDefinitions="140, 270">
<TextBlock Classes="option-label">Character quality</TextBlock>
<ComboBox Grid.Column="1" SelectedIndex="0" Classes="option-value"
ItemsSource="{Binding CharacterQualityOptions}"
SelectedItem="{Binding CharacterQuality}" />
ItemsSource="{Binding CharacterQualityOptions, Converter={enumUtils:EnumCollectionConverter}}"
SelectedItem="{Binding CharacterQuality, Converter={enumUtils:EnumValueConverter}}" />
</Grid>
<Grid Classes="option" ColumnDefinitions="140, 270">
<TextBlock Classes="option-label">Environment detail</TextBlock>
<ComboBox Grid.Column="1" SelectedIndex="0" Classes="option-value"
ItemsSource="{Binding EnvironmentDetailOptions}"
SelectedItem="{Binding EnvironmentDetail}" />
ItemsSource="{Binding EnvironmentDetailOptions, Converter={enumUtils:EnumCollectionConverter}}"
SelectedItem="{Binding EnvironmentDetail, Converter={enumUtils:EnumValueConverter}}" />
</Grid>
<Grid Classes="option" ColumnDefinitions="140, 270">
<TextBlock Classes="option-label">Special effects quality</TextBlock>
<ComboBox Grid.Column="1" SelectedIndex="0" Classes="option-value"
ItemsSource="{Binding SpecialEffectsQualityOptions}"
SelectedItem="{Binding SpecialEffectsQuality}" />
ItemsSource="{Binding SpecialEffectsQualityOptions, Converter={enumUtils:EnumCollectionConverter}}"
SelectedItem="{Binding SpecialEffectsQuality, Converter={enumUtils:EnumValueConverter}}" />
</Grid>
<Grid Classes="option" ColumnDefinitions="140, 270">
<TextBlock Classes="option-label">Bloom effect</TextBlock>
<ComboBox Grid.Column="1" SelectedIndex="0" Classes="option-value"
ItemsSource="{Binding BloomEffectOptions}"
SelectedItem="{Binding BloomEffect}" />
ItemsSource="{Binding BloomEffectOptions, Converter={enumUtils:EnumCollectionConverter}}"
SelectedItem="{Binding BloomEffect, Converter={enumUtils:EnumValueConverter}}" />
</Grid>
<Grid Classes="option" ColumnDefinitions="140, 270">
<TextBlock Classes="option-label">Anti-aliasing</TextBlock>
<ComboBox Grid.Column="1" SelectedIndex="0" Classes="option-value"
ItemsSource="{Binding AntiAliasingOptions}"
SelectedItem="{Binding AntiAliasing}" />
ItemsSource="{Binding AntiAliasingOptions, Converter={enumUtils:EnumCollectionConverter}}"
SelectedItem="{Binding AntiAliasing, Converter={enumUtils:EnumValueConverter}}" />
</Grid>
<Grid Classes="option" ColumnDefinitions="140, 270">
<TextBlock Classes="option-label">Light quality</TextBlock>
<ComboBox Grid.Column="1" SelectedIndex="0" Classes="option-value"
ItemsSource="{Binding LightQualityOptions}"
SelectedItem="{Binding LightQuality}" />
ItemsSource="{Binding LightQualityOptions, Converter={enumUtils:EnumCollectionConverter}}"
SelectedItem="{Binding LightQuality, Converter={enumUtils:EnumValueConverter}}" />
</Grid>
<TextBlock Text="{Binding StatusMessage}"
Foreground="Green"
FontFamily="{DynamicResource AppFontFamily}"/>
FontFamily="{DynamicResource AppFontFamily}" />
<Grid ColumnDefinitions="*,8, *">
<!-- 'accent' is a built-in style that applies the primary button color -->
<!-- ReSharper disable once Xaml.StyleClassNotFound -->
Expand Down

0 comments on commit 3481a7a

Please sign in to comment.