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

Move states to KeyCounter

This commit is contained in:
Roman Kapustin 2018-07-22 23:58:21 +03:00
parent ecd51d70f9
commit 332ad5bb67
2 changed files with 21 additions and 18 deletions

View File

@ -1,7 +1,8 @@
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>. // Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System; using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
@ -16,13 +17,13 @@ namespace osu.Game.Screens.Play
{ {
public abstract class KeyCounter : Container public abstract class KeyCounter : Container
{ {
public event Action KeyPressed;
private Sprite buttonSprite; private Sprite buttonSprite;
private Sprite glowSprite; private Sprite glowSprite;
private Container textLayer; private Container textLayer;
private SpriteText countSpriteText; private SpriteText countSpriteText;
private readonly List<KeyCounterState> states = new List<KeyCounterState>();
public bool IsCounting { get; set; } = true; public bool IsCounting { get; set; } = true;
private int countPresses; private int countPresses;
public int CountPresses public int CountPresses
@ -51,7 +52,7 @@ namespace osu.Game.Screens.Play
if (value && IsCounting) if (value && IsCounting)
{ {
CountPresses++; CountPresses++;
KeyPressed?.Invoke(); SaveState();
} }
} }
} }
@ -139,8 +140,19 @@ namespace osu.Game.Screens.Play
public void ResetCount() => CountPresses = 0; public void ResetCount() => CountPresses = 0;
public KeyCounterState SaveState() => new KeyCounterState(AudioClock.CurrentTime, CountPresses); public void SaveState()
{
var lastState = states.LastOrDefault();
public void RestoreState(KeyCounterState state) => CountPresses = state.Count; if (lastState == null || lastState.Time < AudioClock.CurrentTime)
states.Add(new KeyCounterState(AudioClock.CurrentTime, CountPresses));
}
public void RestoreState(double time)
{
var targetState = states.LastOrDefault(state => state.Time <= time) ?? states.LastOrDefault();
var targetCount = targetState?.Count ?? 0;
CountPresses = targetCount;
}
} }
} }

View File

@ -2,18 +2,17 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Framework.Allocation; using osu.Framework.Allocation;
using osu.Framework.Configuration; using osu.Framework.Configuration;
using osu.Framework.Graphics; using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers; using osu.Framework.Graphics.Containers;
using osu.Framework.Timing;
using OpenTK.Graphics;
using osu.Framework.Input.EventArgs; using osu.Framework.Input.EventArgs;
using osu.Framework.Input.States; using osu.Framework.Input.States;
using osu.Framework.Timing;
using osu.Game.Configuration; using osu.Game.Configuration;
using OpenTK; using OpenTK;
using OpenTK.Graphics;
namespace osu.Game.Screens.Play namespace osu.Game.Screens.Play
{ {
@ -24,8 +23,6 @@ namespace osu.Game.Screens.Play
public readonly Bindable<bool> Visible = new Bindable<bool>(true); public readonly Bindable<bool> Visible = new Bindable<bool>(true);
private readonly Bindable<bool> configVisibility = new Bindable<bool>(); private readonly Bindable<bool> configVisibility = new Bindable<bool>();
private readonly Dictionary<string, List<KeyCounterState>> keyCountersState = new Dictionary<string, List<KeyCounterState>>();
public KeyCounterCollection() public KeyCounterCollection()
{ {
Direction = FillDirection.Horizontal; Direction = FillDirection.Horizontal;
@ -42,18 +39,12 @@ namespace osu.Game.Screens.Play
key.KeyDownTextColor = KeyDownTextColor; key.KeyDownTextColor = KeyDownTextColor;
key.KeyUpTextColor = KeyUpTextColor; key.KeyUpTextColor = KeyUpTextColor;
key.AudioClock = AudioClock; key.AudioClock = AudioClock;
keyCountersState.Add(key.Name, new List<KeyCounterState>());
key.KeyPressed += () => keyCountersState[key.Name].Add(key.SaveState());
} }
public void RestoreKeyCounterState(double time) public void RestoreKeyCounterState(double time)
{ {
foreach (var counter in Children) foreach (var counter in Children)
{ counter.RestoreState(time);
var targetState = keyCountersState[counter.Name].LastOrDefault(state => state.Time <= time);
counter.RestoreState(targetState);
}
} }
public void ResetCount() public void ResetCount()