From 8669a291fb2c8ba6a2b9224ec26e430a68b5b79c Mon Sep 17 00:00:00 2001 From: Ruan Lucas <79365912+RuanLucasGD@users.noreply.github.com> Date: Thu, 19 Jan 2023 15:21:50 -0400 Subject: [PATCH 1/3] Add methods IsAnyKeyDown on Mouse/Gamepad class --- Source/Engine/Input/Gamepad.cpp | 9 +++++++++ Source/Engine/Input/Gamepad.h | 5 +++++ Source/Engine/Input/Input.cpp | 11 +++++++++++ Source/Engine/Input/Mouse.h | 5 +++++ 4 files changed, 30 insertions(+) diff --git a/Source/Engine/Input/Gamepad.cpp b/Source/Engine/Input/Gamepad.cpp index 7835e7724..b7133dfbe 100644 --- a/Source/Engine/Input/Gamepad.cpp +++ b/Source/Engine/Input/Gamepad.cpp @@ -31,6 +31,15 @@ void Gamepad::ResetState() _mappedPrevState.Clear(); } +bool Gamepad::IsAnyKeyDown() const +{ + // TODO: optimize with SIMD + bool result = false; + for (auto e : _state.Buttons) + result |= e; + return result; +} + bool Gamepad::Update(EventQueue& queue) { // Copy state diff --git a/Source/Engine/Input/Gamepad.h b/Source/Engine/Input/Gamepad.h index 63decde7c..1fa0f20b8 100644 --- a/Source/Engine/Input/Gamepad.h +++ b/Source/Engine/Input/Gamepad.h @@ -164,6 +164,11 @@ public: return _mappedState.Buttons[static_cast(button)] && !_mappedPrevState.Buttons[static_cast(button)]; } + /// + /// Checks if any gamepad button is currently pressed. + /// + API_PROPERTY() bool IsAnyKeyDown() const; + /// /// Gets the gamepad button up state (true if was released during the current frame). /// diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index b7c202512..2fc75a15e 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -17,6 +17,8 @@ #include "Engine/Profiler/ProfilerCPU.h" #include "Engine/Serialization/JsonTools.h" +#include "Engine/Debug/DebugLog.h" + struct AxisEvaluation { float RawValue; @@ -183,6 +185,15 @@ void Mouse::OnMouseDown(const Float2& position, const MouseButton button, Window e.MouseData.Position = position; } +bool Mouse::IsAnyKeyDown() const +{ + // TODO: optimize with SIMD + bool result = false; + for (auto e : Mouse::_state.MouseButtons) + result |= e; + return result; +} + void Mouse::OnMouseUp(const Float2& position, const MouseButton button, Window* target) { Event& e = _queue.AddOne(); diff --git a/Source/Engine/Input/Mouse.h b/Source/Engine/Input/Mouse.h index ebf25ebb1..e137948bb 100644 --- a/Source/Engine/Input/Mouse.h +++ b/Source/Engine/Input/Mouse.h @@ -64,6 +64,11 @@ public: return _state.MousePosition; } + /// + /// Checks if any mouse key is currently pressed. + /// + API_PROPERTY() bool IsAnyKeyDown() const; + /// /// Gets the delta position of the mouse in the screen-space coordinates. /// From d20bce9967afc7e9784d8fdb02bd1585630e9350 Mon Sep 17 00:00:00 2001 From: Ruan Lucas <79365912+RuanLucasGD@users.noreply.github.com> Date: Thu, 19 Jan 2023 20:52:20 -0400 Subject: [PATCH 2/3] Add Vector2.Normalized --- Source/Engine/Core/Math/Vector2.cs | 13 +++++++++++++ Source/Engine/Core/Math/Vector2.h | 9 +++++++++ Source/Engine/Input/Input.cpp | 2 -- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/Source/Engine/Core/Math/Vector2.cs b/Source/Engine/Core/Math/Vector2.cs index 25c25ca21..cd1849383 100644 --- a/Source/Engine/Core/Math/Vector2.cs +++ b/Source/Engine/Core/Math/Vector2.cs @@ -301,6 +301,19 @@ namespace FlaxEngine } } + /// + /// Gets the normalized vector. Returned vector has length equal 1. + /// + public Vector2 Normalized + { + get + { + Vector2 result = this; + result.Normalize(); + return result; + } + } + /// /// Creates an array containing the elements of the vector. /// diff --git a/Source/Engine/Core/Math/Vector2.h b/Source/Engine/Core/Math/Vector2.h index 2b8c80293..2f7a143c4 100644 --- a/Source/Engine/Core/Math/Vector2.h +++ b/Source/Engine/Core/Math/Vector2.h @@ -223,6 +223,15 @@ public: return Vector2Base(-X, -Y); } + /// + /// Calculates a normalized vector that has length equal to 1. + /// + Vector2Base GetNormalized() const + { + const T rcp = 1.0f / Length(); + return Vector2Base(X * rcp, Y * rcp); + } + public: /// /// Performs vector normalization (scales vector up to unit length). diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index 2fc75a15e..027c40bf2 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -17,8 +17,6 @@ #include "Engine/Profiler/ProfilerCPU.h" #include "Engine/Serialization/JsonTools.h" -#include "Engine/Debug/DebugLog.h" - struct AxisEvaluation { float RawValue; From 82e9923be3b0f36978735f67ddea74c1dbce9007 Mon Sep 17 00:00:00 2001 From: Ruan Lucas <79365912+RuanLucasGD@users.noreply.github.com> Date: Thu, 26 Jan 2023 08:54:15 -0400 Subject: [PATCH 3/3] change IsAnyKeyDown to IsAnyButtonDown and add Float2.Normalized --- Source/Engine/Core/Math/Float2.cs | 13 +++++++++++++ Source/Engine/Input/Gamepad.cpp | 2 +- Source/Engine/Input/Gamepad.h | 2 +- Source/Engine/Input/Input.cpp | 2 +- Source/Engine/Input/Mouse.h | 4 ++-- 5 files changed, 18 insertions(+), 5 deletions(-) diff --git a/Source/Engine/Core/Math/Float2.cs b/Source/Engine/Core/Math/Float2.cs index 276c0c713..15f412d6e 100644 --- a/Source/Engine/Core/Math/Float2.cs +++ b/Source/Engine/Core/Math/Float2.cs @@ -267,6 +267,19 @@ namespace FlaxEngine /// This method may be preferred to when only a relative length is needed and speed is of the essence. public float LengthSquared => X * X + Y * Y; + /// + /// Gets the normalized vector. Returned vector has length equal 1. + /// + public Float2 Normalized + { + get + { + Float2 result = this; + result.Normalize(); + return result; + } + } + /// /// Converts the vector into a unit vector. /// diff --git a/Source/Engine/Input/Gamepad.cpp b/Source/Engine/Input/Gamepad.cpp index b7133dfbe..278dc5961 100644 --- a/Source/Engine/Input/Gamepad.cpp +++ b/Source/Engine/Input/Gamepad.cpp @@ -31,7 +31,7 @@ void Gamepad::ResetState() _mappedPrevState.Clear(); } -bool Gamepad::IsAnyKeyDown() const +bool Gamepad::IsAnyButtonDown() const { // TODO: optimize with SIMD bool result = false; diff --git a/Source/Engine/Input/Gamepad.h b/Source/Engine/Input/Gamepad.h index 1fa0f20b8..27a53a716 100644 --- a/Source/Engine/Input/Gamepad.h +++ b/Source/Engine/Input/Gamepad.h @@ -167,7 +167,7 @@ public: /// /// Checks if any gamepad button is currently pressed. /// - API_PROPERTY() bool IsAnyKeyDown() const; + API_PROPERTY() bool IsAnyButtonDown() const; /// /// Gets the gamepad button up state (true if was released during the current frame). diff --git a/Source/Engine/Input/Input.cpp b/Source/Engine/Input/Input.cpp index 027c40bf2..7a4547c62 100644 --- a/Source/Engine/Input/Input.cpp +++ b/Source/Engine/Input/Input.cpp @@ -183,7 +183,7 @@ void Mouse::OnMouseDown(const Float2& position, const MouseButton button, Window e.MouseData.Position = position; } -bool Mouse::IsAnyKeyDown() const +bool Mouse::IsAnyButtonDown() const { // TODO: optimize with SIMD bool result = false; diff --git a/Source/Engine/Input/Mouse.h b/Source/Engine/Input/Mouse.h index e137948bb..7fc2be75a 100644 --- a/Source/Engine/Input/Mouse.h +++ b/Source/Engine/Input/Mouse.h @@ -65,9 +65,9 @@ public: } /// - /// Checks if any mouse key is currently pressed. + /// Checks if any mouse button is currently pressed. /// - API_PROPERTY() bool IsAnyKeyDown() const; + API_PROPERTY() bool IsAnyButtonDown() const; /// /// Gets the delta position of the mouse in the screen-space coordinates.