Skip to content

Commit 6755778

Browse files
committed
sdl buttons
1 parent e3f0012 commit 6755778

File tree

3 files changed

+49
-34
lines changed

3 files changed

+49
-34
lines changed

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

+30-2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33

44
namespace Ship {
55
ControllerDefaultMappings::ControllerDefaultMappings(
6-
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> defaultKeyboardKeyToButtonMappings) {
6+
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> defaultKeyboardKeyToButtonMappings,
7+
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
8+
defaultSDLButtonToButtonMappings) {
79
SetDefaultKeyboardKeyToButtonMappings(defaultKeyboardKeyToButtonMappings);
10+
SetDefaultSDLButtonToButtonMappings(defaultSDLButtonToButtonMappings);
811
}
912

1013
ControllerDefaultMappings::ControllerDefaultMappings()
11-
: ControllerDefaultMappings(std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>>()) {
14+
: ControllerDefaultMappings(
15+
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>>(),
16+
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>()) {
1217
}
1318

1419
ControllerDefaultMappings::~ControllerDefaultMappings() {
@@ -41,4 +46,27 @@ void ControllerDefaultMappings::SetDefaultKeyboardKeyToButtonMappings(
4146
mDefaultKeyboardKeyToButtonMappings[BTN_DLEFT] = { KbScancode::LUS_KB_F };
4247
mDefaultKeyboardKeyToButtonMappings[BTN_DRIGHT] = { KbScancode::LUS_KB_H };
4348
}
49+
50+
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
51+
ControllerDefaultMappings::GetDefaultSDLButtonToButtonMappings() {
52+
return mDefaultSDLButtonToButtonMappings;
53+
}
54+
55+
void ControllerDefaultMappings::SetDefaultSDLButtonToButtonMappings(
56+
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
57+
defaultSDLButtonToButtonMappings) {
58+
if (!defaultSDLButtonToButtonMappings.empty()) {
59+
mDefaultSDLButtonToButtonMappings = defaultSDLButtonToButtonMappings;
60+
return;
61+
}
62+
63+
mDefaultSDLButtonToButtonMappings[BTN_A] = { SDL_CONTROLLER_BUTTON_A };
64+
mDefaultSDLButtonToButtonMappings[BTN_B] = { SDL_CONTROLLER_BUTTON_B };
65+
mDefaultSDLButtonToButtonMappings[BTN_L] = { SDL_CONTROLLER_BUTTON_LEFTSHOULDER };
66+
mDefaultSDLButtonToButtonMappings[BTN_START] = { SDL_CONTROLLER_BUTTON_START };
67+
mDefaultSDLButtonToButtonMappings[BTN_DUP] = { SDL_CONTROLLER_BUTTON_DPAD_UP };
68+
mDefaultSDLButtonToButtonMappings[BTN_DDOWN] = { SDL_CONTROLLER_BUTTON_DPAD_DOWN };
69+
mDefaultSDLButtonToButtonMappings[BTN_DLEFT] = { SDL_CONTROLLER_BUTTON_DPAD_LEFT };
70+
mDefaultSDLButtonToButtonMappings[BTN_DRIGHT] = { SDL_CONTROLLER_BUTTON_DPAD_RIGHT };
71+
}
4472
} // namespace Ship

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,25 @@ namespace Ship {
1616
class ControllerDefaultMappings {
1717
public:
1818
ControllerDefaultMappings(
19-
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> defaultKeyboardKeyToButtonMappings);
19+
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> defaultKeyboardKeyToButtonMappings,
20+
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
21+
defaultSDLButtonToButtonMappings);
2022
ControllerDefaultMappings();
2123
~ControllerDefaultMappings();
2224

2325
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> GetDefaultKeyboardKeyToButtonMappings();
26+
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
27+
GetDefaultSDLButtonToButtonMappings();
2428

2529
private:
2630
void SetDefaultKeyboardKeyToButtonMappings(
2731
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> defaultKeyboardKeyToButtonMappings);
2832
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> mDefaultKeyboardKeyToButtonMappings;
2933

30-
// std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>> mDefaultSDLButtonToButton
34+
void SetDefaultSDLButtonToButtonMappings(
35+
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
36+
defaultSDLButtonToButtonMappings);
37+
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
38+
mDefaultSDLButtonToButtonMappings;
3139
};
3240
} // namespace Ship

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

+9-30
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,16 @@ std::vector<std::shared_ptr<ControllerButtonMapping>>
100100
ButtonMappingFactory::CreateDefaultSDLButtonMappings(uint8_t portIndex, CONTROLLERBUTTONS_T bitmask) {
101101
std::vector<std::shared_ptr<ControllerButtonMapping>> mappings;
102102

103+
auto defaultButtonsForBitmask = Context::GetInstance()
104+
->GetControlDeck()
105+
->GetControllerDefaultMappings()
106+
->GetDefaultSDLButtonToButtonMappings()[bitmask];
107+
108+
for (const auto& sdlGamepadButton : defaultButtonsForBitmask) {
109+
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(portIndex, bitmask, sdlGamepadButton));
110+
}
111+
103112
switch (bitmask) {
104-
case BTN_A:
105-
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(portIndex, BTN_A, SDL_CONTROLLER_BUTTON_A));
106-
break;
107-
case BTN_B:
108-
mappings.push_back(std::make_shared<SDLButtonToButtonMapping>(portIndex, BTN_B, SDL_CONTROLLER_BUTTON_B));
109-
break;
110-
case BTN_L:
111-
mappings.push_back(
112-
std::make_shared<SDLButtonToButtonMapping>(portIndex, BTN_L, SDL_CONTROLLER_BUTTON_LEFTSHOULDER));
113-
break;
114113
case BTN_R:
115114
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(portIndex, BTN_R,
116115
SDL_CONTROLLER_AXIS_TRIGGERRIGHT, 1));
@@ -119,10 +118,6 @@ ButtonMappingFactory::CreateDefaultSDLButtonMappings(uint8_t portIndex, CONTROLL
119118
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(portIndex, BTN_Z,
120119
SDL_CONTROLLER_AXIS_TRIGGERLEFT, 1));
121120
break;
122-
case BTN_START:
123-
mappings.push_back(
124-
std::make_shared<SDLButtonToButtonMapping>(portIndex, BTN_START, SDL_CONTROLLER_BUTTON_START));
125-
break;
126121
case BTN_CUP:
127122
mappings.push_back(
128123
std::make_shared<SDLAxisDirectionToButtonMapping>(portIndex, BTN_CUP, SDL_CONTROLLER_AXIS_RIGHTY, -1));
@@ -139,22 +134,6 @@ ButtonMappingFactory::CreateDefaultSDLButtonMappings(uint8_t portIndex, CONTROLL
139134
mappings.push_back(std::make_shared<SDLAxisDirectionToButtonMapping>(portIndex, BTN_CRIGHT,
140135
SDL_CONTROLLER_AXIS_RIGHTX, 1));
141136
break;
142-
case BTN_DUP:
143-
mappings.push_back(
144-
std::make_shared<SDLButtonToButtonMapping>(portIndex, BTN_DUP, SDL_CONTROLLER_BUTTON_DPAD_UP));
145-
break;
146-
case BTN_DDOWN:
147-
mappings.push_back(
148-
std::make_shared<SDLButtonToButtonMapping>(portIndex, BTN_DDOWN, SDL_CONTROLLER_BUTTON_DPAD_DOWN));
149-
break;
150-
case BTN_DLEFT:
151-
mappings.push_back(
152-
std::make_shared<SDLButtonToButtonMapping>(portIndex, BTN_DLEFT, SDL_CONTROLLER_BUTTON_DPAD_LEFT));
153-
break;
154-
case BTN_DRIGHT:
155-
mappings.push_back(
156-
std::make_shared<SDLButtonToButtonMapping>(portIndex, BTN_DRIGHT, SDL_CONTROLLER_BUTTON_DPAD_RIGHT));
157-
break;
158137
}
159138

160139
return mappings;

0 commit comments

Comments
 (0)