Skip to content

Commit

Permalink
custom aspect ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
qhy040404 committed Nov 29, 2024
1 parent 14ce117 commit 23ac4cf
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal sealed partial class SettingEntry
{
public const string GamePath = "GamePath";
public const string GamePathEntries = "GamePathEntries";
public const string AspectRatios = "AspectRatios";
public const string Culture = "Culture";
public const string FirstDayOfWeek = "FirstDayOfWeek";

Expand Down
49 changes: 49 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Service/Game/AspectRatio.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

namespace Snap.Hutao.Service.Game;

internal sealed class AspectRatio : IEquatable<AspectRatio>
{
public AspectRatio(double width, double height)
{
Width = width;
Height = height;
}

[JsonPropertyName("width")]
public double Width { get; }

[JsonPropertyName("height")]
public double Height { get; }

public override string ToString()
{
return Width + ":" + Height;
}

public override int GetHashCode()
{
return HashCode.Combine(Width, Height);
}

public bool Equals(AspectRatio? other)
{
if (other is null)
{
return false;
}

if (ReferenceEquals(this, other))
{
return true;
}

return Width.Equals(other.Width) && Height.Equals(other.Height);
}

public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj) || (obj is AspectRatio other && Equals(other));
}
}
15 changes: 6 additions & 9 deletions src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
// Licensed under the MIT license.

using CommunityToolkit.Mvvm.Messaging;
using CommunityToolkit.WinUI.Controls;
using JetBrains.Annotations;
using Microsoft.UI.Windowing;
using Snap.Hutao.Model;
Expand Down Expand Up @@ -369,14 +368,11 @@ public bool HookingMickyWonderPartner2
}

[UsedImplicitly]
public ImmutableArray<AspectRatio> AspectRatios { get; } =
[
new(3840, 2160),
new(2560, 1600),
new(2560, 1440),
new(2410, 1080),
new(1920, 1080),
];
public ImmutableArray<AspectRatio> AspectRatios
{
get => GetOption(ref fields.AspectRatios, SettingEntry.AspectRatios, raw => JsonSerializer.Deserialize<ImmutableArray<AspectRatio>>(raw), []);
set => SetOption(ref fields.AspectRatios, SettingEntry.AspectRatios, value, static v => JsonSerializer.Serialize(v));
}

