Skip to content

Commit

Permalink
simplify controllers (#793)
Browse files Browse the repository at this point in the history
* start moving towards just using `ShipDeviceIndex::SDLGamepad`

* rename `ShipDeviceIndex` to `ShipDeviceType`

* all sdl mappings are always `ShipDeviceType::SDLGamepad`

* clang format

* create new `physicaldevice` directory where the new stuff can live

* `ConnectedPhysicalDeviceManager`

* start to not need index mapping manager

* fix mapping axis from input

* fix mapping buttons from input

* fix default axis direction mappings

* remove more shipdeviceindex stuff

* remove index from mapping ids

* clang format

* found a spot for axis thresholds

* connected device name list

* bring over editor window changes from soh

* finally remove the device index dir

* led

* clang format

* rumble

* clang format

* gyro

* clang format

* tiny cleanup

* super tiny cleanup

* some naming and cleanup

* clang format

* remove single player mapping mode references

* basic filtering

* bring over stuff from soh input editor
  • Loading branch information
briaguya-ai authored Jan 20, 2025
1 parent ac37dfb commit 82a3406
Show file tree
Hide file tree
Showing 90 changed files with 735 additions and 2,807 deletions.
27 changes: 11 additions & 16 deletions src/controller/controldeck/ControlDeck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
#include "utils/StringHelper.h"
#include "public/bridge/consolevariablebridge.h"
#include <imgui.h>
#include "controller/deviceindex/ShipDeviceIndexMappingManager.h"
#include "controller/controldevice/controller/mapping/mouse/WheelHandler.h"

namespace Ship {

ControlDeck::ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks) : mSinglePlayerMappingMode(false) {
mDeviceIndexMappingManager = std::make_shared<ShipDeviceIndexMappingManager>();
ControlDeck::ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks) {
mConnectedPhysicalDeviceManager = std::make_shared<ConnectedPhysicalDeviceManager>();
mGlobalSDLDeviceSettings = std::make_shared<GlobalSDLDeviceSettings>();
}

ControlDeck::~ControlDeck() {
Expand All @@ -28,13 +28,12 @@ void ControlDeck::Init(uint8_t* controllerBits) {
}
}

// if we don't have a config for controller 1, set default keyboard bindings
// if we don't have a config for controller 1, set default bindings
if (!mPorts[0]->GetConnectedController()->HasConfig()) {
mPorts[0]->GetConnectedController()->AddDefaultMappings(ShipDeviceIndex::Keyboard);
mPorts[0]->GetConnectedController()->AddDefaultMappings(ShipDeviceIndex::Mouse);
mPorts[0]->GetConnectedController()->AddDefaultMappings(PhysicalDeviceType::Keyboard);
mPorts[0]->GetConnectedController()->AddDefaultMappings(PhysicalDeviceType::Mouse);
mPorts[0]->GetConnectedController()->AddDefaultMappings(PhysicalDeviceType::SDLGamepad);
}

Context::GetInstance()->GetWindow()->GetGui()->GetGuiWindow("Controller Reordering")->Show();
}

