Skip to content

Commit

Permalink
Merge pull request #29950 from minetoblend/feature/freeze-select-box-…
Browse files Browse the repository at this point in the history
…buttons

Freeze select box buttons position on press
  • Loading branch information
bdach authored Sep 27, 2024
2 parents eb8cc7f + 99a80b3 commit 766d2d2
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
78 changes: 68 additions & 10 deletions osu.Game/Screens/Edit/Compose/Components/SelectionBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -284,8 +285,12 @@ private SelectionBoxButton addButton(IconUsage icon, string tooltip, Action acti
Action = action
};

button.OperationStarted += freezeButtonPosition;
button.HoverLost += unfreezeButtonPosition;

button.OperationStarted += operationStarted;
button.OperationEnded += operationEnded;

buttons.Add(button);

return button;
Expand Down Expand Up @@ -357,9 +362,35 @@ private void operationStarted()
OperationStarted?.Invoke();
}

private void ensureButtonsOnScreen()
private Vector2? frozenButtonsPosition;

private void freezeButtonPosition()
{
frozenButtonsPosition = buttons.ScreenSpaceDrawQuad.TopLeft;
}

private void unfreezeButtonPosition()
{
buttons.Position = Vector2.Zero;
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;

Expand All @@ -374,24 +405,51 @@ private void ensureButtonsOnScreen()

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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public sealed partial class SelectionBoxButton : SelectionBoxControl, IHasToolti

public Action? Action;

public event Action? HoverLost;

public SelectionBoxButton(IconUsage iconUsage, string tooltip)
{
this.iconUsage = iconUsage;
Expand Down Expand Up @@ -61,6 +63,13 @@ protected override void UpdateHoverState()
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; }
}
}

0 comments on commit 766d2d2

Please sign in to comment.