-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKey.cs
More file actions
180 lines (154 loc) · 5.4 KB
/
Key.cs
File metadata and controls
180 lines (154 loc) · 5.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
using System;
using System.Collections.Generic;
using System.Linq;
using SFML.Graphics;
using SFML.System;
using SFML.Window;
namespace KeyOverlay {
class Key
{
private string name;
private RenderTexture texture;
public Texture Texture {
get {
return texture.Texture;
}
}
public readonly Texture Pressed;
private Keyboard.Key key;
private int counter;
public int Counter {
get {
return counter;
}
}
private Config config;
private bool isPressed = false;
private List<KeyHold> holds = new List<KeyHold>();
public Key(string name, string k, Config config)
{
this.name = name;
this.config = config;
Shape shape;
if(config.Key.Rounded)
{
shape = new RoundedRectangle(
config.Key.Size - config.Key.BorderSize * 2,
config.Key.Size - config.Key.BorderSize * 2,
config.Key.Radius
);
}
else
{
shape = new RectangleShape(
new Vector2f(
config.Key.Size - config.Key.BorderSize * 2,
config.Key.Size - config.Key.BorderSize * 2
)
);
}
shape.FillColor = new Color(0, 0, 0, 0);
shape.OutlineColor = config.Key.Border;
shape.OutlineThickness = config.Key.BorderSize;
shape.Position = new Vector2f(
config.Key.BorderSize,
config.Key.BorderSize
);
Font font = new Font(config.Key.Font);
Text text = new Text(name, font);
text.CharacterSize = config.Key.TextSize;
text.FillColor = config.Key.Text;
FloatRect textBounds = text.GetGlobalBounds();
text.Position = new Vector2f(
(config.Key.Size - textBounds.Width) / 2 - textBounds.Left,
(config.Key.Size - textBounds.Height) / 2 - textBounds.Top
);
texture = new RenderTexture((uint)Math.Ceiling(config.Key.Size), (uint)Math.Ceiling(config.Key.Size));
texture.Clear(Color.Transparent);
texture.Draw(shape);
texture.Draw(text);
var pressed = new RenderTexture((uint)Math.Ceiling(config.Key.Size), (uint)Math.Ceiling(config.Key.Size));
pressed.Clear(Color.Transparent);
shape.FillColor = config.Key.Fill;
shape.OutlineThickness = 0;
pressed.Draw(shape);
Pressed = new Texture(pressed.Texture);
if(!Enum.TryParse(k, out key))
{
throw new ArgumentException($"Invalid key: {k}");
}
}
public Vector2f Draw(RenderTexture t, Vector2f offset)
{
var timeNow = Util.GetMillisecondsNow();
holds = holds.Where((hold) => hold.End > (timeNow - config.Key.Hold.Time)).ToList();
Sprite sprite;
if(IsPressed)
{
sprite = new Sprite(Pressed);
sprite.Position = offset;
t.Draw(sprite);
}
sprite = new Sprite(Texture);
sprite.Position = offset;
t.Draw(sprite);
foreach (var hold in holds)
{
if(hold.Length == 0) continue;
var holdShape = new RectangleShape(new Vector2f(
config.Key.Hold.Size,
hold.Length / config.Key.Hold.Time * (config.Window.Height - offset.Y)
));
holdShape.FillColor = config.Key.Hold.Color;
holdShape.Position = offset + new Vector2f(
(config.Key.Size - config.Key.Hold.Size) / 2,
config.Key.Size + (float)(timeNow - hold.End) / config.Key.Hold.Time * (config.Window.Height - offset.Y)
// (hold.Start - timeNow) / config.Key.Hold.Time * offset.Y
);
t.Draw(holdShape);
}
return offset + new Vector2f(texture.Size.X + config.Key.Margin, 0);
}
public bool IsPressed
{
get {
var isPressedNow = Keyboard.IsKeyPressed(key);
if(isPressedNow && !isPressed)
{
counter++;
holds.Add(new KeyHold());
}
else if(isPressed && !isPressedNow)
{
holds.Last().Stop();
}
isPressed = isPressedNow;
return isPressed;
}
}
private class KeyHold
{
public long Start;
private long end = 0;
public long End {
get {
return end == 0 ? Util.GetMillisecondsNow() : end;
}
}
public float Length {
get {
return End - Start;
}
}
public KeyHold()
{
Start = Util.GetMillisecondsNow();
}
public void Stop()
{
if(end > 0) return;
end = Util.GetMillisecondsNow();
}
}
}
}