Skip to content

Commit

Permalink
Fix TabContainer click detection when UIScale was not == 1.0 (#5456)
Browse files Browse the repository at this point in the history
* Fix tabcontainer click detection when UIScale was not == 1.0

* Remove whitespace

---------

Co-authored-by: Eoin Mcloughlin <[email protected]>
  • Loading branch information
eoineoineoin and Eoin Mcloughlin committed Sep 22, 2024
1 parent 46291af commit e714dcc
Showing 1 changed file with 12 additions and 33 deletions.
45 changes: 12 additions & 33 deletions Robust.Client/UserInterface/Controls/TabContainer.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Numerics;
using Robust.Client.Graphics;
using Robust.Shared.Input;
Expand All @@ -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<float> _tabRight = new();

public int CurrentTab
{
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -214,6 +220,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);
}
}

Expand Down Expand Up @@ -283,46 +291,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))
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];
}
}

Expand Down

0 comments on commit e714dcc

Please sign in to comment.