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

Fix initial skin state being stored wrong to undo history #30060

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
73 changes: 73 additions & 0 deletions osu.Game.Tests/Visual/Navigation/TestSceneSkinEditorNavigation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

using System;
using System.Linq;
using Newtonsoft.Json;
using NUnit.Framework;
using osu.Framework.Allocation;
using osu.Framework.Extensions;
Expand All @@ -23,6 +24,7 @@
using osu.Game.Rulesets.Osu.Mods;
using osu.Game.Screens.Edit.Components;
using osu.Game.Screens.Play;
using osu.Game.Screens.Play.HUD;
using osu.Game.Screens.Play.HUD.HitErrorMeters;
using osu.Game.Skinning;
using osu.Game.Tests.Beatmaps.IO;
Expand Down Expand Up @@ -101,6 +103,77 @@ public void TestMutateProtectedSkinDuringGameplay()
AddUntilStep("current skin is mutable", () => !Game.Dependencies.Get<SkinManager>().CurrentSkin.Value.SkinInfo.Value.Protected);
}

[Test]
public void TestMutateProtectedSkinFromMainMenu_UndoToInitialStateIsCorrect()
{
AddStep("set default skin", () => Game.Dependencies.Get<SkinManager>().CurrentSkinInfo.SetDefault());
AddStep("import beatmap", () => BeatmapImportHelper.LoadQuickOszIntoOsu(Game).WaitSafely());

openSkinEditor();
AddUntilStep("current skin is mutable", () => !Game.Dependencies.Get<SkinManager>().CurrentSkin.Value.SkinInfo.Value.Protected);

AddUntilStep("wait for player", () =>
{
DismissAnyNotifications();
return Game.ScreenStack.CurrentScreen is Player;
});

string state = string.Empty;

AddUntilStep("wait for accuracy counter", () => Game.ChildrenOfType<ArgonAccuracyCounter>().Any(counter => counter.IsLoaded));
AddStep("dump state of accuracy meter", () => state = JsonConvert.SerializeObject(Game.ChildrenOfType<ArgonAccuracyCounter>().First().CreateSerialisedInfo()));
AddStep("add any component", () => Game.ChildrenOfType<SkinComponentToolbox.ToolboxComponentButton>().First().TriggerClick());
AddStep("undo", () =>
{
InputManager.PressKey(Key.ControlLeft);
InputManager.Key(Key.Z);
InputManager.ReleaseKey(Key.ControlLeft);
});
AddUntilStep("only one accuracy meter left",
() => Game.ChildrenOfType<Player>().Single().ChildrenOfType<ArgonAccuracyCounter>().Count(),
() => Is.EqualTo(1));
AddAssert("accuracy meter state unchanged",
() => JsonConvert.SerializeObject(Game.ChildrenOfType<ArgonAccuracyCounter>().First().CreateSerialisedInfo()),
() => Is.EqualTo(state));
}

[Test]
public void TestMutateProtectedSkinFromPlayer_UndoToInitialStateIsCorrect()
{
AddStep("set default skin", () => Game.Dependencies.Get<SkinManager>().CurrentSkinInfo.SetDefault());
AddStep("import beatmap", () => BeatmapImportHelper.LoadQuickOszIntoOsu(Game).WaitSafely());
advanceToSongSelect();
AddUntilStep("wait for selected", () => !Game.Beatmap.IsDefault);

AddStep("enable NF", () => Game.SelectedMods.Value = new[] { new OsuModNoFail() });
AddStep("enter gameplay", () => InputManager.Key(Key.Enter));

AddUntilStep("wait for player", () =>
{
DismissAnyNotifications();
return Game.ScreenStack.CurrentScreen is Player;
});
openSkinEditor();

string state = string.Empty;

AddUntilStep("wait for accuracy counter", () => Game.ChildrenOfType<ArgonAccuracyCounter>().Any(counter => counter.IsLoaded));
AddStep("dump state of accuracy meter", () => state = JsonConvert.SerializeObject(Game.ChildrenOfType<ArgonAccuracyCounter>().First().CreateSerialisedInfo()));
AddStep("add any component", () => Game.ChildrenOfType<SkinComponentToolbox.ToolboxComponentButton>().First().TriggerClick());
AddStep("undo", () =>
{
InputManager.PressKey(Key.ControlLeft);
InputManager.Key(Key.Z);
InputManager.ReleaseKey(Key.ControlLeft);
});
AddUntilStep("only one accuracy meter left",
() => Game.ChildrenOfType<Player>().Single().ChildrenOfType<ArgonAccuracyCounter>().Count(),
() => Is.EqualTo(1));
AddAssert("accuracy meter state unchanged",
() => JsonConvert.SerializeObject(Game.ChildrenOfType<ArgonAccuracyCounter>().First().CreateSerialisedInfo()),
() => Is.EqualTo(state));
}

