From df0e3e2531c2d81ad2cfaf6cd6d3f74122f48cab Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Fri, 10 Feb 2023 00:07:58 +0800 Subject: [PATCH] Revise input mapper. Separate input events into separate headers --- src/app/sdl/sdl-input-manager.cpp | 1 + src/game/state/boot.cpp | 1 + src/game/state/credits.cpp | 66 ++++++--- src/game/state/credits.hpp | 3 +- src/game/state/splash.cpp | 92 +++++++----- src/game/state/splash.hpp | 4 +- src/input/application-events.hpp | 32 ++++ src/input/control-events.hpp | 59 ++++++++ src/input/control-map.hpp | 4 +- src/input/control.hpp | 2 +- src/input/device-events.hpp | 47 ++++++ src/input/device.hpp | 2 +- src/input/gamepad-events.hpp | 71 +++++++++ src/input/gamepad.hpp | 3 +- src/input/input-events.hpp | 238 ------------------------------ src/input/input.hpp | 28 ++++ src/input/keyboard-events.hpp | 66 +++++++++ src/input/keyboard.hpp | 2 +- src/input/mapper.cpp | 19 ++- src/input/mapper.hpp | 49 +++++- src/input/mapping-events.hpp | 83 +++++++++++ src/input/mouse-events.hpp | 95 ++++++++++++ src/input/mouse.hpp | 2 +- src/main.cpp | 7 +- 24 files changed, 656 insertions(+), 320 deletions(-) create mode 100644 src/input/application-events.hpp create mode 100644 src/input/control-events.hpp create mode 100644 src/input/device-events.hpp create mode 100644 src/input/gamepad-events.hpp delete mode 100644 src/input/input-events.hpp create mode 100644 src/input/input.hpp create mode 100644 src/input/keyboard-events.hpp create mode 100644 src/input/mapping-events.hpp create mode 100644 src/input/mouse-events.hpp diff --git a/src/app/sdl/sdl-input-manager.cpp b/src/app/sdl/sdl-input-manager.cpp index f74b3a9..1693f88 100644 --- a/src/app/sdl/sdl-input-manager.cpp +++ b/src/app/sdl/sdl-input-manager.cpp @@ -18,6 +18,7 @@ */ #include "app/sdl/sdl-input-manager.hpp" +#include "input/application-events.hpp" #include "debug/log.hpp" #include "math/map.hpp" #include diff --git a/src/game/state/boot.cpp b/src/game/state/boot.cpp index fce973a..5e224a1 100644 --- a/src/game/state/boot.cpp +++ b/src/game/state/boot.cpp @@ -88,6 +88,7 @@ #include "utility/paths.hpp" #include "utility/dict.hpp" #include "utility/hash/fnv1a.hpp" +#include "input/application-events.hpp" #include #include #include diff --git a/src/game/state/credits.cpp b/src/game/state/credits.cpp index 4eff5fd..1d5b42d 100644 --- a/src/game/state/credits.cpp +++ b/src/game/state/credits.cpp @@ -73,27 +73,56 @@ credits::credits(game::context& ctx): // Start credits fade in animation credits_fade_in_animation.play(); - // Set up credits skipper - input_mapped_subscription = ctx.input_mapper.get_input_mapped_channel().subscribe - ( - [this, &ctx](const auto& event) - { - auto mapping_type = event.mapping->get_mapping_type(); - - if (mapping_type != input::mapping_type::gamepad_axis && - mapping_type != input::mapping_type::mouse_motion && - mapping_type != input::mapping_type::mouse_scroll) + // Construct splash skip function + auto skip = [&](const auto& event) + { + ctx.function_queue.emplace + ( + [&]() { - if (this->credits_text.get_color()[3] > 0.0f) - { - // Change state - ctx.state_machine.pop(); - ctx.state_machine.emplace(new game::state::extras_menu(ctx)); - } + // Black out screen + ctx.window->get_rasterizer()->set_clear_color(0.0f, 0.0f, 0.0f, 1.0f); + ctx.window->get_rasterizer()->clear_framebuffer(true, false, false); + ctx.window->swap_buffers(); + + // Change to main menu state + ctx.state_machine.pop(); + ctx.state_machine.emplace(new game::state::extras_menu(ctx)); } + ); + }; + + // Set up splash skippers + input_mapped_subscriptions.emplace_back + ( + ctx.input_mapper.get_gamepad_button_mapped_channel().subscribe + ( + skip + ) + ); + input_mapped_subscriptions.emplace_back + ( + ctx.input_mapper.get_key_mapped_channel().subscribe + ( + skip + ) + ); + input_mapped_subscriptions.emplace_back + ( + ctx.input_mapper.get_mouse_button_mapped_channel().subscribe + ( + skip + ) + ); + + // Enable credits skippers next frame + ctx.function_queue.push + ( + [&]() + { + ctx.input_mapper.connect(ctx.input_manager->get_event_queue()); } ); - ctx.input_mapper.connect(ctx.input_manager->get_event_queue()); ctx.ui_scene->add_object(&credits_text); @@ -104,8 +133,9 @@ credits::~credits() { debug::log::trace("Exiting credits state..."); - // Disable credits skipper + // Disable credits skippers ctx.input_mapper.disconnect(); + input_mapped_subscriptions.clear(); // Destruct credits text ctx.ui_scene->remove_object(&credits_text); diff --git a/src/game/state/credits.hpp b/src/game/state/credits.hpp index ee82a14..9767265 100644 --- a/src/game/state/credits.hpp +++ b/src/game/state/credits.hpp @@ -24,6 +24,7 @@ #include "scene/text.hpp" #include "animation/animation.hpp" #include "event/subscription.hpp" +#include namespace game { namespace state { @@ -38,7 +39,7 @@ private: scene::text credits_text; animation credits_fade_in_animation; animation credits_scroll_animation; - std::shared_ptr<::event::subscription> input_mapped_subscription; + std::vector> input_mapped_subscriptions; }; } // namespace state diff --git a/src/game/state/splash.cpp b/src/game/state/splash.cpp index fdbd7e9..38e21f1 100644 --- a/src/game/state/splash.cpp +++ b/src/game/state/splash.cpp @@ -18,25 +18,24 @@ */ #include "game/state/splash.hpp" -#include "game/state/main-menu.hpp" -#include "animation/screen-transition.hpp" #include "animation/animation.hpp" #include "animation/animator.hpp" #include "animation/ease.hpp" -#include "render/passes/clear-pass.hpp" -#include "game/context.hpp" +#include "animation/screen-transition.hpp" #include "debug/log.hpp" -#include "resources/resource-manager.hpp" -#include "render/material-flags.hpp" +#include "game/context.hpp" +#include "game/state/main-menu.hpp" #include "math/linear-algebra.hpp" +#include "render/material-flags.hpp" +#include "render/passes/clear-pass.hpp" +#include "resources/resource-manager.hpp" namespace game { namespace state { splash::splash(game::context& ctx): - game::state::base(ctx), - skipped(false) -{ + game::state::base(ctx) +{ debug::log::trace("Entering splash state..."); // Enable color buffer clearing in UI pass @@ -124,38 +123,56 @@ splash::splash(game::context& ctx): // Start splash fade in animation splash_fade_in_animation.play(); - // Set up splash skipper - input_mapped_subscription = ctx.input_mapper.get_input_mapped_channel().subscribe - ( - [this](const auto& event) - { - auto mapping_type = event.mapping->get_mapping_type(); - - if (!this->skipped && - mapping_type != input::mapping_type::gamepad_axis && - mapping_type != input::mapping_type::mouse_motion && - mapping_type != input::mapping_type::mouse_scroll) + // Construct splash skip function + auto skip = [&](const auto& event) + { + ctx.function_queue.emplace + ( + [&]() { - this->skipped = true; + // Black out screen + ctx.window->get_rasterizer()->set_clear_color(0.0f, 0.0f, 0.0f, 1.0f); + ctx.window->get_rasterizer()->clear_framebuffer(true, false, false); + ctx.window->swap_buffers(); - this->ctx.function_queue.emplace - ( - [&ctx = this->ctx]() - { - // Black out screen - ctx.window->get_rasterizer()->set_clear_color(0.0f, 0.0f, 0.0f, 1.0f); - ctx.window->get_rasterizer()->clear_framebuffer(true, false, false); - ctx.window->swap_buffers(); - - // Change to main menu state - ctx.state_machine.pop(); - ctx.state_machine.emplace(new game::state::main_menu(ctx, true)); - } - ); + // Change to main menu state + ctx.state_machine.pop(); + ctx.state_machine.emplace(new game::state::main_menu(ctx, true)); } + ); + }; + + // Set up splash skippers + input_mapped_subscriptions.emplace_back + ( + ctx.input_mapper.get_gamepad_button_mapped_channel().subscribe + ( + skip + ) + ); + input_mapped_subscriptions.emplace_back + ( + ctx.input_mapper.get_key_mapped_channel().subscribe + ( + skip + ) + ); + input_mapped_subscriptions.emplace_back + ( + ctx.input_mapper.get_mouse_button_mapped_channel().subscribe + ( + skip + ) + ); + + // Enable splash skippers next frame + ctx.function_queue.push + ( + [&]() + { + ctx.input_mapper.connect(ctx.input_manager->get_event_queue()); } ); - ctx.input_mapper.connect(ctx.input_manager->get_event_queue()); debug::log::trace("Entered splash state"); } @@ -164,8 +181,9 @@ splash::~splash() { debug::log::trace("Exiting splash state..."); - // Disable splash skipper + // Disable splash skippers ctx.input_mapper.disconnect(); + input_mapped_subscriptions.clear(); // Remove splash fade animations from animator ctx.animator->remove_animation(&splash_fade_in_animation); diff --git a/src/game/state/splash.hpp b/src/game/state/splash.hpp index 9f6dbfe..1f2b855 100644 --- a/src/game/state/splash.hpp +++ b/src/game/state/splash.hpp @@ -25,6 +25,7 @@ #include "scene/billboard.hpp" #include "animation/animation.hpp" #include "event/subscription.hpp" +#include namespace game { namespace state { @@ -40,8 +41,7 @@ private: scene::billboard splash_billboard; animation splash_fade_in_animation; animation splash_fade_out_animation; - std::shared_ptr<::event::subscription> input_mapped_subscription; - bool skipped; + std::vector> input_mapped_subscriptions; }; } // namespace state diff --git a/src/input/application-events.hpp b/src/input/application-events.hpp new file mode 100644 index 0000000..363a9a6 --- /dev/null +++ b/src/input/application-events.hpp @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2023 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_INPUT_APPLICATION_EVENTS_HPP +#define ANTKEEPER_INPUT_APPLICATION_EVENTS_HPP + +namespace input { + +/** + * Event generated when the application has been requested to quit. + */ +struct application_quit_event {}; + +} // namespace input + +#endif // ANTKEEPER_INPUT_APPLICATION_EVENTS_HPP diff --git a/src/input/control-events.hpp b/src/input/control-events.hpp new file mode 100644 index 0000000..c367532 --- /dev/null +++ b/src/input/control-events.hpp @@ -0,0 +1,59 @@ +/* + * Copyright (C) 2023 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_INPUT_CONTROL_EVENTS_HPP +#define ANTKEEPER_INPUT_CONTROL_EVENTS_HPP + +namespace input { + +class control; + +/** + * Event generated when a control has been activated. + */ +struct control_activated_event +{ + /// Control that was activated. + control* control; +}; + +/** + * Event generated while a control is active. + */ +struct control_active_event +{ + /// Active control. + control* control; + + /// Control input value. + float input_value; +}; + +/** + * Event generated when a control has been deactivated. + */ +struct control_deactivated_event +{ + /// Control that was deactivated. + control* control; +}; + +} // namespace input + +#endif // ANTKEEPER_INPUT_CONTROL_EVENTS_HPP diff --git a/src/input/control-map.hpp b/src/input/control-map.hpp index 85c4863..ab18e30 100644 --- a/src/input/control-map.hpp +++ b/src/input/control-map.hpp @@ -23,7 +23,9 @@ #include "event/subscription.hpp" #include "event/queue.hpp" #include "input/control.hpp" -#include "input/input-events.hpp" +#include "input/gamepad-events.hpp" +#include "input/keyboard-events.hpp" +#include "input/mouse-events.hpp" #include "input/mapping.hpp" #include #include diff --git a/src/input/control.hpp b/src/input/control.hpp index 621f5b2..fe50138 100644 --- a/src/input/control.hpp +++ b/src/input/control.hpp @@ -20,7 +20,7 @@ #ifndef ANTKEEPER_INPUT_CONTROL_HPP #define ANTKEEPER_INPUT_CONTROL_HPP -#include "input/input-events.hpp" +#include "input/control-events.hpp" #include "event/publisher.hpp" #include diff --git a/src/input/device-events.hpp b/src/input/device-events.hpp new file mode 100644 index 0000000..15c2a1e --- /dev/null +++ b/src/input/device-events.hpp @@ -0,0 +1,47 @@ +/* + * Copyright (C) 2023 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_INPUT_DEVICE_EVENTS_HPP +#define ANTKEEPER_INPUT_DEVICE_EVENTS_HPP + +namespace input { + +class device; + +/** + * Event generated when an input device has been connected. + */ +struct device_connected_event +{ + /// Device that was connected. + device* device; +}; + +/** + * Event generated when an input device has been disconnected. + */ +struct device_disconnected_event +{ + /// Device that was disconnected. + device* device; +}; + +} // namespace input + +#endif // ANTKEEPER_INPUT_DEVICE_EVENTS_HPP diff --git a/src/input/device.hpp b/src/input/device.hpp index cc14039..630352b 100644 --- a/src/input/device.hpp +++ b/src/input/device.hpp @@ -21,7 +21,7 @@ #define ANTKEEPER_INPUT_DEVICE_HPP #include "input/device-type.hpp" -#include "input/input-events.hpp" +#include "input/device-events.hpp" #include "event/publisher.hpp" #include "utility/uuid.hpp" #include diff --git a/src/input/gamepad-events.hpp b/src/input/gamepad-events.hpp new file mode 100644 index 0000000..b300d3f --- /dev/null +++ b/src/input/gamepad-events.hpp @@ -0,0 +1,71 @@ +/* + * Copyright (C) 2023 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_INPUT_GAMEPAD_EVENTS_HPP +#define ANTKEEPER_INPUT_GAMEPAD_EVENTS_HPP + +#include "input/gamepad-axis.hpp" +#include "input/gamepad-button.hpp" + +namespace input { + +class gamepad; + +/** + * Event generated when a gamepad button has been pressed. + */ +struct gamepad_button_pressed_event +{ + /// Gamepad that generated the event. + gamepad* gamepad; + + /// Gamepad button being pressed. + gamepad_button button; +}; + +/** + * Event generated when a gamepad button has been released. + */ +struct gamepad_button_released_event +{ + /// Gamepad that generated the event. + gamepad* gamepad; + + /// Gamepad button being released. + gamepad_button button; +}; + +/** + * Event generated when a gamepad axis has been moved. + */ +struct gamepad_axis_moved_event +{ + /// Gamepad that generated the event. + gamepad* gamepad; + + /// Gamepad axis being moved. + gamepad_axis axis; + + /// Position of the gamepad axis, on `[-1, 1]`. + float position; +}; + +} // namespace input + +#endif // ANTKEEPER_INPUT_GAMEPAD_EVENTS_HPP diff --git a/src/input/gamepad.hpp b/src/input/gamepad.hpp index 7ae7f3f..6758de6 100644 --- a/src/input/gamepad.hpp +++ b/src/input/gamepad.hpp @@ -21,7 +21,7 @@ #define ANTKEEPER_INPUT_GAMEPAD_HPP #include "input/device.hpp" -#include "input/input-events.hpp" +#include "input/gamepad-events.hpp" #include "input/gamepad-axis.hpp" #include "input/gamepad-button.hpp" #include "event/publisher.hpp" @@ -155,7 +155,6 @@ private: float axis_activation_min[6]; float axis_activation_max[6]; gamepad_response_curve axis_response_curves[6]; - bool left_deadzone_cross; bool right_deadzone_cross; float left_deadzone_roundness; diff --git a/src/input/input-events.hpp b/src/input/input-events.hpp deleted file mode 100644 index b618606..0000000 --- a/src/input/input-events.hpp +++ /dev/null @@ -1,238 +0,0 @@ -/* - * Copyright (C) 2023 Christopher J. Howard - * - * This file is part of Antkeeper source code. - * - * Antkeeper source code is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Antkeeper source code is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Antkeeper source code. If not, see . - */ - -#ifndef ANTKEEPER_INPUT_INPUT_EVENTS_HPP -#define ANTKEEPER_INPUT_INPUT_EVENTS_HPP - -#include "input/gamepad-axis.hpp" -#include "input/gamepad-button.hpp" -#include "input/mouse-motion-axis.hpp" -#include "input/mouse-scroll-axis.hpp" -#include "input/scancode.hpp" -#include "input/mapping.hpp" -#include "input/modifier-key.hpp" -#include "math/vector.hpp" -#include -#include - -namespace input { - -class control; -class device; -class gamepad; -class keyboard; -class mouse; - -/** - * Event generated when a control has been activated. - */ -struct control_activated_event -{ - /// Control that was activated. - control* control; -}; - -/** - * Event generated while a control is active. - */ -struct control_active_event -{ - /// Active control. - control* control; - - /// Control input value. - float input_value; -}; - -/** - * Event generated when a control has been deactivated. - */ -struct control_deactivated_event -{ - /// Control that was deactivated. - control* control; -}; - -/** - * Event generated when an input mapping has been generated. - */ -struct input_mapped_event -{ - /// Input mapping that was generated. - std::shared_ptr mapping; -}; - -/** - * Event generated when an input device has been connected. - */ -struct device_connected_event -{ - /// Device that was connected. - device* device; -}; - -/** - * Event generated when an input device has been disconnected. - */ -struct device_disconnected_event -{ - /// Device that was disconnected. - device* device; -}; - -/** - * Event generated when a gamepad button has been pressed. - */ -struct gamepad_button_pressed_event -{ - /// Gamepad that generated the event. - gamepad* gamepad; - - /// Gamepad button being pressed. - gamepad_button button; -}; - -/** - * Event generated when a gamepad button has been released. - */ -struct gamepad_button_released_event -{ - /// Gamepad that generated the event. - gamepad* gamepad; - - /// Gamepad button being released. - gamepad_button button; -}; - -/** - * Event generated when a gamepad axis has been moved. - */ -struct gamepad_axis_moved_event -{ - /// Gamepad that generated the event. - gamepad* gamepad; - - /// Gamepad axis being moved. - gamepad_axis axis; - - /// Position of the gamepad axis, on `[-1, 1]`. - float position; -}; - -/** - * Event generated when a keyboard key has been pressed. - */ -struct key_pressed_event -{ - /// Keyboard that generated the event. - keyboard* keyboard; - - /// Scancode of the key being pressed. - scancode scancode; - - /// Bit mask containing the active modifier keys. - std::uint16_t modifiers; - - /// `true` if the key press was generated by a key repeat, `false` otherwise. - bool repeat; -}; - -/** - * Event generated when a keyboard key has been released. - */ -struct key_released_event -{ - /// Keyboard that generated the event. - keyboard* keyboard; - - /// Scancode of the key being released. - scancode scancode; - - /// Bit mask containing the active modifier keys. - std::uint16_t modifiers; -}; - -/** - * Event generated when a mouse has been moved. - */ -struct mouse_moved_event -{ - /// Mouse that generated the event. - mouse* mouse; - - /// Mouse position, in pixels, relative to the window. - math::vector position; - - /// Relative movement of the mouse, in pixels. - math::vector difference; -}; - -/** - * Event generated when a mouse button has been pressed. - */ -struct mouse_button_pressed_event -{ - /// Mouse that generated the event. - mouse* mouse; - - /// Mouse position, in pixels, relative to the window, when the button was pressed. - math::vector position; - - /// Mouse button being pressed. - mouse_button button; -}; - -/** - * Event generated when a mouse button has been released. - */ -struct mouse_button_released_event -{ - /// Mouse that generated the event. - mouse* mouse; - - /// Mouse position, in pixels, relative to the window, when the button was released. - math::vector position; - - /// Mouse button being released. - mouse_button button; -}; - -/** - * Event generated when a mouse has been scrolled. - */ -struct mouse_scrolled_event -{ - /// Mouse that generated the event. - mouse* mouse; - - /// Mouse position, in pixels, relative to the window, when the mouse was scrolled. - math::vector position; - - /// Scroll velocity. - math::vector velocity; -}; - -/** - * Event generated when the application has been requested to quit. - */ -struct application_quit_event {}; - -} // namespace input - -#endif // ANTKEEPER_INPUT_INPUT_EVENTS_HPP diff --git a/src/input/input.hpp b/src/input/input.hpp new file mode 100644 index 0000000..0f272c6 --- /dev/null +++ b/src/input/input.hpp @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2023 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_INPUT_HPP +#define ANTKEEPER_INPUT_HPP + +/** + * Input devices, events, and mapping. + */ +namespace input {} + +#endif // ANTKEEPER_INPUT_HPP diff --git a/src/input/keyboard-events.hpp b/src/input/keyboard-events.hpp new file mode 100644 index 0000000..8d84bb3 --- /dev/null +++ b/src/input/keyboard-events.hpp @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2023 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_INPUT_KEYBOARD_EVENTS_HPP +#define ANTKEEPER_INPUT_KEYBOARD_EVENTS_HPP + +#include "input/scancode.hpp" +#include "input/modifier-key.hpp" +#include + +namespace input { + +class keyboard; + +/** + * Event generated when a keyboard key has been pressed. + */ +struct key_pressed_event +{ + /// Keyboard that generated the event. + keyboard* keyboard; + + /// Scancode of the key being pressed. + scancode scancode; + + /// Bit mask containing the active modifier keys. + std::uint16_t modifiers; + + /// `true` if the key press was generated by a key repeat, `false` otherwise. + bool repeat; +}; + +/** + * Event generated when a keyboard key has been released. + */ +struct key_released_event +{ + /// Keyboard that generated the event. + keyboard* keyboard; + + /// Scancode of the key being released. + scancode scancode; + + /// Bit mask containing the active modifier keys. + std::uint16_t modifiers; +}; + +} // namespace input + +#endif // ANTKEEPER_INPUT_KEYBOARD_EVENTS_HPP diff --git a/src/input/keyboard.hpp b/src/input/keyboard.hpp index 2b9bd07..bca7f7f 100644 --- a/src/input/keyboard.hpp +++ b/src/input/keyboard.hpp @@ -21,7 +21,7 @@ #define ANTKEEPER_INPUT_KEYBOARD_HPP #include "input/device.hpp" -#include "input/input-events.hpp" +#include "input/keyboard-events.hpp" #include "input/scancode.hpp" #include "input/modifier-key.hpp" #include "event/publisher.hpp" diff --git a/src/input/mapper.cpp b/src/input/mapper.cpp index a0b02ef..be83bfb 100644 --- a/src/input/mapper.cpp +++ b/src/input/mapper.cpp @@ -39,34 +39,37 @@ void mapper::disconnect() void mapper::handle_gamepad_axis_moved(const gamepad_axis_moved_event& event) { - input_mapped_publisher.publish({std::shared_ptr(new gamepad_axis_mapping(event.gamepad, event.axis, std::signbit(event.position)))}); + gamepad_axis_mapped_publisher.publish({gamepad_axis_mapping(event.gamepad, event.axis, std::signbit(event.position))}); } void mapper::handle_gamepad_button_pressed(const gamepad_button_pressed_event& event) { - input_mapped_publisher.publish({std::shared_ptr(new gamepad_button_mapping(event.gamepad, event.button))}); + gamepad_button_mapped_publisher.publish({gamepad_button_mapping(event.gamepad, event.button)}); } void mapper::handle_key_pressed(const key_pressed_event& event) { - input_mapped_publisher.publish({std::shared_ptr(new key_mapping(event.keyboard, event.scancode))}); + if (!event.repeat) + { + key_mapped_publisher.publish({key_mapping(event.keyboard, event.scancode)}); + } } void mapper::handle_mouse_button_pressed(const mouse_button_pressed_event& event) { - input_mapped_publisher.publish({std::shared_ptr(new mouse_button_mapping(event.mouse, event.button))}); + mouse_button_mapped_publisher.publish({mouse_button_mapping(event.mouse, event.button)}); } void mapper::handle_mouse_moved(const mouse_moved_event& event) { if (event.difference.x()) { - input_mapped_publisher.publish({std::shared_ptr(new mouse_motion_mapping(event.mouse, mouse_motion_axis::x, std::signbit(static_cast(event.difference.x()))))}); + mouse_motion_mapped_publisher.publish({mouse_motion_mapping(event.mouse, mouse_motion_axis::x, std::signbit(static_cast(event.difference.x())))}); } if (event.difference.y()) { - input_mapped_publisher.publish({std::shared_ptr(new mouse_motion_mapping(event.mouse, mouse_motion_axis::y, std::signbit(static_cast(event.difference.y()))))}); + mouse_motion_mapped_publisher.publish({mouse_motion_mapping(event.mouse, mouse_motion_axis::y, std::signbit(static_cast(event.difference.y())))}); } } @@ -74,12 +77,12 @@ void mapper::handle_mouse_scrolled(const mouse_scrolled_event& event) { if (event.velocity.x()) { - input_mapped_publisher.publish({std::shared_ptr(new mouse_scroll_mapping(event.mouse, mouse_scroll_axis::x, std::signbit(event.velocity.x())))}); + mouse_scroll_mapped_publisher.publish({mouse_scroll_mapping(event.mouse, mouse_scroll_axis::x, std::signbit(event.velocity.x()))}); } if (event.velocity.y()) { - input_mapped_publisher.publish({std::shared_ptr(new mouse_scroll_mapping(event.mouse, mouse_scroll_axis::y, std::signbit(event.velocity.y())))}); + mouse_scroll_mapped_publisher.publish({mouse_scroll_mapping(event.mouse, mouse_scroll_axis::y, std::signbit(event.velocity.y()))}); } } diff --git a/src/input/mapper.hpp b/src/input/mapper.hpp index fb06aea..094e17f 100644 --- a/src/input/mapper.hpp +++ b/src/input/mapper.hpp @@ -23,8 +23,10 @@ #include "event/subscription.hpp" #include "event/queue.hpp" #include "event/publisher.hpp" -#include "input/input-events.hpp" -#include "input/mapping.hpp" +#include "input/gamepad-events.hpp" +#include "input/keyboard-events.hpp" +#include "input/mouse-events.hpp" +#include "input/mapping-events.hpp" #include #include @@ -48,10 +50,40 @@ public: */ void disconnect(); - /// Returns the channel through which input mapped events are published. - [[nodiscard]] inline ::event::channel& get_input_mapped_channel() noexcept + /// Returns the channel through which gamepad axis mapped events are published. + [[nodiscard]] inline ::event::channel& get_gamepad_axis_mapped_channel() noexcept { - return input_mapped_publisher.channel(); + return gamepad_axis_mapped_publisher.channel(); + } + + /// Returns the channel through which gamepad button mapped events are published. + [[nodiscard]] inline ::event::channel& get_gamepad_button_mapped_channel() noexcept + { + return gamepad_button_mapped_publisher.channel(); + } + + /// Returns the channel through which key mapped events are published. + [[nodiscard]] inline ::event::channel& get_key_mapped_channel() noexcept + { + return key_mapped_publisher.channel(); + } + + /// Returns the channel through which mouse button mapped events are published. + [[nodiscard]] inline ::event::channel& get_mouse_button_mapped_channel() noexcept + { + return mouse_button_mapped_publisher.channel(); + } + + /// Returns the channel through which mouse motion mapped events are published. + [[nodiscard]] inline ::event::channel& get_mouse_motion_mapped_channel() noexcept + { + return mouse_motion_mapped_publisher.channel(); + } + + /// Returns the channel through which mouse scroll mapped events are published. + [[nodiscard]] inline ::event::channel& get_mouse_scroll_mapped_channel() noexcept + { + return mouse_scroll_mapped_publisher.channel(); } private: @@ -63,7 +95,12 @@ private: void handle_mouse_scrolled(const mouse_scrolled_event& event); std::vector> subscriptions; - ::event::publisher input_mapped_publisher; + ::event::publisher gamepad_axis_mapped_publisher; + ::event::publisher gamepad_button_mapped_publisher; + ::event::publisher key_mapped_publisher; + ::event::publisher mouse_button_mapped_publisher; + ::event::publisher mouse_motion_mapped_publisher; + ::event::publisher mouse_scroll_mapped_publisher; }; } // namespace input diff --git a/src/input/mapping-events.hpp b/src/input/mapping-events.hpp new file mode 100644 index 0000000..64a6be8 --- /dev/null +++ b/src/input/mapping-events.hpp @@ -0,0 +1,83 @@ +/* + * Copyright (C) 2023 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_INPUT_MAPPING_EVENTS_HPP +#define ANTKEEPER_INPUT_MAPPING_EVENTS_HPP + +#include "input/mapping.hpp" + +namespace input { + +/** + * Event generated when a gamepad axis mapping has been generated. + */ +struct gamepad_axis_mapped_event +{ + /// Gamepad axis mapping that was generated. + gamepad_axis_mapping mapping; +}; + +/** + * Event generated when a gamepad button mapping has been generated. + */ +struct gamepad_button_mapped_event +{ + /// Gamepad button mapping that was generated. + gamepad_button_mapping mapping; +}; + +/** + * Event generated when a key mapping has been generated. + */ +struct key_mapped_event +{ + /// Key mapping that was generated. + key_mapping mapping; +}; + +/** + * Event generated when a mouse button mapping has been generated. + */ +struct mouse_button_mapped_event +{ + /// Mouse button mapping that was generated. + mouse_button_mapping mapping; +}; + +/** + * Event generated when a mouse motion mapping has been generated. + */ +struct mouse_motion_mapped_event +{ + /// Mouse motion mapping that was generated. + mouse_motion_mapping mapping; +}; + +/** + * Event generated when a mouse scroll mapping has been generated. + */ +struct mouse_scroll_mapped_event +{ + /// Mouse scroll mapping that was generated. + mouse_scroll_mapping mapping; +}; + +} // namespace input + +#endif // ANTKEEPER_INPUT_MAPPING_EVENTS_HPP diff --git a/src/input/mouse-events.hpp b/src/input/mouse-events.hpp new file mode 100644 index 0000000..c256775 --- /dev/null +++ b/src/input/mouse-events.hpp @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2023 Christopher J. Howard + * + * This file is part of Antkeeper source code. + * + * Antkeeper source code is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Antkeeper source code is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Antkeeper source code. If not, see . + */ + +#ifndef ANTKEEPER_INPUT_MOUSE_EVENTS_HPP +#define ANTKEEPER_INPUT_MOUSE_EVENTS_HPP + +#include "input/mouse-button.hpp" +#include "input/mouse-motion-axis.hpp" +#include "input/mouse-scroll-axis.hpp" +#include "math/vector.hpp" +#include + +namespace input { + +class mouse; + +/** + * Event generated when a mouse has been moved. + */ +struct mouse_moved_event +{ + /// Mouse that generated the event. + mouse* mouse; + + /// Mouse position, in pixels, relative to the window. + math::vector position; + + /// Relative movement of the mouse, in pixels. + math::vector difference; +}; + +/** + * Event generated when a mouse button has been pressed. + */ +struct mouse_button_pressed_event +{ + /// Mouse that generated the event. + mouse* mouse; + + /// Mouse position, in pixels, relative to the window, when the button was pressed. + math::vector position; + + /// Mouse button being pressed. + mouse_button button; +}; + +/** + * Event generated when a mouse button has been released. + */ +struct mouse_button_released_event +{ + /// Mouse that generated the event. + mouse* mouse; + + /// Mouse position, in pixels, relative to the window, when the button was released. + math::vector position; + + /// Mouse button being released. + mouse_button button; +}; + +/** + * Event generated when a mouse has been scrolled. + */ +struct mouse_scrolled_event +{ + /// Mouse that generated the event. + mouse* mouse; + + /// Mouse position, in pixels, relative to the window, when the mouse was scrolled. + math::vector position; + + /// Scroll velocity. + math::vector velocity; +}; + +} // namespace input + +#endif // ANTKEEPER_INPUT_MOUSE_EVENTS_HPP diff --git a/src/input/mouse.hpp b/src/input/mouse.hpp index c29dd4d..f633e57 100644 --- a/src/input/mouse.hpp +++ b/src/input/mouse.hpp @@ -21,7 +21,7 @@ #define ANTKEEPER_INPUT_MOUSE_HPP #include "input/device.hpp" -#include "input/input-events.hpp" +#include "input/mouse-events.hpp" #include "input/mouse-button.hpp" #include "event/publisher.hpp" #include "math/vector.hpp" diff --git a/src/main.cpp b/src/main.cpp index 4b83e02..bd6faa0 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,13 +68,14 @@ int main(int argc, char* argv[]) std::osyncstream(std::cout) << std::format ( - "{}{:8.03f} {}:{}:{}: {}: {}{}\n", - colors[static_cast(event.severity)], + "[{:8.03f}] {}{}: {}:{}:{}: {}{}\n", std::chrono::duration(event.time - launch_time).count(), + colors[static_cast(event.severity)], + //severities[static_cast(event.severity)], + static_cast(event.severity), std::filesystem::path(event.location.file_name()).filename().string(), event.location.line(), event.location.column(), - severities[static_cast(event.severity)], event.message, ansi::reset );