[UsedImplicitly]
public AspectRatio? SelectedAspectRatio
Expand Down Expand Up @@ -445,6 +441,7 @@ private static ImmutableArray<NameValue<int>> InitializeMonitors()
private struct Fields
{
public ImmutableArray<GamePathEntry>? GamePathEntries;
public ImmutableArray<AspectRatio>? AspectRatios;

public bool? UsingHoyolabAccount;
public bool? AreCommandLineArgumentsEnabled;
Expand Down
27 changes: 27 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Service/Game/LaunchOptionsExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using System.Collections.Immutable;

namespace Snap.Hutao.Service.Game;

internal static class LaunchOptionsExtension
{
public static ImmutableArray<AspectRatio> SaveAspectRatio(this LaunchOptions options, AspectRatio aspectRatio)
{
if (options.AspectRatios.Contains(aspectRatio))
{
return options.AspectRatios;
}

options.AspectRatios = options.AspectRatios.Add(aspectRatio);
return options.AspectRatios;
}

public static ImmutableArray<AspectRatio> RemoveAspectRatio(this LaunchOptions options, AspectRatio aspectRatio)
{
options.SelectedAspectRatio = default;
options.AspectRatios = options.AspectRatios.Remove(aspectRatio);
return options.AspectRatios;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ private static System.Diagnostics.Process InitializeGameProcess(LaunchExecutionC
.AppendIf(launchOptions.UsingCloudThirdPartyMobile, "-platform_type CLOUD_THIRD_PARTY_MOBILE")
.AppendIf(launchOptions.UsingHoyolabAccount && !string.IsNullOrEmpty(context.AuthTicket), "login_auth_ticket", context.AuthTicket, CommandLineArgumentSeparator.Equal)
.ToString();

context.TaskContext.InvokeOnMainThread(() =>
{
launchOptions.SaveAspectRatio(new(launchOptions.ScreenWidth, launchOptions.ScreenHeight));
});
}

context.Logger.LogInformation("Command Line Arguments: {commandLine}", commandLine);
Expand Down
35 changes: 29 additions & 6 deletions src/Snap.Hutao/Snap.Hutao/UI/Xaml/View/Page/LaunchGamePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,32 @@
ToolTipService.ToolTip="{shuxm:ResourceString Name=ViewPageLaunchGameSwitchAccountRemoveToolTip}"/>
</Grid>
</DataTemplate>

<shvg:AspectRatioComboBoxTemplateSelector x:Key="AspectRatioComboBoxTemplateSelector">
<shvg:AspectRatioComboBoxTemplateSelector.ListTemplate>
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>

<TextBlock VerticalAlignment="Center" Text="{Binding}"/>
<Button
Grid.Column="1"
MinWidth="32"
MinHeight="32"
Padding="0"
VerticalAlignment="Center"
Command="{Binding DataContext.RemoveAspectRatioCommand, Source={StaticResource BindingProxy}}"
CommandParameter="{Binding}"
Content="&#xE711;"
FontFamily="{ThemeResource SymbolThemeFontFamily}"
Style="{ThemeResource SettingButtonStyle}"/>
</Grid>
</DataTemplate>
</shvg:AspectRatioComboBoxTemplateSelector.ListTemplate>
</shvg:AspectRatioComboBoxTemplateSelector>
</Page.Resources>

<shuxc:StandardView ShowCondition="{Binding GamePathSelectedAndValid, Mode=OneWay}" Style="{ThemeResource DefaultStandardViewStyle}">
Expand Down Expand Up @@ -420,9 +446,7 @@
Padding="16"
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
BorderThickness="0,0,0,1">
<Grid
ColumnSpacing="16"
RowSpacing="16">
<Grid ColumnSpacing="16" RowSpacing="16">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
Expand Down Expand Up @@ -459,9 +483,7 @@
Padding="16"
BorderBrush="{ThemeResource CardStrokeColorDefaultBrush}"
BorderThickness="0,0,0,1">
<Grid
ColumnSpacing="16"
RowSpacing="16">
<Grid ColumnSpacing="16" RowSpacing="16">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
Expand Down Expand Up @@ -585,6 +607,7 @@
<shuxc:SizeRestrictedContentControl Margin="0,0,130,0" VerticalAlignment="Center">
<ComboBox
MinWidth="{ThemeResource SettingsCardContentControlMinWidth2}"
ItemTemplateSelector="{StaticResource AspectRatioComboBoxTemplateSelector}"
ItemsSource="{Binding LaunchOptions.AspectRatios}"
PlaceholderText="{shuxm:ResourceString Name=ViewPageLaunchGameAppearanceAspectRatioPlaceHolder}"
SelectedItem="{Binding LaunchOptions.SelectedAspectRatio, Mode=TwoWay}"/>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;

namespace Snap.Hutao.ViewModel.Game;

internal sealed partial class AspectRatioComboBoxTemplateSelector : DataTemplateSelector
{
public DataTemplate ListTemplate { get; set; } = default!;

protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (container is ContentPresenter)
{
return default!;
}

return ListTemplate;
}
}
13 changes: 13 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/ViewModel/Game/LaunchGameViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ async Task RefreshUIAsync()

public ImmutableArray<GamePathEntry> GamePathEntries { get; set => SetProperty(ref field, value); } = [];

public ImmutableArray<AspectRatio> AspectRatios { get; set => SetProperty(ref field, value); } = [];

/// <summary>
/// Update this property will also:
/// <br/>
Expand Down Expand Up @@ -179,6 +181,7 @@ public async ValueTask<bool> ReceiveAsync(INavigationExtraData data)
protected override ValueTask<bool> LoadOverrideAsync()
{
this.SetGamePathEntriesAndSelectedGamePathEntry(LaunchOptions);
AspectRatios = LaunchOptions.AspectRatios;
return ValueTask.FromResult(true);
}

Expand Down Expand Up @@ -214,11 +217,21 @@ private void RemoveGamePathEntry(GamePathEntry? entry)
SelectedGamePathEntry = selected;
}

[Command("RemoveAspectRatioCommand")]
private void RemoveAspectRatio(AspectRatio aspectRatio)
{
AspectRatios = LaunchOptions.RemoveAspectRatio(aspectRatio);
}

[Command("LaunchCommand")]
private async Task LaunchAsync()
{
UserAndUid? userAndUid = await userService.GetCurrentUserAndUidAsync().ConfigureAwait(false);
await this.LaunchExecutionAsync(SelectedScheme, userAndUid).ConfigureAwait(false);

// AspectRatios might be updated during the launch
await taskContext.SwitchToMainThreadAsync();
AspectRatios = LaunchOptions.AspectRatios;
}

[Command("DetectGameAccountCommand")]
Expand Down

0 comments on commit 23ac4cf

Please sign in to comment.