Merge pull request #29950 from minetoblend/feature/freeze-select-box-buttons

Freeze select box buttons position on press
This commit is contained in:
Bartłomiej Dach 2024-09-27 10:13:06 +02:00 committed by GitHub
commit 766d2d2ad2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 77 additions and 10 deletions

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
@ -284,8 +285,12 @@ namespace osu.Game.Screens.Edit.Compose.Components
Action = action
};
button.OperationStarted += freezeButtonPosition;
button.HoverLost += unfreezeButtonPosition;
button.OperationStarted += operationStarted;
button.OperationEnded += operationEnded;
buttons.Add(button);
return button;
@ -357,9 +362,35 @@ namespace osu.Game.Screens.Edit.Compose.Components
OperationStarted?.Invoke();
}
private void ensureButtonsOnScreen()
private Vector2? frozenButtonsPosition;
private void freezeButtonPosition()
{
buttons.Position = Vector2.Zero;
frozenButtonsPosition = buttons.ScreenSpaceDrawQuad.TopLeft;
}
private void unfreezeButtonPosition()
{
if (frozenButtonsPosition != null)
{
frozenButtonsPosition = null;
ensureButtonsOnScreen(true);
}
}
private void ensureButtonsOnScreen(bool animated = false)
{
if (frozenButtonsPosition != null)
{
buttons.Anchor = Anchor.TopLeft;
buttons.Origin = Anchor.TopLeft;
buttons.Position = ToLocalSpace(frozenButtonsPosition.Value) - new Vector2(button_padding);
return;
}
if (!animated && buttons.Transforms.Any())
return;
var thisQuad = ScreenSpaceDrawQuad;
@ -374,24 +405,51 @@ namespace osu.Game.Screens.Edit.Compose.Components
float minHeight = buttons.ScreenSpaceDrawQuad.Height;
Anchor targetAnchor;
Anchor targetOrigin;
Vector2 targetPosition = Vector2.Zero;
if (topExcess < minHeight && bottomExcess < minHeight)
{
buttons.Anchor = Anchor.BottomCentre;
buttons.Origin = Anchor.BottomCentre;
buttons.Y = Math.Min(0, ToLocalSpace(Parent!.ScreenSpaceDrawQuad.BottomLeft).Y - DrawHeight);
targetAnchor = Anchor.BottomCentre;
targetOrigin = Anchor.BottomCentre;
targetPosition.Y = Math.Min(0, ToLocalSpace(Parent!.ScreenSpaceDrawQuad.BottomLeft).Y - DrawHeight);
}
else if (topExcess > bottomExcess)
{
buttons.Anchor = Anchor.TopCentre;
buttons.Origin = Anchor.BottomCentre;
targetAnchor = Anchor.TopCentre;
targetOrigin = Anchor.BottomCentre;
}
else
{
buttons.Anchor = Anchor.BottomCentre;
buttons.Origin = Anchor.TopCentre;
targetAnchor = Anchor.BottomCentre;
targetOrigin = Anchor.TopCentre;
}
buttons.X += ToLocalSpace(thisQuad.TopLeft - new Vector2(Math.Min(0, leftExcess)) + new Vector2(Math.Min(0, rightExcess))).X;
targetPosition.X += ToLocalSpace(thisQuad.TopLeft - new Vector2(Math.Min(0, leftExcess)) + new Vector2(Math.Min(0, rightExcess))).X;
if (animated)
{
var originalPosition = ToLocalSpace(buttons.ScreenSpaceDrawQuad.TopLeft);
buttons.Origin = targetOrigin;
buttons.Anchor = targetAnchor;
buttons.Position = targetPosition;
var newPosition = ToLocalSpace(buttons.ScreenSpaceDrawQuad.TopLeft);
var delta = newPosition - originalPosition;
buttons.Position -= delta;
buttons.MoveTo(targetPosition, 300, Easing.OutQuint);
}
else
{
buttons.Anchor = targetAnchor;
buttons.Origin = targetOrigin;
buttons.Position = targetPosition;
}
}
}
}

View File

@ -21,6 +21,8 @@ namespace osu.Game.Screens.Edit.Compose.Components
public Action? Action;
public event Action? HoverLost;
public SelectionBoxButton(IconUsage iconUsage, string tooltip)
{
this.iconUsage = iconUsage;
@ -61,6 +63,13 @@ namespace osu.Game.Screens.Edit.Compose.Components
icon.FadeColour(!IsHeld && IsHovered ? Color4.White : Color4.Black, TRANSFORM_DURATION, Easing.OutQuint);
}
protected override void OnHoverLost(HoverLostEvent e)
{
base.OnHoverLost(e);
HoverLost?.Invoke();
}
public LocalisableString TooltipText { get; }
}
}