From b3d1db97d949c099ad2bdf08e4a42697e3323ecc Mon Sep 17 00:00:00 2001 From: Kuukitenshi Date: Mon, 10 Feb 2025 20:12:22 +0000 Subject: [PATCH] feat(input): change Input::pressed, ::axis, etc to use std::string_view --- CHANGELOG.md | 1 + engine/include/cubos/engine/input/input.hpp | 8 +++---- engine/src/input/input.cpp | 24 ++++++++++----------- 3 files changed, 17 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57d684e888..a86f8430e4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -20,6 +20,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Make SSAO optional (#1396, **@tomas7770**). - Made the fixed step accumulator a public resource of the fixed step plugin (**@RiscadoA**). +- Change the name parameter type in input methods like ::pressed, ::axis, etc., to use std::string_view for more readable usage (#1460, **@kuukitenshi**). ### Fixed diff --git a/engine/include/cubos/engine/input/input.hpp b/engine/include/cubos/engine/input/input.hpp index a8b502c5ba..e2b7dba88e 100644 --- a/engine/include/cubos/engine/input/input.hpp +++ b/engine/include/cubos/engine/input/input.hpp @@ -65,25 +65,25 @@ namespace cubos::engine /// @param actionName Name of the action. /// @param player Player whose action state will be retrieved. /// @return Whether the action exists and is pressed. - bool pressed(const char* actionName, int player = 0) const; + bool pressed(std::string_view actionName, int player = 0) const; /// @brief Gets an action state for a specific player. /// @param actionName Name of the action. /// @param player Player whose action state will be retrieved. /// @return Whether the action exists and was just pressed. - bool justPressed(const char* actionName, int player = 0) const; + bool justPressed(std::string_view actionName, int player = 0) const; /// @brief Gets an action state for a specific player. /// @param actionName Name of the action. /// @param player Player whose action state will be retrieved. /// @return Whether the action exists and was just released. - bool justReleased(const char* actionName, int player = 0) const; + bool justReleased(std::string_view actionName, int player = 0) const; /// @brief Gets an axis value for a specific player. /// @param axisName Name of the axis. /// @param player Player whose axis value will be retrieved. /// @return Axis value if the axis exists, 0.0 otherwise. - float axis(const char* axisName, int player = 0) const; + float axis(std::string_view axisName, int player = 0) const; /// @brief Handle a key event. /// @param window Window that received the event. diff --git a/engine/src/input/input.cpp b/engine/src/input/input.cpp index 125f98e2d0..62e82a5a52 100644 --- a/engine/src/input/input.cpp +++ b/engine/src/input/input.cpp @@ -215,7 +215,7 @@ int Input::gamepadCount() const return static_cast(mGamepadStates.size()); } -bool Input::pressed(const char* actionName, int player) const +bool Input::pressed(std::string_view actionName, int player) const { auto pIt = mPlayerBindings.find(player); if (pIt == mPlayerBindings.end()) @@ -224,17 +224,17 @@ bool Input::pressed(const char* actionName, int player) const return false; } - auto aIt = pIt->second.actions().find(actionName); + auto aIt = pIt->second.actions().find(std::string(actionName)); if (aIt == pIt->second.actions().end()) { - CUBOS_WARN("Action {} is not bound to any input for player {}", actionName, player); + CUBOS_WARN("Action {} is not bound to any input for player {}", std::string(actionName), player); return false; } return aIt->second.pressed(); } -bool Input::justPressed(const char* actionName, int player) const +bool Input::justPressed(std::string_view actionName, int player) const { auto pIt = mPlayerBindings.find(player); if (pIt == mPlayerBindings.end()) @@ -243,17 +243,17 @@ bool Input::justPressed(const char* actionName, int player) const return false; } - auto aIt = pIt->second.actions().find(actionName); + auto aIt = pIt->second.actions().find(std::string(actionName)); if (aIt == pIt->second.actions().end()) { - CUBOS_WARN("Action {} is not bound to any input for player {}", actionName, player); + CUBOS_WARN("Action {} is not bound to any input for player {}", std::string(actionName), player); return false; } return aIt->second.justPressed(); } -bool Input::justReleased(const char* actionName, int player) const +bool Input::justReleased(std::string_view actionName, int player) const { auto pIt = mPlayerBindings.find(player); if (pIt == mPlayerBindings.end()) @@ -262,17 +262,17 @@ bool Input::justReleased(const char* actionName, int player) const return false; } - auto aIt = pIt->second.actions().find(actionName); + auto aIt = pIt->second.actions().find(std::string(actionName)); if (aIt == pIt->second.actions().end()) { - CUBOS_WARN("Action {} is not bound to any input for player {}", actionName, player); + CUBOS_WARN("Action {} is not bound to any input for player {}", std::string(actionName), player); return false; } return aIt->second.justReleased(); } -float Input::axis(const char* axisName, int player) const +float Input::axis(std::string_view axisName, int player) const { auto pIt = mPlayerBindings.find(player); if (pIt == mPlayerBindings.end()) @@ -281,10 +281,10 @@ float Input::axis(const char* axisName, int player) const return 0.0F; } - auto aIt = pIt->second.axes().find(axisName); + auto aIt = pIt->second.axes().find(std::string(axisName)); if (aIt == pIt->second.axes().end()) { - CUBOS_WARN("Axis {} is not bound to any input for player {}", axisName, player); + CUBOS_WARN("Axis {} is not bound to any input for player {}", std::string(axisName), player); return 0.0F; }