Skip to content

Commit

Permalink
Jump to next page (#34)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
hbnrmx committed May 3, 2020
1 parent 3cf990c commit a78a6f3
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 27 deletions.
4 changes: 2 additions & 2 deletions SeeSharp/Screens/Edit/EditScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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;
Expand Down
19 changes: 14 additions & 5 deletions SeeSharp/Screens/Play/PlayScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ namespace SeeSharp.Screens.Play
public class PlayScreen : Screen
{
public Action Save;
public Action<Page, bool> 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);

Expand All @@ -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
Expand All @@ -46,7 +49,7 @@ public PlayScreen(BindablePage page)
this.Push(new EditScreen(_page)
{
Save = Save,
SetBarToFirstOrDefault = _playZone.jumpToFirstBar
FinishedEditing = _playZone.jumpToFirstBar
});
}
};
Expand All @@ -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;

Expand All @@ -86,5 +89,11 @@ public override void OnResuming(IScreen _)
this.Exit();
}
}

private void onPageEnd(bool runningStart)
{
this.Exit();
NextPage?.Invoke(_page.Value, runningStart);
}
}
}
65 changes: 51 additions & 14 deletions SeeSharp/Screens/Play/PlayZone.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,6 +12,7 @@ namespace SeeSharp.Screens.Play
{
public class PlayZone : Container
{
public Action<bool> PageEnd;
public Action<float> zoomChanged;
public Action<float> speedChanged;
public Action<float> currentBarChanged;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand All @@ -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;
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions SeeSharp/Screens/Select/MenuItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace SeeSharp.Screens.Select
public class MenuItem : Container, IComparable<MenuItem>
{
private readonly BindablePage _page = new BindablePage();
public Action<BindablePage> PageSelected;
public Action<BindablePage, bool> PageSelected;
private SpriteText _text;

public MenuItem(BindablePage page)
Expand Down Expand Up @@ -67,7 +67,7 @@ private void load()

protected override bool OnClick(ClickEvent e)
{
PageSelected.Invoke(_page);
PageSelected.Invoke(_page, false);
return true;
}

Expand Down
20 changes: 16 additions & 4 deletions SeeSharp/Screens/Select/SelectScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a78a6f3

Please sign in to comment.