diff --git a/osu.Desktop.VisualTests/Tests/TestCaseKeyCounter.cs b/osu.Desktop.VisualTests/Tests/TestCaseKeyCounter.cs new file mode 100644 index 0000000000..87642f3004 --- /dev/null +++ b/osu.Desktop.VisualTests/Tests/TestCaseKeyCounter.cs @@ -0,0 +1,35 @@ +//Copyright (c) 2007-2016 ppy Pty Ltd . +//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System; +using OpenTK.Input; +using osu.Framework.GameModes.Testing; +using osu.Framework.Graphics; +using osu.Game.Graphics.UserInterface; + +namespace osu.Desktop.Tests +{ + class TestCaseKeyCounter : TestCase + { + public override string Name => @"KeyCounter"; + + public override string Description => @"Tests key counter"; + + public override void Reset() + { + base.Reset(); + + KeyCounterCollection kc = new KeyCounterCollection + { + Origin = Anchor.Centre, + Anchor = Anchor.Centre, + IsCounting = true + }; + Add(kc); + kc.AddKey(new KeyCounterKeyboard(@"Z", Key.Z)); + kc.AddKey(new KeyCounterKeyboard(@"X", Key.X)); + kc.AddKey(new KeyCounterMouse(@"M1", MouseButton.Left)); + kc.AddKey(new KeyCounterMouse(@"M2", MouseButton.Right)); + } + } +} diff --git a/osu.Desktop.VisualTests/Tests/TestCaseTextAwesome.cs b/osu.Desktop.VisualTests/Tests/TestCaseTextAwesome.cs index 3e2c1f095d..ace1daf205 100644 --- a/osu.Desktop.VisualTests/Tests/TestCaseTextAwesome.cs +++ b/osu.Desktop.VisualTests/Tests/TestCaseTextAwesome.cs @@ -1,4 +1,7 @@ -using OpenTK; +//Copyright (c) 2007-2016 ppy Pty Ltd . +//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using OpenTK; using OpenTK.Graphics; using osu.Framework.GameModes.Testing; using osu.Framework.Graphics; diff --git a/osu.Desktop.VisualTests/osu.Desktop.VisualTests.csproj b/osu.Desktop.VisualTests/osu.Desktop.VisualTests.csproj index 2291150b9b..04cf5bc060 100644 --- a/osu.Desktop.VisualTests/osu.Desktop.VisualTests.csproj +++ b/osu.Desktop.VisualTests/osu.Desktop.VisualTests.csproj @@ -151,6 +151,7 @@ + diff --git a/osu.Game/Graphics/UserInterface/KeyCounter.cs b/osu.Game/Graphics/UserInterface/KeyCounter.cs new file mode 100644 index 0000000000..d715c7c0fd --- /dev/null +++ b/osu.Game/Graphics/UserInterface/KeyCounter.cs @@ -0,0 +1,129 @@ +//Copyright (c) 2007-2016 ppy Pty Ltd . +//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using OpenTK; +using OpenTK.Graphics; +using osu.Framework.Graphics; +using osu.Framework.Graphics.Containers; +using osu.Framework.Graphics.Sprites; + +namespace osu.Game.Graphics.UserInterface +{ + public abstract class KeyCounter : Container + { + private Sprite buttonSprite; + private Sprite glowSprite; + private Container textLayer; + private SpriteText countSpriteText; + + public override string Name { get; } + public bool IsCounting { get; set; } + 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; + updateGlowSprite(value); + if (value && IsCounting) + 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; + } + + public override void Load() + { + base.Load(); + Children = new Drawable[] + { + buttonSprite = new Sprite + { + Texture = Game.Textures.Get(@"KeyCounter/key-up"), + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + }, + glowSprite = new Sprite + { + Texture = Game.Textures.Get(@"KeyCounter/key-glow"), + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + Alpha = 0 + }, + textLayer = new Container + { + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + SizeMode = InheritMode.XY, + Children = new Drawable[] + { + new SpriteText + { + Text = Name, + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + PositionMode = InheritMode.XY, + Position = new Vector2(0, -0.25f), + Colour = KeyUpTextColor + }, + countSpriteText = new SpriteText + { + Text = Count.ToString(@"#,0"), + Anchor = Anchor.Centre, + Origin = Anchor.Centre, + PositionMode = InheritMode.XY, + 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.Height; + Width = buttonSprite.Width; + } + + private void updateGlowSprite(bool show) + { + if (show) + { + glowSprite.FadeIn(FadeTime); + textLayer.FadeColour(KeyDownTextColor, FadeTime); + } + else + { + glowSprite.FadeOut(FadeTime); + textLayer.FadeColour(KeyUpTextColor, FadeTime); + } + } + + public void ResetCount() => Count = 0; + } +} diff --git a/osu.Game/Graphics/UserInterface/KeyCounterCollection.cs b/osu.Game/Graphics/UserInterface/KeyCounterCollection.cs new file mode 100644 index 0000000000..ee1447c03b --- /dev/null +++ b/osu.Game/Graphics/UserInterface/KeyCounterCollection.cs @@ -0,0 +1,100 @@ +//Copyright (c) 2007-2016 ppy Pty Ltd . +//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using System.Collections.Generic; +using OpenTK; +using OpenTK.Graphics; +using osu.Framework.Graphics.Containers; + +namespace osu.Game.Graphics.UserInterface +{ + public class KeyCounterCollection : FlowContainer + { + public KeyCounterCollection() + { + Direction = FlowDirection.HorizontalOnly; + } + + private List counters = new List(); + public IReadOnlyList Counters => counters; + + public void AddKey(KeyCounter key) + { + counters.Add(key); + key.IsCounting = this.IsCounting; + key.FadeTime = this.FadeTime; + key.KeyDownTextColor = this.KeyDownTextColor; + key.KeyUpTextColor = this.KeyUpTextColor; + base.Add(key); + } + + public void ResetCount() + { + foreach (var counter in counters) + counter.ResetCount(); + } + + public override bool Contains(Vector2 screenSpacePos) => true; + + //further: change default values here and in KeyCounter if needed, instead of passing them in every constructor + private bool isCounting; + public bool IsCounting + { + get { return isCounting; } + set + { + if (value != isCounting) + { + isCounting = value; + foreach (var child in counters) + child.IsCounting = value; + } + } + } + + private int fadeTime = 0; + public int FadeTime + { + get { return fadeTime; } + set + { + if (value != fadeTime) + { + fadeTime = value; + foreach (var child in counters) + child.FadeTime = value; + } + } + } + + private Color4 keyDownTextColor = Color4.DarkGray; + public Color4 KeyDownTextColor + { + get { return keyDownTextColor; } + set + { + if (value != keyDownTextColor) + { + keyDownTextColor = value; + foreach (var child in counters) + child.KeyDownTextColor = value; + } + } + } + + private Color4 keyUpTextColor = Color4.White; + public Color4 KeyUpTextColor + { + get { return keyUpTextColor; } + set + { + if (value != keyUpTextColor) + { + keyUpTextColor = value; + foreach (var child in counters) + child.KeyUpTextColor = value; + } + } + } + } +} diff --git a/osu.Game/Graphics/UserInterface/KeyCounterKeyboard.cs b/osu.Game/Graphics/UserInterface/KeyCounterKeyboard.cs new file mode 100644 index 0000000000..9b75924cb6 --- /dev/null +++ b/osu.Game/Graphics/UserInterface/KeyCounterKeyboard.cs @@ -0,0 +1,30 @@ +//Copyright (c) 2007-2016 ppy Pty Ltd . +//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using OpenTK.Input; +using osu.Framework.Graphics; +using osu.Framework.Input; + +namespace osu.Game.Graphics.UserInterface +{ + public class KeyCounterKeyboard : KeyCounter + { + public Key Key { get; } + public KeyCounterKeyboard(string name, Key key) : base(name) + { + Key = key; + } + + protected override bool OnKeyDown(InputState state, KeyDownEventArgs args) + { + if (args.Key == this.Key) IsLit = true; + return base.OnKeyDown(state, args); + } + + protected override bool OnKeyUp(InputState state, KeyUpEventArgs args) + { + if (args.Key == this.Key) IsLit = false; + return base.OnKeyUp(state, args); + } + } +} diff --git a/osu.Game/Graphics/UserInterface/KeyCounterMouse.cs b/osu.Game/Graphics/UserInterface/KeyCounterMouse.cs new file mode 100644 index 0000000000..982721c7a7 --- /dev/null +++ b/osu.Game/Graphics/UserInterface/KeyCounterMouse.cs @@ -0,0 +1,33 @@ +//Copyright (c) 2007-2016 ppy Pty Ltd . +//Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE + +using OpenTK; +using OpenTK.Input; +using osu.Framework.Graphics; +using osu.Framework.Input; + +namespace osu.Game.Graphics.UserInterface +{ + public class KeyCounterMouse : KeyCounter + { + public MouseButton Button { get; } + public KeyCounterMouse(string name, MouseButton button) : base(name) + { + Button = button; + } + + public override bool Contains(Vector2 screenSpacePos) => true; + + protected override bool OnMouseDown(InputState state, MouseDownEventArgs args) + { + if (args.Button == this.Button) IsLit = true; + return base.OnMouseDown(state, args); + } + + protected override bool OnMouseUp(InputState state, MouseUpEventArgs args) + { + if (args.Button == this.Button) IsLit = false; + return base.OnMouseUp(state, args); + } + } +} diff --git a/osu.Game/osu.Game.csproj b/osu.Game/osu.Game.csproj index 1264f22848..e66fd44977 100644 --- a/osu.Game/osu.Game.csproj +++ b/osu.Game/osu.Game.csproj @@ -53,6 +53,10 @@ + + + +