mirror of
https://github.com/osukey/osukey.git
synced 2025-07-02 00:40:09 +09:00
Allow selected score to be programmatically changed
This commit is contained in:
@ -1,6 +1,7 @@
|
|||||||
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
// 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.
|
// See the LICENCE file in the repository root for full licence text.
|
||||||
|
|
||||||
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
@ -9,58 +10,121 @@ using osu.Framework.Utils;
|
|||||||
using osu.Game.Rulesets.Osu;
|
using osu.Game.Rulesets.Osu;
|
||||||
using osu.Game.Scoring;
|
using osu.Game.Scoring;
|
||||||
using osu.Game.Screens.Ranking;
|
using osu.Game.Screens.Ranking;
|
||||||
using osuTK.Input;
|
|
||||||
|
|
||||||
namespace osu.Game.Tests.Visual.Ranking
|
namespace osu.Game.Tests.Visual.Ranking
|
||||||
{
|
{
|
||||||
public class TestSceneScorePanelList : OsuManualInputManagerTestScene
|
public class TestSceneScorePanelList : OsuManualInputManagerTestScene
|
||||||
{
|
{
|
||||||
private ScoreInfo initialScore;
|
|
||||||
private ScorePanelList list;
|
private ScorePanelList list;
|
||||||
|
|
||||||
[SetUp]
|
[Test]
|
||||||
public void Setup() => Schedule(() =>
|
public void TestEmptyList()
|
||||||
{
|
{
|
||||||
Child = list = new ScorePanelList(initialScore = new TestScoreInfo(new OsuRuleset().RulesetInfo))
|
createListStep(() => new ScorePanelList());
|
||||||
{
|
}
|
||||||
Anchor = Anchor.Centre,
|
|
||||||
Origin = Anchor.Centre,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
[Test]
|
[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();
|
assertPanelCentred();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestAddManyScoresAfter()
|
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++)
|
for (int i = 0; i < 20; i++)
|
||||||
list.AddScore(new TestScoreInfo(new OsuRuleset().RulesetInfo) { TotalScore = initialScore.TotalScore - i - 1 });
|
list.AddScore(new TestScoreInfo(new OsuRuleset().RulesetInfo) { TotalScore = initialScore.TotalScore - i - 1 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
assertScoreState(initialScore, true);
|
||||||
assertPanelCentred();
|
assertPanelCentred();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestAddManyScoresBefore()
|
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", () =>
|
AddStep("add scores", () =>
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 20; i++)
|
for (int i = 0; i < 20; i++)
|
||||||
list.AddScore(new TestScoreInfo(new OsuRuleset().RulesetInfo) { TotalScore = initialScore.TotalScore + i + 1 });
|
list.AddScore(new TestScoreInfo(new OsuRuleset().RulesetInfo) { TotalScore = initialScore.TotalScore + i + 1 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
assertScoreState(initialScore, true);
|
||||||
assertPanelCentred();
|
assertPanelCentred();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void TestAddManyPanelsOnBothSides()
|
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", () =>
|
AddStep("add scores after", () =>
|
||||||
{
|
{
|
||||||
for (int i = 0; i < 20; i++)
|
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 });
|
list.AddScore(new TestScoreInfo(new OsuRuleset().RulesetInfo) { TotalScore = initialScore.TotalScore + i + 1 });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
assertScoreState(initialScore, true);
|
||||||
assertPanelCentred();
|
assertPanelCentred();
|
||||||
}
|
}
|
||||||
|
|
||||||
[Test]
|
private void createListStep(Func<ScorePanelList> creationFunc)
|
||||||
public void TestNullScore()
|
|
||||||
{
|
{
|
||||||
AddStep("create panel with null score", () =>
|
AddStep("create list", () => Child = list = creationFunc().With(d =>
|
||||||
{
|
{
|
||||||
Child = list = new ScorePanelList(null)
|
d.Anchor = Anchor.Centre;
|
||||||
{
|
d.Origin = Anchor.Centre;
|
||||||
Anchor = Anchor.Centre,
|
}));
|
||||||
Origin = Anchor.Centre,
|
|
||||||
};
|
|
||||||
});
|
|
||||||
|
|
||||||
AddStep("add many panels", () =>
|
AddUntilStep("wait for load", () => list.IsLoaded);
|
||||||
{
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertPanelCentred() => AddUntilStep("expanded panel centred", () =>
|
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);
|
var expandedPanel = list.ChildrenOfType<ScorePanel>().Single(p => p.State == PanelState.Expanded);
|
||||||
return Precision.AlmostEquals(expandedPanel.ScreenSpaceDrawQuad.Centre.X, list.ScreenSpaceDrawQuad.Centre.X, 1);
|
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -63,9 +63,10 @@ namespace osu.Game.Screens.Ranking
|
|||||||
{
|
{
|
||||||
new ResultsScrollContainer
|
new ResultsScrollContainer
|
||||||
{
|
{
|
||||||
Child = panels = new ScorePanelList(Score)
|
Child = panels = new ScorePanelList
|
||||||
{
|
{
|
||||||
RelativeSizeAxes = Axes.Both,
|
RelativeSizeAxes = Axes.Both,
|
||||||
|
SelectedScore = { Value = Score }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using osu.Framework.Bindables;
|
||||||
using osu.Framework.Graphics;
|
using osu.Framework.Graphics;
|
||||||
using osu.Framework.Graphics.Containers;
|
using osu.Framework.Graphics.Containers;
|
||||||
using osu.Game.Graphics.Containers;
|
using osu.Game.Graphics.Containers;
|
||||||
@ -23,12 +24,13 @@ namespace osu.Game.Screens.Ranking
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
private const float expanded_panel_spacing = 15;
|
private const float expanded_panel_spacing = 15;
|
||||||
|
|
||||||
|
public readonly Bindable<ScoreInfo> SelectedScore = new Bindable<ScoreInfo>();
|
||||||
|
|
||||||
private readonly Flow flow;
|
private readonly Flow flow;
|
||||||
private readonly Scroll scroll;
|
private readonly Scroll scroll;
|
||||||
|
|
||||||
private ScorePanel expandedPanel;
|
private ScorePanel expandedPanel;
|
||||||
|
|
||||||
public ScorePanelList(ScoreInfo initialScore)
|
public ScorePanelList()
|
||||||
{
|
{
|
||||||
RelativeSizeAxes = Axes.Both;
|
RelativeSizeAxes = Axes.Both;
|
||||||
|
|
||||||
@ -44,12 +46,13 @@ namespace osu.Game.Screens.Ranking
|
|||||||
AutoSizeAxes = Axes.Both,
|
AutoSizeAxes = Axes.Both,
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
|
|
||||||
if (initialScore != null)
|
protected override void LoadComplete()
|
||||||
{
|
{
|
||||||
AddScore(initialScore);
|
base.LoadComplete();
|
||||||
presentScore(initialScore);
|
|
||||||
}
|
SelectedScore.BindValueChanged(selectedScoreChanged, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -67,19 +70,24 @@ namespace osu.Game.Screens.Ranking
|
|||||||
p.StateChanged += s =>
|
p.StateChanged += s =>
|
||||||
{
|
{
|
||||||
if (s == PanelState.Expanded)
|
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.
|
if (SelectedScore.Value == score)
|
||||||
// But when a panel is added before the expanded panel, we need to offset the scroll position by the width of the new panel.
|
selectedScoreChanged(new ValueChangedEvent<ScoreInfo>(SelectedScore.Value, SelectedScore.Value));
|
||||||
if (expandedPanel != null && flow.GetPanelIndex(score) < flow.GetPanelIndex(expandedPanel.Score))
|
else
|
||||||
{
|
{
|
||||||
// A somewhat hacky property is used here because we need to:
|
// 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.
|
||||||
// 1) Scroll after the scroll container's visible range is updated.
|
// But when a panel is added before the expanded panel, we need to offset the scroll position by the width of the new panel.
|
||||||
// 2) Scroll before the scroll container's scroll position is updated.
|
if (expandedPanel != null && flow.GetPanelIndex(score) < flow.GetPanelIndex(expandedPanel.Score))
|
||||||
// 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;
|
// 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.
|
/// Brings a <see cref="ScoreInfo"/> to the centre of the screen and expands it.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="score">The <see cref="ScoreInfo"/> to present.</param>
|
/// <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.
|
// 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.State = PanelState.Contracted;
|
||||||
p.Margin = new MarginPadding();
|
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.
|
// Expand the new panel.
|
||||||
expandedPanel = flow.Single(p => p.Score == score);
|
|
||||||
expandedPanel.State = PanelState.Expanded;
|
expandedPanel.State = PanelState.Expanded;
|
||||||
expandedPanel.Margin = new MarginPadding { Horizontal = expanded_panel_spacing };
|
expandedPanel.Margin = new MarginPadding { Horizontal = expanded_panel_spacing };
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user