bool ControlDeck::ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode) {
Expand Down Expand Up @@ -104,16 +103,12 @@ void ControlDeck::UnblockGameInput(int32_t blockId) {
mGameInputBlockers.erase(blockId);
}

std::shared_ptr<ShipDeviceIndexMappingManager> ControlDeck::GetDeviceIndexMappingManager() {
return mDeviceIndexMappingManager;
}

void ControlDeck::SetSinglePlayerMappingMode(bool singlePlayer) {
mSinglePlayerMappingMode = singlePlayer;
std::shared_ptr<ConnectedPhysicalDeviceManager> ControlDeck::GetConnectedPhysicalDeviceManager() {
return mConnectedPhysicalDeviceManager;
}

bool ControlDeck::IsSinglePlayerMappingMode() {
return mSinglePlayerMappingMode;
std::shared_ptr<GlobalSDLDeviceSettings> ControlDeck::GetGlobalSDLDeviceSettings() {
return mGlobalSDLDeviceSettings;
}
} // namespace Ship

Expand Down
12 changes: 6 additions & 6 deletions src/controller/controldeck/ControlDeck.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
#include <vector>
#include <config/Config.h>
#include "controller/controldevice/controller/mapping/keyboard/KeyboardScancodes.h"
#include "controller/deviceindex/ShipDeviceIndexMappingManager.h"
#include "controller/physicaldevice/ConnectedPhysicalDeviceManager.h"
#include "controller/physicaldevice/GlobalSDLDeviceSettings.h"

namespace Ship {

Expand All @@ -22,22 +23,21 @@ class ControlDeck {
bool GamepadGameInputBlocked();
bool KeyboardGameInputBlocked();
bool MouseGameInputBlocked();
void SetSinglePlayerMappingMode(bool singlePlayer);
bool IsSinglePlayerMappingMode();
bool ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode);
bool ProcessMouseButtonEvent(bool isPressed, MouseBtn button);

std::shared_ptr<ShipDeviceIndexMappingManager> GetDeviceIndexMappingManager();
std::shared_ptr<ConnectedPhysicalDeviceManager> GetConnectedPhysicalDeviceManager();
std::shared_ptr<GlobalSDLDeviceSettings> GetGlobalSDLDeviceSettings();

protected:
bool AllGameInputBlocked();
std::vector<std::shared_ptr<ControlPort>> mPorts = {};

private:
uint8_t* mControllerBits = nullptr;
bool mSinglePlayerMappingMode;
std::unordered_map<int32_t, bool> mGameInputBlockers;
std::shared_ptr<ShipDeviceIndexMappingManager> mDeviceIndexMappingManager;
std::shared_ptr<ConnectedPhysicalDeviceManager> mConnectedPhysicalDeviceManager;
std::shared_ptr<GlobalSDLDeviceSettings> mGlobalSDLDeviceSettings;
};
} // namespace Ship

Expand Down
126 changes: 18 additions & 108 deletions src/controller/controldevice/controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,28 +86,28 @@ void Controller::ClearAllMappings() {
GetLED()->ClearAllMappings();
}

void Controller::ClearAllMappingsForDevice(ShipDeviceIndex shipDeviceIndex) {
void Controller::ClearAllMappingsForDeviceType(PhysicalDeviceType physicalDeviceType) {
for (auto [bitmask, button] : GetAllButtons()) {
button->ClearAllButtonMappingsForDevice(shipDeviceIndex);
button->ClearAllButtonMappingsForDeviceType(physicalDeviceType);
}
GetLeftStick()->ClearAllMappingsForDevice(shipDeviceIndex);
GetRightStick()->ClearAllMappingsForDevice(shipDeviceIndex);
GetLeftStick()->ClearAllMappingsForDeviceType(physicalDeviceType);
GetRightStick()->ClearAllMappingsForDeviceType(physicalDeviceType);

auto gyroMapping = GetGyro()->GetGyroMapping();
if (gyroMapping != nullptr && gyroMapping->GetShipDeviceIndex() == shipDeviceIndex) {
if (gyroMapping != nullptr && gyroMapping->GetPhysicalDeviceType() == physicalDeviceType) {
GetGyro()->ClearGyroMapping();
}

GetRumble()->ClearAllMappingsForDevice(shipDeviceIndex);
GetLED()->ClearAllMappingsForDevice(shipDeviceIndex);
GetRumble()->ClearAllMappingsForDeviceType(physicalDeviceType);
GetLED()->ClearAllMappingsForDeviceType(physicalDeviceType);
}

void Controller::AddDefaultMappings(ShipDeviceIndex shipDeviceIndex) {
void Controller::AddDefaultMappings(PhysicalDeviceType physicalDeviceType) {
for (auto [bitmask, button] : GetAllButtons()) {
button->AddDefaultMappings(shipDeviceIndex);
button->AddDefaultMappings(physicalDeviceType);
}
GetLeftStick()->AddDefaultMappings(shipDeviceIndex);
GetRumble()->AddDefaultMappings(shipDeviceIndex);
GetLeftStick()->AddDefaultMappings(physicalDeviceType);
GetRumble()->AddDefaultMappings(physicalDeviceType);

const std::string hasConfigCvarKey =
StringHelper::Sprintf(CVAR_PREFIX_CONTROLLERS ".Port%d.HasConfig", mPortIndex + 1);
Expand Down Expand Up @@ -146,25 +146,25 @@ bool Controller::ProcessMouseButtonEvent(bool isPressed, MouseBtn mouseButton) {
return result;
}

bool Controller::HasMappingsForShipDeviceIndex(ShipDeviceIndex lusIndex) {
bool Controller::HasMappingsForPhysicalDeviceType(PhysicalDeviceType physicalDeviceType) {
for (auto [bitmask, button] : GetAllButtons()) {
if (button->HasMappingsForShipDeviceIndex(lusIndex)) {
if (button->HasMappingsForPhysicalDeviceType(physicalDeviceType)) {
return true;
}
}
if (GetLeftStick()->HasMappingsForShipDeviceIndex(lusIndex)) {
if (GetLeftStick()->HasMappingsForPhysicalDeviceType(physicalDeviceType)) {
return true;
}
if (GetRightStick()->HasMappingsForShipDeviceIndex(lusIndex)) {
if (GetRightStick()->HasMappingsForPhysicalDeviceType(physicalDeviceType)) {
return true;
}
if (GetGyro()->HasMappingForShipDeviceIndex(lusIndex)) {
if (GetGyro()->HasMappingForPhysicalDeviceType(physicalDeviceType)) {
return true;
}
if (GetRumble()->HasMappingsForShipDeviceIndex(lusIndex)) {
if (GetRumble()->HasMappingsForPhysicalDeviceType(physicalDeviceType)) {
return true;
}
if (GetLED()->HasMappingsForShipDeviceIndex(lusIndex)) {
if (GetLED()->HasMappingsForPhysicalDeviceType(physicalDeviceType)) {
return true;
}

Expand All @@ -175,96 +175,6 @@ std::shared_ptr<ControllerButton> Controller::GetButtonByBitmask(CONTROLLERBUTTO
return mButtons[bitmask];
}

void Controller::MoveMappingsToDifferentController(std::shared_ptr<Controller> newController,
ShipDeviceIndex lusIndex) {
for (auto [bitmask, button] : GetAllButtons()) {
std::vector<std::string> buttonMappingIdsToRemove;
for (auto [id, mapping] : button->GetAllButtonMappings()) {
if (mapping->GetShipDeviceIndex() == lusIndex) {
buttonMappingIdsToRemove.push_back(id);

mapping->SetPortIndex(newController->GetPortIndex());
mapping->SaveToConfig();

newController->GetButtonByBitmask(bitmask)->AddButtonMapping(mapping);
}
}
newController->GetButtonByBitmask(bitmask)->SaveButtonMappingIdsToConfig();
for (auto id : buttonMappingIdsToRemove) {
button->ClearButtonMappingId(id);
}
}

for (auto stick : { GetLeftStick(), GetRightStick() }) {
auto newControllerStick =
stick->GetStickIndex() == LEFT_STICK ? newController->GetLeftStick() : newController->GetRightStick();
for (auto [direction, mappings] : stick->GetAllAxisDirectionMappings()) {
std::vector<std::string> axisDirectionMappingIdsToRemove;
for (auto [id, mapping] : mappings) {
if (mapping->GetShipDeviceIndex() == lusIndex) {
axisDirectionMappingIdsToRemove.push_back(id);

mapping->SetPortIndex(newController->GetPortIndex());
mapping->SaveToConfig();

newControllerStick->AddAxisDirectionMapping(direction, mapping);
}
}
newControllerStick->SaveAxisDirectionMappingIdsToConfig();
for (auto id : axisDirectionMappingIdsToRemove) {
stick->ClearAxisDirectionMappingId(direction, id);
}
}
}

if (GetGyro()->GetGyroMapping() != nullptr && GetGyro()->GetGyroMapping()->GetShipDeviceIndex() == lusIndex) {
GetGyro()->GetGyroMapping()->SetPortIndex(newController->GetPortIndex());
GetGyro()->GetGyroMapping()->SaveToConfig();

auto oldGyroMappingFromNewController = newController->GetGyro()->GetGyroMapping();
if (oldGyroMappingFromNewController != nullptr) {
oldGyroMappingFromNewController->SetPortIndex(GetPortIndex());
oldGyroMappingFromNewController->SaveToConfig();
}
newController->GetGyro()->SetGyroMapping(GetGyro()->GetGyroMapping());
newController->GetGyro()->SaveGyroMappingIdToConfig();
GetGyro()->SetGyroMapping(oldGyroMappingFromNewController);
GetGyro()->SaveGyroMappingIdToConfig();
}

std::vector<std::string> rumbleMappingIdsToRemove;
for (auto [id, mapping] : GetRumble()->GetAllRumbleMappings()) {
if (mapping->GetShipDeviceIndex() == lusIndex) {
rumbleMappingIdsToRemove.push_back(id);

mapping->SetPortIndex(newController->GetPortIndex());
mapping->SaveToConfig();

newController->GetRumble()->AddRumbleMapping(mapping);
}
}
newController->GetRumble()->SaveRumbleMappingIdsToConfig();
for (auto id : rumbleMappingIdsToRemove) {
GetRumble()->ClearRumbleMappingId(id);
}

std::vector<std::string> ledMappingIdsToRemove;
for (auto [id, mapping] : GetLED()->GetAllLEDMappings()) {
if (mapping->GetShipDeviceIndex() == lusIndex) {
ledMappingIdsToRemove.push_back(id);

mapping->SetPortIndex(newController->GetPortIndex());
mapping->SaveToConfig();

newController->GetLED()->AddLEDMapping(mapping);
}
}
newController->GetLED()->SaveLEDMappingIdsToConfig();
for (auto id : ledMappingIdsToRemove) {
GetLED()->ClearLEDMappingId(id);
}
}

std::vector<std::shared_ptr<ControllerMapping>> Controller::GetAllMappings() {
std::vector<std::shared_ptr<ControllerMapping>> allMappings;
for (auto [bitmask, button] : GetAllButtons()) {
Expand Down
7 changes: 3 additions & 4 deletions src/controller/controldevice/controller/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ class Controller : public ControlDevice {
void Disconnect();

void ClearAllMappings();
void ClearAllMappingsForDevice(ShipDeviceIndex shipDeviceIndex);
void AddDefaultMappings(ShipDeviceIndex shipDeviceIndex);
void ClearAllMappingsForDeviceType(PhysicalDeviceType physicalDeviceType);
void AddDefaultMappings(PhysicalDeviceType physicalDeviceType);
std::unordered_map<CONTROLLERBUTTONS_T, std::shared_ptr<ControllerButton>> GetAllButtons();
std::shared_ptr<ControllerButton> GetButtonByBitmask(CONTROLLERBUTTONS_T bitmask);
std::shared_ptr<ControllerButton> GetButton(CONTROLLERBUTTONS_T bitmask);
Expand All @@ -51,8 +51,7 @@ class Controller : public ControlDevice {
bool ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode);
bool ProcessMouseButtonEvent(bool isPressed, MouseBtn button);

bool HasMappingsForShipDeviceIndex(ShipDeviceIndex lusIndex);
void MoveMappingsToDifferentController(std::shared_ptr<Controller> newController, ShipDeviceIndex lusIndex);
bool HasMappingsForPhysicalDeviceType(PhysicalDeviceType physicalDeviceType);

protected:
std::unordered_map<CONTROLLERBUTTONS_T, std::shared_ptr<ControllerButton>> mButtons;
Expand Down
17 changes: 9 additions & 8 deletions src/controller/controldevice/controller/ControllerButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,10 @@ void ControllerButton::ClearAllButtonMappings() {
SaveButtonMappingIdsToConfig();
}

void ControllerButton::ClearAllButtonMappingsForDevice(ShipDeviceIndex shipDeviceIndex) {
void ControllerButton::ClearAllButtonMappingsForDeviceType(PhysicalDeviceType physicalDeviceType) {
std::vector<std::string> mappingIdsToRemove;
for (auto [id, mapping] : mButtonMappings) {
if (mapping->GetShipDeviceIndex() == shipDeviceIndex) {
if (mapping->GetPhysicalDeviceType() == physicalDeviceType) {
mapping->EraseFromConfig();
mappingIdsToRemove.push_back(id);
}
Expand All @@ -170,9 +170,10 @@ void ControllerButton::UpdatePad(CONTROLLERBUTTONS_T& padButtons) {
}
}

bool ControllerButton::HasMappingsForShipDeviceIndex(ShipDeviceIndex lusIndex) {
return std::any_of(mButtonMappings.begin(), mButtonMappings.end(),
[lusIndex](const auto& mapping) { return mapping.second->GetShipDeviceIndex() == lusIndex; });
bool ControllerButton::HasMappingsForPhysicalDeviceType(PhysicalDeviceType physicalDeviceType) {
return std::any_of(mButtonMappings.begin(), mButtonMappings.end(), [physicalDeviceType](const auto& mapping) {
return mapping.second->GetPhysicalDeviceType() == physicalDeviceType;
});
}

bool ControllerButton::AddOrEditButtonMappingFromRawPress(CONTROLLERBUTTONS_T bitmask, std::string id) {
Expand Down Expand Up @@ -264,12 +265,12 @@ bool ControllerButton::ProcessMouseButtonEvent(bool isPressed, MouseBtn button)
return result;
}

void ControllerButton::AddDefaultMappings(ShipDeviceIndex shipDeviceIndex) {
for (auto mapping : ButtonMappingFactory::CreateDefaultSDLButtonMappings(shipDeviceIndex, mPortIndex, mBitmask)) {
void ControllerButton::AddDefaultMappings(PhysicalDeviceType physicalDeviceType) {
for (auto mapping : ButtonMappingFactory::CreateDefaultSDLButtonMappings(mPortIndex, mBitmask)) {
AddButtonMapping(mapping);
}

if (shipDeviceIndex == ShipDeviceIndex::Keyboard) {
if (physicalDeviceType == PhysicalDeviceType::Keyboard) {
for (auto mapping : ButtonMappingFactory::CreateDefaultKeyboardButtonMappings(mPortIndex, mBitmask)) {
AddButtonMapping(mapping);
}
Expand Down
6 changes: 3 additions & 3 deletions src/controller/controldevice/controller/ControllerButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ class ControllerButton {
void ClearButtonMappingId(std::string id);
void ClearButtonMapping(std::string id);
void ClearButtonMapping(std::shared_ptr<ControllerButtonMapping> mapping);
void AddDefaultMappings(ShipDeviceIndex shipDeviceIndex);
void AddDefaultMappings(PhysicalDeviceType physicalDeviceType);

void LoadButtonMappingFromConfig(std::string id);
void SaveButtonMappingIdsToConfig();
void ReloadAllMappingsFromConfig();
void ClearAllButtonMappings();
void ClearAllButtonMappingsForDevice(ShipDeviceIndex shipDeviceIndex);
void ClearAllButtonMappingsForDeviceType(PhysicalDeviceType physicalDeviceType);

bool AddOrEditButtonMappingFromRawPress(CONTROLLERBUTTONS_T bitmask, std::string id);

Expand All @@ -39,7 +39,7 @@ class ControllerButton {
bool ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode);
bool ProcessMouseButtonEvent(bool isPressed, Ship::MouseBtn button);

bool HasMappingsForShipDeviceIndex(ShipDeviceIndex lusIndex);
bool HasMappingsForPhysicalDeviceType(PhysicalDeviceType physicalDeviceType);

private:
uint8_t mPortIndex;
Expand Down
4 changes: 2 additions & 2 deletions src/controller/controldevice/controller/ControllerGyro.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ void ControllerGyro::ReloadGyroMappingFromConfig() {
SaveGyroMappingIdToConfig();
}

bool ControllerGyro::HasMappingForShipDeviceIndex(ShipDeviceIndex lusIndex) {
bool ControllerGyro::HasMappingForPhysicalDeviceType(PhysicalDeviceType physicalDeviceType) {
if (mGyroMapping == nullptr) {
return false;
}

return mGyroMapping->GetShipDeviceIndex() == lusIndex;
return mGyroMapping->GetPhysicalDeviceType() == physicalDeviceType;
}
} // namespace Ship
3 changes: 1 addition & 2 deletions src/controller/controldevice/controller/ControllerGyro.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class ControllerGyro {
ControllerGyro(uint8_t portIndex);
~ControllerGyro();

// void AddOrReplaceGyroMapping(std::shared_ptr<ControllerGyroMapping> mapping);
void ReloadGyroMappingFromConfig();
void ClearGyroMapping();
void SaveGyroMappingIdToConfig();
Expand All @@ -22,7 +21,7 @@ class ControllerGyro {

void UpdatePad(float& x, float& y);

bool HasMappingForShipDeviceIndex(ShipDeviceIndex lusIndex);
bool HasMappingForPhysicalDeviceType(PhysicalDeviceType physicalDeviceType);

private:
uint8_t mPortIndex;
Expand Down
Loading

0 comments on commit 82a3406

Please sign in to comment.