Browse Source

Revise input mapper. Separate input events into separate headers

master
C. J. Howard 1 year ago
parent
commit
df0e3e2531
24 changed files with 656 additions and 320 deletions
  1. +1
    -0
      src/app/sdl/sdl-input-manager.cpp
  2. +1
    -0
      src/game/state/boot.cpp
  3. +48
    -18
      src/game/state/credits.cpp
  4. +2
    -1
      src/game/state/credits.hpp
  5. +55
    -37
      src/game/state/splash.cpp
  6. +2
    -2
      src/game/state/splash.hpp
  7. +32
    -0
      src/input/application-events.hpp
  8. +59
    -0
      src/input/control-events.hpp
  9. +3
    -1
      src/input/control-map.hpp
  10. +1
    -1
      src/input/control.hpp
  11. +47
    -0
      src/input/device-events.hpp
  12. +1
    -1
      src/input/device.hpp
  13. +71
    -0
      src/input/gamepad-events.hpp
  14. +1
    -2
      src/input/gamepad.hpp
  15. +0
    -238
      src/input/input-events.hpp
  16. +28
    -0
      src/input/input.hpp
  17. +66
    -0
      src/input/keyboard-events.hpp
  18. +1
    -1
      src/input/keyboard.hpp
  19. +11
    -8
      src/input/mapper.cpp
  20. +43
    -6
      src/input/mapper.hpp
  21. +83
    -0
      src/input/mapping-events.hpp
  22. +95
    -0
      src/input/mouse-events.hpp
  23. +1
    -1
      src/input/mouse.hpp
  24. +4
    -3
      src/main.cpp

+ 1
- 0
src/app/sdl/sdl-input-manager.cpp View File

@ -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 <SDL2/SDL.h>

+ 1
- 0
src/game/state/boot.cpp View File

@ -88,6 +88,7 @@
#include "utility/paths.hpp"
#include "utility/dict.hpp"
#include "utility/hash/fnv1a.hpp"
#include "input/application-events.hpp"
#include <algorithm>
#include <entt/entt.hpp>
#include <execution>

+ 48
- 18
src/game/state/credits.cpp View File

@ -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);

+ 2
- 1
src/game/state/credits.hpp View File

@ -24,6 +24,7 @@
#include "scene/text.hpp"
#include "animation/animation.hpp"
#include "event/subscription.hpp"
#include <vector>
namespace game {
namespace state {
@ -38,7 +39,7 @@ private:
scene::text credits_text;
animation<float> credits_fade_in_animation;
animation<float> credits_scroll_animation;
std::shared_ptr<::event::subscription> input_mapped_subscription;
std::vector<std::shared_ptr<::event::subscription>> input_mapped_subscriptions;
};
} // namespace state

+ 55
- 37
src/game/state/splash.cpp View File

@ -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);

+ 2
- 2
src/game/state/splash.hpp View File

@ -25,6 +25,7 @@
#include "scene/billboard.hpp"
#include "animation/animation.hpp"
#include "event/subscription.hpp"
#include <vector>
namespace game {
namespace state {
@ -40,8 +41,7 @@ private:
scene::billboard splash_billboard;
animation<float> splash_fade_in_animation;
animation<float> splash_fade_out_animation;
std::shared_ptr<::event::subscription> input_mapped_subscription;
bool skipped;
std::vector<std::shared_ptr<::event::subscription>> input_mapped_subscriptions;
};
} // namespace state

+ 32
- 0
src/input/application-events.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

+ 59
- 0
src/input/control-events.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

+ 3
- 1
src/input/control-map.hpp View File

@ -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 <memory>
#include <tuple>

+ 1
- 1
src/input/control.hpp View File

@ -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 <functional>

+ 47
- 0
src/input/device-events.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

+ 1
- 1
src/input/device.hpp View File

@ -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 <optional>

+ 71
- 0
src/input/gamepad-events.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

+ 1
- 2
src/input/gamepad.hpp View File

@ -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;

+ 0
- 238
src/input/input-events.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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 <cstdint>
#include <memory>
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> 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<std::int32_t, 2> position;
/// Relative movement of the mouse, in pixels.
math::vector<std::int32_t, 2> 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<std::int32_t, 2> 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<std::int32_t, 2> 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<std::int32_t, 2> position;
/// Scroll velocity.
math::vector<float, 2> velocity;
};
/**
* Event generated when the application has been requested to quit.
*/
struct application_quit_event {};
} // namespace input
#endif // ANTKEEPER_INPUT_INPUT_EVENTS_HPP

