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

Freeze select box buttons position on press #29950

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
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
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; }
}
}
Loading