Skip to content

Commit

Permalink
feat(input): allow changing mouse state through input plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
mcanais committed Mar 8, 2025
1 parent dd6346f commit d769171
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Made collision layer and mask their own components (#535, **@fallenatlas**).
- Make RenderPicker optional (#1407, **@tomas7770**).
- Allow mouse state to be changed through the input plugin (#1401, **@mcanais**).

## [v0.6.0] - 2025-02-10

Expand Down
22 changes: 22 additions & 0 deletions engine/include/cubos/engine/input/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ namespace cubos::engine
/// @brief Alias for @ref core::io::MouseButton.
using MouseButton = core::io::MouseButton;

/// @brief Alias for @ref core::io::MouseState.
using MouseState = core::io::MouseState;

Input() = default;
~Input() = default;

Expand Down Expand Up @@ -123,6 +126,22 @@ namespace cubos::engine
/// @return Mouse displacement.
glm::ivec2 mouseDelta() const;

/// @brief Sets the mouse state when the window is focused.
/// @param state Mouse state.
void mouseState(MouseState state);

/// @brief Gets the mouse state when the window is focused.
/// @return Mouse state when the window is focused.
MouseState mouseState() const;

/// @brief Sets the mouse state to be updated.
/// @param window Window.
void updateMouseState(core::io::Window& window);

/// @brief Gets the update mouse state.
/// @return Whether the mouse state should be updated.
bool updateMouseState() const;

/// @brief Handle all other events - discards them.
///
/// This is method exists so that `std::visit` can be used with @ref core::io::WindowEvent
Expand Down Expand Up @@ -171,5 +190,8 @@ namespace cubos::engine

glm::ivec2 mMousePosition = {0, 0};
glm::ivec2 mPreviousMousePosition = {0, 0};

MouseState mMouseState = MouseState::Default;
bool mUpdateMouseState = false;
};
} // namespace cubos::engine
23 changes: 23 additions & 0 deletions engine/src/input/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ using cubos::core::io::KeyEvent;
using cubos::core::io::MouseButton;
using cubos::core::io::MouseButtonEvent;
using cubos::core::io::MouseMoveEvent;
using cubos::core::io::MouseState;
using cubos::core::io::Window;

using namespace cubos::engine;
Expand Down Expand Up @@ -444,6 +445,28 @@ glm::ivec2 Input::mouseDelta() const
return mMousePosition - mPreviousMousePosition;
}

void Input::mouseState(MouseState state)
{
mUpdateMouseState = true;
mMouseState = state;
}

MouseState Input::mouseState() const
{
return mMouseState;
}

void Input::updateMouseState(Window& window)
{
window->mouseState(mMouseState);
mUpdateMouseState = false;
}

bool Input::updateMouseState() const
{
return mUpdateMouseState;
}

void Input::pollGamepads(const Window& window)
{
for (auto& [gamepad, state] : mGamepadStates)
Expand Down
7 changes: 6 additions & 1 deletion engine/src/input/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void cubos::engine::inputPlugin(Cubos& cubos)
cubos.system("handle WindowEvents for Input")
.tagged(inputUpdateTag)
.after(windowPollTag)
.call([](const Window& window, Input& input, EventReader<WindowEvent> events) {
.call([](Window& window, Input& input, EventReader<WindowEvent> events) {
input.updateMouse();
input.updateActions();

Expand All @@ -35,6 +35,11 @@ void cubos::engine::inputPlugin(Cubos& cubos)
std::visit([window, &input](auto e) { input.handle(window, e); }, event);
}

if (input.updateMouseState())
{
input.updateMouseState(window);
}

input.pollGamepads(window);
});
}
10 changes: 5 additions & 5 deletions engine/src/utils/free_camera/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void cubos::engine::freeCameraPlugin(Cubos& cubos)
cubos.resource<State>();

cubos.system("rotate FreeCameraController on mouse motion")
.call([](const Input& input, Window& window, const DeltaTime& deltaTime, State& state,
.call([](Input& input, const DeltaTime& deltaTime, State& state,
Query<FreeCameraController&, Position&, Rotation&> entities) {
bool anyEnabled = false;

Expand Down Expand Up @@ -68,16 +68,16 @@ void cubos::engine::freeCameraPlugin(Cubos& cubos)
}

// Lock/unlock mouse depending on if any FreeCameraController is enabled.
if (anyEnabled && window->mouseState() != MouseState::Locked)
if (anyEnabled && input.mouseState() != MouseState::Locked)
{
window->mouseState(MouseState::Locked);
input.mouseState(MouseState::Locked);
state.tookOverMouse = true;
}

if (!anyEnabled && state.tookOverMouse)
{
window->mouseState(MouseState::Default);
input.mouseState(MouseState::Default);
state.tookOverMouse = false;
}
});
}
}

0 comments on commit d769171

Please # to comment.