Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify controllers #793

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
308fe81
start moving towards just using `ShipDeviceIndex::SDLGamepad`
briaguya-ai Jan 19, 2025
03fa9c1
rename `ShipDeviceIndex` to `ShipDeviceType`
briaguya-ai Jan 19, 2025
2bb7917
all sdl mappings are always `ShipDeviceType::SDLGamepad`
briaguya-ai Jan 19, 2025
1e1fe6d
clang format
briaguya-ai Jan 19, 2025
e7d28a5
create new `physicaldevice` directory where the new stuff can live
briaguya-ai Jan 19, 2025
7f0f02d
`ConnectedPhysicalDeviceManager`
briaguya-ai Jan 19, 2025
9be3448
start to not need index mapping manager
briaguya-ai Jan 19, 2025
618c77c
fix mapping axis from input
briaguya-ai Jan 19, 2025
81e2431
fix mapping buttons from input
briaguya-ai Jan 19, 2025
7c269f3
fix default axis direction mappings
briaguya-ai Jan 19, 2025
923239c
remove more shipdeviceindex stuff
briaguya-ai Jan 19, 2025
d674cf8
remove index from mapping ids
briaguya-ai Jan 19, 2025
e5babe3
clang format
briaguya-ai Jan 19, 2025
5dba443
found a spot for axis thresholds
briaguya-ai Jan 19, 2025
ab9ca34
connected device name list
briaguya-ai Jan 19, 2025
c8400d0
bring over editor window changes from soh
briaguya-ai Jan 19, 2025
b6a0b39
finally remove the device index dir
briaguya-ai Jan 19, 2025
7d7a2cb
led
briaguya-ai Jan 19, 2025
d14fdb7
clang format
briaguya-ai Jan 19, 2025
305320d
rumble
briaguya-ai Jan 19, 2025
4676b78
clang format
briaguya-ai Jan 19, 2025
a1806e0
gyro
briaguya-ai Jan 19, 2025
e5e1c92
clang format
briaguya-ai Jan 19, 2025
9def8a6
tiny cleanup
briaguya-ai Jan 19, 2025
f0c03ea
super tiny cleanup
briaguya-ai Jan 19, 2025
6162887
some naming and cleanup
briaguya-ai Jan 19, 2025
4f95a27
clang format
briaguya-ai Jan 19, 2025
5bbe727
remove single player mapping mode references
briaguya-ai Jan 19, 2025
96e7d20
basic filtering
briaguya-ai Jan 20, 2025
640ef65
bring over stuff from soh input editor
briaguya-ai Jan 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading