2018-04-13 17:19:50 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
|
|
|
|
|
2018-07-23 04:58:21 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
using osu.Framework.Allocation;
|
|
|
|
|
using osu.Framework.Graphics;
|
|
|
|
|
using osu.Framework.Graphics.Containers;
|
|
|
|
|
using osu.Framework.Graphics.Sprites;
|
|
|
|
|
using osu.Framework.Graphics.Textures;
|
|
|
|
|
using osu.Game.Graphics.Sprites;
|
|
|
|
|
using OpenTK;
|
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Screens.Play
|
|
|
|
|
{
|
|
|
|
|
public abstract class KeyCounter : Container
|
|
|
|
|
{
|
|
|
|
|
private Sprite buttonSprite;
|
|
|
|
|
private Sprite glowSprite;
|
|
|
|
|
private Container textLayer;
|
|
|
|
|
private SpriteText countSpriteText;
|
|
|
|
|
|
2018-07-23 04:58:21 +08:00
|
|
|
|
private readonly List<KeyCounterState> states = new List<KeyCounterState>();
|
2018-07-28 18:22:52 +08:00
|
|
|
|
private KeyCounterState lastState;
|
2018-07-23 04:58:21 +08:00
|
|
|
|
|
2018-07-20 16:08:25 +08:00
|
|
|
|
public bool IsCounting { get; set; } = true;
|
2018-04-13 17:19:50 +08:00
|
|
|
|
private int countPresses;
|
|
|
|
|
public int CountPresses
|
|
|
|
|
{
|
|
|
|
|
get { return countPresses; }
|
|
|
|
|
private set
|
|
|
|
|
{
|
|
|
|
|
if (countPresses != value)
|
|
|
|
|
{
|
|
|
|
|
countPresses = value;
|
|
|
|
|
countSpriteText.Text = value.ToString(@"#,0");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool isLit;
|
|
|
|
|
public bool IsLit
|
|
|
|
|
{
|
|
|
|
|
get { return isLit; }
|
|
|
|
|
protected set
|
|
|
|
|
{
|
|
|
|
|
if (isLit != value)
|
|
|
|
|
{
|
|
|
|
|
isLit = value;
|
|
|
|
|
updateGlowSprite(value);
|
|
|
|
|
if (value && IsCounting)
|
2018-07-22 22:35:42 +08:00
|
|
|
|
{
|
2018-04-13 17:19:50 +08:00
|
|
|
|
CountPresses++;
|
2018-07-23 04:58:21 +08:00
|
|
|
|
SaveState();
|
2018-07-22 22:35:42 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//further: change default values here and in KeyCounterCollection if needed, instead of passing them in every constructor
|
|
|
|
|
public Color4 KeyDownTextColor { get; set; } = Color4.DarkGray;
|
|
|
|
|
public Color4 KeyUpTextColor { get; set; } = Color4.White;
|
|
|
|
|
public int FadeTime { get; set; }
|
|
|
|
|
|
|
|
|
|
protected KeyCounter(string name)
|
|
|
|
|
{
|
|
|
|
|
Name = name;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[BackgroundDependencyLoader]
|
|
|
|
|
private void load(TextureStore textures)
|
|
|
|
|
{
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
buttonSprite = new Sprite
|
|
|
|
|
{
|
|
|
|
|
Texture = textures.Get(@"KeyCounter/key-up"),
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
},
|
|
|
|
|
glowSprite = new Sprite
|
|
|
|
|
{
|
|
|
|
|
Texture = textures.Get(@"KeyCounter/key-glow"),
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
Alpha = 0
|
|
|
|
|
},
|
|
|
|
|
textLayer = new Container
|
|
|
|
|
{
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
RelativeSizeAxes = Axes.Both,
|
|
|
|
|
Children = new Drawable[]
|
|
|
|
|
{
|
|
|
|
|
new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Text = Name,
|
|
|
|
|
Font = @"Venera",
|
|
|
|
|
TextSize = 12,
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
RelativePositionAxes = Axes.Both,
|
|
|
|
|
Position = new Vector2(0, -0.25f),
|
|
|
|
|
Colour = KeyUpTextColor
|
|
|
|
|
},
|
|
|
|
|
countSpriteText = new OsuSpriteText
|
|
|
|
|
{
|
|
|
|
|
Text = CountPresses.ToString(@"#,0"),
|
|
|
|
|
Anchor = Anchor.Centre,
|
|
|
|
|
Origin = Anchor.Centre,
|
|
|
|
|
RelativePositionAxes = Axes.Both,
|
|
|
|
|
Position = new Vector2(0, 0.25f),
|
|
|
|
|
Colour = KeyUpTextColor
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
//Set this manually because an element with Alpha=0 won't take it size to AutoSizeContainer,
|
|
|
|
|
//so the size can be changing between buttonSprite and glowSprite.
|
|
|
|
|
Height = buttonSprite.DrawHeight;
|
|
|
|
|
Width = buttonSprite.DrawWidth;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateGlowSprite(bool show)
|
|
|
|
|
{
|
|
|
|
|
if (show)
|
|
|
|
|
{
|
|
|
|
|
glowSprite.FadeIn(FadeTime);
|
|
|
|
|
textLayer.FadeColour(KeyDownTextColor, FadeTime);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
glowSprite.FadeOut(FadeTime);
|
|
|
|
|
textLayer.FadeColour(KeyUpTextColor, FadeTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-06 06:01:36 +08:00
|
|
|
|
public void ResetCount()
|
|
|
|
|
{
|
|
|
|
|
CountPresses = 0;
|
|
|
|
|
states.Clear();
|
|
|
|
|
}
|
2018-07-22 22:16:17 +08:00
|
|
|
|
|
2018-07-23 04:58:21 +08:00
|
|
|
|
public void SaveState()
|
|
|
|
|
{
|
2018-07-28 18:22:52 +08:00
|
|
|
|
if (lastState == null || lastState.Time < Clock.CurrentTime)
|
|
|
|
|
states.Add(lastState = new KeyCounterState(Clock.CurrentTime, CountPresses));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override void Update()
|
|
|
|
|
{
|
|
|
|
|
base.Update();
|
2018-07-22 22:16:17 +08:00
|
|
|
|
|
2018-07-28 18:22:52 +08:00
|
|
|
|
if (lastState?.Time > Clock.CurrentTime)
|
|
|
|
|
RestoreState(Clock.CurrentTime);
|
2018-07-23 04:58:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-16 03:18:48 +08:00
|
|
|
|
public void RestoreState(double time)
|
|
|
|
|
{
|
|
|
|
|
var targetState = states.LastOrDefault(state => state.Time <= time);
|
2018-09-06 06:01:36 +08:00
|
|
|
|
if (targetState == null)
|
|
|
|
|
{
|
|
|
|
|
ResetCount();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-16 03:18:48 +08:00
|
|
|
|
var targetIndex = states.IndexOf(targetState);
|
|
|
|
|
|
|
|
|
|
states.RemoveRange(targetIndex + 1, states.Count - (targetIndex + 1));
|
|
|
|
|
|
|
|
|
|
lastState = targetState;
|
2018-09-08 02:35:32 +08:00
|
|
|
|
CountPresses = targetState.Count;
|
2018-08-16 03:18:48 +08:00
|
|
|
|
}
|
2018-04-13 17:19:50 +08:00
|
|
|
|
}
|
|
|
|
|
}
|