Skip to content

Commit

Permalink
Merge pull request #214 from stanriders/add-sorting
Browse files Browse the repository at this point in the history
Add sorting controls on Profile screen, add calculation cancellation with Esc key
  • Loading branch information
smoogipoo authored Aug 31, 2024
2 parents 085a853 + a445487 commit 5196551
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public partial class ExtendedProfileScore : CompositeDrawable

private const float performance_background_shear = 0.45f;

protected readonly ExtendedScore Score;
public readonly ExtendedScore Score;

[Resolved]
private OsuColour colours { get; set; }
Expand Down
12 changes: 12 additions & 0 deletions PerformanceCalculatorGUI/Components/ProfileSortCriteria.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) ppy Pty Ltd <[email protected]>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

namespace PerformanceCalculatorGUI.Components
{
public enum ProfileSortCriteria
{
Live,
Difference,
Local
}
}
92 changes: 87 additions & 5 deletions PerformanceCalculatorGUI/Screens/ProfileScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Input.Events;
using osu.Framework.Logging;
using osu.Game.Graphics.Containers;
using osu.Game.Graphics.UserInterfaceV2;
Expand All @@ -19,9 +20,11 @@
using osu.Game.Rulesets;
using osu.Game.Rulesets.Mods;
using osuTK.Graphics;
using osuTK.Input;
using PerformanceCalculatorGUI.Components;
using PerformanceCalculatorGUI.Components.TextBoxes;
using PerformanceCalculatorGUI.Configuration;
using ButtonState = PerformanceCalculatorGUI.Components.ButtonState;

namespace PerformanceCalculatorGUI.Screens
{
Expand All @@ -35,7 +38,7 @@ public partial class ProfileScreen : PerformanceCalculatorScreen

private GridContainer layout;

private FillFlowContainer scores;
private FillFlowContainer<ExtendedProfileScore> scores;

private LabelledTextBox usernameTextBox;
private Container userPanelContainer;
Expand All @@ -45,6 +48,9 @@ public partial class ProfileScreen : PerformanceCalculatorScreen

private CancellationTokenSource calculationCancellatonToken;

private OverlaySortTabControl<ProfileSortCriteria> sortingTabControl;
private readonly Bindable<ProfileSortCriteria> sorting = new Bindable<ProfileSortCriteria>(ProfileSortCriteria.Local);

[Resolved]
private NotificationDisplay notificationDisplay { get; set; }

Expand Down Expand Up @@ -78,7 +84,13 @@ private void load()
{
RelativeSizeAxes = Axes.Both,
ColumnDimensions = new[] { new Dimension() },
RowDimensions = new[] { new Dimension(GridSizeMode.Absolute, username_container_height), new Dimension(GridSizeMode.Absolute), new Dimension() },
RowDimensions = new[]
{
new Dimension(GridSizeMode.Absolute, username_container_height),
new Dimension(GridSizeMode.Absolute),
new Dimension(GridSizeMode.AutoSize),
new Dimension()
},
Content = new[]
{
new Drawable[]
Expand Down Expand Up @@ -128,11 +140,30 @@ private void load()
}
},
new Drawable[]
{
new Container
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Children = new Drawable[]
{
sortingTabControl = new OverlaySortTabControl<ProfileSortCriteria>
{
Anchor = Anchor.CentreRight,
Origin = Anchor.CentreRight,
Margin = new MarginPadding { Right = 22 },
Current = { BindTarget = sorting },
Alpha = 0
}
}
}
},
new Drawable[]
{
new OsuScrollContainer(Direction.Vertical)
{
RelativeSizeAxes = Axes.Both,
Child = scores = new FillFlowContainer
Child = scores = new FillFlowContainer<ExtendedProfileScore>
{
RelativeSizeAxes = Axes.X,
AutoSizeAxes = Axes.Y,
Expand All @@ -149,6 +180,7 @@ private void load()
};

usernameTextBox.OnCommit += (_, _) => { calculateProfile(usernameTextBox.Current.Value); };
sorting.ValueChanged += e => { updateSorting(e.NewValue); };

if (RuntimeInfo.IsDesktop)
HotReloadCallbackReceiver.CompilationFinished += _ => Schedule(() => { calculateProfile(currentUser); });
Expand Down Expand Up @@ -191,7 +223,16 @@ private void calculateProfile(string username)
RelativeSizeAxes = Axes.X
});
layout.RowDimensions = new[] { new Dimension(GridSizeMode.Absolute, username_container_height), new Dimension(GridSizeMode.AutoSize), new Dimension() };
sortingTabControl.Alpha = 1.0f;
sortingTabControl.Current.Value = ProfileSortCriteria.Local;
layout.RowDimensions = new[]
{
new Dimension(GridSizeMode.Absolute, username_container_height),
new Dimension(GridSizeMode.AutoSize),
new Dimension(GridSizeMode.AutoSize),
new Dimension()
};
});
if (token.IsCancellationRequested)
Expand Down Expand Up @@ -284,7 +325,7 @@ private void calculateProfile(string username)
loadingLayer.Hide();
calculationButton.State.Value = ButtonState.Done;
});
}, token);
}, TaskContinuationOptions.None);
}

protected override void Dispose(bool isDisposing)
Expand All @@ -295,5 +336,46 @@ protected override void Dispose(bool isDisposing)
calculationCancellatonToken?.Dispose();
calculationCancellatonToken = null;
}

protected override bool OnKeyDown(KeyDownEvent e)
{
if (e.Key == Key.Escape && !calculationCancellatonToken.IsCancellationRequested)
{
calculationCancellatonToken?.Cancel();
}

return base.OnKeyDown(e);
}

private void updateSorting(ProfileSortCriteria sortCriteria)
{
if (!scores.Children.Any())
return;

ExtendedProfileScore[] sortedScores;

switch (sortCriteria)
{
case ProfileSortCriteria.Live:
sortedScores = scores.Children.OrderByDescending(x => x.Score.LivePP).ToArray();
break;

case ProfileSortCriteria.Local:
sortedScores = scores.Children.OrderByDescending(x => x.Score.PerformanceAttributes.Total).ToArray();
break;

case ProfileSortCriteria.Difference:
sortedScores = scores.Children.OrderByDescending(x => x.Score.PerformanceAttributes.Total - x.Score.LivePP).ToArray();
break;

default:
throw new ArgumentOutOfRangeException(nameof(sortCriteria), sortCriteria, null);
}

for (int i = 0; i < sortedScores.Length; i++)
{
scores.SetLayoutPosition(sortedScores[i], i);
}
}
}
}

0 comments on commit 5196551

Please sign in to comment.