Started making Input framebased and added KeyCodes

This commit is contained in:
Simon Lübeß
2020-11-14 15:09:59 +01:00
parent 356011119b
commit 696961e46c
9 changed files with 221 additions and 23 deletions
+3 -1
View File
@@ -48,7 +48,9 @@ namespace GlitchyEngine
{
while(_running)
{
_gameTime.Tick();
_gameTime.NewFrame();
Input.NewFrame();
DirectX.ImmediateContext.ClearRenderTargetView(DirectX.BackBufferTarget, .(1, 0, 1));
+5 -5
View File
@@ -4,14 +4,14 @@ namespace GlitchyEngine.Events
{
public abstract class KeyEvent : Event
{
protected int32 _keyCode;
protected Key _keyCode;
[Inline]
public int32 KeyCode => _keyCode;
public Key KeyCode => _keyCode;
public override EventCategory Category => .Input | .Keyboard
protected this(int32 keyCode)
protected this(Key keyCode)
{
_keyCode = keyCode;
}
@@ -28,7 +28,7 @@ namespace GlitchyEngine.Events
public override StringView Name => "KeyPressed";
public this(int32 keyCode, int32 repeatCount) : base(keyCode)
public this(Key keyCode, int32 repeatCount) : base(keyCode)
{
_repeatCount = repeatCount;
}
@@ -46,7 +46,7 @@ namespace GlitchyEngine.Events
public override StringView Name => "KeyReleased";
public this(int32 keyCode) : base(keyCode) { }
public this(Key keyCode) : base(keyCode) { }
public override void ToString(String strBuffer)
{
+1 -1
View File
@@ -78,7 +78,7 @@ namespace GlitchyEngine
/**
* Tells the timer that a frame has passed and updates the counters.
*/
public void Tick()
public void NewFrame()
{
TimeSpan old = _totalTime;
_totalTime = Elapsed;
+2 -2
View File
@@ -125,7 +125,7 @@ namespace GlitchyEngine.ImGui
dispatcher.Dispatch<KeyPressedEvent>(scope (e) =>
{
ref ImGui.IO io = ref ImGui.GetIO();
io.KeysDown[e.KeyCode] = true;
io.KeysDown[(int32)e.KeyCode] = true;
io.KeyCtrl = io.KeysDown[VK_CONTROL];
io.KeyShift = io.KeysDown[VK_SHIFT];
@@ -138,7 +138,7 @@ namespace GlitchyEngine.ImGui
dispatcher.Dispatch<KeyReleasedEvent>(scope (e) =>
{
ref ImGui.IO io = ref ImGui.GetIO();
io.KeysDown[e.KeyCode] = false;
io.KeysDown[(int32)e.KeyCode] = false;
io.KeyCtrl = io.KeysDown[VK_CONTROL];
io.KeyShift = io.KeysDown[VK_SHIFT];
+27 -3
View File
@@ -6,16 +6,40 @@ namespace GlitchyEngine
{
public static class Input
{
public static extern bool IsKeyPressed(int32 keycode);
public static extern bool IsKeyReleased(int32 keycode);
public static extern bool IsKeyToggled(int32 keycode);
public static extern bool IsKeyPressed(Key keycode);
public static extern bool IsKeyReleased(Key keycode);
public static extern bool IsKeyToggled(Key keycode);
public static extern bool WasKeyPressed(Key keycode);
public static extern bool WasKeyReleased(Key keycode);
public static extern bool WasKeyToggled(Key keycode);
/*
* Determines whether or not the key is being pressed down.
* @remarks IsKeyPressing(kc) == IsKeyPressed(kc) && !IsKeyReleased(kc)
*/
public static extern bool IsKeyPressing(Key keycode);
/*
* Determines whether or not the key is being released down.
* @remarks IsKeyReleasing(kc) == !IsKeyPressed(kc) && IsKeyReleased(kc)
*/
public static extern bool IsKeyReleasing(Key keycode);
public static extern bool IsMouseButtonPressed(MouseButton button);
public static extern bool IsMouseButtonReleased(MouseButton button);
public static extern bool WasMouseButtonPressed(MouseButton button);
public static extern bool WasMouseButtonReleased(MouseButton button);
public static extern int32 GetMouseX();
public static extern int32 GetMouseY();
public static extern int32 GetLastMouseX();
public static extern int32 GetLastMouseY();
public static extern Point GetMousePosition();
public static extern Point GetLastMousePosition();
public static extern void NewFrame();
}
}
+120
View File
@@ -0,0 +1,120 @@
namespace GlitchyEngine
{
public enum Key : int32
{
// Based on Win32 Virtual Keys
LeftButton = 1, // Left mouse button
RightRutton = 2, // Right mouse button
MiddleButton = 4, // Middle mouse button(three - button mouse)
XButton1 = 5, // X1 mouse button
XButton2 = 6, // X2 mouse button
Backspace = 8, // Backspace key
Tab = 9, // Tab key
Return = 13, // Enter key
Shift = 16, // Shift key
Control = 17, // Ctrl key
Alt = 18, // Alt key
Pause = 19, // Pause key
CapsLock = 20, // Caps Lock key
Escape = 27, // ESC key
Space = 32, // Spacebar
Prior = 33, // Page Up key
Next = 34, // Page Down key
End = 35, // END key
Home = 36, // HOME key
Left = 37, // LEFT ARROW key
Up = 38, // UP ARROW key
Right = 39, // RIGHT ARROW key
Down = 40, // DOWN ARROW key
Print = 42, // PRINT key
Insert = 45, // INS key
Delete = 46, // DEL key
Help = 47, // HELP key
Zero = 48, // 0 key
One = 49, // 1 key
Two = 50, // 2 key
Three = 51, // 3 key
Four = 52, // 4 key
Five = 53, // 5 key
Six = 54, // 6 key
Seven = 55, // 7 key
Eight = 56, // 8 key
Nine = 57, // 9 key
A = 65, // A key
B = 66, // B key
C = 67, // C key
D = 68, // D key
E = 69, // E key
F = 70, // F key
G = 71, // G key
H = 72, // H key
I = 73, // I key
J = 74, // J key
K = 75, // K key
L = 76, // L key
M = 77, // M key
N = 78, // N key
O = 79, // O key
P = 80, // P key
Q = 81, // Q key
R = 82, // R key
S = 83, // S key
T = 84, // T key
U = 85, // U key
V = 86, // V key
W = 87, // W key
X = 88, // X key
Y = 89, // Y key
Z = 90, // Z key
LeftSuper = 91, // Left Windows key(Microsoft® Natural® keyboard)
RightSuper = 92, // Right Windows key(Natural keyboard)
Numpad0 = 96, // Numeric keypad 0 key
Numpad1 = 97, // Numeric keypad 1 key
Numpad2 = 98, // Numeric keypad 2 key
Numpad3 = 99, // Numeric keypad 3 key
Numpad4 = 100, // Numeric keypad 4 key
Numpad5 = 101, // Numeric keypad 5 key
Numpad6 = 102, // Numeric keypad 6 key
Numpad7 = 103, // Numeric keypad 7 key
Numpad8 = 104, // Numeric keypad 8 key
Numpad9 = 105, // Numeric keypad 9 key
Multiply = 106, // Multiply key
Add = 107, // Add key
Separator = 108, // Separator key
Subtract = 109, // Subtract key
Decimal = 110, // Decimal key
Divide = 111, // Divide key
F1 = 112, // F1 key
F2 = 113, // F2 key
F3 = 114, // F3 key
F4 = 115, // F4 key
F5 = 116, // F5 key
F6 = 117, // F6 key
F7 = 118, // F7 key
F8 = 119, // F8 key
F9 = 120, // F9 key
F10 = 121, // F10 key
F11 = 122, // F11 key
F12 = 123, // F12 key
F13 = 124, // F13 key
F14 = 125, // F14 key
F15 = 126, // F15 key
F16 = 127, // F16 key
F17 = 128, // F17 key
F18 = 129, // F18 key
F19 = 130, // F19 key
F20 = 131, // F20 key
F21 = 132, // F21 key
F22 = 133, // F22 key
F23 = 134, // F23 key
F24 = 135, // F24 key
Numlock = 144, // NUM LOCK key
Scroll = 145, // SCROLL LOCK key
LeftShift = 160, // Left SHIFT key
RightShift = 161, // Right SHIFT key
LeftControl = 162, // Left CONTROL key
RightControl = 163, // Right CONTROL key
LeftAlt = 164, // Left Alt key
RightAlt = 165 // Right Alt key
}
}
@@ -9,33 +9,63 @@ namespace GlitchyEngine
{
extension Input
{
static int8* LastKeyStates = new int8[256]* ~ delete _;
static int8* CurrentkeyStates = new int8[256]* ~ delete _;
[CLink, CallingConvention(.Stdcall)]
static extern int16 GetKeyState(int32 keycode);
public override static bool IsKeyPressed(int32 keycode)
public override static bool IsKeyPressed(Key keycode)
{
int16 state = GetKeyState(keycode);
//int16 state = GetKeyState((.)keycode);
int8 state = CurrentkeyStates[(int)keycode];
return state < 0;
}
public override static bool IsKeyReleased(int32 keycode)
public override static bool IsKeyReleased(Key keycode)
{
int16 state = GetKeyState(keycode);
//int16 state = GetKeyState((.)keycode);
int8 state = CurrentkeyStates[(int)keycode];
return state >= 0;
}
public override static bool IsKeyToggled(int32 keycode)
public override static bool IsKeyToggled(Key keycode)
{
int16 state = GetKeyState(keycode);
//int16 state = GetKeyState((.)keycode);
int8 state = CurrentkeyStates[(int)keycode];
return (state & 0x1) == 1;
}
public override static bool WasKeyPressed(Key keycode)
{
int8 state = LastKeyStates[(int)keycode];
return state < 0;
}
public override static bool WasKeyReleased(Key keycode)
{
int8 state = LastKeyStates[(int)keycode];
return state >= 0;
}
public override static bool WasKeyToggled(Key keycode)
{
int8 state = LastKeyStates[(int)keycode];
return (state & 0x1) == 1;
}
public override static bool IsKeyPressing(Key keycode)
{
return default;
//return IsKeyPressed(keycode) && WasKeyReleased(keycode);
}
private static mixin MouseButtonToKeyCode(MouseButton button)
{
int32 keycode;
switch(button)
{
case .LeftButton:
case MouseButton.LeftButton:
keycode = VK_LBUTTON;
case .RightButton:
keycode = VK_RBUTTON;
@@ -54,13 +84,15 @@ namespace GlitchyEngine
public override static bool IsMouseButtonPressed(MouseButton button)
{
int16 state = GetKeyState(MouseButtonToKeyCode!(button));
//int16 state = GetKeyState(MouseButtonToKeyCode!(button));
int8 state = CurrentkeyStates[MouseButtonToKeyCode!(button)];
return state < 0;
}
public override static bool IsMouseButtonReleased(MouseButton button)
{
int16 state = GetKeyState(MouseButtonToKeyCode!(button));
//int16 state = GetKeyState(MouseButtonToKeyCode!(button));
int8 state = CurrentkeyStates[MouseButtonToKeyCode!(button)];
return state >= 0;
}
@@ -98,5 +130,23 @@ namespace GlitchyEngine
{
return GetMousePos!().Y;
}
public override static void NewFrame()
{
// Switch last and current keyStates
int8* buffer = LastKeyStates;
LastKeyStates = CurrentkeyStates;
CurrentkeyStates = buffer;
// Get current keyboard state
var result = DirectX.Windows.Winuser.GetKeyboardState((uint8*)CurrentkeyStates);
if(result == 0)
{
DirectX.Common.HResult res = (.)GetLastError();
Log.EngineLogger.Error("Failed to get keyboard state Message({}){}:", (int32)res, res);
}
}
}
}
@@ -297,12 +297,12 @@ namespace GlitchyEngine.Platform.Windows
case WM_KEYDOWN:
{
var event = scope KeyPressedEvent((int32)wParam, (int32)lParam & 0xFFFF);
var event = scope KeyPressedEvent((Key)wParam, (int32)lParam & 0xFFFF);
window._eventCallback(event);
}
case WM_KEYUP:
{
var event = scope KeyReleasedEvent((int32)wParam);
var event = scope KeyReleasedEvent((Key)wParam);
window._eventCallback(event);
}