|
| 1 | +//Copyright (c) 2007-2016 ppy Pty Ltd <contact@ppy.sh>. |
| 2 | +//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE |
| 3 | + |
| 4 | +using OpenTK; |
| 5 | +using OpenTK.Graphics; |
| 6 | +using osu.Framework.Graphics; |
| 7 | +using osu.Framework.Graphics.Containers; |
| 8 | +using osu.Framework.Graphics.Sprites; |
| 9 | + |
| 10 | +namespace osu.Game.Graphics.UserInterface |
| 11 | +{ |
| 12 | + public abstract class KeyCounter : Container |
| 13 | + { |
| 14 | + private Sprite buttonSprite; |
| 15 | + private Sprite glowSprite; |
| 16 | + private Container textLayer; |
| 17 | + private SpriteText countSpriteText; |
| 18 | + |
| 19 | + public override string Name { get; } |
| 20 | + public bool IsCounting { get; set; } |
| 21 | + private int count; |
| 22 | + public int Count |
| 23 | + { |
| 24 | + get { return count; } |
| 25 | + private set |
| 26 | + { |
| 27 | + if (count != value) |
| 28 | + { |
| 29 | + count = value; |
| 30 | + countSpriteText.Text = value.ToString(@"#,0"); |
| 31 | + } |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + private bool isLit; |
| 36 | + public bool IsLit |
| 37 | + { |
| 38 | + get { return isLit; } |
| 39 | + protected set |
| 40 | + { |
| 41 | + if (isLit != value) |
| 42 | + { |
| 43 | + isLit = value; |
| 44 | + updateGlowSprite(value); |
| 45 | + if (value && IsCounting) |
| 46 | + Count++; |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + //further: change default values here and in KeyCounterCollection if needed, instead of passing them in every constructor |
| 52 | + public Color4 KeyDownTextColor { get; set; } = Color4.DarkGray; |
| 53 | + public Color4 KeyUpTextColor { get; set; } = Color4.White; |
| 54 | + public int FadeTime { get; set; } = 0; |
| 55 | + |
| 56 | + protected KeyCounter(string name) |
| 57 | + { |
| 58 | + Name = name; |
| 59 | + } |
| 60 | + |
| 61 | + public override void Load() |
| 62 | + { |
| 63 | + base.Load(); |
| 64 | + Children = new Drawable[] |
| 65 | + { |
| 66 | + buttonSprite = new Sprite |
| 67 | + { |
| 68 | + Texture = Game.Textures.Get(@"KeyCounter/key-up"), |
| 69 | + Anchor = Anchor.Centre, |
| 70 | + Origin = Anchor.Centre, |
| 71 | + }, |
| 72 | + glowSprite = new Sprite |
| 73 | + { |
| 74 | + Texture = Game.Textures.Get(@"KeyCounter/key-glow"), |
| 75 | + Anchor = Anchor.Centre, |
| 76 | + Origin = Anchor.Centre, |
| 77 | + Alpha = 0 |
| 78 | + }, |
| 79 | + textLayer = new Container |
| 80 | + { |
| 81 | + Anchor = Anchor.Centre, |
| 82 | + Origin = Anchor.Centre, |
| 83 | + SizeMode = InheritMode.XY, |
| 84 | + Children = new Drawable[] |
| 85 | + { |
| 86 | + new SpriteText |
| 87 | + { |
| 88 | + Text = Name, |
| 89 | + Anchor = Anchor.Centre, |
| 90 | + Origin = Anchor.Centre, |
| 91 | + PositionMode = InheritMode.XY, |
| 92 | + Position = new Vector2(0, -0.25f), |
| 93 | + Colour = KeyUpTextColor |
| 94 | + }, |
| 95 | + countSpriteText = new SpriteText |
| 96 | + { |
| 97 | + Text = Count.ToString(@"#,0"), |
| 98 | + Anchor = Anchor.Centre, |
| 99 | + Origin = Anchor.Centre, |
| 100 | + PositionMode = InheritMode.XY, |
| 101 | + Position = new Vector2(0, 0.25f), |
| 102 | + Colour = KeyUpTextColor |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + }; |
| 107 | + //Set this manually because an element with Alpha=0 won't take it size to AutoSizeContainer, |
| 108 | + //so the size can be changing between buttonSprite and glowSprite. |
| 109 | + Height = buttonSprite.Height; |
| 110 | + Width = buttonSprite.Width; |
| 111 | + } |
| 112 | + |
| 113 | + private void updateGlowSprite(bool show) |
| 114 | + { |
| 115 | + if (show) |
| 116 | + { |
| 117 | + glowSprite.FadeIn(FadeTime); |
| 118 | + textLayer.FadeColour(KeyDownTextColor, FadeTime); |
| 119 | + } |
| 120 | + else |
| 121 | + { |
| 122 | + glowSprite.FadeOut(FadeTime); |
| 123 | + textLayer.FadeColour(KeyUpTextColor, FadeTime); |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + public void ResetCount() => Count = 0; |
| 128 | + } |
| 129 | +} |
0 commit comments