From e714dcc83c5e967c9db19132561a4e025e2d6572 Mon Sep 17 00:00:00 2001 From: eoineoineoin Date: Sun, 22 Sep 2024 13:41:27 +0100 Subject: [PATCH] Fix TabContainer click detection when UIScale was not == 1.0 (#5456) * Fix tabcontainer click detection when UIScale was not == 1.0 * Remove whitespace --------- Co-authored-by: Eoin Mcloughlin --- .../UserInterface/Controls/TabContainer.cs | 45 +++++-------------- 1 file changed, 12 insertions(+), 33 deletions(-) diff --git a/Robust.Client/UserInterface/Controls/TabContainer.cs b/Robust.Client/UserInterface/Controls/TabContainer.cs index 623ac18ac..5ffd9724c 100644 --- a/Robust.Client/UserInterface/Controls/TabContainer.cs +++ b/Robust.Client/UserInterface/Controls/TabContainer.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Numerics; using Robust.Client.Graphics; using Robust.Shared.Input; @@ -21,6 +22,8 @@ namespace Robust.Client.UserInterface.Controls private int _currentTab; private bool _tabsVisible = true; + // The right-most coordinate of each tab header + private List _tabRight = new(); public int CurrentTab { @@ -157,11 +160,14 @@ namespace Robust.Client.UserInterface.Controls var headerOffset = 0f; + _tabRight.Clear(); + // Then, draw the tabs. for (var i = 0; i < ChildCount; i++) { if (!GetTabVisible(i)) { + _tabRight.Add(headerOffset); continue; } @@ -214,6 +220,8 @@ namespace Robust.Client.UserInterface.Controls } headerOffset += boxAdvance; + // Remember the right-most point of this tab, for testing clicked areas + _tabRight.Add(headerOffset); } } @@ -283,46 +291,17 @@ namespace Robust.Client.UserInterface.Controls args.Handle(); var relX = args.RelativePixelPosition.X; - - var font = _getFont(); - var boxActive = _getTabBoxActive(); - var boxInactive = _getTabBoxInactive(); - - var headerOffset = 0f; - + float tabLeft = 0; for (var i = 0; i < ChildCount; i++) { - if (!GetTabVisible(i)) + if (relX > tabLeft && relX <= _tabRight[i]) { - continue; - } - - var title = GetActualTabTitle(i); - - var titleLength = 0; - // Get string length. - foreach (var rune in title.EnumerateRunes()) - { - if (!font.TryGetCharMetrics(rune, UIScale, out var metrics)) - { - continue; - } - - titleLength += metrics.Advance; - } - - var active = _currentTab == i; - var box = active ? boxActive : boxInactive; - var boxAdvance = titleLength + (box?.MinimumSize.X ?? 0); - - if (headerOffset < relX && headerOffset + boxAdvance > relX) - { - // Got em. CurrentTab = i; return; } - headerOffset += boxAdvance; + // Next tab starts here + tabLeft = _tabRight[i]; } }