1
0
mirror of https://github.com/ppy/osu.git synced 2024-11-11 14:17:26 +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>.
// 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.Graphics;
using osu.Framework.Graphics.Containers;
@ -16,13 +17,13 @@ namespace osu.Game.Screens.Play
{
public abstract class KeyCounter : Container
{
public event Action KeyPressed;
private Sprite buttonSprite;
private Sprite glowSprite;
private Container textLayer;
private SpriteText countSpriteText;
private readonly List<KeyCounterState> states = new List<KeyCounterState>();
public bool IsCounting { get; set; } = true;
private int countPresses;
public int CountPresses
@ -51,7 +52,7 @@ namespace osu.Game.Screens.Play
if (value && IsCounting)
{
CountPresses++;
KeyPressed?.Invoke();
SaveState();
}
}
}
@ -139,8 +140,19 @@ namespace osu.Game.Screens.Play
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
using System;
using System.Collections.Generic;
using System.Linq;
using osu.Framework.Allocation;
using osu.Framework.Configuration;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Timing;
using OpenTK.Graphics;
using osu.Framework.Input.EventArgs;
using osu.Framework.Input.States;
using osu.Framework.Timing;
using osu.Game.Configuration;
using OpenTK;
using OpenTK.Graphics;
namespace osu.Game.Screens.Play
{
@ -24,8 +23,6 @@ namespace osu.Game.Screens.Play
public readonly Bindable<bool> Visible = new Bindable<bool>(true);
private readonly Bindable<bool> configVisibility = new Bindable<bool>();
private readonly Dictionary<string, List<KeyCounterState>> keyCountersState = new Dictionary<string, List<KeyCounterState>>();
public KeyCounterCollection()
{
Direction = FillDirection.Horizontal;
@ -42,18 +39,12 @@ namespace osu.Game.Screens.Play
key.KeyDownTextColor = KeyDownTextColor;
key.KeyUpTextColor = KeyUpTextColor;
key.AudioClock = AudioClock;
keyCountersState.Add(key.Name, new List<KeyCounterState>());
key.KeyPressed += () => keyCountersState[key.Name].Add(key.SaveState());
}
public void RestoreKeyCounterState(double time)
{
foreach (var counter in Children)
{
var targetState = keyCountersState[counter.Name].LastOrDefault(state => state.Time <= time);
counter.RestoreState(targetState);
}
counter.RestoreState(time);
}
public void ResetCount()