From a78a6f3a69059067e363299e4b7571f8cd1a22fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Max=20H=C3=BCbner?= <46070579+hbnrmx@users.noreply.github.com> Date: Sun, 3 May 2020 16:58:20 +0200 Subject: [PATCH] Jump to next page (#34) * find PageEnd * next page - open next page on PageEnd, allow for running start * next page - only trigger running start if previous page was running, fix skipping bugs * next page - fix load stutter caused by hack --- SeeSharp/Screens/Edit/EditScreen.cs | 4 +- SeeSharp/Screens/Play/PlayScreen.cs | 19 ++++++-- SeeSharp/Screens/Play/PlayZone.cs | 65 +++++++++++++++++++------ SeeSharp/Screens/Select/MenuItem.cs | 4 +- SeeSharp/Screens/Select/SelectScreen.cs | 20 ++++++-- 5 files changed, 85 insertions(+), 27 deletions(-) diff --git a/SeeSharp/Screens/Edit/EditScreen.cs b/SeeSharp/Screens/Edit/EditScreen.cs index c0a800b..82beac8 100644 --- a/SeeSharp/Screens/Edit/EditScreen.cs +++ b/SeeSharp/Screens/Edit/EditScreen.cs @@ -12,7 +12,7 @@ namespace SeeSharp.Screens.Edit { public class EditScreen : Screen { - public Action SetBarToFirstOrDefault; + public Action FinishedEditing; public Action Save; public EditScreen(BindablePage page) @@ -38,7 +38,7 @@ protected override bool OnKeyDown(KeyDownEvent e) case Key.Enter: case Key.KeypadEnter: case Key.Space: - SetBarToFirstOrDefault.Invoke(); + FinishedEditing.Invoke(); Save.Invoke(); this.Exit(); return true; diff --git a/SeeSharp/Screens/Play/PlayScreen.cs b/SeeSharp/Screens/Play/PlayScreen.cs index a527b4e..380013c 100644 --- a/SeeSharp/Screens/Play/PlayScreen.cs +++ b/SeeSharp/Screens/Play/PlayScreen.cs @@ -14,10 +14,12 @@ namespace SeeSharp.Screens.Play public class PlayScreen : Screen { public Action Save; + public Action NextPage; + private readonly BindablePage _page = new BindablePage(); private readonly PlayZone _playZone; - public PlayScreen(BindablePage page) + public PlayScreen(BindablePage page, bool runningStart = false) { _page.BindTo(page); @@ -31,11 +33,12 @@ public PlayScreen(BindablePage page) AddInternal(currentBar = new BarInfoText(_page)); AddInternal(speed = new SpeedInfoText(_page)); AddInternal(zoom = new ZoomInfoText(_page)); - AddInternal(_playZone = new PlayZone(_page) + AddInternal(_playZone = new PlayZone(_page, runningStart) { speedChanged = speed.UpdateInfo, zoomChanged = zoom.UpdateInfo, - currentBarChanged = currentBar.UpdateInfo + currentBarChanged = currentBar.UpdateInfo, + PageEnd = onPageEnd }); //skip right to EditScreen when empty @@ -46,7 +49,7 @@ public PlayScreen(BindablePage page) this.Push(new EditScreen(_page) { Save = Save, - SetBarToFirstOrDefault = _playZone.jumpToFirstBar + FinishedEditing = _playZone.jumpToFirstBar }); } }; @@ -65,7 +68,7 @@ protected override bool OnKeyDown(KeyDownEvent e) this.Push(new EditScreen(_page) { Save = Save, - SetBarToFirstOrDefault = _playZone.jumpToFirstBar + FinishedEditing = _playZone.jumpToFirstBar }); return true; @@ -86,5 +89,11 @@ public override void OnResuming(IScreen _) this.Exit(); } } + + private void onPageEnd(bool runningStart) + { + this.Exit(); + NextPage?.Invoke(_page.Value, runningStart); + } } } \ No newline at end of file diff --git a/SeeSharp/Screens/Play/PlayZone.cs b/SeeSharp/Screens/Play/PlayZone.cs index 25d1e5b..bede5b1 100644 --- a/SeeSharp/Screens/Play/PlayZone.cs +++ b/SeeSharp/Screens/Play/PlayZone.cs @@ -1,6 +1,5 @@ using System; using System.Linq; -using osu.Framework.Allocation; using osu.Framework.Bindables; using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; @@ -13,6 +12,7 @@ namespace SeeSharp.Screens.Play { public class PlayZone : Container { + public Action PageEnd; public Action zoomChanged; public Action speedChanged; public Action currentBarChanged; @@ -22,12 +22,14 @@ public class PlayZone : Container private readonly BindableFloat currentBar = new BindableFloat(); private readonly PageSprite spriteA, spriteB; private PageSprite spriteFront, spriteBack; - private bool running; + private bool _running; private const float PAGE_SEPARATOR_WIDTH = 3f; - public PlayZone(BindablePage page) + public PlayZone(BindablePage page, bool runningStart = false) { _page.BindTo(page); + + _running = runningStart; RelativeSizeAxes = Axes.Both; RelativePositionAxes = Axes.Both; Origin = Anchor.Centre; @@ -55,18 +57,26 @@ public PlayZone(BindablePage page) } }; - currentBar.ValueChanged += _ => resetBar(); + currentBar.ValueChanged += e => + { + resetBar(); + currentBarChanged?.Invoke(e.NewValue); + }; + zoomContainer.ScaleTo(_page.Value.Zoom.Value); } - - [BackgroundDependencyLoader] - private void Load() => Scheduler.AddDelayed(jumpToFirstBar, 100); + + protected override void LoadComplete() + { + base.LoadComplete(); + jumpToFirstBar(); + } protected override void Update() { base.Update(); - if (running) + if (_running) { //horizontal Panning float xOffset = (float) Time.Elapsed * _page.Value.Speed.Value / 10f; @@ -107,11 +117,30 @@ private void resetOrPreviousBar() jumpToPreviousBar(); } - public void jumpToFirstBar() => currentBar.Value = firstBar(); + public void jumpToFirstBar() + { + if (currentBar.Value == firstBar()) + { + currentBar.TriggerChange(); + } + else + { + currentBar.Value = firstBar(); + } + } private void jumpToPreviousBar(bool loop = false) => currentBar.Value = previousBar(loop); - private void jumpToNextBar(bool loop = false) => currentBar.Value = nextBar(loop); + private void jumpToNextBar(bool loop = false) + { + if (currentBar.Value == lastBar()) + { + PageEnd?.Invoke(_running); + return; + } + + currentBar.Value = nextBar(loop); + } private float firstBar() => _page.Value.Bars.DefaultIfEmpty(0.5f).First(); @@ -149,7 +178,15 @@ private void resetBar() spriteFront.Y = -(currentBar.Value - 0.5f) * spriteA.DrawHeight; spriteBack.X = spriteA.DrawWidth + PAGE_SEPARATOR_WIDTH; spriteBack.Y = -(nextBar(true) - 0.5f) * spriteA.DrawHeight; - currentBarChanged.Invoke(currentBar.Value); + + if (currentBar.Value == lastBar()) + { + spriteBack.Hide(); + } + else + { + spriteBack.Show(); + } } private void wrapAround() @@ -189,7 +226,7 @@ protected override bool OnKeyDown(KeyDownEvent e) switch (e.Key) { case Key.Space: - running = !running; + _running = !_running; return true; case Key.Up: @@ -205,7 +242,7 @@ protected override bool OnKeyDown(KeyDownEvent e) case Key.Left: case Key.A: case Key.BackSpace: - if (running) + if (_running) { adjustSpeed(-0.1f); return true; @@ -216,7 +253,7 @@ protected override bool OnKeyDown(KeyDownEvent e) case Key.Right: case Key.D: - if (running) + if (_running) { adjustSpeed(0.1f); return true; diff --git a/SeeSharp/Screens/Select/MenuItem.cs b/SeeSharp/Screens/Select/MenuItem.cs index 9ba7747..c8ef073 100644 --- a/SeeSharp/Screens/Select/MenuItem.cs +++ b/SeeSharp/Screens/Select/MenuItem.cs @@ -15,7 +15,7 @@ namespace SeeSharp.Screens.Select public class MenuItem : Container, IComparable { private readonly BindablePage _page = new BindablePage(); - public Action PageSelected; + public Action PageSelected; private SpriteText _text; public MenuItem(BindablePage page) @@ -67,7 +67,7 @@ private void load() protected override bool OnClick(ClickEvent e) { - PageSelected.Invoke(_page); + PageSelected.Invoke(_page, false); return true; } diff --git a/SeeSharp/Screens/Select/SelectScreen.cs b/SeeSharp/Screens/Select/SelectScreen.cs index f8d9c73..d104786 100644 --- a/SeeSharp/Screens/Select/SelectScreen.cs +++ b/SeeSharp/Screens/Select/SelectScreen.cs @@ -5,7 +5,6 @@ using osu.Framework.Graphics; using osu.Framework.Graphics.Containers; using osu.Framework.Input.Events; -using osu.Framework.Lists; using osu.Framework.Screens; using osuTK; using osuTK.Input; @@ -85,14 +84,27 @@ private void load() } } - private void pageSelected(BindablePage page) + private void pageSelected(BindablePage page, bool runningStart = false) { - this.Push(new PlayScreen(page) + this.Push(new PlayScreen(page, runningStart) { - Save = Save + Save = Save, + NextPage = selectNextPage }); } + private void selectNextPage(Page currentPage, bool runningStart) + { + var pages = _state.Value.Pages; + + var currentBindable = pages.First(bind => bind.Value == currentPage); + + var nextBindableIndex = (pages.IndexOf(currentBindable) + 1) % pages.Count; + var nextBindable = pages[nextBindableIndex]; + + pageSelected(nextBindable, runningStart); + } + protected override bool OnKeyDown(KeyDownEvent e) { switch (e.Key)