Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Separated HUD bugs and set limits to keep UI pretty #33482

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Content.Client/UserInterface/Controls/MainViewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public void UpdateCfg()
{
var toleranceMargin = i * cfgToleranceMargin;
var toleranceClip = i * cfgToleranceClip;
var scaled = (Vector2) Viewport.ViewportSize * i;
var scaled = (Vector2)Viewport.ViewportSize * i;
SpaceManiac marked this conversation as resolved.
Show resolved Hide resolved
var (dx, dy) = PixelSize - scaled;

// The rule for which snap fits is that at LEAST one axis needs to be in the tolerance size wise.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</BoxContainer>
<alerts:AlertsUI Name="Alerts" Access="Protected" />
</LayoutContainer>
<PanelContainer Name="SeparatedChatPanel" MinWidth="300">
<PanelContainer Name="SeparatedChatPanel" MinWidth="372">
<PanelContainer.PanelOverride>
<graphics:StyleBoxFlat BackgroundColor="#2B2C3B" />
</PanelContainer.PanelOverride>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,36 @@
using System.Numerics;
using Content.Client.UserInterface.Systems.Chat.Widgets;
using Content.Shared.CCVar;
using Robust.Client.AutoGenerated;
using Robust.Client.Graphics;
using Robust.Client.UserInterface;
using Robust.Client.UserInterface.Controls;
using Robust.Client.UserInterface.XAML;
using Robust.Shared;
using Robust.Shared.Configuration;

namespace Content.Client.UserInterface.Screens;

[GenerateTypedNameReferences]
public sealed partial class SeparatedChatGameScreen : InGameScreen
{
[Dependency] private readonly IConfigurationManager _cfg = default!;
[Dependency] private readonly IUserInterfaceManager _uiManager = default!;

// The minimum width of InventoryGui + HotbarGui combined.
// Used to keep the PDA from overlapping the shoes slot.
private const int INVENTORY_MINIMUM_WIDTH =
(125 /* width */ - 2 /* negative margin */) * 2 /* ItemStatusPanel */
+ 68 /* width */ * 10 /* ItemSlotButton, including inventory */
+ 67 /* width */ * 2 /* hands */;
Comment on lines +20 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These values are bound to fall out of line so you can't just rely on magic numbers like this.


private readonly float _chatMinWidth;
private float? _deferredSplitFraction;

public SeparatedChatGameScreen()
{
RobustXamlLoader.Load(this);
SpaceManiac marked this conversation as resolved.
Show resolved Hide resolved
IoCManager.InjectDependencies(this);

AutoscaleMaxResolution = new Vector2i(1080, 770);

Expand All @@ -28,6 +47,19 @@ public SeparatedChatGameScreen()
OnChatResized?.Invoke(new Vector2(ScreenContainer.SplitFraction, 0));

ViewportContainer.OnResized += ResizeActionContainer;

OnResized += RecalculateViewportDesiredSize;
// Note: currently these run after the like callbacks on MainViewport,
// and may misbehave if that changes.
_cfg.OnValueChanged(CCVars.ViewportMinimumWidth, _ => RecalculateViewportDesiredSize());
_cfg.OnValueChanged(CCVars.ViewportStretch, _ => RecalculateViewportDesiredSize());
_cfg.OnValueChanged(CCVars.ViewportSnapToleranceClip, _ => RecalculateViewportDesiredSize());
_cfg.OnValueChanged(CCVars.ViewportSnapToleranceMargin, _ => RecalculateViewportDesiredSize());
_cfg.OnValueChanged(CCVars.ViewportScaleRender, _ => RecalculateViewportDesiredSize());
_cfg.OnValueChanged(CCVars.ViewportFixedScaleFactor, _ => RecalculateViewportDesiredSize());
_cfg.OnValueChanged(CVars.DisplayUIScale, _ => RecalculateViewportDesiredSize());

_chatMinWidth = SeparatedChatPanel.MinWidth;
}

private void ResizeActionContainer()
Expand All @@ -36,10 +68,73 @@ private void ResizeActionContainer()
Actions.ActionsContainer.MaxGridWidth = ViewportContainer.Size.X - indent;
}

private void RecalculateViewportDesiredSize()
{
// When the main window is resized, calculate a new min/max for the
// ViewportContainer so that the chat will prefer to resize instead
// of letterboxing the viewport.
var uiScale = _cfg.GetCVar(CVars.DisplayUIScale);
if (uiScale == 0f)
uiScale = _uiManager.DefaultUIScale;

// CurrentRenderScale would be more correct, but it gets updated after us.
var gameScale = CalculateRenderScale();
// svp.ViewportSize.X = EyeManager.PixelsPerMeter * gameScale * ViewportWidth
var minFromVp = EyeManager.PixelsPerMeter * gameScale * _cfg.GetCVar(CCVars.ViewportMinimumWidth) / uiScale;
var maxFromVp = EyeManager.PixelsPerMeter * gameScale * _cfg.GetCVar(CCVars.ViewportMaximumWidth) / uiScale;
// Inventory bar also supplies a minWidth such that body+shoes don't overlap PDA.
var minFromUi = INVENTORY_MINIMUM_WIDTH;
// In case of overscaled viewport, chat hard-minWidth overpowers them both.
var minFromChat = Width - _chatMinWidth;

var min = Math.Min(Math.Max(minFromVp, minFromUi), minFromChat);
var max = Math.Max(maxFromVp, min);

// SplitContainer doesn't respect MaxSize, so set a MinSize on the chat instead.
ViewportContainer.MinWidth = min;
SeparatedChatPanel.MinWidth = Math.Max(_chatMinWidth, Width - max);

if (_deferredSplitFraction is { } fraction)
{
ScreenContainer.SplitFraction = fraction;
_deferredSplitFraction = null;
}

// Correct for naughty behavior in SplitContainer by forcing a relayout.
ScreenContainer.SplitCenter = Math.Clamp(ScreenContainer.SplitCenter, min, max);
}

// Combines logic of EnsureViewportCreated and GetDrawBox to calculate the
// effective rendering scale.
private float CalculateRenderScale()
{
var svp = MainViewport.Viewport;

if (svp.FixedStretchSize is { } fixedAmt)
{
return fixedAmt.X / svp.ViewportSize.X;
}
else
{
// Always act like IgnoreDimension is Horizontal - otherwise the
// current separator position would influence the acceptable bounds
// of the separator position, which is weird.
return Height / svp.ViewportSize.Y;
}
}

public override ChatBox ChatBox => GetWidget<ChatBox>()!;

public override void SetChatSize(Vector2 size)
{
ScreenContainer.ResizeMode = SplitContainer.SplitResizeMode.RespectChildrenMinSize;

ScreenContainer.SplitFraction = size.X;
if (ScreenContainer.SplitFraction == 0)
{
// Failed because SplitContainer tried to lay out its children
// despite being 0x0 before first draw. Queue the size for later.
_deferredSplitFraction = size.X;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
BoundKey = "{x:Static ic:EngineKeyFunctions.EscapeMenu}"
ToolTip="{Loc 'game-hud-open-escape-menu-button-tooltip'}"
MinSize="70 64"
SizeFlagsStretchRatio="70"
SpaceManiac marked this conversation as resolved.
Show resolved Hide resolved
HorizontalExpand="True"
AppendStyleClass="{x:Static style:StyleBase.ButtonOpenRight}"
/>
Expand All @@ -30,6 +31,7 @@
ToolTip="{Loc 'game-hud-open-guide-menu-button-tooltip'}"
BoundKey = "{x:Static is:ContentKeyFunctions.OpenGuidebook}"
MinSize="42 64"
SizeFlagsStretchRatio="42"
HorizontalExpand="True"
AppendStyleClass="{x:Static style:StyleBase.ButtonSquare}"
/>
Expand All @@ -40,6 +42,7 @@
ToolTip="{Loc 'game-hud-open-character-menu-button-tooltip'}"
BoundKey = "{x:Static is:ContentKeyFunctions.OpenCharacterMenu}"
MinSize="42 64"
SizeFlagsStretchRatio="42"
HorizontalExpand="True"
AppendStyleClass="{x:Static style:StyleBase.ButtonSquare}"
/>
Expand All @@ -50,6 +53,7 @@
ToolTip="{Loc 'game-hud-open-emotes-menu-button-tooltip'}"
BoundKey = "{x:Static is:ContentKeyFunctions.OpenEmotesMenu}"
MinSize="42 64"
SizeFlagsStretchRatio="42"
HorizontalExpand="True"
AppendStyleClass="{x:Static style:StyleBase.ButtonSquare}"
/>
Expand All @@ -60,6 +64,7 @@
BoundKey = "{x:Static is:ContentKeyFunctions.OpenCraftingMenu}"
ToolTip="{Loc 'game-hud-open-crafting-menu-button-tooltip'}"
MinSize="42 64"
SizeFlagsStretchRatio="42"
HorizontalExpand="True"
AppendStyleClass="{x:Static style:StyleBase.ButtonSquare}"
/>
Expand All @@ -70,6 +75,7 @@
BoundKey = "{x:Static is:ContentKeyFunctions.OpenActionsMenu}"
ToolTip="{Loc 'game-hud-open-actions-menu-button-tooltip'}"
MinSize="42 64"
SizeFlagsStretchRatio="42"
HorizontalExpand="True"
AppendStyleClass="{x:Static style:StyleBase.ButtonSquare}"
/>
Expand All @@ -80,6 +86,7 @@
BoundKey = "{x:Static is:ContentKeyFunctions.OpenAdminMenu}"
ToolTip="{Loc 'game-hud-open-admin-menu-button-tooltip'}"
MinSize="42 64"
SizeFlagsStretchRatio="42"
HorizontalExpand="True"
AppendStyleClass="{x:Static style:StyleBase.ButtonSquare}"
/>
Expand All @@ -90,6 +97,7 @@
BoundKey = "{x:Static is:ContentKeyFunctions.OpenSandboxWindow}"
ToolTip="{Loc 'game-hud-open-sandbox-menu-button-tooltip'}"
MinSize="42 64"
SizeFlagsStretchRatio="42"
HorizontalExpand="True"
AppendStyleClass="{x:Static style:StyleBase.ButtonSquare}"
/>
Expand All @@ -100,6 +108,7 @@
BoundKey = "{x:Static is:ContentKeyFunctions.OpenAHelp}"
ToolTip="{Loc 'ui-options-function-open-a-help'}"
MinSize="42 64"
SizeFlagsStretchRatio="42"
HorizontalExpand="True"
AppendStyleClass="{x:Static style:StyleBase.ButtonOpenLeft}"
/>
Expand Down
Loading
Loading