From 65184dce62de6d533fb4b982414e5746a19f75b8 Mon Sep 17 00:00:00 2001 From: Eoin Mcloughlin Date: Sun, 22 Sep 2024 11:38:17 +0100 Subject: [PATCH 1/2] Fix tabcontainer click detection when UIScale was not == 1.0 --- .../UserInterface/Controls/TabContainer.cs | 46 ++++++------------- 1 file changed, 13 insertions(+), 33 deletions(-) diff --git a/Robust.Client/UserInterface/Controls/TabContainer.cs b/Robust.Client/UserInterface/Controls/TabContainer.cs index 623ac18ac1a..96b2b32dd47 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 @@ public class TabContainer : Container 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 @@ protected internal override void Draw(DrawingHandleScreen handle) var headerOffset = 0f; + _tabRight.Clear(); + // Then, draw the tabs. for (var i = 0; i < ChildCount; i++) { if (!GetTabVisible(i)) { + _tabRight.Add(headerOffset); continue; } @@ -200,6 +206,7 @@ protected internal override void Draw(DrawingHandleScreen handle) contentBox = UIBox2.FromDimensions(topLeft, size); } + var baseLine = new Vector2(0, font.GetAscent(UIScale)) + contentBox.TopLeft; foreach (var rune in title.EnumerateRunes()) @@ -214,6 +221,8 @@ protected internal override void Draw(DrawingHandleScreen handle) } headerOffset += boxAdvance; + // Remember the right-most point of this tab, for testing clicked areas + _tabRight.Add(headerOffset); } } @@ -283,46 +292,17 @@ protected internal override void KeyBindDown(GUIBoundKeyEventArgs args) 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)) - { - 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) + if (relX > tabLeft && relX <= _tabRight[i]) { - // Got em. CurrentTab = i; return; } - headerOffset += boxAdvance; + // Next tab starts here + tabLeft = _tabRight[i]; } } From de4630071c81f0781f40379f0fbf1a7a8fd26f51 Mon Sep 17 00:00:00 2001 From: Eoin Mcloughlin Date: Sun, 22 Sep 2024 11:53:49 +0100 Subject: [PATCH 2/2] Remove whitespace --- Robust.Client/UserInterface/Controls/TabContainer.cs | 1 - 1 file changed, 1 deletion(-) diff --git a/Robust.Client/UserInterface/Controls/TabContainer.cs b/Robust.Client/UserInterface/Controls/TabContainer.cs index 96b2b32dd47..5ffd9724ce1 100644 --- a/Robust.Client/UserInterface/Controls/TabContainer.cs +++ b/Robust.Client/UserInterface/Controls/TabContainer.cs @@ -206,7 +206,6 @@ protected internal override void Draw(DrawingHandleScreen handle) contentBox = UIBox2.FromDimensions(topLeft, size); } - var baseLine = new Vector2(0, font.GetAscent(UIScale)) + contentBox.TopLeft; foreach (var rune in title.EnumerateRunes())