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

split ControlDeck between Ship:: and LUS:: #758

Merged
merged 15 commits into from
Dec 16, 2024
2 changes: 1 addition & 1 deletion src/Context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void Context::InitControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmask
return;
}

mControlDeck = std::make_shared<ControlDeck>(additionalBitmasks);
mControlDeck = std::make_shared<LUS::ControlDeck>(additionalBitmasks);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reason this is in draft

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm. What is the additional bitmasks doing again?

We could push this to the derived class. If we do that, we want some kind of internal overload able init function that's call from the main one.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't have a derived context

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant for the ControlDeck class.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even without additionalBitmasks we can't make an instance of an abstract class

error: invalid new-expression of abstract class type ‘Ship::ControlDeck’

}

void Context::InitCrashHandler() {
Expand Down
70 changes: 39 additions & 31 deletions src/controller/controldeck/ControlDeck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,10 @@

namespace Ship {

ControlDeck::ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks)
: mPads(nullptr), mSinglePlayerMappingMode(false) {
for (int32_t i = 0; i < MAXCONTROLLERS; i++) {
mPorts.push_back(std::make_shared<ControlPort>(i, std::make_shared<Controller>(i, additionalBitmasks)));
}

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

ControlDeck::ControlDeck() : ControlDeck(std::vector<CONTROLLERBUTTONS_T>()) {
}

ControlDeck::~ControlDeck() {
SPDLOG_TRACE("destruct control deck");
}
Expand Down Expand Up @@ -74,28 +66,6 @@ bool ControlDeck::KeyboardGameInputBlocked() {
return AllGameInputBlocked() || ImGui::GetIO().WantCaptureKeyboard;
}

void ControlDeck::WriteToPad(OSContPad* pad) {
SDL_PumpEvents();

if (AllGameInputBlocked()) {
return;
}

mPads = pad;

for (size_t i = 0; i < mPorts.size(); i++) {
const std::shared_ptr<Controller> controller = mPorts[i]->GetConnectedController();

if (controller != nullptr) {
controller->ReadToPad(&pad[i]);
}
}
}

OSContPad* ControlDeck::GetPads() {
return mPads;
}

std::shared_ptr<Controller> ControlDeck::GetControllerByPort(uint8_t port) {
return mPorts[port]->GetConnectedController();
}
Expand All @@ -120,3 +90,41 @@ bool ControlDeck::IsSinglePlayerMappingMode() {
return mSinglePlayerMappingMode;
}
} // namespace Ship

namespace LUS {
ControlDeck::ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks)
: Ship::ControlDeck(additionalBitmasks), mPads(nullptr) {
for (int32_t i = 0; i < MAXCONTROLLERS; i++) {
mPorts.push_back(std::make_shared<Ship::ControlPort>(i, std::make_shared<Controller>(i, additionalBitmasks)));
}
}

ControlDeck::ControlDeck() : ControlDeck(std::vector<CONTROLLERBUTTONS_T>()) {
}

OSContPad* ControlDeck::GetPads() {
return mPads;
}

void ControlDeck::WriteToPad(void* pad) {
WriteToOSContPad((OSContPad*)pad);
}

void ControlDeck::WriteToOSContPad(OSContPad* pad) {
SDL_PumpEvents();

if (AllGameInputBlocked()) {
return;
}

mPads = pad;

for (size_t i = 0; i < mPorts.size(); i++) {
const std::shared_ptr<Ship::Controller> controller = mPorts[i]->GetConnectedController();

if (controller != nullptr) {
controller->ReadToPad(&pad[i]);
}
}
}
} // namespace LUS
30 changes: 21 additions & 9 deletions src/controller/controldeck/ControlDeck.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ namespace Ship {

class ControlDeck {
public:
ControlDeck();
ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks);
~ControlDeck();

void Init(uint8_t* controllerBits);
void WriteToPad(OSContPad* pad);
OSContPad* GetPads();
virtual void WriteToPad(void* pads) = 0;
uint8_t* GetControllerBits();
std::shared_ptr<Controller> GetControllerByPort(uint8_t port);
void BlockGameInput(int32_t blockId);
Expand All @@ -25,19 +23,33 @@ class ControlDeck {
bool KeyboardGameInputBlocked();
void SetSinglePlayerMappingMode(bool singlePlayer);
bool IsSinglePlayerMappingMode();

bool ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode);

std::shared_ptr<ShipDeviceIndexMappingManager> GetDeviceIndexMappingManager();

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

private:
uint8_t* mControllerBits = nullptr;
OSContPad* mPads;
bool mSinglePlayerMappingMode;

bool AllGameInputBlocked();
std::unordered_map<int32_t, bool> mGameInputBlockers;
std::shared_ptr<ShipDeviceIndexMappingManager> mDeviceIndexMappingManager;
};
} // namespace Ship

namespace LUS {
class ControlDeck : public Ship::ControlDeck {
public:
ControlDeck();
ControlDeck(std::vector<CONTROLLERBUTTONS_T> additionalBitmasks);

OSContPad* GetPads();
void WriteToPad(void* pad) override;

private:
void WriteToOSContPad(OSContPad* pad);

OSContPad* mPads;
};
} // namespace LUS
111 changes: 62 additions & 49 deletions src/controller/controldevice/controller/Controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,55 +126,6 @@ void Controller::ReloadAllMappingsFromConfig() {
GetLED()->ReloadAllMappingsFromConfig();
}

