osu/osu.Game/Screens/Play/KeyCounter.cs

134 lines
4.5 KiB
C#
Raw Normal View History

// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
2016-11-08 23:13:20 +00:00
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Graphics.Containers;
using osu.Framework.Graphics.Sprites;
2016-11-08 23:13:20 +00:00
using osu.Framework.Graphics.Textures;
using osu.Game.Graphics.Sprites;
2017-01-27 12:57:22 +00:00
using OpenTK;
using OpenTK.Graphics;
2017-01-27 12:57:22 +00:00
namespace osu.Game.Screens.Play
{
2016-09-24 02:19:50 +00:00
public abstract class KeyCounter : Container
{
private Sprite buttonSprite;
private Sprite glowSprite;
2016-09-24 02:19:50 +00:00
private Container textLayer;
private SpriteText countSpriteText;
public bool IsCounting { get; set; }
2017-07-11 13:58:06 +00:00
private int countPresses;
public int CountPresses
2016-09-26 06:07:43 +00:00
{
2017-07-11 13:58:06 +00:00
get { return countPresses; }
2016-09-26 06:07:43 +00:00
private set
{
2017-07-11 13:58:06 +00:00
if (countPresses != value)
2016-09-26 06:07:43 +00:00
{
2017-07-11 13:58:06 +00:00
countPresses = value;
2016-09-26 06:07:43 +00:00
countSpriteText.Text = value.ToString(@"#,0");
}
}
}
private bool isLit;
public bool IsLit
{
get { return isLit; }
protected set
{
if (isLit != value)
{
isLit = value;
2016-09-26 06:07:43 +00:00
updateGlowSprite(value);
if (value && IsCounting)
2017-07-11 13:58:06 +00:00
CountPresses++;
}
}
}
//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;
2017-03-09 05:24:16 +00:00
public int FadeTime { get; set; }
protected KeyCounter(string name)
{
Name = name;
}
[BackgroundDependencyLoader]
private void load(TextureStore textures)
2016-09-23 11:14:11 +00:00
{
Children = new Drawable[]
{
buttonSprite = new Sprite
{
2016-11-08 23:13:20 +00:00
Texture = textures.Get(@"KeyCounter/key-up"),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
},
2016-09-24 02:19:50 +00:00
glowSprite = new Sprite
{
2016-11-08 23:13:20 +00:00
Texture = textures.Get(@"KeyCounter/key-glow"),
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
2016-09-24 02:19:50 +00:00
Alpha = 0
},
2016-09-24 02:19:50 +00:00
textLayer = new Container
{
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativeSizeAxes = Axes.Both,
2016-09-24 02:19:50 +00:00
Children = new Drawable[]
{
new OsuSpriteText
2016-09-24 02:19:50 +00:00
{
Text = Name,
2017-02-16 13:43:49 +00:00
Font = @"Venera",
TextSize = 12,
2016-09-24 02:19:50 +00:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativePositionAxes = Axes.Both,
Position = new Vector2(0, -0.25f),
2016-09-24 02:19:50 +00:00
Colour = KeyUpTextColor
},
countSpriteText = new OsuSpriteText
2016-09-24 02:19:50 +00:00
{
2017-07-11 13:58:06 +00:00
Text = CountPresses.ToString(@"#,0"),
2016-09-24 02:19:50 +00:00
Anchor = Anchor.Centre,
Origin = Anchor.Centre,
RelativePositionAxes = Axes.Both,
Position = new Vector2(0, 0.25f),
2016-09-24 02:19:50 +00:00
Colour = KeyUpTextColor
}
}
}
};
2016-09-24 02:19:50 +00:00
//Set this manually because an element with Alpha=0 won't take it size to AutoSizeContainer,
2016-09-24 06:28:59 +00:00
//so the size can be changing between buttonSprite and glowSprite.
Height = buttonSprite.DrawHeight;
Width = buttonSprite.DrawWidth;
2016-09-23 11:14:11 +00:00
}
2016-09-26 06:07:43 +00:00
private void updateGlowSprite(bool show)
{
2016-09-26 06:07:43 +00:00
if (show)
{
glowSprite.FadeIn(FadeTime);
textLayer.FadeColour(KeyDownTextColor, FadeTime);
}
else
{
2016-09-26 06:07:43 +00:00
glowSprite.FadeOut(FadeTime);
textLayer.FadeColour(KeyUpTextColor, FadeTime);
}
}
2016-09-26 06:10:51 +00:00
2017-07-11 13:58:06 +00:00
public void ResetCount() => CountPresses = 0;
}
}