Skip to content

Commit f281c04

Browse files
committed
button to axis direction
1 parent 020f5b7 commit f281c04

File tree

3 files changed

+52
-10
lines changed

3 files changed

+52
-10
lines changed

src/controller/controldevice/controller/mapping/ControllerDefaultMappings.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@ ControllerDefaultMappings::ControllerDefaultMappings(
88
defaultKeyboardKeyToAxisDirectionMappings,
99
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
1010
defaultSDLButtonToButtonMappings,
11+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, SDL_GameControllerButton>>>
12+
defaultSDLButtonToAxisDirectionMappings,
1113
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
1214
defaultSDLAxisDirectionToButtonMappings) {
1315
SetDefaultKeyboardKeyToButtonMappings(defaultKeyboardKeyToButtonMappings);
1416
SetDefaultKeyboardKeyToAxisDirectionMappings(defaultKeyboardKeyToAxisDirectionMappings);
17+
1518
SetDefaultSDLButtonToButtonMappings(defaultSDLButtonToButtonMappings);
19+
SetDefaultSDLButtonToAxisDirectionMappings(defaultSDLButtonToAxisDirectionMappings);
20+
1621
SetDefaultSDLAxisDirectionToButtonMappings(defaultSDLAxisDirectionToButtonMappings);
1722
}
1823

@@ -21,6 +26,7 @@ ControllerDefaultMappings::ControllerDefaultMappings()
2126
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>>(),
2227
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>(),
2328
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>(),
29+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, SDL_GameControllerButton>>>(),
2430
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>()) {
2531
}
2632

@@ -97,6 +103,20 @@ void ControllerDefaultMappings::SetDefaultSDLButtonToButtonMappings(
97103
mDefaultSDLButtonToButtonMappings[BTN_DRIGHT] = { SDL_CONTROLLER_BUTTON_DPAD_RIGHT };
98104
}
99105

106+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, SDL_GameControllerButton>>>
107+
ControllerDefaultMappings::GetDefaultSDLButtonToAxisDirectionMappings() {
108+
return mDefaultSDLButtonToAxisDirectionMappings;
109+
}
110+
111+
void ControllerDefaultMappings::SetDefaultSDLButtonToAxisDirectionMappings(
112+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, SDL_GameControllerButton>>>
113+
defaultSDLButtonToAxisDirectionMappings) {
114+
if (!defaultSDLButtonToAxisDirectionMappings.empty()) {
115+
mDefaultSDLButtonToAxisDirectionMappings = defaultSDLButtonToAxisDirectionMappings;
116+
return;
117+
}
118+
}
119+
100120
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
101121
ControllerDefaultMappings::GetDefaultSDLAxisDirectionToButtonMappings() {
102122
return mDefaultSDLAxisDirectionToButtonMappings;

src/controller/controldevice/controller/mapping/ControllerDefaultMappings.h

+12
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class ControllerDefaultMappings {
2323
defaultKeyboardKeyToAxisDirectionMappings,
2424
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
2525
defaultSDLButtonToButtonMappings,
26+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, SDL_GameControllerButton>>>
27+
defaultSDLButtonToAxisDirectionMappings,
2628
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
2729
defaultSDLAxisDirectionToButtonMappings);
2830
ControllerDefaultMappings();
@@ -31,8 +33,12 @@ class ControllerDefaultMappings {
3133
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> GetDefaultKeyboardKeyToButtonMappings();
3234
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
3335
GetDefaultKeyboardKeyToAxisDirectionMappings();
36+
3437
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
3538
GetDefaultSDLButtonToButtonMappings();
39+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, SDL_GameControllerButton>>>
40+
GetDefaultSDLButtonToAxisDirectionMappings();
41+
3642
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
3743
GetDefaultSDLAxisDirectionToButtonMappings();
3844

@@ -53,6 +59,12 @@ class ControllerDefaultMappings {
5359
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
5460
mDefaultSDLButtonToButtonMappings;
5561

62+
void SetDefaultSDLButtonToAxisDirectionMappings(
63+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, SDL_GameControllerButton>>>
64+
defaultSDLButtonToAxisDirectionMappings);
65+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, SDL_GameControllerButton>>>
66+
mDefaultSDLButtonToAxisDirectionMappings;
67+
5668
void SetDefaultSDLAxisDirectionToButtonMappings(
5769
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
5870
defaultSDLAxisDirectionToButtonMappings);

src/controller/controldevice/controller/mapping/factories/AxisDirectionMappingFactory.cpp

+20-10
Original file line numberDiff line numberDiff line change
@@ -126,16 +126,26 @@ AxisDirectionMappingFactory::CreateDefaultKeyboardAxisDirectionMappings(uint8_t
126126

127127
std::vector<std::shared_ptr<ControllerAxisDirectionMapping>>
128128
AxisDirectionMappingFactory::CreateDefaultSDLAxisDirectionMappings(uint8_t portIndex, StickIndex stickIndex) {
129-
std::vector<std::shared_ptr<ControllerAxisDirectionMapping>> mappings = {
130-
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, LEFT,
131-
stickIndex == LEFT_STICK ? 0 : 2, -1),
132-
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, RIGHT,
133-
stickIndex == LEFT_STICK ? 0 : 2, 1),
134-
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, UP,
135-
stickIndex == LEFT_STICK ? 1 : 3, -1),
136-
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, DOWN,
137-
stickIndex == LEFT_STICK ? 1 : 3, 1)
138-
};
129+
std::vector<std::shared_ptr<ControllerAxisDirectionMapping>> mappings;
130+
131+
auto defaultButtonsForStick = Context::GetInstance()
132+
->GetControlDeck()
133+
->GetControllerDefaultMappings()
134+
->GetDefaultSDLButtonToAxisDirectionMappings()[stickIndex];
135+
136+
for (const auto& [direction, sdlGamepadButton] : defaultButtonsForStick) {
137+
mappings.push_back(
138+
std::make_shared<SDLButtonToAxisDirectionMapping>(portIndex, stickIndex, direction, sdlGamepadButton));
139+
}
140+
141+
mappings.push_back(std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, LEFT,
142+
stickIndex == LEFT_STICK ? 0 : 2, -1));
143+
mappings.push_back(std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, RIGHT,
144+
stickIndex == LEFT_STICK ? 0 : 2, 1));
145+
mappings.push_back(std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, UP,
146+
stickIndex == LEFT_STICK ? 1 : 3, -1));
147+
mappings.push_back(std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(portIndex, stickIndex, DOWN,
148+
stickIndex == LEFT_STICK ? 1 : 3, 1));
139149

140150
return mappings;
141151
}

0 commit comments

Comments
 (0)