Skip to content

Commit cca5de2

Browse files
authored
Merge pull request #31 from huoyaoyuan/key-counter
Key Counter
2 parents cd57661 + 25fecfc commit cca5de2

8 files changed

+336
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 System;
5+
using OpenTK.Input;
6+
using osu.Framework.GameModes.Testing;
7+
using osu.Framework.Graphics;
8+
using osu.Game.Graphics.UserInterface;
9+
10+
namespace osu.Desktop.Tests
11+
{
12+
class TestCaseKeyCounter : TestCase
13+
{
14+
public override string Name => @"KeyCounter";
15+
16+
public override string Description => @"Tests key counter";
17+
18+
public override void Reset()
19+
{
20+
base.Reset();
21+
22+
KeyCounterCollection kc = new KeyCounterCollection
23+
{
24+
Origin = Anchor.Centre,
25+
Anchor = Anchor.Centre,
26+
IsCounting = true
27+
};
28+
Add(kc);
29+
kc.AddKey(new KeyCounterKeyboard(@"Z", Key.Z));
30+
kc.AddKey(new KeyCounterKeyboard(@"X", Key.X));
31+
kc.AddKey(new KeyCounterMouse(@"M1", MouseButton.Left));
32+
kc.AddKey(new KeyCounterMouse(@"M2", MouseButton.Right));
33+
}
34+
}
35+
}

osu.Desktop.VisualTests/Tests/TestCaseTextAwesome.cs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
using OpenTK;
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;
25
using OpenTK.Graphics;
36
using osu.Framework.GameModes.Testing;
47
using osu.Framework.Graphics;

osu.Desktop.VisualTests/osu.Desktop.VisualTests.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
</ItemGroup>
151151
<ItemGroup>
152152
<Compile Include="Program.cs" />
153+
<Compile Include="Tests\TestCaseKeyCounter.cs" />
153154
<Compile Include="Tests\TestCaseTextAwesome.cs" />
154155
<Compile Include="VisualTestGame.cs" />
155156
</ItemGroup>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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 System.Collections.Generic;
5+
using OpenTK;
6+
using OpenTK.Graphics;
7+
using osu.Framework.Graphics.Containers;
8+
9+
namespace osu.Game.Graphics.UserInterface
10+
{
11+
public class KeyCounterCollection : FlowContainer
12+
{
13+
public KeyCounterCollection()
14+
{
15+
Direction = FlowDirection.HorizontalOnly;
16+
}
17+
18+
private List<KeyCounter> counters = new List<KeyCounter>();
19+
public IReadOnlyList<KeyCounter> Counters => counters;
20+
21+
public void AddKey(KeyCounter key)
22+
{
23+
counters.Add(key);
24+
key.IsCounting = this.IsCounting;
25+
key.FadeTime = this.FadeTime;
26+
key.KeyDownTextColor = this.KeyDownTextColor;
27+
key.KeyUpTextColor = this.KeyUpTextColor;
28+
base.Add(key);
29+
}
30+
31+
public void ResetCount()
32+
{
33+
foreach (var counter in counters)
34+
counter.ResetCount();
35+
}
36+
37+
public override bool Contains(Vector2 screenSpacePos) => true;
38+
39+
//further: change default values here and in KeyCounter if needed, instead of passing them in every constructor
40+
private bool isCounting;
41+
public bool IsCounting
42+
{
43+
get { return isCounting; }
44+
set
45+
{
46+
if (value != isCounting)
47+
{
48+
isCounting = value;
49+
foreach (var child in counters)
50+
child.IsCounting = value;
51+
}
52+
}
53+
}
54+
55+
private int fadeTime = 0;
56+
public int FadeTime
57+
{
58+
get { return fadeTime; }
59+
set
60+
{
61+
if (value != fadeTime)
62+
{
63+
fadeTime = value;
64+
foreach (var child in counters)
65+
child.FadeTime = value;
66+
}
67+
}
68+
}
69+
70+
private Color4 keyDownTextColor = Color4.DarkGray;
71+
public Color4 KeyDownTextColor
72+
{
73+
get { return keyDownTextColor; }
74+
set
75+
{
76+
if (value != keyDownTextColor)
77+
{
78+
keyDownTextColor = value;
79+
foreach (var child in counters)
80+
child.KeyDownTextColor = value;
81+
}
82+
}
83+
}
84+
85+
private Color4 keyUpTextColor = Color4.White;
86+
public Color4 KeyUpTextColor
87+
{
88+
get { return keyUpTextColor; }
89+
set
90+
{
91+
if (value != keyUpTextColor)
92+
{
93+
keyUpTextColor = value;
94+
foreach (var child in counters)
95+
child.KeyUpTextColor = value;
96+
}
97+
}
98+
}
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.Input;
5+
using osu.Framework.Graphics;
6+
using osu.Framework.Input;
7+
8+
namespace osu.Game.Graphics.UserInterface
9+
{
10+
public class KeyCounterKeyboard : KeyCounter
11+
{
12+
public Key Key { get; }
13+
public KeyCounterKeyboard(string name, Key key) : base(name)
14+
{
15+
Key = key;
16+
}
17+
18+
protected override bool OnKeyDown(InputState state, KeyDownEventArgs args)
19+
{
20+
if (args.Key == this.Key) IsLit = true;
21+
return base.OnKeyDown(state, args);
22+
}
23+
24+
protected override bool OnKeyUp(InputState state, KeyUpEventArgs args)
25+
{
26+
if (args.Key == this.Key) IsLit = false;
27+
return base.OnKeyUp(state, args);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.Input;
6+
using osu.Framework.Graphics;
7+
using osu.Framework.Input;
8+
9+
namespace osu.Game.Graphics.UserInterface
10+
{
11+
public class KeyCounterMouse : KeyCounter
12+
{
13+
public MouseButton Button { get; }
14+
public KeyCounterMouse(string name, MouseButton button) : base(name)
15+
{
16+
Button = button;
17+
}
18+
19+
public override bool Contains(Vector2 screenSpacePos) => true;
20+
21+
protected override bool OnMouseDown(InputState state, MouseDownEventArgs args)
22+
{
23+
if (args.Button == this.Button) IsLit = true;
24+
return base.OnMouseDown(state, args);
25+
}
26+
27+
protected override bool OnMouseUp(InputState state, MouseUpEventArgs args)
28+
{
29+
if (args.Button == this.Button) IsLit = false;
30+
return base.OnMouseUp(state, args);
31+
}
32+
}
33+
}

osu.Game/osu.Game.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@
5353
<Compile Include="Graphics\Containers\OsuLargeComponent.cs" />
5454
<Compile Include="Graphics\Processing\RatioAdjust.cs" />
5555
<Compile Include="Graphics\TextAwesome.cs" />
56+
<Compile Include="Graphics\UserInterface\KeyCounter.cs" />
57+
<Compile Include="Graphics\UserInterface\KeyCounterKeyboard.cs" />
58+
<Compile Include="Graphics\UserInterface\KeyCounterCollection.cs" />
59+
<Compile Include="Graphics\UserInterface\KeyCounterMouse.cs" />
5660
<Compile Include="Online\API\APIAccess.cs" />
5761
<Compile Include="Online\API\APIRequest.cs" />
5862
<Compile Include="Online\API\OAuth.cs" />

0 commit comments

Comments
 (0)