From 60a2ed94289a1377a17b31d132e8014f1fa8806f Mon Sep 17 00:00:00 2001 From: "C. J. Howard" Date: Mon, 13 Feb 2023 16:21:14 +0800 Subject: [PATCH] Rename control and control_map to action and action_map, respectively --- CMakeLists.txt | 1 + src/game/context.hpp | 48 +++--- src/game/controls.cpp | 158 +++++++++--------- src/game/state/gamepad-config-menu.cpp | 4 +- src/game/state/gamepad-config-menu.hpp | 4 +- src/game/state/graphics-menu.cpp | 8 +- src/game/state/keyboard-config-menu.cpp | 40 ++--- src/game/state/keyboard-config-menu.hpp | 8 +- src/game/state/sound-menu.cpp | 8 +- .../{control-events.hpp => action-events.hpp} | 28 ++-- src/input/{control-map.cpp => action-map.cpp} | 158 +++++++++--------- src/input/action-map.hpp | 154 +++++++++++++++++ src/input/{control.cpp => action.cpp} | 10 +- src/input/{control.hpp => action.hpp} | 48 +++--- src/input/control-map.hpp | 154 ----------------- 15 files changed, 416 insertions(+), 415 deletions(-) rename src/input/{control-events.hpp => action-events.hpp} (67%) rename src/input/{control-map.cpp => action-map.cpp} (51%) create mode 100644 src/input/action-map.hpp rename src/input/{control.cpp => action.cpp} (89%) rename src/input/{control.hpp => action.hpp} (53%) delete mode 100644 src/input/control-map.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index baf682a..c9deac5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,6 @@ cmake_minimum_required(VERSION 3.25) + option(APPLICATION_NAME "Application name" "Antkeeper") option(APPLICATION_VERSION "Application version string" "0.0.0") option(APPLICATION_AUTHOR "Application author" "C. J. Howard") diff --git a/src/game/context.hpp b/src/game/context.hpp index 512a97f..7718081 100644 --- a/src/game/context.hpp +++ b/src/game/context.hpp @@ -37,8 +37,8 @@ #include "gl/vertex-buffer.hpp" #include "i18n/string-map.hpp" #include "i18n/string-table.hpp" -#include "input/control-map.hpp" -#include "input/control.hpp" +#include "input/action-map.hpp" +#include "input/action.hpp" #include "input/mapper.hpp" #include "math/moving-average.hpp" #include "render/anti-aliasing-method.hpp" @@ -176,30 +176,30 @@ struct context render::material menu_font_material; render::material title_font_material; - // Control maps, controls, and control event handling + // Action maps, actions, and action event handling input::mapper input_mapper; - input::control_map window_controls; - input::control fullscreen_control; - input::control screenshot_control; - std::vector> window_control_subscriptions; - input::control_map menu_controls; - input::control menu_up_control; - input::control menu_down_control; - input::control menu_left_control; - input::control menu_right_control; - input::control menu_select_control; - input::control menu_back_control; - input::control menu_modifier_control; - std::vector> menu_control_subscriptions; + input::action_map window_actions; + input::action fullscreen_action; + input::action screenshot_action; + std::vector> window_action_subscriptions; + input::action_map menu_actions; + input::action menu_up_action; + input::action menu_down_action; + input::action menu_left_action; + input::action menu_right_action; + input::action menu_select_action; + input::action menu_back_action; + input::action menu_modifier_action; + std::vector> menu_action_subscriptions; std::vector> menu_mouse_subscriptions; - input::control_map movement_controls; - input::control move_forward_control; - input::control move_back_control; - input::control move_left_control; - input::control move_right_control; - input::control move_up_control; - input::control move_down_control; - input::control pause_control; + input::action_map movement_actions; + input::action move_forward_action; + input::action move_back_action; + input::action move_left_action; + input::action move_right_action; + input::action move_up_action; + input::action move_down_action; + input::action pause_action; // Debugging math::moving_average average_frame_time; diff --git a/src/game/controls.cpp b/src/game/controls.cpp index 84d3fbb..136d982 100644 --- a/src/game/controls.cpp +++ b/src/game/controls.cpp @@ -29,15 +29,15 @@ namespace game { void setup_window_controls(game::context& ctx) { // Map window controls - ctx.window_controls.add_mapping(ctx.fullscreen_control, input::key_mapping(nullptr, input::scancode::f11, false)); - ctx.window_controls.add_mapping(ctx.fullscreen_control, input::key_mapping(nullptr, input::scancode::enter, false, input::modifier_key::alt)); - ctx.window_controls.add_mapping(ctx.screenshot_control, input::key_mapping(nullptr, input::scancode::f12, false)); - ctx.window_controls.add_mapping(ctx.screenshot_control, input::key_mapping(nullptr, input::scancode::print_screen, false)); + ctx.window_actions.add_mapping(ctx.fullscreen_action, input::key_mapping(nullptr, input::scancode::f11, false)); + ctx.window_actions.add_mapping(ctx.fullscreen_action, input::key_mapping(nullptr, input::scancode::enter, false, input::modifier_key::alt)); + ctx.window_actions.add_mapping(ctx.screenshot_action, input::key_mapping(nullptr, input::scancode::f12, false)); + ctx.window_actions.add_mapping(ctx.screenshot_action, input::key_mapping(nullptr, input::scancode::print_screen, false)); // Setup fullscreen control - ctx.window_control_subscriptions.emplace_back + ctx.window_action_subscriptions.emplace_back ( - ctx.fullscreen_control.get_activated_channel().subscribe + ctx.fullscreen_action.get_activated_channel().subscribe ( [&ctx](const auto& event) { @@ -47,9 +47,9 @@ void setup_window_controls(game::context& ctx) ); // Setup screenshot control - ctx.window_control_subscriptions.emplace_back + ctx.window_action_subscriptions.emplace_back ( - ctx.screenshot_control.get_activated_channel().subscribe + ctx.screenshot_action.get_activated_channel().subscribe ( [&ctx](const auto& event) { @@ -62,56 +62,56 @@ void setup_window_controls(game::context& ctx) void setup_menu_controls(game::context& ctx) { // Map menu controls - ctx.menu_controls.add_mapping(ctx.menu_up_control, input::key_mapping(nullptr, input::scancode::up, true)); - ctx.menu_controls.add_mapping(ctx.menu_up_control, input::key_mapping(nullptr, input::scancode::w, true)); - ctx.menu_controls.add_mapping(ctx.menu_up_control, input::key_mapping(nullptr, input::scancode::i, true)); - ctx.menu_controls.add_mapping(ctx.menu_up_control, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::left_stick_y, true)); - ctx.menu_controls.add_mapping(ctx.menu_up_control, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::right_stick_y, true)); - ctx.menu_controls.add_mapping(ctx.menu_up_control, input::gamepad_button_mapping(nullptr, input::gamepad_button::dpad_up)); + ctx.menu_actions.add_mapping(ctx.menu_up_action, input::key_mapping(nullptr, input::scancode::up, true)); + ctx.menu_actions.add_mapping(ctx.menu_up_action, input::key_mapping(nullptr, input::scancode::w, true)); + ctx.menu_actions.add_mapping(ctx.menu_up_action, input::key_mapping(nullptr, input::scancode::i, true)); + ctx.menu_actions.add_mapping(ctx.menu_up_action, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::left_stick_y, true)); + ctx.menu_actions.add_mapping(ctx.menu_up_action, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::right_stick_y, true)); + ctx.menu_actions.add_mapping(ctx.menu_up_action, input::gamepad_button_mapping(nullptr, input::gamepad_button::dpad_up)); - ctx.menu_controls.add_mapping(ctx.menu_down_control, input::key_mapping(nullptr, input::scancode::down, true)); - ctx.menu_controls.add_mapping(ctx.menu_down_control, input::key_mapping(nullptr, input::scancode::s, true)); - ctx.menu_controls.add_mapping(ctx.menu_down_control, input::key_mapping(nullptr, input::scancode::k, true)); - ctx.menu_controls.add_mapping(ctx.menu_down_control, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::left_stick_y, false)); - ctx.menu_controls.add_mapping(ctx.menu_down_control, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::right_stick_y, false)); - ctx.menu_controls.add_mapping(ctx.menu_down_control, input::gamepad_button_mapping(nullptr, input::gamepad_button::dpad_down)); + ctx.menu_actions.add_mapping(ctx.menu_down_action, input::key_mapping(nullptr, input::scancode::down, true)); + ctx.menu_actions.add_mapping(ctx.menu_down_action, input::key_mapping(nullptr, input::scancode::s, true)); + ctx.menu_actions.add_mapping(ctx.menu_down_action, input::key_mapping(nullptr, input::scancode::k, true)); + ctx.menu_actions.add_mapping(ctx.menu_down_action, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::left_stick_y, false)); + ctx.menu_actions.add_mapping(ctx.menu_down_action, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::right_stick_y, false)); + ctx.menu_actions.add_mapping(ctx.menu_down_action, input::gamepad_button_mapping(nullptr, input::gamepad_button::dpad_down)); - ctx.menu_controls.add_mapping(ctx.menu_left_control, input::key_mapping(nullptr, input::scancode::left, true)); - ctx.menu_controls.add_mapping(ctx.menu_left_control, input::key_mapping(nullptr, input::scancode::a, true)); - ctx.menu_controls.add_mapping(ctx.menu_left_control, input::key_mapping(nullptr, input::scancode::j, true)); - ctx.menu_controls.add_mapping(ctx.menu_left_control, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::left_stick_x, true)); - ctx.menu_controls.add_mapping(ctx.menu_left_control, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::right_stick_x, true)); - ctx.menu_controls.add_mapping(ctx.menu_left_control, input::gamepad_button_mapping(nullptr, input::gamepad_button::dpad_left)); - ctx.menu_controls.add_mapping(ctx.menu_left_control, input::mouse_scroll_mapping(nullptr, input::mouse_scroll_axis::x, true)); - ctx.menu_controls.add_mapping(ctx.menu_left_control, input::mouse_scroll_mapping(nullptr, input::mouse_scroll_axis::y, true)); + ctx.menu_actions.add_mapping(ctx.menu_left_action, input::key_mapping(nullptr, input::scancode::left, true)); + ctx.menu_actions.add_mapping(ctx.menu_left_action, input::key_mapping(nullptr, input::scancode::a, true)); + ctx.menu_actions.add_mapping(ctx.menu_left_action, input::key_mapping(nullptr, input::scancode::j, true)); + ctx.menu_actions.add_mapping(ctx.menu_left_action, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::left_stick_x, true)); + ctx.menu_actions.add_mapping(ctx.menu_left_action, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::right_stick_x, true)); + ctx.menu_actions.add_mapping(ctx.menu_left_action, input::gamepad_button_mapping(nullptr, input::gamepad_button::dpad_left)); + ctx.menu_actions.add_mapping(ctx.menu_left_action, input::mouse_scroll_mapping(nullptr, input::mouse_scroll_axis::x, true)); + ctx.menu_actions.add_mapping(ctx.menu_left_action, input::mouse_scroll_mapping(nullptr, input::mouse_scroll_axis::y, true)); - ctx.menu_controls.add_mapping(ctx.menu_right_control, input::key_mapping(nullptr, input::scancode::right, true)); - ctx.menu_controls.add_mapping(ctx.menu_right_control, input::key_mapping(nullptr, input::scancode::d, true)); - ctx.menu_controls.add_mapping(ctx.menu_right_control, input::key_mapping(nullptr, input::scancode::l, true)); - ctx.menu_controls.add_mapping(ctx.menu_right_control, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::left_stick_x, false)); - ctx.menu_controls.add_mapping(ctx.menu_right_control, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::right_stick_x, false)); - ctx.menu_controls.add_mapping(ctx.menu_right_control, input::gamepad_button_mapping(nullptr, input::gamepad_button::dpad_right)); - ctx.menu_controls.add_mapping(ctx.menu_right_control, input::mouse_scroll_mapping(nullptr, input::mouse_scroll_axis::x, false)); - ctx.menu_controls.add_mapping(ctx.menu_right_control, input::mouse_scroll_mapping(nullptr, input::mouse_scroll_axis::y, false)); + ctx.menu_actions.add_mapping(ctx.menu_right_action, input::key_mapping(nullptr, input::scancode::right, true)); + ctx.menu_actions.add_mapping(ctx.menu_right_action, input::key_mapping(nullptr, input::scancode::d, true)); + ctx.menu_actions.add_mapping(ctx.menu_right_action, input::key_mapping(nullptr, input::scancode::l, true)); + ctx.menu_actions.add_mapping(ctx.menu_right_action, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::left_stick_x, false)); + ctx.menu_actions.add_mapping(ctx.menu_right_action, input::gamepad_axis_mapping(nullptr, input::gamepad_axis::right_stick_x, false)); + ctx.menu_actions.add_mapping(ctx.menu_right_action, input::gamepad_button_mapping(nullptr, input::gamepad_button::dpad_right)); + ctx.menu_actions.add_mapping(ctx.menu_right_action, input::mouse_scroll_mapping(nullptr, input::mouse_scroll_axis::x, false)); + ctx.menu_actions.add_mapping(ctx.menu_right_action, input::mouse_scroll_mapping(nullptr, input::mouse_scroll_axis::y, false)); - ctx.menu_controls.add_mapping(ctx.menu_select_control, input::key_mapping(nullptr, input::scancode::enter, false)); - ctx.menu_controls.add_mapping(ctx.menu_select_control, input::key_mapping(nullptr, input::scancode::space, false)); - ctx.menu_controls.add_mapping(ctx.menu_select_control, input::key_mapping(nullptr, input::scancode::e, false)); - ctx.menu_controls.add_mapping(ctx.menu_select_control, input::gamepad_button_mapping(nullptr, input::gamepad_button::a)); + ctx.menu_actions.add_mapping(ctx.menu_select_action, input::key_mapping(nullptr, input::scancode::enter, false)); + ctx.menu_actions.add_mapping(ctx.menu_select_action, input::key_mapping(nullptr, input::scancode::space, false)); + ctx.menu_actions.add_mapping(ctx.menu_select_action, input::key_mapping(nullptr, input::scancode::e, false)); + ctx.menu_actions.add_mapping(ctx.menu_select_action, input::gamepad_button_mapping(nullptr, input::gamepad_button::a)); - ctx.menu_controls.add_mapping(ctx.menu_back_control, input::key_mapping(nullptr, input::scancode::escape, false)); - ctx.menu_controls.add_mapping(ctx.menu_back_control, input::key_mapping(nullptr, input::scancode::backspace, false)); - ctx.menu_controls.add_mapping(ctx.menu_back_control, input::key_mapping(nullptr, input::scancode::q, false)); - ctx.menu_controls.add_mapping(ctx.menu_back_control, input::gamepad_button_mapping(nullptr, input::gamepad_button::b)); - ctx.menu_controls.add_mapping(ctx.menu_back_control, input::gamepad_button_mapping(nullptr, input::gamepad_button::back)); + ctx.menu_actions.add_mapping(ctx.menu_back_action, input::key_mapping(nullptr, input::scancode::escape, false)); + ctx.menu_actions.add_mapping(ctx.menu_back_action, input::key_mapping(nullptr, input::scancode::backspace, false)); + ctx.menu_actions.add_mapping(ctx.menu_back_action, input::key_mapping(nullptr, input::scancode::q, false)); + ctx.menu_actions.add_mapping(ctx.menu_back_action, input::gamepad_button_mapping(nullptr, input::gamepad_button::b)); + ctx.menu_actions.add_mapping(ctx.menu_back_action, input::gamepad_button_mapping(nullptr, input::gamepad_button::back)); - ctx.menu_controls.add_mapping(ctx.menu_modifier_control, input::key_mapping(nullptr, input::scancode::left_shift, false)); - ctx.menu_controls.add_mapping(ctx.menu_modifier_control, input::key_mapping(nullptr, input::scancode::right_shift, false)); + ctx.menu_actions.add_mapping(ctx.menu_modifier_action, input::key_mapping(nullptr, input::scancode::left_shift, false)); + ctx.menu_actions.add_mapping(ctx.menu_modifier_action, input::key_mapping(nullptr, input::scancode::right_shift, false)); // Setup menu controls - ctx.menu_control_subscriptions.emplace_back + ctx.menu_action_subscriptions.emplace_back ( - ctx.menu_up_control.get_activated_channel().subscribe + ctx.menu_up_action.get_activated_channel().subscribe ( [&ctx](const auto& event) { @@ -123,9 +123,9 @@ void setup_menu_controls(game::context& ctx) } ) ); - ctx.menu_control_subscriptions.emplace_back + ctx.menu_action_subscriptions.emplace_back ( - ctx.menu_down_control.get_activated_channel().subscribe + ctx.menu_down_action.get_activated_channel().subscribe ( [&ctx](const auto& event) { @@ -137,9 +137,9 @@ void setup_menu_controls(game::context& ctx) } ) ); - ctx.menu_control_subscriptions.emplace_back + ctx.menu_action_subscriptions.emplace_back ( - ctx.menu_left_control.get_activated_channel().subscribe + ctx.menu_left_action.get_activated_channel().subscribe ( [&ctx](const auto& event) { @@ -149,9 +149,9 @@ void setup_menu_controls(game::context& ctx) } ) ); - ctx.menu_control_subscriptions.emplace_back + ctx.menu_action_subscriptions.emplace_back ( - ctx.menu_right_control.get_activated_channel().subscribe + ctx.menu_right_action.get_activated_channel().subscribe ( [&ctx](const auto& event) { @@ -161,9 +161,9 @@ void setup_menu_controls(game::context& ctx) } ) ); - ctx.menu_control_subscriptions.emplace_back + ctx.menu_action_subscriptions.emplace_back ( - ctx.menu_select_control.get_activated_channel().subscribe + ctx.menu_select_action.get_activated_channel().subscribe ( [&ctx](const auto& event) { @@ -173,9 +173,9 @@ void setup_menu_controls(game::context& ctx) } ) ); - ctx.menu_control_subscriptions.emplace_back + ctx.menu_action_subscriptions.emplace_back ( - ctx.menu_back_control.get_activated_channel().subscribe + ctx.menu_back_action.get_activated_channel().subscribe ( [&ctx](const auto& event) { @@ -186,24 +186,24 @@ void setup_menu_controls(game::context& ctx) ); // Set activation threshold for menu navigation controls to mitigate drifting gamepad axes - auto menu_control_threshold = [](float x) -> bool + auto menu_action_threshold = [](float x) -> bool { return x > 0.5f; }; - ctx.menu_up_control.set_threshold_function(menu_control_threshold); - ctx.menu_down_control.set_threshold_function(menu_control_threshold); - ctx.menu_left_control.set_threshold_function(menu_control_threshold); - ctx.menu_right_control.set_threshold_function(menu_control_threshold); + ctx.menu_up_action.set_threshold_function(menu_action_threshold); + ctx.menu_down_action.set_threshold_function(menu_action_threshold); + ctx.menu_left_action.set_threshold_function(menu_action_threshold); + ctx.menu_right_action.set_threshold_function(menu_action_threshold); } void enable_window_controls(game::context& ctx) { - ctx.window_controls.connect(ctx.input_manager->get_event_queue()); + ctx.window_actions.connect(ctx.input_manager->get_event_queue()); } void enable_menu_controls(game::context& ctx) { - ctx.menu_controls.connect(ctx.input_manager->get_event_queue()); + ctx.menu_actions.connect(ctx.input_manager->get_event_queue()); // Function to select menu item at mouse position auto select_menu_item = [&ctx](const math::vector& mouse_position) -> bool @@ -303,21 +303,21 @@ void enable_menu_controls(game::context& ctx) void disable_window_controls(game::context& ctx) { - ctx.window_controls.disconnect(); + ctx.window_actions.disconnect(); } void disable_menu_controls(game::context& ctx) { - // Reset menu control states - ctx.menu_up_control.reset(); - ctx.menu_down_control.reset(); - ctx.menu_left_control.reset(); - ctx.menu_right_control.reset(); - ctx.menu_select_control.reset(); - ctx.menu_back_control.reset(); - ctx.menu_modifier_control.reset(); + // Reset menu action states + ctx.menu_up_action.reset(); + ctx.menu_down_action.reset(); + ctx.menu_left_action.reset(); + ctx.menu_right_action.reset(); + ctx.menu_select_action.reset(); + ctx.menu_back_action.reset(); + ctx.menu_modifier_action.reset(); - ctx.menu_controls.disconnect(); + ctx.menu_actions.disconnect(); ctx.menu_mouse_subscriptions.clear(); } @@ -473,14 +473,14 @@ void apply_control_profile(game::context& ctx, const json& profile) std::string control_name = control_element.key(); // Find or create control - input::control* control; + input::action* control; if (ctx.controls.count(control_name)) { control = ctx.controls[control_name]; } else { - control = new input::control(); + control = new input::action(); ctx.controls[control_name] = control; } @@ -665,7 +665,7 @@ void save_control_profile(game::context& ctx) for (auto controls_it = ctx.controls.begin(); controls_it != ctx.controls.end(); ++controls_it) { const std::string& control_name = controls_it->first; - input::control* control = controls_it->second; + input::action* control = controls_it->second; // Add control element auto& control_element = controls_element[control_name]; diff --git a/src/game/state/gamepad-config-menu.cpp b/src/game/state/gamepad-config-menu.cpp index ffad869..f7aa6d2 100644 --- a/src/game/state/gamepad-config-menu.cpp +++ b/src/game/state/gamepad-config-menu.cpp @@ -132,7 +132,7 @@ gamepad_config_menu::~gamepad_config_menu() debug::log::trace("Exited gamepad config menu state"); } -std::string gamepad_config_menu::get_binding_string(input::control* control) +std::string gamepad_config_menu::get_binding_string(input::action* control) { std::string binding_string; /* @@ -286,7 +286,7 @@ std::string gamepad_config_menu::get_binding_string(input::control* control) void gamepad_config_menu::add_control_item(const std::string& control_name) { // Get pointer to control - //input::control* control = ctx.controls[control_name]; + //input::action* control = ctx.controls[control_name]; // Construct texts scene::text* name_text = new scene::text(); diff --git a/src/game/state/gamepad-config-menu.hpp b/src/game/state/gamepad-config-menu.hpp index 865776d..28d4acf 100644 --- a/src/game/state/gamepad-config-menu.hpp +++ b/src/game/state/gamepad-config-menu.hpp @@ -21,7 +21,7 @@ #define ANTKEEPER_GAME_STATE_GAMEPAD_CONFIG_MENU_HPP #include "game/state/base.hpp" -#include "input/control.hpp" +#include "input/action.hpp" #include namespace game { @@ -34,7 +34,7 @@ public: virtual ~gamepad_config_menu(); private: - std::string get_binding_string(input::control* control); + std::string get_binding_string(input::action* control); void add_control_item(const std::string& control_name); }; diff --git a/src/game/state/graphics-menu.cpp b/src/game/state/graphics-menu.cpp index 3d645be..ec33a1c 100644 --- a/src/game/state/graphics-menu.cpp +++ b/src/game/state/graphics-menu.cpp @@ -103,7 +103,7 @@ graphics_menu::graphics_menu(game::context& ctx): auto increase_resolution_callback = [this, &ctx]() { // Increase resolution - if (ctx.menu_modifier_control.is_active()) + if (ctx.menu_modifier_action.is_active()) ctx.render_scale += 0.05f; else ctx.render_scale += 0.25f; @@ -127,7 +127,7 @@ graphics_menu::graphics_menu(game::context& ctx): auto decrease_resolution_callback = [this, &ctx]() { // Increase resolution - if (ctx.menu_modifier_control.is_active()) + if (ctx.menu_modifier_action.is_active()) ctx.render_scale -= 0.05f; else ctx.render_scale -= 0.25f; @@ -219,7 +219,7 @@ graphics_menu::graphics_menu(game::context& ctx): auto increase_font_scale_callback = [this, &ctx]() { // Increase font scale - if (ctx.menu_modifier_control.is_active()) + if (ctx.menu_modifier_action.is_active()) ctx.font_scale += 0.01f; else ctx.font_scale += 0.1f; @@ -248,7 +248,7 @@ graphics_menu::graphics_menu(game::context& ctx): auto decrease_font_scale_callback = [this, &ctx]() { // Decrease font scale - if (ctx.menu_modifier_control.is_active()) + if (ctx.menu_modifier_action.is_active()) ctx.font_scale -= 0.01f; else ctx.font_scale -= 0.1f; diff --git a/src/game/state/keyboard-config-menu.cpp b/src/game/state/keyboard-config-menu.cpp index be1d3f0..dac0de0 100644 --- a/src/game/state/keyboard-config-menu.cpp +++ b/src/game/state/keyboard-config-menu.cpp @@ -41,13 +41,13 @@ keyboard_config_menu::keyboard_config_menu(game::context& ctx): debug::log::trace("Entering keyboard config menu state..."); // Add control menu items - add_control_item(ctx.movement_controls, ctx.move_forward_control, "control_move_forward"_fnv1a32); - add_control_item(ctx.movement_controls, ctx.move_back_control, "control_move_back"_fnv1a32); - add_control_item(ctx.movement_controls, ctx.move_left_control, "control_move_left"_fnv1a32); - add_control_item(ctx.movement_controls, ctx.move_right_control, "control_move_right"_fnv1a32); - add_control_item(ctx.movement_controls, ctx.move_up_control, "control_move_up"_fnv1a32); - add_control_item(ctx.movement_controls, ctx.move_down_control, "control_move_down"_fnv1a32); - add_control_item(ctx.movement_controls, ctx.pause_control, "control_pause"_fnv1a32); + add_control_item(ctx.movement_actions, ctx.move_forward_action, "control_move_forward"_fnv1a32); + add_control_item(ctx.movement_actions, ctx.move_back_action, "control_move_back"_fnv1a32); + add_control_item(ctx.movement_actions, ctx.move_left_action, "control_move_left"_fnv1a32); + add_control_item(ctx.movement_actions, ctx.move_right_action, "control_move_right"_fnv1a32); + add_control_item(ctx.movement_actions, ctx.move_up_action, "control_move_up"_fnv1a32); + add_control_item(ctx.movement_actions, ctx.move_down_action, "control_move_down"_fnv1a32); + add_control_item(ctx.movement_actions, ctx.pause_action, "control_pause"_fnv1a32); // Construct menu item texts scene::text* back_text = new scene::text(); @@ -130,11 +130,11 @@ keyboard_config_menu::~keyboard_config_menu() debug::log::trace("Exited keyboard config menu state..."); } -std::string keyboard_config_menu::get_mapping_string(const input::control_map& control_map, const input::control& control) +std::string keyboard_config_menu::get_mapping_string(const input::action_map& action_map, const input::action& control) { std::string mapping_string; - if (auto key_mappings = control_map.get_key_mappings(control); !key_mappings.empty()) + if (auto key_mappings = action_map.get_key_mappings(control); !key_mappings.empty()) { const auto& key_mapping = key_mappings.front(); @@ -144,7 +144,7 @@ std::string keyboard_config_menu::get_mapping_string(const input::control_map& c // Set mapping string to scancode string mapping_string = get_string(ctx, hash::fnv1a32(scancode_string_name.data(), scancode_string_name.length())); } - else if (auto mouse_button_mappings = control_map.get_mouse_button_mappings(control); !mouse_button_mappings.empty()) + else if (auto mouse_button_mappings = action_map.get_mouse_button_mappings(control); !mouse_button_mappings.empty()) { const auto& mouse_button_mapping = mouse_button_mappings.front(); switch (mouse_button_mapping.button) @@ -169,7 +169,7 @@ std::string keyboard_config_menu::get_mapping_string(const input::control_map& c } } } - else if (auto mouse_scroll_mappings = control_map.get_mouse_scroll_mappings(control); !mouse_scroll_mappings.empty()) + else if (auto mouse_scroll_mappings = action_map.get_mouse_scroll_mappings(control); !mouse_scroll_mappings.empty()) { const auto& mouse_scroll_mapping = mouse_scroll_mappings.front(); @@ -204,7 +204,7 @@ std::string keyboard_config_menu::get_mapping_string(const input::control_map& c return mapping_string; } -void keyboard_config_menu::add_control_item(input::control_map& control_map, input::control& control, std::uint32_t control_name_hash) +void keyboard_config_menu::add_control_item(input::action_map& action_map, input::action& control, std::uint32_t control_name_hash) { // Construct texts scene::text* name_text = new scene::text(); @@ -215,24 +215,24 @@ void keyboard_config_menu::add_control_item(input::control_map& control_map, inp // Set control name and mapping texts name_text->set_content(get_string(ctx, control_name_hash)); - value_text->set_content(get_mapping_string(control_map, control)); + value_text->set_content(get_mapping_string(action_map, control)); // Callback invoked when an input has been mapped to the control - auto input_mapped_callback = [this, &ctx = this->ctx, control_map = &control_map, control = &control, value_text](const auto& event) + auto input_mapped_callback = [this, &ctx = this->ctx, action_map = &action_map, control = &control, value_text](const auto& event) { // Remove key mappings, mouse button mappings, and mouse scroll mappings mapped to the control - control_map->remove_mappings(*control, input::mapping_type::key); - control_map->remove_mappings(*control, input::mapping_type::mouse_button); - control_map->remove_mappings(*control, input::mapping_type::mouse_scroll); + action_map->remove_mappings(*control, input::mapping_type::key); + action_map->remove_mappings(*control, input::mapping_type::mouse_button); + action_map->remove_mappings(*control, input::mapping_type::mouse_scroll); //if (event.mapping.scancode != input::scancode::escape && event.mapping.scancode != input::scancode::backspace) { // Map generated input mapping to the control - control_map->add_mapping(*control, event.mapping); + action_map->add_mapping(*control, event.mapping); } // Update control mapping text - value_text->set_content(this->get_mapping_string(*control_map, *control)); + value_text->set_content(this->get_mapping_string(*action_map, *control)); game::menu::align_text(ctx); game::menu::update_text_tweens(ctx); @@ -248,7 +248,7 @@ void keyboard_config_menu::add_control_item(input::control_map& control_map, inp }; // Callback invoked when the control menu item has been selected - auto select_callback = [this, &ctx = this->ctx, control_map = &control_map, control = &control, value_text, input_mapped_callback]() + auto select_callback = [this, &ctx = this->ctx, action_map = &action_map, control = &control, value_text, input_mapped_callback]() { // Set control mapping text to "..." value_text->set_content(get_string(ctx, "control_mapping"_fnv1a32)); diff --git a/src/game/state/keyboard-config-menu.hpp b/src/game/state/keyboard-config-menu.hpp index 96ba89e..588276d 100644 --- a/src/game/state/keyboard-config-menu.hpp +++ b/src/game/state/keyboard-config-menu.hpp @@ -21,8 +21,8 @@ #define ANTKEEPER_GAME_STATE_KEYBOARD_CONFIG_MENU_HPP #include "game/state/base.hpp" -#include "input/control.hpp" -#include "input/control-map.hpp" +#include "input/action.hpp" +#include "input/action-map.hpp" #include "event/subscription.hpp" #include #include @@ -37,8 +37,8 @@ public: virtual ~keyboard_config_menu(); private: - std::string get_mapping_string(const input::control_map& control_map, const input::control& control); - void add_control_item(input::control_map& control_map, input::control& control, std::uint32_t control_name_hash); + std::string get_mapping_string(const input::action_map& action_map, const input::action& control); + void add_control_item(input::action_map& action_map, input::action& control, std::uint32_t control_name_hash); std::shared_ptr key_mapped_subscription; std::shared_ptr mouse_button_mapped_subscription; diff --git a/src/game/state/sound-menu.cpp b/src/game/state/sound-menu.cpp index fa6b555..c0e913f 100644 --- a/src/game/state/sound-menu.cpp +++ b/src/game/state/sound-menu.cpp @@ -84,7 +84,7 @@ sound_menu::sound_menu(game::context& ctx): auto increase_volume_callback = [this, &ctx](float* volume) { // Increase volume - if (ctx.menu_modifier_control.is_active()) + if (ctx.menu_modifier_action.is_active()) *volume += 0.01f; else *volume += 0.1f; @@ -100,7 +100,7 @@ sound_menu::sound_menu(game::context& ctx): auto decrease_volume_callback = [this, &ctx](float* volume) { // Decrease volume - if (ctx.menu_modifier_control.is_active()) + if (ctx.menu_modifier_action.is_active()) *volume -= 0.01f; else *volume -= 0.1f; @@ -135,7 +135,7 @@ sound_menu::sound_menu(game::context& ctx): auto increase_captions_size_callback = [this, &ctx]() { // Increase size - if (ctx.menu_modifier_control.is_active()) + if (ctx.menu_modifier_action.is_active()) ctx.captions_size += 0.01f; else ctx.captions_size += 0.1f; @@ -152,7 +152,7 @@ sound_menu::sound_menu(game::context& ctx): auto decrease_captions_size_callback = [this, &ctx]() { // Decrease size - if (ctx.menu_modifier_control.is_active()) + if (ctx.menu_modifier_action.is_active()) ctx.captions_size -= 0.01f; else ctx.captions_size -= 0.1f; diff --git a/src/input/control-events.hpp b/src/input/action-events.hpp similarity index 67% rename from src/input/control-events.hpp rename to src/input/action-events.hpp index c367532..458fcc1 100644 --- a/src/input/control-events.hpp +++ b/src/input/action-events.hpp @@ -17,43 +17,43 @@ * along with Antkeeper source code. If not, see . */ -#ifndef ANTKEEPER_INPUT_CONTROL_EVENTS_HPP -#define ANTKEEPER_INPUT_CONTROL_EVENTS_HPP +#ifndef ANTKEEPER_INPUT_ACTION_EVENTS_HPP +#define ANTKEEPER_INPUT_ACTION_EVENTS_HPP namespace input { -class control; +class action; /** - * Event generated when a control has been activated. + * Event generated when a action has been activated. */ -struct control_activated_event +struct action_activated_event { /// Control that was activated. - control* control; + action* action; }; /** - * Event generated while a control is active. + * Event generated while a action is active. */ -struct control_active_event +struct action_active_event { - /// Active control. - control* control; + /// Active action. + action* action; /// Control input value. float input_value; }; /** - * Event generated when a control has been deactivated. + * Event generated when a action has been deactivated. */ -struct control_deactivated_event +struct action_deactivated_event { /// Control that was deactivated. - control* control; + action* action; }; } // namespace input -#endif // ANTKEEPER_INPUT_CONTROL_EVENTS_HPP +#endif // ANTKEEPER_INPUT_ACTION_EVENTS_HPP diff --git a/src/input/control-map.cpp b/src/input/action-map.cpp similarity index 51% rename from src/input/control-map.cpp rename to src/input/action-map.cpp index 63a9036..ae6f1d0 100644 --- a/src/input/control-map.cpp +++ b/src/input/action-map.cpp @@ -17,7 +17,7 @@ * along with Antkeeper source code. If not, see . */ -#include "input/control-map.hpp" +#include "input/action-map.hpp" #include #include #include @@ -25,62 +25,62 @@ namespace input { -void control_map::connect(::event::queue& queue) +void action_map::connect(::event::queue& queue) { if (subscriptions.empty()) { - subscriptions.emplace_back(queue.subscribe(std::bind_front(&control_map::handle_gamepad_axis_moved, this))); - subscriptions.emplace_back(queue.subscribe(std::bind_front(&control_map::handle_gamepad_button_pressed, this))); - subscriptions.emplace_back(queue.subscribe(std::bind_front(&control_map::handle_gamepad_button_released, this))); - subscriptions.emplace_back(queue.subscribe(std::bind_front(&control_map::handle_key_pressed, this))); - subscriptions.emplace_back(queue.subscribe(std::bind_front(&control_map::handle_key_released, this))); - subscriptions.emplace_back(queue.subscribe(std::bind_front(&control_map::handle_mouse_button_pressed, this))); - subscriptions.emplace_back(queue.subscribe(std::bind_front(&control_map::handle_mouse_button_released, this))); - subscriptions.emplace_back(queue.subscribe(std::bind_front(&control_map::handle_mouse_moved, this))); - subscriptions.emplace_back(queue.subscribe(std::bind_front(&control_map::handle_mouse_scrolled, this))); + subscriptions.emplace_back(queue.subscribe(std::bind_front(&action_map::handle_gamepad_axis_moved, this))); + subscriptions.emplace_back(queue.subscribe(std::bind_front(&action_map::handle_gamepad_button_pressed, this))); + subscriptions.emplace_back(queue.subscribe(std::bind_front(&action_map::handle_gamepad_button_released, this))); + subscriptions.emplace_back(queue.subscribe(std::bind_front(&action_map::handle_key_pressed, this))); + subscriptions.emplace_back(queue.subscribe(std::bind_front(&action_map::handle_key_released, this))); + subscriptions.emplace_back(queue.subscribe(std::bind_front(&action_map::handle_mouse_button_pressed, this))); + subscriptions.emplace_back(queue.subscribe(std::bind_front(&action_map::handle_mouse_button_released, this))); + subscriptions.emplace_back(queue.subscribe(std::bind_front(&action_map::handle_mouse_moved, this))); + subscriptions.emplace_back(queue.subscribe(std::bind_front(&action_map::handle_mouse_scrolled, this))); } } -void control_map::disconnect() +void action_map::disconnect() { subscriptions.clear(); } -void control_map::add_mapping(control& control, gamepad_axis_mapping mapping) +void action_map::add_mapping(action& action, gamepad_axis_mapping mapping) { - gamepad_axis_mappings.emplace_back(&control, std::move(mapping)); + gamepad_axis_mappings.emplace_back(&action, std::move(mapping)); } -void control_map::add_mapping(control& control, gamepad_button_mapping mapping) +void action_map::add_mapping(action& action, gamepad_button_mapping mapping) { - gamepad_button_mappings.emplace_back(&control, std::move(mapping)); + gamepad_button_mappings.emplace_back(&action, std::move(mapping)); } -void control_map::add_mapping(control& control, key_mapping mapping) +void action_map::add_mapping(action& action, key_mapping mapping) { - key_mappings.emplace_back(&control, std::move(mapping)); + key_mappings.emplace_back(&action, std::move(mapping)); } -void control_map::add_mapping(control& control, mouse_button_mapping mapping) +void action_map::add_mapping(action& action, mouse_button_mapping mapping) { - mouse_button_mappings.emplace_back(&control, std::move(mapping)); + mouse_button_mappings.emplace_back(&action, std::move(mapping)); } -void control_map::add_mapping(control& control, mouse_motion_mapping mapping) +void action_map::add_mapping(action& action, mouse_motion_mapping mapping) { - mouse_motion_mappings.emplace_back(&control, std::move(mapping)); + mouse_motion_mappings.emplace_back(&action, std::move(mapping)); } -void control_map::add_mapping(control& control, mouse_scroll_mapping mapping) +void action_map::add_mapping(action& action, mouse_scroll_mapping mapping) { - mouse_scroll_mappings.emplace_back(&control, std::move(mapping)); + mouse_scroll_mappings.emplace_back(&action, std::move(mapping)); } -void control_map::remove_mappings(const control& control, mapping_type type) +void action_map::remove_mappings(const action& action, mapping_type type) { auto predicate = [&](const auto& tuple) -> bool { - return std::get<0>(tuple) == &control; + return std::get<0>(tuple) == &action; }; switch (type) @@ -115,11 +115,11 @@ void control_map::remove_mappings(const control& control, mapping_type type) } } -void control_map::remove_mappings(const control& control) +void action_map::remove_mappings(const action& action) { auto predicate = [&](const auto& tuple) -> bool { - return std::get<0>(tuple) == &control; + return std::get<0>(tuple) == &action; }; std::erase_if(gamepad_axis_mappings, predicate); @@ -130,7 +130,7 @@ void control_map::remove_mappings(const control& control) std::erase_if(mouse_scroll_mappings, predicate); } -void control_map::remove_mappings() +void action_map::remove_mappings() { gamepad_axis_mappings.clear(); gamepad_button_mappings.clear(); @@ -140,52 +140,52 @@ void control_map::remove_mappings() mouse_scroll_mappings.clear(); } -void control_map::handle_gamepad_axis_moved(const gamepad_axis_moved_event& event) +void action_map::handle_gamepad_axis_moved(const gamepad_axis_moved_event& event) { - for (const auto& [control, mapping]: gamepad_axis_mappings) + for (const auto& [action, mapping]: gamepad_axis_mappings) { if (mapping.axis == event.axis && (!mapping.gamepad || mapping.gamepad == event.gamepad)) { if (std::signbit(event.position) == mapping.direction) { - control->evaluate(std::abs(event.position)); + action->evaluate(std::abs(event.position)); } else { - control->evaluate(0.0f); + action->evaluate(0.0f); } } } } -void control_map::handle_gamepad_button_pressed(const gamepad_button_pressed_event& event) +void action_map::handle_gamepad_button_pressed(const gamepad_button_pressed_event& event) { - for (const auto& [control, mapping]: gamepad_button_mappings) + for (const auto& [action, mapping]: gamepad_button_mappings) { if (mapping.button == event.button && (!mapping.gamepad || mapping.gamepad == event.gamepad)) { - control->evaluate(1.0f); + action->evaluate(1.0f); } } } -void control_map::handle_gamepad_button_released(const gamepad_button_released_event& event) +void action_map::handle_gamepad_button_released(const gamepad_button_released_event& event) { - for (const auto& [control, mapping]: gamepad_button_mappings) + for (const auto& [action, mapping]: gamepad_button_mappings) { if (mapping.button == event.button && (!mapping.gamepad || mapping.gamepad == event.gamepad)) { - control->evaluate(0.0f); + action->evaluate(0.0f); } } } -void control_map::handle_key_pressed(const key_pressed_event& event) +void action_map::handle_key_pressed(const key_pressed_event& event) { - for (const auto& [control, mapping]: key_mappings) + for (const auto& [action, mapping]: key_mappings) { if (mapping.scancode == event.scancode && (!mapping.keyboard || mapping.keyboard == event.keyboard) && @@ -193,32 +193,32 @@ void control_map::handle_key_pressed(const key_pressed_event& event) { if (!event.repeat) { - control->evaluate(1.0f); + action->evaluate(1.0f); } else if (mapping.repeat) { - control->evaluate(0.0f); - control->evaluate(1.0f); + action->evaluate(0.0f); + action->evaluate(1.0f); } } } } -void control_map::handle_key_released(const key_released_event& event) +void action_map::handle_key_released(const key_released_event& event) { - for (const auto& [control, mapping]: key_mappings) + for (const auto& [action, mapping]: key_mappings) { if (mapping.scancode == event.scancode && (!mapping.keyboard || mapping.keyboard == event.keyboard)) { - control->evaluate(0.0f); + action->evaluate(0.0f); } } } -void control_map::handle_mouse_moved(const mouse_moved_event& event) +void action_map::handle_mouse_moved(const mouse_moved_event& event) { - for (const auto& [control, mapping]: mouse_motion_mappings) + for (const auto& [action, mapping]: mouse_motion_mappings) { if (!mapping.mouse || mapping.mouse == event.mouse) { @@ -226,16 +226,16 @@ void control_map::handle_mouse_moved(const mouse_moved_event& event) if (difference && std::signbit(difference) == mapping.direction) { - control->evaluate(std::abs(difference)); - control->evaluate(0.0f); + action->evaluate(std::abs(difference)); + action->evaluate(0.0f); } } } } -void control_map::handle_mouse_scrolled(const mouse_scrolled_event& event) +void action_map::handle_mouse_scrolled(const mouse_scrolled_event& event) { - for (const auto& [control, mapping]: mouse_scroll_mappings) + for (const auto& [action, mapping]: mouse_scroll_mappings) { if (!mapping.mouse || mapping.mouse == event.mouse) { @@ -243,44 +243,44 @@ void control_map::handle_mouse_scrolled(const mouse_scrolled_event& event) if (velocity && std::signbit(velocity) == mapping.direction) { - control->evaluate(std::abs(velocity)); - control->evaluate(0.0f); + action->evaluate(std::abs(velocity)); + action->evaluate(0.0f); } } } } -void control_map::handle_mouse_button_pressed(const mouse_button_pressed_event& event) +void action_map::handle_mouse_button_pressed(const mouse_button_pressed_event& event) { - for (const auto& [control, mapping]: mouse_button_mappings) + for (const auto& [action, mapping]: mouse_button_mappings) { if (mapping.button == event.button && (!mapping.mouse || mapping.mouse == event.mouse)) { - control->evaluate(1.0f); + action->evaluate(1.0f); } } } -void control_map::handle_mouse_button_released(const mouse_button_released_event& event) +void action_map::handle_mouse_button_released(const mouse_button_released_event& event) { - for (const auto& [control, mapping]: mouse_button_mappings) + for (const auto& [action, mapping]: mouse_button_mappings) { if (mapping.button == event.button && (!mapping.mouse || mapping.mouse == event.mouse)) { - control->evaluate(0.0f); + action->evaluate(0.0f); } } } -std::vector control_map::get_gamepad_axis_mappings(const control& control) const +std::vector action_map::get_gamepad_axis_mappings(const action& action) const { std::vector mappings; - for (const auto& [mapped_control, mapping]: gamepad_axis_mappings) + for (const auto& [mapped_action, mapping]: gamepad_axis_mappings) { - if (mapped_control == &control) + if (mapped_action == &action) { mappings.emplace_back(mapping); } @@ -289,13 +289,13 @@ std::vector control_map::get_gamepad_axis_mappings(const c return mappings; } -std::vector control_map::get_gamepad_button_mappings(const control& control) const +std::vector action_map::get_gamepad_button_mappings(const action& action) const { std::vector mappings; - for (const auto& [mapped_control, mapping]: gamepad_button_mappings) + for (const auto& [mapped_action, mapping]: gamepad_button_mappings) { - if (mapped_control == &control) + if (mapped_action == &action) { mappings.emplace_back(mapping); } @@ -304,13 +304,13 @@ std::vector control_map::get_gamepad_button_mappings(con return mappings; } -std::vector control_map::get_key_mappings(const control& control) const +std::vector action_map::get_key_mappings(const action& action) const { std::vector mappings; - for (const auto& [mapped_control, mapping]: key_mappings) + for (const auto& [mapped_action, mapping]: key_mappings) { - if (mapped_control == &control) + if (mapped_action == &action) { mappings.emplace_back(mapping); } @@ -319,13 +319,13 @@ std::vector control_map::get_key_mappings(const control& control) c return mappings; } -std::vector control_map::get_mouse_button_mappings(const control& control) const +std::vector action_map::get_mouse_button_mappings(const action& action) const { std::vector mappings; - for (const auto& [mapped_control, mapping]: mouse_button_mappings) + for (const auto& [mapped_action, mapping]: mouse_button_mappings) { - if (mapped_control == &control) + if (mapped_action == &action) { mappings.emplace_back(mapping); } @@ -334,13 +334,13 @@ std::vector control_map::get_mouse_button_mappings(const c return mappings; } -std::vector control_map::get_mouse_motion_mappings(const control& control) const +std::vector action_map::get_mouse_motion_mappings(const action& action) const { std::vector mappings; - for (const auto& [mapped_control, mapping]: mouse_motion_mappings) + for (const auto& [mapped_action, mapping]: mouse_motion_mappings) { - if (mapped_control == &control) + if (mapped_action == &action) { mappings.emplace_back(mapping); } @@ -349,13 +349,13 @@ std::vector control_map::get_mouse_motion_mappings(const c return mappings; } -std::vector control_map::get_mouse_scroll_mappings(const control& control) const +std::vector action_map::get_mouse_scroll_mappings(const action& action) const { std::vector mappings; - for (const auto& [mapped_control, mapping]: mouse_scroll_mappings) + for (const auto& [mapped_action, mapping]: mouse_scroll_mappings) { - if (mapped_control == &control) + if (mapped_action == &action) { mappings.emplace_back(mapping); } diff --git a/src/input/action-map.hpp b/src/input/action-map.hpp new file mode 100644 index 0000000..b92a84a --- /dev/null +++ b/src/input/action-map.hpp @@ -0,0 +1,154 @@ +/* + * 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 ANTKEEER_INPUT_ACTION_MAP_HPP +#define ANTKEEER_INPUT_ACTION_MAP_HPP + +#include "event/subscription.hpp" +#include "event/queue.hpp" +#include "input/action.hpp" +#include "input/gamepad-events.hpp" +#include "input/keyboard-events.hpp" +#include "input/mouse-events.hpp" +#include "input/mapping.hpp" +#include +#include +#include +#include + +namespace input { + +/** + * Maps input to a set of contextually-related actions. + */ +class action_map +{ +public: + /** + * Connects the input event signals of an event queue to the action map. + * + * @param queue Event queue to connect. + */ + void connect(::event::queue& queue); + + /** + * Disconnects all input event signals from the action map. + */ + void disconnect(); + + /** + * Maps input to a action. + * + * @param action Action to which input will be mapped. + * @param mapping Input mapping to add. + */ + /// @{ + void add_mapping(action& action, gamepad_axis_mapping mapping); + void add_mapping(action& action, gamepad_button_mapping mapping); + void add_mapping(action& action, key_mapping mapping); + void add_mapping(action& action, mouse_button_mapping mapping); + void add_mapping(action& action, mouse_motion_mapping mapping); + void add_mapping(action& action, mouse_scroll_mapping mapping); + /// @} + + /** + * Unmaps input from a action. + * + * @param action Action from which input will be unmapped. + * @param type Type of input mapping to remove. + */ + void remove_mappings(const action& action, mapping_type type); + + /** + * Unmaps all input from a action. + * + * @param action Action from which input will be unmapped. + */ + void remove_mappings(const action& action); + + /** + * Unmaps all input from all actions in the action map. + */ + void remove_mappings(); + + /** + * Returns all of the gamepad axis mappings associated with a action. + * + * @param action Action with which associated mappings will be returned. + */ + std::vector get_gamepad_axis_mappings(const action& action) const; + + /** + * Returns all of the gamepad button mappings associated with a action. + * + * @param action Action with which associated mappings will be returned. + */ + std::vector get_gamepad_button_mappings(const action& action) const; + + /** + * Returns all of the key mappings associated with a action. + * + * @param action Action with which associated mappings will be returned. + */ + std::vector get_key_mappings(const action& action) const; + + /** + * Returns all of the mouse button mappings associated with a action. + * + * @param action Action with which associated mappings will be returned. + */ + std::vector get_mouse_button_mappings(const action& action) const; + + /** + * Returns all of the mouse motion mappings associated with a action. + * + * @param action Action with which associated mappings will be returned. + */ + std::vector get_mouse_motion_mappings(const action& action) const; + + /** + * Returns all of the mouse scroll associated with a action. + * + * @param action Action with which associated mappings will be returned. + */ + std::vector get_mouse_scroll_mappings(const action& action) const; + +private: + void handle_gamepad_axis_moved(const gamepad_axis_moved_event& event); + void handle_gamepad_button_pressed(const gamepad_button_pressed_event& event); + void handle_gamepad_button_released(const gamepad_button_released_event& event); + void handle_key_pressed(const key_pressed_event& event); + void handle_key_released(const key_released_event& event); + void handle_mouse_button_pressed(const mouse_button_pressed_event& event); + void handle_mouse_button_released(const mouse_button_released_event& event); + void handle_mouse_moved(const mouse_moved_event& event); + void handle_mouse_scrolled(const mouse_scrolled_event& event); + + std::vector> subscriptions; + std::vector> gamepad_axis_mappings; + std::vector> gamepad_button_mappings; + std::vector> key_mappings; + std::vector> mouse_button_mappings; + std::vector> mouse_motion_mappings; + std::vector> mouse_scroll_mappings; +}; + +} // namespace input + +#endif // ANTKEEER_INPUT_ACTION_MAP_HPP diff --git a/src/input/control.cpp b/src/input/action.cpp similarity index 89% rename from src/input/control.cpp rename to src/input/action.cpp index ff0f164..03f1ab3 100644 --- a/src/input/control.cpp +++ b/src/input/action.cpp @@ -17,7 +17,7 @@ * along with Antkeeper source code. If not, see . */ -#include "input/control.hpp" +#include "input/action.hpp" namespace input { @@ -26,7 +26,7 @@ static bool default_threshold_function(float x) noexcept return x > 0.0f; } -control::control(): +action::action(): threshold_function(default_threshold_function), active(false), activated_event{this}, @@ -34,12 +34,12 @@ control::control(): deactivated_event{this} {} -void control::set_threshold_function(const threshold_function_type& function) +void action::set_threshold_function(const threshold_function_type& function) { threshold_function = function; } -void control::evaluate(float value) +void action::evaluate(float value) { // Store activation state const bool was_active = active; @@ -67,7 +67,7 @@ void control::evaluate(float value) } } -void control::reset() +void action::reset() { active = false; } diff --git a/src/input/control.hpp b/src/input/action.hpp similarity index 53% rename from src/input/control.hpp rename to src/input/action.hpp index 4751455..8351548 100644 --- a/src/input/control.hpp +++ b/src/input/action.hpp @@ -17,30 +17,30 @@ * along with Antkeeper source code. If not, see . */ -#ifndef ANTKEEPER_INPUT_CONTROL_HPP -#define ANTKEEPER_INPUT_CONTROL_HPP +#ifndef ANTKEEPER_INPUT_ACTION_HPP +#define ANTKEEPER_INPUT_ACTION_HPP -#include "input/control-events.hpp" +#include "input/action-events.hpp" #include "event/publisher.hpp" #include namespace input { /** - * Generates control-related input events on activation state changes. + * Evaluates an activation state given input values and publishes events on activation state changes. */ -class control +class action { public: /** * Threshold function type. * - * Given an input value, returns `true` if the control should be considered active, and `false` otherwise. + * Given an input value, returns `true` if the action should be considered active, and `false` otherwise. */ typedef std::function threshold_function_type; - /// Constructs a control. - control(); + /// Constructs a action. + action(); /** * Sets the threshold function. @@ -50,14 +50,14 @@ public: void set_threshold_function(const threshold_function_type& function); /** - * Evaluates the activation state of the control, according to its threshold function and an input value. + * Evaluates the activation state of the action, according to its threshold function and an input value. * * @param value Input value. */ void evaluate(float value); /** - * Resets the activation state of the control without publishing any events. + * Resets the activation state of the action without publishing any events. */ void reset(); @@ -67,26 +67,26 @@ public: return threshold_function; } - /// Returns `true` if the control is active, `false` otherwise. + /// Returns `true` if the action is active, `false` otherwise. [[nodiscard]] inline bool is_active() const noexcept { return active; } - /// Returns the channel through which control activated events are published. - [[nodiscard]] inline ::event::channel& get_activated_channel() noexcept + /// Returns the channel through which action activated events are published. + [[nodiscard]] inline ::event::channel& get_activated_channel() noexcept { return activated_publisher.channel(); } - /// Returns the channel through which control active events are published. - [[nodiscard]] inline ::event::channel& get_active_channel() noexcept + /// Returns the channel through which action active events are published. + [[nodiscard]] inline ::event::channel& get_active_channel() noexcept { return active_publisher.channel(); } - /// Returns the channel through which control deactivated events are published. - [[nodiscard]] inline ::event::channel& get_deactivated_channel() noexcept + /// Returns the channel through which action deactivated events are published. + [[nodiscard]] inline ::event::channel& get_deactivated_channel() noexcept { return deactivated_publisher.channel(); } @@ -95,15 +95,15 @@ private: threshold_function_type threshold_function; bool active; - control_activated_event activated_event; - control_active_event active_event; - control_deactivated_event deactivated_event; + action_activated_event activated_event; + action_active_event active_event; + action_deactivated_event deactivated_event; - ::event::publisher activated_publisher; - ::event::publisher active_publisher; - ::event::publisher deactivated_publisher; + ::event::publisher activated_publisher; + ::event::publisher active_publisher; + ::event::publisher deactivated_publisher; }; } // namespace input -#endif // ANTKEEPER_INPUT_CONTROL_HPP +#endif // ANTKEEPER_INPUT_ACTION_HPP diff --git a/src/input/control-map.hpp b/src/input/control-map.hpp deleted file mode 100644 index 7722e25..0000000 --- a/src/input/control-map.hpp +++ /dev/null @@ -1,154 +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 ANTKEEER_INPUT_CONTROL_MAP_HPP -#define ANTKEEER_INPUT_CONTROL_MAP_HPP - -#include "event/subscription.hpp" -#include "event/queue.hpp" -#include "input/control.hpp" -#include "input/gamepad-events.hpp" -#include "input/keyboard-events.hpp" -#include "input/mouse-events.hpp" -#include "input/mapping.hpp" -#include -#include -#include -#include - -namespace input { - -/** - * Maps input to a set of contextually-related controls. - */ -class control_map -{ -public: - /** - * Connects the input event signals of an event queue to the control map. - * - * @param queue Event queue to connect. - */ - void connect(::event::queue& queue); - - /** - * Disconnects all input event signals from the control map. - */ - void disconnect(); - - /** - * Maps input to a control. - * - * @param control Control to which input will be mapped. - * @param mapping Input mapping to add. - */ - /// @{ - void add_mapping(control& control, gamepad_axis_mapping mapping); - void add_mapping(control& control, gamepad_button_mapping mapping); - void add_mapping(control& control, key_mapping mapping); - void add_mapping(control& control, mouse_button_mapping mapping); - void add_mapping(control& control, mouse_motion_mapping mapping); - void add_mapping(control& control, mouse_scroll_mapping mapping); - /// @} - - /** - * Unmaps input from a control. - * - * @param control Control from which input will be unmapped. - * @param type Type of input mapping to remove. - */ - void remove_mappings(const control& control, mapping_type type); - - /** - * Unmaps all input from a control. - * - * @param control Control from which input will be unmapped. - */ - void remove_mappings(const control& control); - - /** - * Unmaps all input from all controls in the control map. - */ - void remove_mappings(); - - /** - * Returns all of the gamepad axis mappings associated with a control. - * - * @param control Control with which associated mappings will be returned. - */ - std::vector get_gamepad_axis_mappings(const control& control) const; - - /** - * Returns all of the gamepad button mappings associated with a control. - * - * @param control Control with which associated mappings will be returned. - */ - std::vector get_gamepad_button_mappings(const control& control) const; - - /** - * Returns all of the key mappings associated with a control. - * - * @param control Control with which associated mappings will be returned. - */ - std::vector get_key_mappings(const control& control) const; - - /** - * Returns all of the mouse button mappings associated with a control. - * - * @param control Control with which associated mappings will be returned. - */ - std::vector get_mouse_button_mappings(const control& control) const; - - /** - * Returns all of the mouse motion mappings associated with a control. - * - * @param control Control with which associated mappings will be returned. - */ - std::vector get_mouse_motion_mappings(const control& control) const; - - /** - * Returns all of the mouse scroll associated with a control. - * - * @param control Control with which associated mappings will be returned. - */ - std::vector get_mouse_scroll_mappings(const control& control) const; - -private: - void handle_gamepad_axis_moved(const gamepad_axis_moved_event& event); - void handle_gamepad_button_pressed(const gamepad_button_pressed_event& event); - void handle_gamepad_button_released(const gamepad_button_released_event& event); - void handle_key_pressed(const key_pressed_event& event); - void handle_key_released(const key_released_event& event); - void handle_mouse_button_pressed(const mouse_button_pressed_event& event); - void handle_mouse_button_released(const mouse_button_released_event& event); - void handle_mouse_moved(const mouse_moved_event& event); - void handle_mouse_scrolled(const mouse_scrolled_event& event); - - std::vector> subscriptions; - std::vector> gamepad_axis_mappings; - std::vector> gamepad_button_mappings; - std::vector> key_mappings; - std::vector> mouse_button_mappings; - std::vector> mouse_motion_mappings; - std::vector> mouse_scroll_mappings; -}; - -} // namespace input - -#endif // ANTKEEER_INPUT_CONTROL_MAP_HPP