Skip to content

Commit

Permalink
feat(input): change Input::pressed, ::axis, etc to use std::string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
kuukitenshi committed Feb 10, 2025
1 parent 7e3ecad commit b3d1db9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 4 additions & 4 deletions engine/include/cubos/engine/input/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
24 changes: 12 additions & 12 deletions engine/src/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ int Input::gamepadCount() const
return static_cast<int>(mGamepadStates.size());
}

bool Input::pressed(const char* actionName, int player) const
bool Input::pressed(std::string_view actionName, int player) const

Check warning on line 218 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L218

Added line #L218 was not covered by tests
{
auto pIt = mPlayerBindings.find(player);
if (pIt == mPlayerBindings.end())
Expand All @@ -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));

Check warning on line 227 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L227

Added line #L227 was not covered by tests
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);

Check warning on line 230 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L230

Added line #L230 was not covered by tests
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

Check warning on line 237 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L237

Added line #L237 was not covered by tests
{
auto pIt = mPlayerBindings.find(player);
if (pIt == mPlayerBindings.end())
Expand All @@ -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));

Check warning on line 246 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L246

Added line #L246 was not covered by tests
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);

Check warning on line 249 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L249

Added line #L249 was not covered by tests
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

Check warning on line 256 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L256

Added line #L256 was not covered by tests
{
auto pIt = mPlayerBindings.find(player);
if (pIt == mPlayerBindings.end())
Expand All @@ -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));

Check warning on line 265 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L265

Added line #L265 was not covered by tests
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);

Check warning on line 268 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L268

Added line #L268 was not covered by tests
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

Check warning on line 275 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L275

Added line #L275 was not covered by tests
{
auto pIt = mPlayerBindings.find(player);
if (pIt == mPlayerBindings.end())
Expand All @@ -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));

Check warning on line 284 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L284

Added line #L284 was not covered by tests
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);

Check warning on line 287 in engine/src/input/input.cpp

View check run for this annotation

Codecov / codecov/patch

engine/src/input/input.cpp#L287

Added line #L287 was not covered by tests
return 0.0F;
}

Expand Down

0 comments on commit b3d1db9

Please sign in to comment.