Skip to content

Commit 82a3406

Browse files
authored
simplify controllers (Kenix3#793)
* 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
1 parent ac37dfb commit 82a3406

File tree

90 files changed

+735
-2807
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+735
-2807
lines changed

src/controller/controldeck/ControlDeck.cpp

+11-16
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
#include "utils/StringHelper.h"
66
#include "public/bridge/consolevariablebridge.h"
77
#include <imgui.h>
8-
#include "controller/deviceindex/ShipDeviceIndexMappingManager.h"
98
#include "controller/controldevice/controller/mapping/mouse/WheelHandler.h"
109

1110
namespace Ship {
1211

13-
ControlDeck::ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks) : mSinglePlayerMappingMode(false) {
14-
mDeviceIndexMappingManager = std::make_shared<ShipDeviceIndexMappingManager>();
12+
ControlDeck::ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks) {
13+
mConnectedPhysicalDeviceManager = std::make_shared<ConnectedPhysicalDeviceManager>();
14+
mGlobalSDLDeviceSettings = std::make_shared<GlobalSDLDeviceSettings>();
1515
}
1616

1717
ControlDeck::~ControlDeck() {
@@ -28,13 +28,12 @@ void ControlDeck::Init(uint8_t* controllerBits) {
2828
}
2929
}
3030

31-
// if we don't have a config for controller 1, set default keyboard bindings
31+
// if we don't have a config for controller 1, set default bindings
3232
if (!mPorts[0]->GetConnectedController()->HasConfig()) {
33-
mPorts[0]->GetConnectedController()->AddDefaultMappings(ShipDeviceIndex::Keyboard);
34-
mPorts[0]->GetConnectedController()->AddDefaultMappings(ShipDeviceIndex::Mouse);
33+
mPorts[0]->GetConnectedController()->AddDefaultMappings(PhysicalDeviceType::Keyboard);
34+
mPorts[0]->GetConnectedController()->AddDefaultMappings(PhysicalDeviceType::Mouse);
35+
mPorts[0]->GetConnectedController()->AddDefaultMappings(PhysicalDeviceType::SDLGamepad);
3536
}
36-
37-
Context::GetInstance()->GetWindow()->GetGui()->GetGuiWindow("Controller Reordering")->Show();
3837
}
3938

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

107-
std::shared_ptr<ShipDeviceIndexMappingManager> ControlDeck::GetDeviceIndexMappingManager() {
108-
return mDeviceIndexMappingManager;
109-
}
110-
111-
void ControlDeck::SetSinglePlayerMappingMode(bool singlePlayer) {
112-
mSinglePlayerMappingMode = singlePlayer;
106+
std::shared_ptr<ConnectedPhysicalDeviceManager> ControlDeck::GetConnectedPhysicalDeviceManager() {
107+
return mConnectedPhysicalDeviceManager;
113108
}
114109

115-
bool ControlDeck::IsSinglePlayerMappingMode() {
116-
return mSinglePlayerMappingMode;
110+
std::shared_ptr<GlobalSDLDeviceSettings> ControlDeck::GetGlobalSDLDeviceSettings() {
111+
return mGlobalSDLDeviceSettings;
117112
}
118113
} // namespace Ship
119114

src/controller/controldeck/ControlDeck.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
#include <vector>
55
#include <config/Config.h>
66
#include "controller/controldevice/controller/mapping/keyboard/KeyboardScancodes.h"
7-
#include "controller/deviceindex/ShipDeviceIndexMappingManager.h"
7+
#include "controller/physicaldevice/ConnectedPhysicalDeviceManager.h"
8+
#include "controller/physicaldevice/GlobalSDLDeviceSettings.h"
89

910
namespace Ship {
1011

@@ -22,22 +23,21 @@ class ControlDeck {
2223
bool GamepadGameInputBlocked();
2324
bool KeyboardGameInputBlocked();
2425
bool MouseGameInputBlocked();
25-
void SetSinglePlayerMappingMode(bool singlePlayer);
26-
bool IsSinglePlayerMappingMode();
2726
bool ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode);
2827
bool ProcessMouseButtonEvent(bool isPressed, MouseBtn button);
2928

30-
std::shared_ptr<ShipDeviceIndexMappingManager> GetDeviceIndexMappingManager();
29+
std::shared_ptr<ConnectedPhysicalDeviceManager> GetConnectedPhysicalDeviceManager();
30+
std::shared_ptr<GlobalSDLDeviceSettings> GetGlobalSDLDeviceSettings();
3131

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

3636
private:
3737
uint8_t* mControllerBits = nullptr;
38-
bool mSinglePlayerMappingMode;
3938
std::unordered_map<int32_t, bool> mGameInputBlockers;
40-
std::shared_ptr<ShipDeviceIndexMappingManager> mDeviceIndexMappingManager;
39+
std::shared_ptr<ConnectedPhysicalDeviceManager> mConnectedPhysicalDeviceManager;
40+
std::shared_ptr<GlobalSDLDeviceSettings> mGlobalSDLDeviceSettings;
4141
};
4242
} // namespace Ship
4343

