Skip to content

Commit

Permalink
rename Stick -> StickIndex (#754)
Browse files Browse the repository at this point in the history
  • Loading branch information
briaguya-ai authored Dec 16, 2024
1 parent 8280f41 commit efcf668
Show file tree
Hide file tree
Showing 15 changed files with 94 additions and 85 deletions.
2 changes: 1 addition & 1 deletion src/controller/controldevice/controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ void Controller::MoveMappingsToDifferentController(std::shared_ptr<Controller> n

for (auto stick : { GetLeftStick(), GetRightStick() }) {
auto newControllerStick =
stick->LeftOrRightStick() == LEFT_STICK ? newController->GetLeftStick() : newController->GetRightStick();
stick->GetStickIndex() == LEFT_STICK ? newController->GetLeftStick() : newController->GetRightStick();
for (auto [direction, mappings] : stick->GetAllAxisDirectionMappings()) {
std::vector<std::string> axisDirectionMappingIdsToRemove;
for (auto [id, mapping] : mappings) {
Expand Down
60 changes: 32 additions & 28 deletions src/controller/controldevice/controller/ControllerStick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
#define MINIMUM_RADIUS_TO_MAP_NOTCH 0.9

namespace Ship {
ControllerStick::ControllerStick(uint8_t portIndex, Stick stick)
: mPortIndex(portIndex), mStick(stick), mUseKeydownEventToCreateNewMapping(false),
ControllerStick::ControllerStick(uint8_t portIndex, StickIndex stickIndex)
: mPortIndex(portIndex), mStickIndex(stickIndex), mUseKeydownEventToCreateNewMapping(false),
mKeyboardScancodeForNewMapping(KbScancode::LUS_KB_UNKNOWN) {
mSensitivityPercentage = DEFAULT_STICK_SENSITIVITY_PERCENTAGE;
mSensitivity = 1.0f;
Expand Down Expand Up @@ -69,8 +69,8 @@ void ControllerStick::ClearAllMappingsForDevice(ShipDeviceIndex lusIndex) {
}

// todo: where should this live?
std::unordered_map<Stick, std::string> stickToConfigStickName = { { LEFT_STICK, "LeftStick" },
{ RIGHT_STICK, "RightStick" } };
std::unordered_map<StickIndex, std::string> stickIndexToConfigStickIndexName = { { LEFT_STICK, "LeftStick" },
{ RIGHT_STICK, "RightStick" } };

// todo: where should this live?
std::unordered_map<Direction, std::string> directionToConfigDirectionName = {
Expand All @@ -89,7 +89,7 @@ void ControllerStick::SaveAxisDirectionMappingIdsToConfig() {

const std::string axisDirectionMappingIdsCvarKey = StringHelper::Sprintf(
CVAR_PREFIX_CONTROLLERS ".Port%d.%s.%sAxisDirectionMappingIds", mPortIndex + 1,
stickToConfigStickName[mStick].c_str(), directionToConfigDirectionName[direction].c_str());
stickIndexToConfigStickIndexName[mStickIndex].c_str(), directionToConfigDirectionName[direction].c_str());
if (axisDirectionMappingIdListString == "") {
CVarClear(axisDirectionMappingIdsCvarKey.c_str());
} else {
Expand Down Expand Up @@ -123,13 +123,13 @@ void ControllerStick::AddAxisDirectionMapping(Direction direction,

void ControllerStick::AddDefaultMappings(ShipDeviceIndex lusIndex) {
for (auto mapping :
AxisDirectionMappingFactory::CreateDefaultSDLAxisDirectionMappings(lusIndex, mPortIndex, mStick)) {
AxisDirectionMappingFactory::CreateDefaultSDLAxisDirectionMappings(lusIndex, mPortIndex, mStickIndex)) {
AddAxisDirectionMapping(mapping->GetDirection(), mapping);
}

if (lusIndex == ShipDeviceIndex::Keyboard) {
for (auto mapping :
AxisDirectionMappingFactory::CreateDefaultKeyboardAxisDirectionMappings(mPortIndex, mStick)) {
AxisDirectionMappingFactory::CreateDefaultKeyboardAxisDirectionMappings(mPortIndex, mStickIndex)) {
AddAxisDirectionMapping(mapping->GetDirection(), mapping);
}
}
Expand All @@ -143,7 +143,7 @@ void ControllerStick::AddDefaultMappings(ShipDeviceIndex lusIndex) {
}

void ControllerStick::LoadAxisDirectionMappingFromConfig(std::string id) {
auto mapping = AxisDirectionMappingFactory::CreateAxisDirectionMappingFromConfig(mPortIndex, mStick, id);
auto mapping = AxisDirectionMappingFactory::CreateAxisDirectionMappingFromConfig(mPortIndex, mStickIndex, id);

if (mapping == nullptr) {
return;
Expand All @@ -163,7 +163,7 @@ void ControllerStick::ReloadAllMappingsFromConfig() {
for (auto direction : { LEFT, RIGHT, UP, DOWN }) {
const std::string axisDirectionMappingIdsCvarKey = StringHelper::Sprintf(
CVAR_PREFIX_CONTROLLERS ".Port%d.%s.%sAxisDirectionMappingIds", mPortIndex + 1,
stickToConfigStickName[mStick].c_str(), directionToConfigDirectionName[direction].c_str());
stickIndexToConfigStickIndexName[mStickIndex].c_str(), directionToConfigDirectionName[direction].c_str());

std::stringstream axisDirectionMappingIdsStringStream(
CVarGetString(axisDirectionMappingIdsCvarKey.c_str(), ""));
Expand All @@ -173,20 +173,23 @@ void ControllerStick::ReloadAllMappingsFromConfig() {
}
}

SetSensitivity(CVarGetInteger(StringHelper::Sprintf(CVAR_PREFIX_CONTROLLERS ".Port%d.%s.SensitivityPercentage",
mPortIndex + 1, stickToConfigStickName[mStick].c_str())
.c_str(),
DEFAULT_STICK_SENSITIVITY_PERCENTAGE));
SetSensitivity(
CVarGetInteger(StringHelper::Sprintf(CVAR_PREFIX_CONTROLLERS ".Port%d.%s.SensitivityPercentage", mPortIndex + 1,
stickIndexToConfigStickIndexName[mStickIndex].c_str())
.c_str(),
DEFAULT_STICK_SENSITIVITY_PERCENTAGE));

SetDeadzone(CVarGetInteger(StringHelper::Sprintf(CVAR_PREFIX_CONTROLLERS ".Port%d.%s.DeadzonePercentage",
mPortIndex + 1, stickToConfigStickName[mStick].c_str())
.c_str(),
DEFAULT_STICK_DEADZONE_PERCENTAGE));
SetDeadzone(
CVarGetInteger(StringHelper::Sprintf(CVAR_PREFIX_CONTROLLERS ".Port%d.%s.DeadzonePercentage", mPortIndex + 1,
stickIndexToConfigStickIndexName[mStickIndex].c_str())
.c_str(),
DEFAULT_STICK_DEADZONE_PERCENTAGE));

SetNotchSnapAngle(CVarGetInteger(StringHelper::Sprintf(CVAR_PREFIX_CONTROLLERS ".Port%d.%s.NotchSnapAngle",
mPortIndex + 1, stickToConfigStickName[mStick].c_str())
.c_str(),
0));
SetNotchSnapAngle(
CVarGetInteger(StringHelper::Sprintf(CVAR_PREFIX_CONTROLLERS ".Port%d.%s.NotchSnapAngle", mPortIndex + 1,
stickIndexToConfigStickIndexName[mStickIndex].c_str())
.c_str(),
0));
}

double ControllerStick::GetClosestNotch(double angle, double approximationThreshold) {
Expand Down Expand Up @@ -267,12 +270,13 @@ bool ControllerStick::AddOrEditAxisDirectionMappingFromRawPress(Direction direct

mUseKeydownEventToCreateNewMapping = true;
if (mKeyboardScancodeForNewMapping != LUS_KB_UNKNOWN) {
mapping = std::make_shared<KeyboardKeyToAxisDirectionMapping>(mPortIndex, mStick, direction,
mapping = std::make_shared<KeyboardKeyToAxisDirectionMapping>(mPortIndex, mStickIndex, direction,
mKeyboardScancodeForNewMapping);
}

if (mapping == nullptr) {
mapping = AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(mPortIndex, mStick, direction);
mapping =
AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(mPortIndex, mStickIndex, direction);
}

if (mapping == nullptr) {
Expand Down Expand Up @@ -339,7 +343,7 @@ void ControllerStick::SetSensitivity(uint8_t sensitivityPercentage) {
mSensitivityPercentage = sensitivityPercentage;
mSensitivity = sensitivityPercentage / 100.0f;
CVarSetInteger(StringHelper::Sprintf(CVAR_PREFIX_CONTROLLERS ".Port%d.%s.SensitivityPercentage", mPortIndex + 1,
stickToConfigStickName[mStick].c_str())
stickIndexToConfigStickIndexName[mStickIndex].c_str())
.c_str(),
mSensitivityPercentage);
CVarSave();
Expand All @@ -361,7 +365,7 @@ void ControllerStick::SetDeadzone(uint8_t deadzonePercentage) {
mDeadzonePercentage = deadzonePercentage;
mDeadzone = MAX_AXIS_RANGE * (deadzonePercentage / 100.0f);
CVarSetInteger(StringHelper::Sprintf(CVAR_PREFIX_CONTROLLERS ".Port%d.%s.DeadzonePercentage", mPortIndex + 1,
stickToConfigStickName[mStick].c_str())
stickIndexToConfigStickIndexName[mStickIndex].c_str())
.c_str(),
mDeadzonePercentage);
CVarSave();
Expand All @@ -382,7 +386,7 @@ bool ControllerStick::DeadzoneIsDefault() {
void ControllerStick::SetNotchSnapAngle(uint8_t notchSnapAngle) {
mNotchSnapAngle = notchSnapAngle;
CVarSetInteger(StringHelper::Sprintf(CVAR_PREFIX_CONTROLLERS ".Port%d.%s.NotchSnapAngle", mPortIndex + 1,
stickToConfigStickName[mStick].c_str())
stickIndexToConfigStickIndexName[mStickIndex].c_str())
.c_str(),
mNotchSnapAngle);
CVarSave();
Expand Down Expand Up @@ -415,7 +419,7 @@ ControllerStick::GetAllAxisDirectionMappings() {
return mAxisDirectionMappings;
}

Stick ControllerStick::LeftOrRightStick() {
return mStick;
StickIndex ControllerStick::GetStickIndex() {
return mStickIndex;
}
} // namespace Ship
6 changes: 3 additions & 3 deletions src/controller/controldevice/controller/ControllerStick.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace Ship {

class ControllerStick {
public:
ControllerStick(uint8_t portIndex, Stick stick);
ControllerStick(uint8_t portIndex, StickIndex stickIndex);
~ControllerStick();

void ReloadAllMappingsFromConfig();
Expand Down Expand Up @@ -54,15 +54,15 @@ class ControllerStick {
bool ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode);

bool HasMappingsForShipDeviceIndex(ShipDeviceIndex lusIndex);
Stick LeftOrRightStick();
StickIndex GetStickIndex();

private:
double GetClosestNotch(double angle, double approximationThreshold);
void LoadAxisDirectionMappingFromConfig(std::string id);
float GetAxisDirectionValue(Direction direction);

uint8_t mPortIndex;
Stick mStick;
StickIndex mStickIndex;

uint8_t mSensitivityPercentage;
float mSensitivity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

namespace Ship {
ControllerAxisDirectionMapping::ControllerAxisDirectionMapping(ShipDeviceIndex shipDeviceIndex, uint8_t portIndex,
Stick stick, Direction direction)
: ControllerInputMapping(shipDeviceIndex), mPortIndex(portIndex), mStick(stick), mDirection(direction) {
StickIndex stickIndex, Direction direction)
: ControllerInputMapping(shipDeviceIndex), mPortIndex(portIndex), mStickIndex(stickIndex), mDirection(direction) {
}

ControllerAxisDirectionMapping::~ControllerAxisDirectionMapping() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@
#define MAX_AXIS_RANGE 85.0f

namespace Ship {
enum Stick { LEFT_STICK, RIGHT_STICK };
enum StickIndex { LEFT_STICK, RIGHT_STICK };
enum Direction { LEFT, RIGHT, UP, DOWN };

class ControllerAxisDirectionMapping : virtual public ControllerInputMapping {
public:
ControllerAxisDirectionMapping(ShipDeviceIndex shipDeviceIndex, uint8_t portIndex, Stick stick,
ControllerAxisDirectionMapping(ShipDeviceIndex shipDeviceIndex, uint8_t portIndex, StickIndex stickIndex,
Direction direction);
~ControllerAxisDirectionMapping();
virtual float GetNormalizedAxisDirectionValue() = 0;
Expand All @@ -28,7 +28,7 @@ class ControllerAxisDirectionMapping : virtual public ControllerInputMapping {

protected:
uint8_t mPortIndex;
Stick mStick;
StickIndex mStickIndex;
Direction mDirection;
};
} // namespace Ship
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@

namespace Ship {
std::shared_ptr<ControllerAxisDirectionMapping>
AxisDirectionMappingFactory::CreateAxisDirectionMappingFromConfig(uint8_t portIndex, Stick stick, std::string id) {
AxisDirectionMappingFactory::CreateAxisDirectionMappingFromConfig(uint8_t portIndex, StickIndex stickIndex,
std::string id) {
const std::string mappingCvarKey = CVAR_PREFIX_CONTROLLERS ".AxisDirectionMappings." + id;
const std::string mappingClass =
CVarGetString(StringHelper::Sprintf("%s.AxisDirectionMappingClass", mappingCvarKey.c_str()).c_str(), "");
Expand All @@ -34,7 +35,7 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromConfig(uint8_t portIn
}

return std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(
static_cast<ShipDeviceIndex>(shipDeviceIndex), portIndex, stick, static_cast<Direction>(direction),
static_cast<ShipDeviceIndex>(shipDeviceIndex), portIndex, stickIndex, static_cast<Direction>(direction),
sdlControllerAxis, axisDirection);
}

Expand All @@ -53,9 +54,9 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromConfig(uint8_t portIn
return nullptr;
}

return std::make_shared<SDLButtonToAxisDirectionMapping>(static_cast<ShipDeviceIndex>(shipDeviceIndex),
portIndex, stick, static_cast<Direction>(direction),
sdlControllerButton);
return std::make_shared<SDLButtonToAxisDirectionMapping>(
static_cast<ShipDeviceIndex>(shipDeviceIndex), portIndex, stickIndex, static_cast<Direction>(direction),
sdlControllerButton);
}

if (mappingClass == "KeyboardKeyToAxisDirectionMapping") {
Expand All @@ -70,28 +71,31 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromConfig(uint8_t portIn
return nullptr;
}

return std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stick, static_cast<Direction>(direction),
static_cast<KbScancode>(scancode));
return std::make_shared<KeyboardKeyToAxisDirectionMapping>(
portIndex, stickIndex, static_cast<Direction>(direction), static_cast<KbScancode>(scancode));
}

return nullptr;
}

std::vector<std::shared_ptr<ControllerAxisDirectionMapping>>
AxisDirectionMappingFactory::CreateDefaultKeyboardAxisDirectionMappings(uint8_t portIndex, Stick stick) {
AxisDirectionMappingFactory::CreateDefaultKeyboardAxisDirectionMappings(uint8_t portIndex, StickIndex stickIndex) {
std::vector<std::shared_ptr<ControllerAxisDirectionMapping>> mappings = {
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stick, LEFT, LUS_DEFAULT_KB_MAPPING_STICKLEFT),
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stick, RIGHT, LUS_DEFAULT_KB_MAPPING_STICKRIGHT),
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stick, UP, LUS_DEFAULT_KB_MAPPING_STICKUP),
std::make_shared<KeyboardKeyToAxisDirectionMapping>(portIndex, stick, DOWN, LUS_DEFAULT_KB_MAPPING_STICKDOWN)
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)
};

return mappings;
}

std::vector<std::shared_ptr<ControllerAxisDirectionMapping>>
AxisDirectionMappingFactory::CreateDefaultSDLAxisDirectionMappings(ShipDeviceIndex shipDeviceIndex, uint8_t portIndex,
Stick stick) {
StickIndex stickIndex) {
auto sdlIndexMapping = std::dynamic_pointer_cast<ShipDeviceIndexToSDLDeviceIndexMapping>(
Context::GetInstance()
->GetControlDeck()
Expand All @@ -102,21 +106,21 @@ AxisDirectionMappingFactory::CreateDefaultSDLAxisDirectionMappings(ShipDeviceInd
}

std::vector<std::shared_ptr<ControllerAxisDirectionMapping>> mappings = {
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(shipDeviceIndex, portIndex, stick, LEFT,
stick == LEFT_STICK ? 0 : 2, -1),
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(shipDeviceIndex, portIndex, stick, RIGHT,
stick == LEFT_STICK ? 0 : 2, 1),
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(shipDeviceIndex, portIndex, stick, UP,
stick == LEFT_STICK ? 1 : 3, -1),
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(shipDeviceIndex, portIndex, stick, DOWN,
stick == LEFT_STICK ? 1 : 3, 1)
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(shipDeviceIndex, portIndex, stickIndex, LEFT,
stickIndex == LEFT_STICK ? 0 : 2, -1),
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(shipDeviceIndex, portIndex, stickIndex, RIGHT,
stickIndex == LEFT_STICK ? 0 : 2, 1),
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(shipDeviceIndex, portIndex, stickIndex, UP,
stickIndex == LEFT_STICK ? 1 : 3, -1),
std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(shipDeviceIndex, portIndex, stickIndex, DOWN,
stickIndex == LEFT_STICK ? 1 : 3, 1)
};

return mappings;
}

std::shared_ptr<ControllerAxisDirectionMapping>
AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t portIndex, Stick stick,
AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t portIndex, StickIndex stickIndex,
Direction direction) {
std::unordered_map<ShipDeviceIndex, SDL_GameController*> sdlControllers;
std::shared_ptr<ControllerAxisDirectionMapping> mapping = nullptr;
Expand All @@ -142,8 +146,8 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t port
for (auto [lusIndex, controller] : sdlControllers) {
for (int32_t button = SDL_CONTROLLER_BUTTON_A; button < SDL_CONTROLLER_BUTTON_MAX; button++) {
if (SDL_GameControllerGetButton(controller, static_cast<SDL_GameControllerButton>(button))) {
mapping =
std::make_shared<SDLButtonToAxisDirectionMapping>(lusIndex, portIndex, stick, direction, button);
mapping = std::make_shared<SDLButtonToAxisDirectionMapping>(lusIndex, portIndex, stickIndex, direction,
button);
break;
}
}
Expand All @@ -166,8 +170,8 @@ AxisDirectionMappingFactory::CreateAxisDirectionMappingFromSDLInput(uint8_t port
continue;
}

mapping = std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(lusIndex, portIndex, stick, direction,
axis, axisDirection);
mapping = std::make_shared<SDLAxisDirectionToAxisDirectionMapping>(lusIndex, portIndex, stickIndex,
direction, axis, axisDirection);
break;
}
}
Expand Down
Loading

0 comments on commit efcf668

Please sign in to comment.