1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-28 16:12:57 +08:00

Allow selected score to be programmatically changed

This commit is contained in:
smoogipoo 2020-05-28 21:08:47 +09:00
parent a55ce26130
commit 666cbd0f40
3 changed files with 120 additions and 62 deletions

View File

@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.
using System;
using System.Linq;
using NUnit.Framework;
using osu.Framework.Graphics;
@ -9,58 +10,121 @@ using osu.Framework.Utils;
using osu.Game.Rulesets.Osu;
using osu.Game.Scoring;
using osu.Game.Screens.Ranking;
using osuTK.Input;
namespace osu.Game.Tests.Visual.Ranking
{
public class TestSceneScorePanelList : OsuManualInputManagerTestScene
{
private ScoreInfo initialScore;
private ScorePanelList list;
[SetUp]
public void Setup() => Schedule(() =>
[Test]
public void TestEmptyList()
{
Child = list = new ScorePanelList(initialScore = new TestScoreInfo(new OsuRuleset().RulesetInfo))
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
};
});
createListStep(() => new ScorePanelList());
}
[Test]
public void TestSingleScore()
public void TestEmptyListWithSelectedScore()
{
createListStep(() => new ScorePanelList
{
SelectedScore = { Value = new TestScoreInfo(new OsuRuleset().RulesetInfo) }
});
}
[Test]
public void TestAddPanelAfterSelectingScore()
{
var score = new TestScoreInfo(new OsuRuleset().RulesetInfo);
createListStep(() => new ScorePanelList
{
SelectedScore = { Value = score }
});
AddStep("add panel", () => list.AddScore(score));
assertScoreState(score, true);
assertPanelCentred();
}
[Test]
public void TestAddPanelBeforeSelectingScore()
{
var score = new TestScoreInfo(new OsuRuleset().RulesetInfo);
createListStep(() => new ScorePanelList());
AddStep("add panel", () => list.AddScore(score));
assertScoreState(score, false);
assertPanelCentred();
AddStep("select score", () => list.SelectedScore.Value = score);
assertScoreState(score, true);
assertPanelCentred();
}
[Test]
public void TestAddManyScoresAfter()
{
AddStep("add scores", () =>
var initialScore = new TestScoreInfo(new OsuRuleset().RulesetInfo);
createListStep(() => new ScorePanelList());
AddStep("add initial panel and select", () =>
{
list.AddScore(initialScore);
list.SelectedScore.Value = initialScore;
});
AddStep("add many scores", () =>
{
for (int i = 0; i < 20; i++)
list.AddScore(new TestScoreInfo(new OsuRuleset().RulesetInfo) { TotalScore = initialScore.TotalScore - i - 1 });
});
assertScoreState(initialScore, true);
assertPanelCentred();
}
[Test]
public void TestAddManyScoresBefore()
{
var initialScore = new TestScoreInfo(new OsuRuleset().RulesetInfo);
createListStep(() => new ScorePanelList());
AddStep("add initial panel and select", () =>
{
list.AddScore(initialScore);
list.SelectedScore.Value = initialScore;
});
AddStep("add scores", () =>
{
for (int i = 0; i < 20; i++)
list.AddScore(new TestScoreInfo(new OsuRuleset().RulesetInfo) { TotalScore = initialScore.TotalScore + i + 1 });
});
assertScoreState(initialScore, true);
assertPanelCentred();
}
[Test]
public void TestAddManyPanelsOnBothSides()
{
var initialScore = new TestScoreInfo(new OsuRuleset().RulesetInfo);
createListStep(() => new ScorePanelList());
AddStep("add initial panel and select", () =>
{
list.AddScore(initialScore);
list.SelectedScore.Value = initialScore;
});
AddStep("add scores after", () =>
{
for (int i = 0; i < 20; i++)
@ -70,42 +134,19 @@ namespace osu.Game.Tests.Visual.Ranking
list.AddScore(new TestScoreInfo(new OsuRuleset().RulesetInfo) { TotalScore = initialScore.TotalScore + i + 1 });
});
assertScoreState(initialScore, true);
assertPanelCentred();
}
[Test]
public void TestNullScore()
private void createListStep(Func<ScorePanelList> creationFunc)
{
AddStep("create panel with null score", () =>
AddStep("create list", () => Child = list = creationFunc().With(d =>
{
Child = list = new ScorePanelList(null)
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
};
});
d.Anchor = Anchor.Centre;
d.Origin = Anchor.Centre;
}));
AddStep("add many panels", () =>
{
for (int i = 0; i < 20; i++)
list.AddScore(new TestScoreInfo(new OsuRuleset().RulesetInfo) { TotalScore = initialScore.TotalScore - i - 1 });
for (int i = 0; i < 20; i++)
list.AddScore(new TestScoreInfo(new OsuRuleset().RulesetInfo) { TotalScore = initialScore.TotalScore + i + 1 });
});
AddWaitStep("wait for panel animation", 5);
AddAssert("no panel selected", () => list.ChildrenOfType<ScorePanel>().All(p => p.State != PanelState.Expanded));
AddStep("expand second panel", () =>
{
var expandedPanel = list.ChildrenOfType<ScorePanel>().OrderBy(p => p.DrawPosition.X).ElementAt(1);
InputManager.MoveMouseTo(expandedPanel);
InputManager.Click(MouseButton.Left);
});
assertPanelCentred();
AddUntilStep("wait for load", () => list.IsLoaded);
}
private void assertPanelCentred() => AddUntilStep("expanded panel centred", () =>
@ -113,5 +154,8 @@ namespace osu.Game.Tests.Visual.Ranking
var expandedPanel = list.ChildrenOfType<ScorePanel>().Single(p => p.State == PanelState.Expanded);
return Precision.AlmostEquals(expandedPanel.ScreenSpaceDrawQuad.Centre.X, list.ScreenSpaceDrawQuad.Centre.X, 1);
});
private void assertScoreState(ScoreInfo score, bool expanded)
=> AddUntilStep($"correct score expanded = {expanded}", () => (list.ChildrenOfType<ScorePanel>().Single(p => p.Score == score).State == PanelState.Expanded) == expanded);
}
}

