This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
KeyboardInputChip.MonoGame.cs
124 lines (96 loc) · 3.74 KB
/
KeyboardInputChip.MonoGame.cs
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
using System;
using System.Text;
using Microsoft.Xna.Framework.Input;
namespace PixelVision8.Player
{
public class KeyboardInputChip : AbstractChip, IUpdate
{
public KeyboardState currentKeyboardState;
public KeyboardState previousKeyboardState;
private static readonly int repsPerSec = 20;
private readonly float timeUntilRepInMillis = 500f;
private DateTime downSince = DateTime.Now;
private DateTime lastRep = DateTime.Now;
private Keys? repChar;
private readonly StringBuilder inputStringBuilder = new StringBuilder();
protected override void Configure()
{
Player.KeyboardInputChip = this;
}
public void Update(int timeDelta)
{
// Build the input string
// inputStringBuilder.Clear();
// Save the one and only (if available) keyboardstate
previousKeyboardState = currentKeyboardState;
currentKeyboardState = Keyboard.GetState();
foreach (var key in currentKeyboardState.GetPressedKeys())
{
var pv8Key = (Keys) (int) key;
if (JustPressed(pv8Key))
{
downSince = DateTime.Now;
repChar = pv8Key;
// BuildInputString(pv8Key);
}
else if (GetKeyUp(pv8Key))
{
if (repChar == pv8Key) repChar = null;
}
var tmpKey = (Keys) (int) key;
if (repChar != null && repChar == pv8Key && currentKeyboardState.IsKeyDown(tmpKey))
{
var now = DateTime.Now;
var downFor = now.Subtract(downSince);
if (downFor.CompareTo(TimeSpan.FromMilliseconds(timeUntilRepInMillis)) > 0)
{
// Should repeat since the wait time is over now.
var repeatSince = now.Subtract(lastRep);
if (repeatSince.CompareTo(TimeSpan.FromMilliseconds(1000f / repsPerSec)) > 0)
{
// Time for another key-stroke.
lastRep = now;
// BuildInputString(pv8Key);
}
}
}
}
}
public void SetInputText(char character, Keys key)
{
var value = Convert.ToInt32(character);
if (value > 31 && value != 127 && value < 169
) // TODO we don't support all the characters to need to define a limit
{
inputStringBuilder.Append(character);
}
}
public string ReadInputString()
{
// Get the text value of the string builder
var text = inputStringBuilder.ToString();
// Clear the string builder
inputStringBuilder.Clear();
return text;
}
public bool GetKeyDown(Keys key)
{
var tmpKey = (Keys) (int) key;
return currentKeyboardState.IsKeyDown(tmpKey) && previousKeyboardState.IsKeyDown(tmpKey);
}
public bool GetKeyUp(Keys key)
{
var tmpKey = (Keys) (int) key;
return !currentKeyboardState.IsKeyDown(tmpKey) && previousKeyboardState.IsKeyDown(tmpKey);
}
public bool JustPressed(Keys key)
{
var tmpKey = (Keys) (int) key;
return currentKeyboardState.IsKeyDown(tmpKey) && !previousKeyboardState.IsKeyDown(tmpKey);
}
}
public partial class PixelVision
{
public KeyboardInputChip KeyboardInputChip { get; set; }
}
}