[Test]
public void TestComponentsDeselectedOnSkinEditorHide()
{
Expand Down
39 changes: 29 additions & 10 deletions osu.Game/Overlays/SkinEditor/SkinEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,9 +353,10 @@ private void targetChanged(ValueChangedEvent<GlobalSkinnableContainerLookup?> ta
return;
}

changeHandler = new SkinEditorChangeHandler(skinComponentsContainer);
changeHandler.CanUndo.BindValueChanged(v => undoMenuItem.Action.Disabled = !v.NewValue, true);
changeHandler.CanRedo.BindValueChanged(v => redoMenuItem.Action.Disabled = !v.NewValue, true);
if (skinComponentsContainer.IsLoaded)
bindChangeHandler(skinComponentsContainer);
else
skinComponentsContainer.OnLoadComplete += d => Schedule(() => bindChangeHandler((SkinnableContainer)d));

content.Child = new SkinBlueprintContainer(skinComponentsContainer);

Expand Down Expand Up @@ -397,10 +398,21 @@ void requestPlacement(Type type)
SelectedComponents.Clear();
placeComponent(component);
}

void bindChangeHandler(SkinnableContainer skinnableContainer)
{
changeHandler = new SkinEditorChangeHandler(skinnableContainer);
changeHandler.CanUndo.BindValueChanged(v => undoMenuItem.Action.Disabled = !v.NewValue, true);
changeHandler.CanRedo.BindValueChanged(v => redoMenuItem.Action.Disabled = !v.NewValue, true);
}
}

private void skinChanged()
{
if (skins.EnsureMutableSkin())
// Another skin changed event will arrive which will complete the process.
return;

headerText.Clear();

headerText.AddParagraph(SkinEditorStrings.SkinEditor, cp => cp.Font = OsuFont.Default.With(size: 16));
Expand All @@ -418,17 +430,24 @@ private void skinChanged()
});

changeHandler?.Dispose();
changeHandler = null;

skins.EnsureMutableSkin();
// Schedule is required to ensure that all layout in `LoadComplete` methods has been completed
// before storing an undo state.
//
// See https://github.com/ppy/osu/blob/8e6a4559e3ae8c9892866cf9cf8d4e8d1b72afd0/osu.Game/Skinning/SkinReloadableDrawable.cs#L76.
Schedule(() =>
{
var targetContainer = getTarget(selectedTarget.Value);

var targetContainer = getTarget(selectedTarget.Value);
if (targetContainer != null)
changeHandler = new SkinEditorChangeHandler(targetContainer);

if (targetContainer != null)
changeHandler = new SkinEditorChangeHandler(targetContainer);
hasBegunMutating = true;
hasBegunMutating = true;

// Reload sidebar components.
selectedTarget.TriggerChange();
// Reload sidebar components.
selectedTarget.TriggerChange();
});
}

/// <summary>
Expand Down
2 changes: 1 addition & 1 deletion osu.Game/Overlays/SkinEditor/SkinEditorChangeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public SkinEditorChangeHandler(Drawable targetScreen)
return;

components = new BindableList<ISerialisableDrawable> { BindTarget = firstTarget.Components };
components.BindCollectionChanged((_, _) => SaveState());
components.BindCollectionChanged((_, _) => SaveState(), true);
}

protected override void WriteCurrentStateToStream(MemoryStream stream)
Expand Down
Loading