Skip to content

Commit

Permalink
keyboard key 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 0516cc2 commit 020f5b7
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/controller/controldevice/controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ void Controller::AddDefaultMappings(PhysicalDeviceType physicalDeviceType) {
button->AddDefaultMappings(physicalDeviceType);
}
GetLeftStick()->AddDefaultMappings(physicalDeviceType);
GetRightStick()->AddDefaultMappings(physicalDeviceType);
GetRumble()->AddDefaultMappings(physicalDeviceType);

const std::string hasConfigCvarKey =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
namespace Ship {
ControllerDefaultMappings::ControllerDefaultMappings(
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> defaultKeyboardKeyToButtonMappings,
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
defaultKeyboardKeyToAxisDirectionMappings,
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
defaultSDLButtonToButtonMappings,
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
defaultSDLAxisDirectionToButtonMappings) {
SetDefaultKeyboardKeyToButtonMappings(defaultKeyboardKeyToButtonMappings);
SetDefaultKeyboardKeyToAxisDirectionMappings(defaultKeyboardKeyToAxisDirectionMappings);
SetDefaultSDLButtonToButtonMappings(defaultSDLButtonToButtonMappings);
SetDefaultSDLAxisDirectionToButtonMappings(defaultSDLAxisDirectionToButtonMappings);
}

ControllerDefaultMappings::ControllerDefaultMappings()
: ControllerDefaultMappings(
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>>(),
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>(),
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>(),
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>()) {
}
Expand Down Expand Up @@ -51,6 +55,25 @@ void ControllerDefaultMappings::SetDefaultKeyboardKeyToButtonMappings(
mDefaultKeyboardKeyToButtonMappings[BTN_DRIGHT] = { KbScancode::LUS_KB_H };
}

std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
ControllerDefaultMappings::GetDefaultKeyboardKeyToAxisDirectionMappings() {
return mDefaultKeyboardKeyToAxisDirectionMappings;
}

void ControllerDefaultMappings::SetDefaultKeyboardKeyToAxisDirectionMappings(
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
defaultKeyboardKeyToAxisDirectionMappings) {
if (!defaultKeyboardKeyToAxisDirectionMappings.empty()) {
mDefaultKeyboardKeyToAxisDirectionMappings = defaultKeyboardKeyToAxisDirectionMappings;
return;
}

mDefaultKeyboardKeyToAxisDirectionMappings[LEFT_STICK] = { { LEFT, KbScancode::LUS_KB_A },
{ RIGHT, KbScancode::LUS_KB_D },
{ UP, KbScancode::LUS_KB_W },
{ DOWN, KbScancode::LUS_KB_S } };
}

std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
ControllerDefaultMappings::GetDefaultSDLButtonToButtonMappings() {
return mDefaultSDLButtonToButtonMappings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <unordered_set>
#include <vector>
#include <SDL2/SDL.h>
#include "ControllerAxisDirectionMapping.h"

#ifndef CONTROLLERBUTTONS_T
#define CONTROLLERBUTTONS_T uint16_t
Expand All @@ -18,6 +19,8 @@ class ControllerDefaultMappings {
public:
ControllerDefaultMappings(
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> defaultKeyboardKeyToButtonMappings,
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
defaultKeyboardKeyToAxisDirectionMappings,
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
defaultSDLButtonToButtonMappings,
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
Expand All @@ -26,6 +29,8 @@ class ControllerDefaultMappings {
~ControllerDefaultMappings();

std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> GetDefaultKeyboardKeyToButtonMappings();
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
GetDefaultKeyboardKeyToAxisDirectionMappings();
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
GetDefaultSDLButtonToButtonMappings();
std::unordered_map<CONTROLLERBUTTONS_T, std::vector<std::pair<SDL_GameControllerAxis, int32_t>>>
Expand All @@ -36,6 +41,12 @@ class ControllerDefaultMappings {
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> defaultKeyboardKeyToButtonMappings);
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<KbScancode>> mDefaultKeyboardKeyToButtonMappings;

void SetDefaultKeyboardKeyToAxisDirectionMappings(
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
defaultKeyboardKeyToAxisDirectionMappings);
std::unordered_map<StickIndex, std::vector<std::pair<Direction, KbScancode>>>
mDefaultKeyboardKeyToAxisDirectionMappings;

void SetDefaultSDLButtonToButtonMappings(
std::unordered_map<CONTROLLERBUTTONS_T, std::unordered_set<SDL_GameControllerButton>>
defaultSDLButtonToButtonMappings);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,17 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromConfig(uint8_t portIn

std::vector<std::shared_ptr<ControllerAxisDirectionMapping>>
AxisDirectionMappingFactory::CreateDefaultKeyboardAxisDirectionMappings(uint8_t portIndex, StickIndex stickIndex) {
std::vector<std::shared_ptr<ControllerAxisDirectionMapping>> mappings = {
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stickIndex, LEFT,
LUS_DEFAULT_KB_MAPPING_STICKLEFT),
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stickIndex, RIGHT,
LUS_DEFAULT_KB_MAPPING_STICKRIGHT),
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stickIndex, UP, LUS_DEFAULT_KB_MAPPING_STICKUP),
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stickIndex, DOWN,
LUS_DEFAULT_KB_MAPPING_STICKDOWN)
};
std::vector<std::shared_ptr<ControllerAxisDirectionMapping>> mappings;

auto defaultsForStick = Context::GetInstance()
->GetControlDeck()
->GetControllerDefaultMappings()
->GetDefaultKeyboardKeyToAxisDirectionMappings()[stickIndex];

for (const auto& [direction, scancode] : defaultsForStick) {
mappings.push_back(
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stickIndex, direction, scancode));
}

return mappings;
}
Expand Down

0 comments on commit 020f5b7

Please sign in to comment.