5#include <boost/program_options.hpp>
9using namespace std::literals;
13 const std::array<std::function<bool()>, 14> buttons;
14 const std::array<std::function<std::array<uint8_t, 2>()>, 3> sticks;
18 "controls.y"sv,
"controls.b"sv,
"controls.a"sv,
19 "controls.x"sv,
"controls.l"sv,
"controls.r"sv,
20 "controls.xl"sv,
"controls.xr"sv,
"controls.Select"sv,
21 "controls.start"sv,
"controls.lClick"sv,
"controls.rClick"sv,
22 "controls.home"sv,
"controls.capture"sv,
29 static auto getButtons(
const boost::program_options::variables_map &vm) {
31 static_cast<unsigned int>(vm[
"controls.modifier"].as<
int>());
32 auto modifier = [modifierButton] {
33 return sf::Joystick::isButtonPressed(0, modifierButton);
36 [modifier, turboButton =
static_cast<unsigned int>(
37 vm[
"controls.turbo"].as<
int>())] {
38 return sf::Joystick::isButtonPressed(0, turboButton) &&
43 std::array<std::function<bool()>, 14> buttons;
44 auto transform = [&](
auto button_str) {
45 auto button_id =
static_cast<unsigned int>(
46 vm[std::string{button_str}].as<
int>());
47 auto event = [modifier, button_id] {
48 return sf::Joystick::isButtonPressed(0, button_id) &&
51 return [turboToggle, event,
52 turboEvent =
TurboEvent{event, 20ms}]()
mutable {
53 return turboToggle() ? turboEvent() : event();
57 buttons.begin(), transform);
64 float SFML_RATIO = 255.0f / 200.0f;
65 float SCALE_FACTOR = 1.4f;
66 float SCALE = SCALE_FACTOR * SFML_RATIO;
69 int scaled =
static_cast<int>(pos * SCALE) + OFFSET;
70 return static_cast<uint8_t
>(std::clamp(scaled, 0, 255));
73 static auto getSticks(
const boost::program_options::variables_map &vm) {
74 std::array<std::function<std::array<uint8_t, 2>()>, 3> sticks;
77 static_cast<sf::Joystick::Axis
>(vm[
"controls.leftXAxis"].as<
int>());
79 static_cast<sf::Joystick::Axis
>(vm[
"controls.leftYAxis"].as<
int>());
81 return std::array<uint8_t, 2>{
84 sf::Joystick::getAxisPosition(0, left_y_axis))};
87 auto right_x_axis =
static_cast<sf::Joystick::Axis
>(
88 vm[
"controls.rightXAxis"].as<
int>());
89 auto right_y_axis =
static_cast<sf::Joystick::Axis
>(
90 vm[
"controls.rightYAxis"].as<
int>());
92 return std::array<uint8_t, 2>{
94 sf::Joystick::getAxisPosition(0, right_x_axis)),
96 sf::Joystick::getAxisPosition(0, right_y_axis))};
100 static_cast<sf::Joystick::Axis
>(vm[
"controls.hatXAxis"].as<
int>());
102 static_cast<sf::Joystick::Axis
>(vm[
"controls.hatYAxis"].as<
int>());
104 return std::array<uint8_t, 2>{
114 constexpr static std::array<uint8_t, 2>
116 return {uint8_t(128 + 127 * up - 128 * down),
117 uint8_t(128 + 127 *
right - 128 *
left)};
120 constexpr static std::array<bool, 4>
128 constexpr static std::array<uint8_t, 2>
164 std::array<uint8_t, 8> res;
168 for (std::size_t i = 0; i < 8; i++) {
169 res[1] |= (buttons[i]() << i);
171 for (std::size_t i = 0; i < 6; i++) {
172 res[2] |= (buttons[i + 8]() << i);
174 auto [leftX, leftY] =
178 auto [rightX, rightY] =
Definition: Controller.h:11
static constexpr int JOYSTICK_DEADZONE
Definition: Controller.h:27
static constexpr auto buttonMapping
Definition: Controller.h:17
static auto getButtons(const boost::program_options::variables_map &vm)
Definition: Controller.h:29
static constexpr std::array< uint8_t, 2 > adjustStickValueForDeadzone(std::array< uint8_t, 2 > sticks)
Definition: Controller.h:129
static auto getSticks(const boost::program_options::variables_map &vm)
Definition: Controller.h:73
std::array< uint8_t, 8 > getData() const
Definition: Controller.h:163
static constexpr auto convertSFML_Axis(float pos)
Definition: Controller.h:61
static constexpr std::array< bool, 4 > analogToDigitalConversion(std::array< uint8_t, 2 > sticks)
Definition: Controller.h:121
static constexpr std::array< uint8_t, 2 > digtalToAnalogConversion(bool up, bool right, bool down, bool left)
Definition: Controller.h:115
stickIndicies
Definition: Controller.h:25
@ hat
Definition: Controller.h:25
@ left
Definition: Controller.h:25
@ right
Definition: Controller.h:25
static constexpr uint8_t convertSticksToHat(std::array< uint8_t, 2 > sticks)
Definition: Controller.h:140
Controller(const boost::program_options::variables_map &vm)
Definition: Controller.h:111
Definition: EventToggle.h:35
Definition: EventToggle.h:7