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 TabContainer click detection when UIScale was not == 1.0 #5456

Merged
Merged
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
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
Loading