Skip to content

Commit 15afaae

Browse files
committed
UI: Preserve aspect ratio in game selection previews
1 parent 7037bdd commit 15afaae

File tree

4 files changed

+42
-7
lines changed

4 files changed

+42
-7
lines changed

Core/Shared/SaveStateManager.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,13 @@ void SaveStateManager::SaveRecentGame(string romName, string romPath, string pat
282282
romInfoStream << romName << std::endl;
283283
romInfoStream << romPath << std::endl;
284284
romInfoStream << patchPath << std::endl;
285+
286+
FrameInfo baseFrameSize = _emu->GetVideoDecoder()->GetBaseFrameInfo(true);
287+
double aspectRatio = _emu->GetSettings()->GetAspectRatio(_emu->GetRegion(), baseFrameSize);
288+
if(aspectRatio > 0) {
289+
romInfoStream << "aspectratio=" << aspectRatio << std::endl;
290+
}
291+
285292
writer.AddFile(romInfoStream, "RomInfo.txt");
286293
writer.Save();
287294
}

UI/Controls/ImageAspectRatio.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,37 @@
11
using Avalonia;
22
using Avalonia.Controls;
33
using System;
4-
using Avalonia.Interactivity;
5-
using Avalonia.Styling;
6-
using Avalonia.Input;
7-
using Mesen.Debugger.Utilities;
8-
using System.Globalization;
94

105
namespace Mesen.Controls
116
{
127
public class ImageAspectRatio : Image
138
{
149
protected override Type StyleKeyOverride => typeof(Image);
10+
11+
public static readonly StyledProperty<double> AspectRatioProperty = AvaloniaProperty.Register<StateGridEntry, double>(nameof(AspectRatio));
1512

1613
public ImageAspectRatio()
1714
{
1815
}
1916

17+
public double AspectRatio
18+
{
19+
get { return GetValue(AspectRatioProperty); }
20+
set { SetValue(AspectRatioProperty, value); }
21+
}
22+
2023
protected override Size ArrangeOverride(Size finalSize)
2124
{
2225
finalSize = base.ArrangeOverride(finalSize);
2326
if(Source == null) {
2427
return finalSize;
2528
}
2629

27-
double ratio = Source.Size.Width / Source.Size.Height;
30+
double ratio = AspectRatio;
31+
if(ratio == 0) {
32+
ratio = Source.Size.Width / Source.Size.Height;
33+
}
34+
2835
if(finalSize.Width >= finalSize.Height * ratio) {
2936
return new Size(finalSize.Height * ratio, finalSize.Height);
3037
} else {

UI/Controls/StateGridEntry.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
>
6060
<c:ImageAspectRatio
6161
Source="{Binding Image, ElementName=root}"
62+
AspectRatio="{Binding AspectRatio, ElementName=root}"
6263
RenderOptions.BitmapInterpolationMode="LowQuality"
6364
Stretch="Fill"
6465
StretchDirection="Both"

UI/Controls/StateGridEntry.axaml.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using Mesen.Localization;
99
using Mesen.Utilities;
1010
using Mesen.ViewModels;
11-
using ReactiveUI.Fody.Helpers;
1211
using System;
1312
using System.IO;
1413
using System.IO.Compression;
@@ -26,6 +25,7 @@ public class StateGridEntry : UserControl
2625
public static readonly StyledProperty<string> SubTitleProperty = AvaloniaProperty.Register<StateGridEntry, string>(nameof(SubTitle));
2726
public static readonly StyledProperty<bool> EnabledProperty = AvaloniaProperty.Register<StateGridEntry, bool>(nameof(Enabled));
2827
public static readonly StyledProperty<bool> IsActiveEntryProperty = AvaloniaProperty.Register<StateGridEntry, bool>(nameof(IsActiveEntry));
28+
public static readonly StyledProperty<double> AspectRatioProperty = AvaloniaProperty.Register<StateGridEntry, double>(nameof(AspectRatio));
2929

3030
public RecentGameInfo Entry
3131
{
@@ -39,6 +39,12 @@ public Bitmap? Image
3939
set { SetValue(ImageProperty, value); }
4040
}
4141

42+
public double AspectRatio
43+
{
44+
get { return GetValue(AspectRatioProperty); }
45+
set { SetValue(AspectRatioProperty, value); }
46+
}
47+
4248
public bool Enabled
4349
{
4450
get { return GetValue(EnabledProperty); }
@@ -120,6 +126,7 @@ public void Init()
120126
if(fileExists) {
121127
Task.Run(() => {
122128
Bitmap? img = null;
129+
double aspectRatio = 0;
123130
try {
124131
if(Path.GetExtension(game.FileName) == "." + FileDialogHelper.MesenSaveStateExt) {
125132
img = EmuApi.GetSaveStatePreview(game.FileName);
@@ -138,12 +145,25 @@ public void Init()
138145

139146
img = new Bitmap(ms);
140147
}
148+
149+
entry = zip.GetEntry("RomInfo.txt");
150+
if(entry != null) {
151+
using StreamReader reader = new StreamReader(entry.Open());
152+
string? line = null;
153+
while((line = reader.ReadLine()) != null) {
154+
if(line.StartsWith("aspectratio=")) {
155+
double.TryParse(line.Split('=')[1], out aspectRatio);
156+
break;
157+
}
158+
}
159+
}
141160
}
142161
}
143162
} catch { }
144163

145164
Dispatcher.UIThread.Post(() => {
146165
Image = img ?? StateGridEntry.EmptyImage;
166+
AspectRatio = aspectRatio;
147167
});
148168
});
149169
}

0 commit comments

Comments
 (0)