Skip to content

Commit 020f5b7

Browse files
committed
keyboard key to axis direction
1 parent 0516cc2 commit 020f5b7

File tree

4 files changed

+46
-9
lines changed

4 files changed

+46
-9
lines changed

src/controller/controldevice/controller/Controller.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ void Controller::AddDefaultMappings(PhysicalDeviceType physicalDeviceType) {
107107
button->AddDefaultMappings(physicalDeviceType);
108108
}
109109
GetLeftStick()->AddDefaultMappings(physicalDeviceType);
110+
GetRightStick()->AddDefaultMappings(physicalDeviceType);
110111
GetRumble()->AddDefaultMappings(physicalDeviceType);
111112

112113
const std::string hasConfigCvarKey =

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

+23
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,22 @@
44
namespace Ship {
55
ControllerDefaultMappings::ControllerDefaultMappings(
66
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> defaultKeyboardKeyToButtonMappings,
7+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
8+
defaultKeyboardKeyToAxisDirectionMappings,
79
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
810
defaultSDLButtonToButtonMappings,
911
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
1012
defaultSDLAxisDirectionToButtonMappings) {
1113
SetDefaultKeyboardKeyToButtonMappings(defaultKeyboardKeyToButtonMappings);
14+
SetDefaultKeyboardKeyToAxisDirectionMappings(defaultKeyboardKeyToAxisDirectionMappings);
1215
SetDefaultSDLButtonToButtonMappings(defaultSDLButtonToButtonMappings);
1316
SetDefaultSDLAxisDirectionToButtonMappings(defaultSDLAxisDirectionToButtonMappings);
1417
}
1518

1619
ControllerDefaultMappings::ControllerDefaultMappings()
1720
: ControllerDefaultMappings(
1821
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>>(),
22+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>(),
1923
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>(),
2024
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>()) {
2125
}
@@ -51,6 +55,25 @@ void ControllerDefaultMappings::SetDefaultKeyboardKeyToButtonMappings(
5155
mDefaultKeyboardKeyToButtonMappings[BTN_DRIGHT] = { KbScancode::LUS_KB_H };
5256
}
5357

58+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
59+
ControllerDefaultMappings::GetDefaultKeyboardKeyToAxisDirectionMappings() {
60+
return mDefaultKeyboardKeyToAxisDirectionMappings;
61+
}
62+
63+
void ControllerDefaultMappings::SetDefaultKeyboardKeyToAxisDirectionMappings(
64+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
65+
defaultKeyboardKeyToAxisDirectionMappings) {
66+
if (!defaultKeyboardKeyToAxisDirectionMappings.empty()) {
67+
mDefaultKeyboardKeyToAxisDirectionMappings = defaultKeyboardKeyToAxisDirectionMappings;
68+
return;
69+
}
70+
71+
mDefaultKeyboardKeyToAxisDirectionMappings[LEFT_STICK] = { { LEFT, KbScancode::LUS_KB_A },
72+
{ RIGHT, KbScancode::LUS_KB_D },
73+
{ UP, KbScancode::LUS_KB_W },
74+
{ DOWN, KbScancode::LUS_KB_S } };
75+
}
76+
5477
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
5578
ControllerDefaultMappings::GetDefaultSDLButtonToButtonMappings() {
5679
return mDefaultSDLButtonToButtonMappings;

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

+11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <unordered_set>
66
#include <vector>
77
#include <SDL2/SDL.h>
8+
#include "ControllerAxisDirectionMapping.h"
89

910
#ifndef CONTROLLERBUTTONS_T
1011
#define CONTROLLERBUTTONS_T uint16_t
@@ -18,6 +19,8 @@ class ControllerDefaultMappings {
1819
public:
1920
ControllerDefaultMappings(
2021
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> defaultKeyboardKeyToButtonMappings,
22+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
23+
defaultKeyboardKeyToAxisDirectionMappings,
2124
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
2225
defaultSDLButtonToButtonMappings,
2326
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
@@ -26,6 +29,8 @@ class ControllerDefaultMappings {
2629
~ControllerDefaultMappings();
2730

2831
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> GetDefaultKeyboardKeyToButtonMappings();
32+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
33+
GetDefaultKeyboardKeyToAxisDirectionMappings();
2934
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
3035
GetDefaultSDLButtonToButtonMappings();
3136
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
@@ -36,6 +41,12 @@ class ControllerDefaultMappings {
3641
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> defaultKeyboardKeyToButtonMappings);
3742
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> mDefaultKeyboardKeyToButtonMappings;
3843

44+
void SetDefaultKeyboardKeyToAxisDirectionMappings(
45+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
46+
defaultKeyboardKeyToAxisDirectionMappings);
47+
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
48+
mDefaultKeyboardKeyToAxisDirectionMappings;
49+
3950
void SetDefaultSDLButtonToButtonMappings(
4051
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
4152
defaultSDLButtonToButtonMappings);

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

+11-9
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,17 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromConfig(uint8_t portIn
109109

110110
std::vector<std::shared_ptr<ControllerAxisDirectionMapping>>
111111
AxisDirectionMappingFactory::CreateDefaultKeyboardAxisDirectionMappings(uint8_t portIndex, StickIndex stickIndex) {
112-
std::vector<std::shared_ptr<ControllerAxisDirectionMapping>> mappings = {
113-
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stickIndex, LEFT,
114-
LUS_DEFAULT_KB_MAPPING_STICKLEFT),
115-
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stickIndex, RIGHT,
116-
LUS_DEFAULT_KB_MAPPING_STICKRIGHT),
117-
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stickIndex, UP, LUS_DEFAULT_KB_MAPPING_STICKUP),
118-
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stickIndex, DOWN,
119-
LUS_DEFAULT_KB_MAPPING_STICKDOWN)
120-
};
112+
std::vector<std::shared_ptr<ControllerAxisDirectionMapping>> mappings;
113+
114+
auto defaultsForStick = Context::GetInstance()
115+
->GetControlDeck()
116+
->GetControllerDefaultMappings()
117+
->GetDefaultKeyboardKeyToAxisDirectionMappings()[stickIndex];
118+
119+
for (const auto& [direction, scancode] : defaultsForStick) {
120+
mappings.push_back(
121+
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stickIndex, direction, scancode));
122+
}
121123

122124
return mappings;
123125
}

0 commit comments

Comments
 (0)