+ 28
- 0
src/input/input.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_INPUT_HPP
#define ANTKEEPER_INPUT_HPP
/**
* Input devices, events, and mapping.
*/
namespace input {}
#endif // ANTKEEPER_INPUT_HPP

+ 66
- 0
src/input/keyboard-events.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef ANTKEEPER_INPUT_KEYBOARD_EVENTS_HPP
#define ANTKEEPER_INPUT_KEYBOARD_EVENTS_HPP
#include "input/scancode.hpp"
#include "input/modifier-key.hpp"
#include <cstdint>
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

+ 1
- 1
src/input/keyboard.hpp View File

@ -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"

+ 11
- 8
src/input/mapper.cpp View File

@ -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<mapping>(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<mapping>(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<mapping>(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<mapping>(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<mapping>(new mouse_motion_mapping(event.mouse, mouse_motion_axis::x, std::signbit(static_cast<float>(event.difference.x()))))});
mouse_motion_mapped_publisher.publish({mouse_motion_mapping(event.mouse, mouse_motion_axis::x, std::signbit(static_cast<float>(event.difference.x())))});
}
if (event.difference.y())
{
input_mapped_publisher.publish({std::shared_ptr<mapping>(new mouse_motion_mapping(event.mouse, mouse_motion_axis::y, std::signbit(static_cast<float>(event.difference.y()))))});
mouse_motion_mapped_publisher.publish({mouse_motion_mapping(event.mouse, mouse_motion_axis::y, std::signbit(static_cast<float>(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<mapping>(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<mapping>(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()))});
}
}

+ 43
- 6
src/input/mapper.hpp View File

@ -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 <memory>
#include <vector>
@ -48,10 +50,40 @@ public:
*/
void disconnect();
/// Returns the channel through which input mapped events are published.
[[nodiscard]] inline ::event::channel<input_mapped_event>& get_input_mapped_channel() noexcept
/// Returns the channel through which gamepad axis mapped events are published.
[[nodiscard]] inline ::event::channel<gamepad_axis_mapped_event>& 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<gamepad_button_mapped_event>& 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<key_mapped_event>& 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<mouse_button_mapped_event>& 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<mouse_motion_mapped_event>& 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<mouse_scroll_mapped_event>& 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<std::shared_ptr<::event::subscription>> subscriptions;
::event::publisher<input_mapped_event> input_mapped_publisher;
::event::publisher<gamepad_axis_mapped_event> gamepad_axis_mapped_publisher;
::event::publisher<gamepad_button_mapped_event> gamepad_button_mapped_publisher;
::event::publisher<key_mapped_event> key_mapped_publisher;
::event::publisher<mouse_button_mapped_event> mouse_button_mapped_publisher;
::event::publisher<mouse_motion_mapped_event> mouse_motion_mapped_publisher;
::event::publisher<mouse_scroll_mapped_event> mouse_scroll_mapped_publisher;
};
} // namespace input

+ 83
- 0
src/input/mapping-events.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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

+ 95
- 0
src/input/mouse-events.hpp View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#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 <cstdint>
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<std::int32_t, 2> position;
/// Relative movement of the mouse, in pixels.
math::vector<std::int32_t, 2> 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<std::int32_t, 2> 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<std::int32_t, 2> 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<std::int32_t, 2> position;
/// Scroll velocity.
math::vector<float, 2> velocity;
};
} // namespace input
#endif // ANTKEEPER_INPUT_MOUSE_EVENTS_HPP

+ 1
- 1
src/input/mouse.hpp View File

@ -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"

+ 4
- 3
src/main.cpp View File

@ -68,13 +68,14 @@ int main(int argc, char* argv[])
std::osyncstream(std::cout) << std::format
(
"{}{:8.03f} {}:{}:{}: {}: {}{}\n",
colors[static_cast<int>(event.severity)],
"[{:8.03f}] {}{}: {}:{}:{}: {}{}\n",
std::chrono::duration<float>(event.time - launch_time).count(),
colors[static_cast<int>(event.severity)],
//severities[static_cast<int>(event.severity)],
static_cast<int>(event.severity),
std::filesystem::path(event.location.file_name()).filename().string(),
event.location.line(),
event.location.column(),
severities[static_cast<int>(event.severity)],
event.message,
ansi::reset
);

Loading…
Cancel
Save