void Controller::ReadToPad(OSContPad* pad) {
OSContPad padToBuffer = { 0 };

// Button Inputs
for (auto [bitmask, button] : mButtons) {
button->UpdatePad(padToBuffer.button);
}

// Stick Inputs
GetLeftStick()->UpdatePad(padToBuffer.stick_x, padToBuffer.stick_y);
GetRightStick()->UpdatePad(padToBuffer.right_stick_x, padToBuffer.right_stick_y);

// Gyro
GetGyro()->UpdatePad(padToBuffer.gyro_x, padToBuffer.gyro_y);

mPadBuffer.push_front(padToBuffer);
if (pad != nullptr) {
auto& padFromBuffer =
mPadBuffer[std::min(mPadBuffer.size() - 1, (size_t)CVarGetInteger(CVAR_SIMULATED_INPUT_LAG, 0))];

pad->button |= padFromBuffer.button;

if (pad->stick_x == 0) {
pad->stick_x = padFromBuffer.stick_x;
}
if (pad->stick_y == 0) {
pad->stick_y = padFromBuffer.stick_y;
}

if (pad->right_stick_x == 0) {
pad->right_stick_x = padFromBuffer.right_stick_x;
}
if (pad->right_stick_y == 0) {
pad->right_stick_y = padFromBuffer.right_stick_y;
}

if (pad->gyro_x == 0) {
pad->gyro_x = padFromBuffer.gyro_x;
}
if (pad->gyro_y == 0) {
pad->gyro_y = padFromBuffer.gyro_y;
}
}

while (mPadBuffer.size() > 6) {
mPadBuffer.pop_back();
}
}

bool Controller::ProcessKeyboardEvent(KbEventType eventType, KbScancode scancode) {
bool result = false;
for (auto [bitmask, button] : GetAllButtons()) {
Expand Down Expand Up @@ -333,3 +284,65 @@ std::vector<std::shared_ptr<ControllerMapping>> Controller::GetAllMappings() {
return allMappings;
}
} // namespace Ship

namespace LUS {
Controller::Controller(uint8_t portIndex, std::vector<CONTROLLERBUTTONS_T> additionalBitmasks)
: Ship::Controller(portIndex, additionalBitmasks) {
}

Controller::Controller(uint8_t portIndex) : Ship::Controller(portIndex, {}) {
}

void Controller::ReadToPad(void* pad) {
ReadToOSContPad((OSContPad*)pad);
}

void Controller::ReadToOSContPad(OSContPad* pad) {
OSContPad padToBuffer = { 0 };

// Button Inputs
for (auto [bitmask, button] : mButtons) {
button->UpdatePad(padToBuffer.button);
}

// Stick Inputs
GetLeftStick()->UpdatePad(padToBuffer.stick_x, padToBuffer.stick_y);
GetRightStick()->UpdatePad(padToBuffer.right_stick_x, padToBuffer.right_stick_y);

// Gyro
GetGyro()->UpdatePad(padToBuffer.gyro_x, padToBuffer.gyro_y);

mPadBuffer.push_front(padToBuffer);
if (pad != nullptr) {
auto& padFromBuffer =
mPadBuffer[std::min(mPadBuffer.size() - 1, (size_t)CVarGetInteger(CVAR_SIMULATED_INPUT_LAG, 0))];

pad->button |= padFromBuffer.button;

if (pad->stick_x == 0) {
pad->stick_x = padFromBuffer.stick_x;
}
if (pad->stick_y == 0) {
pad->stick_y = padFromBuffer.stick_y;
}

if (pad->right_stick_x == 0) {
pad->right_stick_x = padFromBuffer.right_stick_x;
}
if (pad->right_stick_y == 0) {
pad->right_stick_y = padFromBuffer.right_stick_y;
}

if (pad->gyro_x == 0) {
pad->gyro_x = padFromBuffer.gyro_x;
}
if (pad->gyro_y == 0) {
pad->gyro_y = padFromBuffer.gyro_y;
}
}

while (mPadBuffer.size() > 6) {
mPadBuffer.pop_back();
}
}
} // namespace LUS
21 changes: 18 additions & 3 deletions src/controller/controldevice/controller/Controller.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Controller : public ControlDevice {
std::shared_ptr<ControllerGyro> GetGyro();
std::shared_ptr<ControllerRumble> GetRumble();
std::shared_ptr<ControllerLED> GetLED();
void ReadToPad(OSContPad* pad);
virtual void ReadToPad(void* pad) = 0;
bool HasConfig();
uint8_t GetPortIndex();
std::vector<std::shared_ptr<ControllerMapping>> GetAllMappings();
Expand All @@ -53,16 +53,31 @@ class Controller : public ControlDevice {
bool HasMappingsForShipDeviceIndex(ShipDeviceIndex lusIndex);
void MoveMappingsToDifferentController(std::shared_ptr<Controller> newController, ShipDeviceIndex lusIndex);

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

private:
void LoadButtonMappingFromConfig(std::string id);
void SaveButtonMappingIdsToConfig();

std::unordered_map<CONTROLLERBUTTONS_T, std::shared_ptr<ControllerButton>> mButtons;
std::shared_ptr<ControllerStick> mLeftStick, mRightStick;
std::shared_ptr<ControllerGyro> mGyro;
std::shared_ptr<ControllerRumble> mRumble;
std::shared_ptr<ControllerLED> mLED;
};
} // namespace Ship

namespace LUS {
class Controller : public Ship::Controller {
public:
Controller(uint8_t portIndex);
Controller(uint8_t portIndex, std::vector<CONTROLLERBUTTONS_T> additionalBitmasks);

void ReadToPad(void* pad) override;

private:
void ReadToOSContPad(OSContPad* pad);

std::deque<OSContPad> mPadBuffer;
};
} // namespace Ship
} // namespace LUS
Loading