From 398fc4be37b3f5993571037ef86789f448546ab3 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 2 Jun 2021 09:47:37 +0200 Subject: [PATCH] Add `Keyboard.IsAnyKeyDown` --- Source/Engine/Input/Input.cpp | 21 ++++++++++++++++++ Source/Engine/Input/Keyboard.h | 39 +++++++--------------------------- 2 files changed, 29 insertions(+), 31 deletions(-) diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index d7796691b..44131504d 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -223,6 +223,27 @@ bool Mouse::Update(EventQueue& queue) return false; } +void Keyboard::State::Clear() +{ + Platform::MemoryClear(this, sizeof(State)); +} + +Keyboard::Keyboard() + : InputDevice(SpawnParams(Guid::New(), TypeInitializer), TEXT("Keyboard")) +{ + _state.Clear(); + _prevState.Clear(); +} + +bool Keyboard::IsAnyKeyDown() const +{ + // TODO: optimize with SIMD + bool result = false; + for (auto e : _state.Keys) + result |= e; + return result; +} + void Keyboard::OnCharInput(Char c, Window* target) { // Skip control characters diff --git a/Source/Engine/Input/Keyboard.h b/Source/Engine/Input/Keyboard.h index f182f9731..7877b3f13 100644 --- a/Source/Engine/Input/Keyboard.h +++ b/Source/Engine/Input/Keyboard.h @@ -10,48 +10,20 @@ API_CLASS(NoSpawn) class FLAXENGINE_API Keyboard : public InputDevice { DECLARE_SCRIPTING_TYPE_NO_SPAWN(Keyboard); -public: +protected: - /// - /// The keyboard state. - /// struct State { - /// - /// The input text length (characters count). - /// uint16 InputTextLength; - - /// - /// The input text. - /// Char InputText[32]; - - /// - /// The keys. - /// bool Keys[(int32)KeyboardKeys::MAX]; - - /// - /// Clears the state. - /// - void Clear() - { - Platform::MemoryClear(this, sizeof(State)); - } + void Clear(); }; -protected: - State _state; State _prevState; - explicit Keyboard() - : InputDevice(SpawnParams(Guid::New(), TypeInitializer), TEXT("Keyboard")) - { - _state.Clear(); - _prevState.Clear(); - } + explicit Keyboard(); public: @@ -94,6 +66,11 @@ public: return !_state.Keys[static_cast(key)] && _prevState.Keys[static_cast(key)]; } + /// + /// Checks if any keyboard key is currently pressed. + /// + API_PROPERTY() bool IsAnyKeyDown() const; + public: ///