View File

@ -63,9 +63,10 @@ namespace osu.Game.Screens.Ranking
{
new ResultsScrollContainer
{
Child = panels = new ScorePanelList(Score)
Child = panels = new ScorePanelList
{
RelativeSizeAxes = Axes.Both,
SelectedScore = { Value = Score }
}
}
},

View File

@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Bindables;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Game.Graphics.Containers;
@ -23,12 +24,13 @@ namespace osu.Game.Screens.Ranking
/// </summary>
private const float expanded_panel_spacing = 15;
public readonly Bindable<ScoreInfo> SelectedScore = new Bindable<ScoreInfo>();
private readonly Flow flow;
private readonly Scroll scroll;
private ScorePanel expandedPanel;
public ScorePanelList(ScoreInfo initialScore)
public ScorePanelList()
{
RelativeSizeAxes = Axes.Both;
@ -44,12 +46,13 @@ namespace osu.Game.Screens.Ranking
AutoSizeAxes = Axes.Both,
}
};
}
if (initialScore != null)
{
AddScore(initialScore);
presentScore(initialScore);
}
protected override void LoadComplete()
{
base.LoadComplete();
SelectedScore.BindValueChanged(selectedScoreChanged, true);
}
/// <summary>
@ -67,19 +70,24 @@ namespace osu.Game.Screens.Ranking
p.StateChanged += s =>
{
if (s == PanelState.Expanded)
presentScore(score);
SelectedScore.Value = p.Score;
};
}));
// We want the scroll position to remain relative to the expanded panel. When a new panel is added after the expanded panel, nothing needs to be done.
// But when a panel is added before the expanded panel, we need to offset the scroll position by the width of the new panel.
if (expandedPanel != null && flow.GetPanelIndex(score) < flow.GetPanelIndex(expandedPanel.Score))
if (SelectedScore.Value == score)
selectedScoreChanged(new ValueChangedEvent<ScoreInfo>(SelectedScore.Value, SelectedScore.Value));
else
{
// A somewhat hacky property is used here because we need to:
// 1) Scroll after the scroll container's visible range is updated.
// 2) Scroll before the scroll container's scroll position is updated.
// Without this, we would have a 1-frame positioning error which looks very jarring.
scroll.InstantScrollTarget = (scroll.InstantScrollTarget ?? scroll.Target) + ScorePanel.CONTRACTED_WIDTH + panel_spacing;
// We want the scroll position to remain relative to the expanded panel. When a new panel is added after the expanded panel, nothing needs to be done.
// But when a panel is added before the expanded panel, we need to offset the scroll position by the width of the new panel.
if (expandedPanel != null && flow.GetPanelIndex(score) < flow.GetPanelIndex(expandedPanel.Score))
{
// A somewhat hacky property is used here because we need to:
// 1) Scroll after the scroll container's visible range is updated.
// 2) Scroll before the scroll container's scroll position is updated.
// Without this, we would have a 1-frame positioning error which looks very jarring.
scroll.InstantScrollTarget = (scroll.InstantScrollTarget ?? scroll.Target) + ScorePanel.CONTRACTED_WIDTH + panel_spacing;
}
}
}
@ -87,17 +95,22 @@ namespace osu.Game.Screens.Ranking
/// Brings a <see cref="ScoreInfo"/> to the centre of the screen and expands it.
/// </summary>
/// <param name="score">The <see cref="ScoreInfo"/> to present.</param>
private void presentScore(ScoreInfo score)
private void selectedScoreChanged(ValueChangedEvent<ScoreInfo> score)
{
// Contract the old panel.
foreach (var p in flow.Where(p => p.Score != score))
foreach (var p in flow.Where(p => p.Score != score.OldValue))
{
p.State = PanelState.Contracted;
p.Margin = new MarginPadding();
}
// Find the panel corresponding to the new score.
expandedPanel = flow.SingleOrDefault(p => p.Score == score.NewValue);
if (expandedPanel == null)
return;
// Expand the new panel.
expandedPanel = flow.Single(p => p.Score == score);
expandedPanel.State = PanelState.Expanded;
expandedPanel.Margin = new MarginPadding { Horizontal = expanded_panel_spacing };