Port window and input events to flecs

This commit is contained in:
2025-04-19 12:22:22 +02:00
parent e590203234
commit ba6a560129
8 changed files with 158 additions and 167 deletions

View File

@@ -4,36 +4,40 @@
namespace Input {
// void KeyListener::key_event(KeyInput const& key_input_event)
// {
// auto& key_state = registry.ctx().get<State<KeyCode>>();
static void keyboard_input_observer(KeyboardInput const& keyboard_input);
// if (key_input_event.action == static_cast<Action>(GLFW_PRESS)) {
// key_state.press(key_input_event.key_code);
// } else if (key_input_event.action == static_cast<Action>(GLFW_RELEASE)) {
// key_state.release(key_input_event.key_code);
// }
// }
// void CursorListener::cursor_event(MouseMotion const& mouse_motion_event)
// {
// auto& mouse_motion = registry.ctx().get<MouseMotion>();
// mouse_motion.delta += mouse_motion_event.delta;
// }
void reset_mouse_motion(entt::registry& registry)
InputModule::InputModule(flecs::world& world)
{
auto& mouse_motion = registry.ctx().get<MouseMotion>();
mouse_motion = {};
world.component<Input::KeyboardInput>();
world.component<Input::MouseButtonInput>();
world.component<Input::MouseMotion>();
world.system<ButtonInput<KeyCode>>("ClearButtonInput")
.kind(flecs::PostUpdate)
.each(ButtonInput<KeyCode>::clear_button_input);
world.observer<KeyboardInput>("KeyboardInputObserver")
.event(flecs::OnSet)
.each(keyboard_input_observer);
}
template <typename T> void State<T>::update_state(flecs::entity window_entity)
void keyboard_input_observer(KeyboardInput const& keyboard_input)
{
auto* state = window_entity.get_mut<State<T>>();
state->just_pressed_keys.clear();
state->just_released_keys.clear();
auto* key_state = keyboard_input.window.get_mut<Input::ButtonInput<Input::KeyCode>>();
if (keyboard_input.action == static_cast<Input::Action>(GLFW_PRESS)) {
key_state->press(keyboard_input.key_code);
} else if (keyboard_input.action == static_cast<Input::Action>(GLFW_RELEASE)) {
key_state->release(keyboard_input.key_code);
}
}
template class State<KeyCode>;
template <typename T> void ButtonInput<T>::clear_button_input(ButtonInput<T>& state)
{
state.just_pressed_keys.clear();
state.just_released_keys.clear();
}
template class ButtonInput<KeyCode>;
} // namespace Input