Skip to content

Commit

Permalink
UI: Fixed window size rounding issues when setting video scale
Browse files Browse the repository at this point in the history
  • Loading branch information
SourMesen committed Nov 11, 2024
1 parent e18db92 commit 7b84a75
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 29 deletions.
8 changes: 6 additions & 2 deletions UI/ViewModels/HistoryViewerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ public void InitActions(HistoryViewerWindow wnd)
GetScaleMenuItem(wnd, 3),
GetScaleMenuItem(wnd, 4),
GetScaleMenuItem(wnd, 5),
GetScaleMenuItem(wnd, 6)
GetScaleMenuItem(wnd, 6),
GetScaleMenuItem(wnd, 7),
GetScaleMenuItem(wnd, 8),
GetScaleMenuItem(wnd, 9),
GetScaleMenuItem(wnd, 10)
}
}
};
Expand Down Expand Up @@ -195,7 +199,7 @@ private ContextMenuAction GetScaleMenuItem(HistoryViewerWindow wnd, int scale)
return new ContextMenuAction() {
ActionType = ActionType.Custom,
CustomText = scale + "x",
Shortcut = () => new DbgShortKeys(KeyModifiers.Alt, Key.D0 + scale),
Shortcut = () => new DbgShortKeys(KeyModifiers.Alt, scale == 10 ? Key.D0 : Key.D0 + scale),
OnClick = () => wnd.SetScale(scale),
IsSelected = () => (int)((double)RendererSize.Height / EmuApi.GetBaseScreenSize().Height) == scale
};
Expand Down
24 changes: 10 additions & 14 deletions UI/Windows/HistoryViewerWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,21 +147,20 @@ public void SetScale(double scale)
private void InternalSetScale(double scale)
{
double dpiScale = LayoutHelper.GetLayoutScale(this);
scale /= dpiScale;
double aspectRatio = EmuApi.GetAspectRatio();

FrameInfo screenSize = EmuApi.GetBaseScreenSize();
if(WindowState == WindowState.Normal) {
_rendererSize = new Size();

double aspectRatio = EmuApi.GetAspectRatio();
double menuHeight = _mainMenu.Bounds.Height;

double width = Math.Max(MinWidth, Math.Round(screenSize.Height * aspectRatio) * scale);
double height = Math.Max(MinHeight, screenSize.Height * scale);
double width = Math.Max(MinWidth, Math.Round(screenSize.Height * aspectRatio * scale) / dpiScale);
double height = Math.Max(MinHeight, screenSize.Height * scale / dpiScale);
ClientSize = new Size(width, height + menuHeight + _controlBar.Bounds.Height);
ResizeRenderer();
} else if(WindowState == WindowState.Maximized || WindowState == WindowState.FullScreen) {
_rendererSize = new Size(Math.Floor(screenSize.Width * scale), Math.Floor(screenSize.Height * scale));
_rendererSize = new Size(Math.Floor(screenSize.Width * scale * aspectRatio) / dpiScale, Math.Floor(screenSize.Height * scale) / dpiScale);
ResizeRenderer();
}
}
Expand All @@ -175,27 +174,24 @@ private void ResizeRenderer()
private void RendererPanel_LayoutUpdated(object? sender, EventArgs e)
{
double aspectRatio = EmuApi.GetAspectRatio();
double dpiScale = LayoutHelper.GetLayoutScale(this);

Size finalSize = _rendererSize == default ? _rendererPanel.Bounds.Size : _rendererSize;
double height = finalSize.Height;
double width = finalSize.Height * aspectRatio;
if(width > finalSize.Width) {
width = finalSize.Width;
height = width / aspectRatio;
}


if(ConfigManager.Config.Video.FullscreenForceIntegerScale && VisualRoot is Window wnd && (wnd.WindowState == WindowState.FullScreen || wnd.WindowState == WindowState.Maximized)) {
FrameInfo baseSize = EmuApi.GetBaseScreenSize();
double scale = height * LayoutHelper.GetLayoutScale(this) / baseSize.Height;
double scale = height * dpiScale / baseSize.Height;
if(scale != Math.Floor(scale)) {
height = baseSize.Height * Math.Max(1, Math.Floor(scale / LayoutHelper.GetLayoutScale(this)));
height = baseSize.Height * Math.Max(1, Math.Floor(scale / dpiScale));
width = height * aspectRatio;
}
}

_model.RendererSize = new Size(
Math.Round(width * LayoutHelper.GetLayoutScale(this)),
Math.Round(height * LayoutHelper.GetLayoutScale(this))
Math.Round(width * dpiScale),
Math.Round(height * dpiScale)
);

_renderer.Width = width;
Expand Down
22 changes: 9 additions & 13 deletions UI/Windows/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -427,23 +427,22 @@ public void SetScale(double scale)
private void InternalSetScale(double scale)
{
double dpiScale = LayoutHelper.GetLayoutScale(this);
scale /= dpiScale;
double aspectRatio = EmuApi.GetAspectRatio();

FrameInfo screenSize = EmuApi.GetBaseScreenSize();
if(WindowState == WindowState.Normal) {
_rendererSize = new Size();

double aspectRatio = EmuApi.GetAspectRatio();

//When menu is set to auto-hide, don't count its height when calculating the window's final size
double menuHeight = ConfigManager.Config.Preferences.AutoHideMenu ? 0 : _mainMenu.Bounds.Height;

double width = Math.Max(MinWidth, Math.Round(screenSize.Height * aspectRatio) * scale);
double height = Math.Max(MinHeight, screenSize.Height * scale);
double width = Math.Max(MinWidth, Math.Round(screenSize.Height * aspectRatio * scale) / dpiScale);
double height = Math.Max(MinHeight, screenSize.Height * scale / dpiScale);
ClientSize = new Size(width, height + menuHeight + _audioPlayer.Bounds.Height);
ResizeRenderer();
} else if(WindowState == WindowState.Maximized || WindowState == WindowState.FullScreen) {
_rendererSize = new Size(Math.Floor(screenSize.Width * scale), Math.Floor(screenSize.Height * scale));
_rendererSize = new Size(Math.Round(screenSize.Width * scale * aspectRatio) / dpiScale, Math.Round(screenSize.Height * scale) / dpiScale);
ResizeRenderer();
}
}
Expand All @@ -457,26 +456,23 @@ private void ResizeRenderer()
private void RendererPanel_LayoutUpdated(object? sender, EventArgs e)
{
double aspectRatio = EmuApi.GetAspectRatio();
double dpiScale = LayoutHelper.GetLayoutScale(this);

Size finalSize = _rendererSize == default ? _rendererPanel.Bounds.Size : _rendererSize;
double height = finalSize.Height;
double width = finalSize.Height * aspectRatio;
if(width > finalSize.Width) {
width = finalSize.Width;
height = width / aspectRatio;
}

if(ConfigManager.Config.Video.FullscreenForceIntegerScale && VisualRoot is Window wnd && (wnd.WindowState == WindowState.FullScreen || wnd.WindowState == WindowState.Maximized)) {
FrameInfo baseSize = EmuApi.GetBaseScreenSize();
double scale = height * LayoutHelper.GetLayoutScale(this) / baseSize.Height;
double scale = height * dpiScale / baseSize.Height;
if(scale != Math.Floor(scale)) {
height = baseSize.Height * Math.Max(1, Math.Floor(scale / LayoutHelper.GetLayoutScale(this)));
height = baseSize.Height * Math.Max(1, Math.Floor(scale / dpiScale));
width = height * aspectRatio;
}
}

uint realWidth = (uint)Math.Round(width * LayoutHelper.GetLayoutScale(this));
uint realHeight = (uint)Math.Round(height * LayoutHelper.GetLayoutScale(this));
uint realWidth = (uint)Math.Round(width * dpiScale);
uint realHeight = (uint)Math.Round(height * dpiScale);
EmuApi.SetRendererSize(realWidth, realHeight);
_model.RendererSize = new Size(realWidth, realHeight);

Expand Down

0 comments on commit 7b84a75

Please sign in to comment.