1
0
mirror of https://github.com/ppy/osu.git synced 2024-09-23 06:07:25 +08:00
osu-lazer/osu.Game/Graphics/UserInterface/KeyCounter.cs

133 lines
4.4 KiB
C#
Raw Normal View History

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