src/controller/controldevice/controller/Controller.cpp

+18-108
Original file line numberDiff line numberDiff line change
@@ -86,28 +86,28 @@ void Controller::ClearAllMappings() {
8686
GetLED()->ClearAllMappings();
8787
}
8888

89-
void Controller::ClearAllMappingsForDevice(ShipDeviceIndex shipDeviceIndex) {
89+
void Controller::ClearAllMappingsForDeviceType(PhysicalDeviceType physicalDeviceType) {
9090
for (auto [bitmask, button] : GetAllButtons()) {
91-
button->ClearAllButtonMappingsForDevice(shipDeviceIndex);
91+
button->ClearAllButtonMappingsForDeviceType(physicalDeviceType);
9292
}
93-
GetLeftStick()->ClearAllMappingsForDevice(shipDeviceIndex);
94-
GetRightStick()->ClearAllMappingsForDevice(shipDeviceIndex);
93+
GetLeftStick()->ClearAllMappingsForDeviceType(physicalDeviceType);
94+
GetRightStick()->ClearAllMappingsForDeviceType(physicalDeviceType);
9595

9696
auto gyroMapping = GetGyro()->GetGyroMapping();
97-
if (gyroMapping != nullptr && gyroMapping->GetShipDeviceIndex() == shipDeviceIndex) {
97+
if (gyroMapping != nullptr && gyroMapping->GetPhysicalDeviceType() == physicalDeviceType) {
9898
GetGyro()->ClearGyroMapping();
9999
}
100100

101-
GetRumble()->ClearAllMappingsForDevice(shipDeviceIndex);
102-
GetLED()->ClearAllMappingsForDevice(shipDeviceIndex);
101+
GetRumble()->ClearAllMappingsForDeviceType(physicalDeviceType);
102+
GetLED()->ClearAllMappingsForDeviceType(physicalDeviceType);
103103
}
104104

105-
void Controller::AddDefaultMappings(ShipDeviceIndex shipDeviceIndex) {
105+
void Controller::AddDefaultMappings(PhysicalDeviceType physicalDeviceType) {
106106
for (auto [bitmask, button] : GetAllButtons()) {
107-
button->AddDefaultMappings(shipDeviceIndex);
107+
button->AddDefaultMappings(physicalDeviceType);
108108
}
109-
GetLeftStick()->AddDefaultMappings(shipDeviceIndex);
110-
GetRumble()->AddDefaultMappings(shipDeviceIndex);
109+
GetLeftStick()->AddDefaultMappings(physicalDeviceType);
110+
GetRumble()->AddDefaultMappings(physicalDeviceType);
111111

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

149-
bool Controller::HasMappingsForShipDeviceIndex(ShipDeviceIndex lusIndex) {
149+
bool Controller::HasMappingsForPhysicalDeviceType(PhysicalDeviceType physicalDeviceType) {
150150
for (auto [bitmask, button] : GetAllButtons()) {
151-
if (button->HasMappingsForShipDeviceIndex(lusIndex)) {
151+
if (button->HasMappingsForPhysicalDeviceType(physicalDeviceType)) {
152152
return true;
153153
}
154154
}
155-
if (GetLeftStick()->HasMappingsForShipDeviceIndex(lusIndex)) {
155+
if (GetLeftStick()->HasMappingsForPhysicalDeviceType(physicalDeviceType)) {
156156
return true;
157157
}
158-
if (GetRightStick()->HasMappingsForShipDeviceIndex(lusIndex)) {
158+
if (GetRightStick()->HasMappingsForPhysicalDeviceType(physicalDeviceType)) {
159159
return true;
160160
}
161-
if (GetGyro()->HasMappingForShipDeviceIndex(lusIndex)) {
161+
if (GetGyro()->HasMappingForPhysicalDeviceType(physicalDeviceType)) {
162162
return true;
163163
}
164-
if (GetRumble()->HasMappingsForShipDeviceIndex(lusIndex)) {
164+
if (GetRumble()->HasMappingsForPhysicalDeviceType(physicalDeviceType)) {
165165
return true;
166166
}
167-
if (GetLED()->HasMappingsForShipDeviceIndex(lusIndex)) {
167+
if (GetLED()->HasMappingsForPhysicalDeviceType(physicalDeviceType)) {
168168
return true;
169169
}
170170

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

178-
void Controller::MoveMappingsToDifferentController(std::shared_ptr<Controller> newController,
179-
ShipDeviceIndex lusIndex) {
180-
for (auto [bitmask, button] : GetAllButtons()) {
181-
std::vector<std::string> buttonMappingIdsToRemove;
182-
for (auto [id, mapping] : button->GetAllButtonMappings()) {
183-
if (mapping->GetShipDeviceIndex() == lusIndex) {
184-
buttonMappingIdsToRemove.push_back(id);
185-
186-
mapping->SetPortIndex(newController->GetPortIndex());
187-
mapping->SaveToConfig();
188-
189-
newController->GetButtonByBitmask(bitmask)->AddButtonMapping(mapping);
190-
}
191-
}
192-
newController->GetButtonByBitmask(bitmask)->SaveButtonMappingIdsToConfig();
193-
for (auto id : buttonMappingIdsToRemove) {
194-
button->ClearButtonMappingId(id);
195-
}
196-
}
197-
198-
for (auto stick : { GetLeftStick(), GetRightStick() }) {
199-
auto newControllerStick =
200-
stick->GetStickIndex() == LEFT_STICK ? newController->GetLeftStick() : newController->GetRightStick();
201-
for (auto [direction, mappings] : stick->GetAllAxisDirectionMappings()) {
202-
std::vector<std::string> axisDirectionMappingIdsToRemove;
203-
for (auto [id, mapping] : mappings) {
204-
if (mapping->GetShipDeviceIndex() == lusIndex) {
205-
axisDirectionMappingIdsToRemove.push_back(id);
206-
207-
mapping->SetPortIndex(newController->GetPortIndex());
208-
mapping->SaveToConfig();
209-
210-
newControllerStick->AddAxisDirectionMapping(direction, mapping);
211-
}
212-
}
213-
newControllerStick->SaveAxisDirectionMappingIdsToConfig();
214-
for (auto id : axisDirectionMappingIdsToRemove) {
215-
stick->ClearAxisDirectionMappingId(direction, id);
216-
}
217-
}
218-
}
219-
220-
if (GetGyro()->GetGyroMapping() != nullptr && GetGyro()->GetGyroMapping()->GetShipDeviceIndex() == lusIndex) {
221-
GetGyro()->GetGyroMapping()->SetPortIndex(newController->GetPortIndex());
222-
GetGyro()->GetGyroMapping()->SaveToConfig();
223-
224-
auto oldGyroMappingFromNewController = newController->GetGyro()->GetGyroMapping();
225-
if (oldGyroMappingFromNewController != nullptr) {
226-
oldGyroMappingFromNewController->SetPortIndex(GetPortIndex());
227-
oldGyroMappingFromNewController->SaveToConfig();
228-
}
229-
newController->GetGyro()->SetGyroMapping(GetGyro()->GetGyroMapping());
230-
newController->GetGyro()->SaveGyroMappingIdToConfig();
231-
GetGyro()->SetGyroMapping(oldGyroMappingFromNewController);
232-
GetGyro()->SaveGyroMappingIdToConfig();
233-
}
234-
235-
std::vector<std::string> rumbleMappingIdsToRemove;
236-
for (auto [id, mapping] : GetRumble()->GetAllRumbleMappings()) {
237-
if (mapping->GetShipDeviceIndex() == lusIndex) {
238-
rumbleMappingIdsToRemove.push_back(id);
239-
240-
mapping->SetPortIndex(newController->GetPortIndex());
241-
mapping->SaveToConfig();
242-
243-
newController->GetRumble()->AddRumbleMapping(mapping);
244-
}
245-
}
246-
newController->GetRumble()->SaveRumbleMappingIdsToConfig();
247-
for (auto id : rumbleMappingIdsToRemove) {
248-
GetRumble()->ClearRumbleMappingId(id);
249-
}
250-
251-
std::vector<std::string> ledMappingIdsToRemove;
252-
for (auto [id, mapping] : GetLED()->GetAllLEDMappings()) {
253-
if (mapping->GetShipDeviceIndex() == lusIndex) {
254-
ledMappingIdsToRemove.push_back(id);
255-
256-
mapping->SetPortIndex(newController->GetPortIndex());
257-
mapping->SaveToConfig();
258-
259-
newController->GetLED()->AddLEDMapping(mapping);
260-
}
261-
}
262-
newController->GetLED()->SaveLEDMappingIdsToConfig();
263-
for (auto id : ledMappingIdsToRemove) {
264-
GetLED()->ClearLEDMappingId(id);
265-
}
266-
}
267-
268178
std::vector<std::shared_ptr<ControllerMapping>> Controller::GetAllMappings() {
269179
std::vector<std::shared_ptr<ControllerMapping>> allMappings;
270180
for (auto [bitmask, button] : GetAllButtons()) {

src/controller/controldevice/controller/Controller.h

+3-4
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class Controller : public ControlDevice {
3333
void Disconnect();
3434

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

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

5756
protected:
5857
std::unordered_map<CONTROLLERBUTTONS_T, std::shared_ptr<ControllerButton>> mButtons;

src/controller/controldevice/controller/ControllerButton.cpp

+9-8
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,10 @@ void ControllerButton::ClearAllButtonMappings() {
146146
SaveButtonMappingIdsToConfig();
147147
}
148148

149-
void ControllerButton::ClearAllButtonMappingsForDevice(ShipDeviceIndex shipDeviceIndex) {
149+
void ControllerButton::ClearAllButtonMappingsForDeviceType(PhysicalDeviceType physicalDeviceType) {
150150
std::vector<std::string> mappingIdsToRemove;
151151
for (auto [id, mapping] : mButtonMappings) {
152-
if (mapping->GetShipDeviceIndex() == shipDeviceIndex) {
152+
if (mapping->GetPhysicalDeviceType() == physicalDeviceType) {
153153
mapping->EraseFromConfig();
154154
mappingIdsToRemove.push_back(id);
155155
}
@@ -170,9 +170,10 @@ void ControllerButton::UpdatePad(CONTROLLERBUTTONS_T& padButtons) {
170170
}
171171
}
172172

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

178179
bool ControllerButton::AddOrEditButtonMappingFromRawPress(CONTROLLERBUTTONS_T bitmask, std::string id) {
@@ -264,12 +265,12 @@ bool ControllerButton::ProcessMouseButtonEvent(bool isPressed, MouseBtn button)
264265
return result;
265266
}
266267

267-
void ControllerButton::AddDefaultMappings(ShipDeviceIndex shipDeviceIndex) {
268-
for (auto mapping : ButtonMappingFactory::CreateDefaultSDLButtonMappings(shipDeviceIndex, mPortIndex, mBitmask)) {
268+
void ControllerButton::AddDefaultMappings(PhysicalDeviceType physicalDeviceType) {
269+
for (auto mapping : ButtonMappingFactory::CreateDefaultSDLButtonMappings(mPortIndex, mBitmask)) {
269270
AddButtonMapping(mapping);
270271
}
271272

272-
if (shipDeviceIndex == ShipDeviceIndex::Keyboard) {
273+
if (physicalDeviceType == PhysicalDeviceType::Keyboard) {
273274
for (auto mapping : ButtonMappingFactory::CreateDefaultKeyboardButtonMappings(mPortIndex, mBitmask)) {
274275
AddButtonMapping(mapping);
275276
}

src/controller/controldevice/controller/ControllerButton.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ class ControllerButton {
2424
void ClearButtonMappingId(std::string id);
2525
void ClearButtonMapping(std::string id);
2626
void ClearButtonMapping(std::shared_ptr<ControllerButtonMapping> mapping);
27-
void AddDefaultMappings(ShipDeviceIndex shipDeviceIndex);
27+
void AddDefaultMappings(PhysicalDeviceType physicalDeviceType);
2828

2929
void LoadButtonMappingFromConfig(std::string id);
3030
void SaveButtonMappingIdsToConfig();
3131
void ReloadAllMappingsFromConfig();
3232
void ClearAllButtonMappings();
33-
void ClearAllButtonMappingsForDevice(ShipDeviceIndex shipDeviceIndex);
33+
void ClearAllButtonMappingsForDeviceType(PhysicalDeviceType physicalDeviceType);
3434

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

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

42-
bool HasMappingsForShipDeviceIndex(ShipDeviceIndex lusIndex);
42+
bool HasMappingsForPhysicalDeviceType(PhysicalDeviceType physicalDeviceType);
4343

4444
private:
4545
uint8_t mPortIndex;

src/controller/controldevice/controller/ControllerGyro.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ void ControllerGyro::ReloadGyroMappingFromConfig() {
8484
SaveGyroMappingIdToConfig();
8585
}
8686

87-
bool ControllerGyro::HasMappingForShipDeviceIndex(ShipDeviceIndex lusIndex) {
87+
bool ControllerGyro::HasMappingForPhysicalDeviceType(PhysicalDeviceType physicalDeviceType) {
8888
if (mGyroMapping == nullptr) {
8989
return false;
9090
}
9191

92-
return mGyroMapping->GetShipDeviceIndex() == lusIndex;
92+
return mGyroMapping->GetPhysicalDeviceType() == physicalDeviceType;
9393
}
9494
} // namespace Ship

src/controller/controldevice/controller/ControllerGyro.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ class ControllerGyro {
1111
ControllerGyro(uint8_t portIndex);
1212
~ControllerGyro();
1313

14-
// void AddOrReplaceGyroMapping(std::shared_ptr<ControllerGyroMapping> mapping);
1514
void ReloadGyroMappingFromConfig();
1615
void ClearGyroMapping();
1716
void SaveGyroMappingIdToConfig();
@@ -22,7 +21,7 @@ class ControllerGyro {
2221

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

25-
bool HasMappingForShipDeviceIndex(ShipDeviceIndex lusIndex);
24+
bool HasMappingForPhysicalDeviceType(PhysicalDeviceType physicalDeviceType);
2625

2726
private:
2827
uint8_t mPortIndex;

0 commit comments

Comments
 (0)