Skip to content

Commit

Permalink
axis direction to axis direction
Browse files Browse the repository at this point in the history
  • Loading branch information
briaguya-ai committed Jan 20, 2025
1 parent f281c04 commit 4ab8c42
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ ControllerDefaultMappings::ControllerDefaultMappings(
std::unordered_map<StickIndex, std::vector<std::pair<Direction, SDL_GameControllerButton>>>
defaultSDLButtonToAxisDirectionMappings,
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
defaultSDLAxisDirectionToButtonMappings) {
defaultSDLAxisDirectionToButtonMappings,
std::unordered_map<StickIndex, std::vector<std::pair<Direction, std::pair<SDL_GameControllerAxis, int32_t>>>>
defaultSDLAxisDirectionToAxisDirectionMappings) {
SetDefaultKeyboardKeyToButtonMappings(defaultKeyboardKeyToButtonMappings);
SetDefaultKeyboardKeyToAxisDirectionMappings(defaultKeyboardKeyToAxisDirectionMappings);

SetDefaultSDLButtonToButtonMappings(defaultSDLButtonToButtonMappings);
SetDefaultSDLButtonToAxisDirectionMappings(defaultSDLButtonToAxisDirectionMappings);

SetDefaultSDLAxisDirectionToButtonMappings(defaultSDLAxisDirectionToButtonMappings);
SetDefaultSDLAxisDirectionToAxisDirectionMappings(defaultSDLAxisDirectionToAxisDirectionMappings);
}

ControllerDefaultMappings::ControllerDefaultMappings()
Expand All @@ -27,7 +30,9 @@ ControllerDefaultMappings::ControllerDefaultMappings()
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>(),
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>(),
std::unordered_map<StickIndex, std::vector<std::pair<Direction, SDL_GameControllerButton>>>(),
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>()) {
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>(),
std::unordered_map<StickIndex,
std::vector<std::pair<Direction, std::pair<SDL_GameControllerAxis, int32_t>>>>()) {
}

ControllerDefaultMappings::~ControllerDefaultMappings() {
Expand Down Expand Up @@ -137,4 +142,33 @@ void ControllerDefaultMappings::SetDefaultSDLAxisDirectionToButtonMappings(
mDefaultSDLAxisDirectionToButtonMappings[BTN_CLEFT] = { { SDL_CONTROLLER_AXIS_RIGHTX, -1 } };
mDefaultSDLAxisDirectionToButtonMappings[BTN_CRIGHT] = { { SDL_CONTROLLER_AXIS_RIGHTX, 1 } };
}

std::unordered_map<StickIndex, std::vector<std::pair<Direction, std::pair<SDL_GameControllerAxis, int32_t>>>>
ControllerDefaultMappings::GetDefaultSDLAxisDirectionToAxisDirectionMappings() {
return mDefaultSDLAxisDirectionToAxisDirectionMappings;
}

void ControllerDefaultMappings::SetDefaultSDLAxisDirectionToAxisDirectionMappings(
std::unordered_map<StickIndex, std::vector<std::pair<Direction, std::pair<SDL_GameControllerAxis, int32_t>>>>
defaultSDLAxisDirectionToAxisDirectionMappings) {
if (!defaultSDLAxisDirectionToAxisDirectionMappings.empty()) {
mDefaultSDLAxisDirectionToAxisDirectionMappings = defaultSDLAxisDirectionToAxisDirectionMappings;
return;
}

mDefaultSDLAxisDirectionToAxisDirectionMappings[LEFT_STICK] = {
{ LEFT, { SDL_CONTROLLER_AXIS_LEFTX, -1 } },
{ RIGHT, { SDL_CONTROLLER_AXIS_LEFTX, 1 } },
{ UP, { SDL_CONTROLLER_AXIS_LEFTY, -1 } },
{ DOWN, { SDL_CONTROLLER_AXIS_LEFTY, 1 } },
};

mDefaultSDLAxisDirectionToAxisDirectionMappings[RIGHT_STICK] = {
{ LEFT, { SDL_CONTROLLER_AXIS_RIGHTX, -1 } },
{ RIGHT, { SDL_CONTROLLER_AXIS_RIGHTX, 1 } },
{ UP, { SDL_CONTROLLER_AXIS_RIGHTY, -1 } },
{ DOWN, { SDL_CONTROLLER_AXIS_RIGHTY, 1 } },
};
}

} // namespace Ship
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ class ControllerDefaultMappings {
std::unordered_map<StickIndex, std::vector<std::pair<Direction, SDL_GameControllerButton>>>
defaultSDLButtonToAxisDirectionMappings,
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
defaultSDLAxisDirectionToButtonMappings);
defaultSDLAxisDirectionToButtonMappings,
std::unordered_map<StickIndex, std::vector<std::pair<Direction, std::pair<SDL_GameControllerAxis, int32_t>>>>
defaultSDLAxisDirectionToAxisDirectionMappings);
ControllerDefaultMappings();
~ControllerDefaultMappings();

Expand All @@ -41,6 +43,8 @@ class ControllerDefaultMappings {

std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
GetDefaultSDLAxisDirectionToButtonMappings();
std::unordered_map<StickIndex, std::vector<std::pair<Direction, std::pair<SDL_GameControllerAxis, int32_t>>>>
GetDefaultSDLAxisDirectionToAxisDirectionMappings();

private:
void SetDefaultKeyboardKeyToButtonMappings(
Expand Down Expand Up @@ -70,5 +74,11 @@ class ControllerDefaultMappings {
defaultSDLAxisDirectionToButtonMappings);
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
mDefaultSDLAxisDirectionToButtonMappings;

void SetDefaultSDLAxisDirectionToAxisDirectionMappings(
std::unordered_map<StickIndex, std::vector<std::pair<Direction, std::pair<SDL_GameControllerAxis, int32_t>>>>
defaultSDLAxisDirectionToAxisDirectionMappings);
std::unordered_map<StickIndex, std::vector<std::pair<Direction, std::pair<SDL_GameControllerAxis, int32_t>>>>
mDefaultSDLAxisDirectionToAxisDirectionMappings;
};
} // namespace Ship
Original file line number Diff line number Diff line change
Expand Up @@ -138,14 +138,16 @@ AxisDirectionMappingFactory::CreateDefaultSDLAxisDirectionMappings(uint8_t portI
std::make_shared<SDLButtonToAxisDirectionMapping>(portIndex, stickIndex, direction, sdlGamepadButton));
}

mappings.push_back(std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, LEFT,
stickIndex == LEFT_STICK ? 0 : 2, -1));
mappings.push_back(std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, RIGHT,
stickIndex == LEFT_STICK ? 0 : 2, 1));
mappings.push_back(std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, UP,
stickIndex == LEFT_STICK ? 1 : 3, -1));
mappings.push_back(std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, DOWN,
stickIndex == LEFT_STICK ? 1 : 3, 1));
auto defaultAxisDirectionsForStick = Context::GetInstance()
->GetControlDeck()
->GetControllerDefaultMappings()
->GetDefaultSDLAxisDirectionToAxisDirectionMappings()[stickIndex];

for (const auto& [direction, sdlGamepadAxisDirection] : defaultAxisDirectionsForStick) {
auto [sdlGamepadAxis, sdlGamepadDirection] = sdlGamepadAxisDirection;
mappings.push_back(std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(
portIndex, stickIndex, direction, sdlGamepadAxis, sdlGamepadDirection));
}

return mappings;
}
Expand Down

0 comments on commit 4ab8c42

Please sign in to comment.