Skip to content

Commit

Permalink
split readtopad
Browse files Browse the repository at this point in the history
  • Loading branch information
briaguya-ai committed Dec 14, 2024
1 parent 46901ba commit 2e01242
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 52 deletions.
2 changes: 1 addition & 1 deletion src/controller/controldeck/ControlDeck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ 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)));
mPorts.push_back(std::make_shared<ControlPort>(i, std::make_shared<LUS::Controller>(i, additionalBitmasks)));
}

mDeviceIndexMappingManager = std::make_shared<ShipDeviceIndexMappingManager>();
Expand Down
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();
}
}
}
17 changes: 15 additions & 2 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,7 +53,7 @@ class Controller : public ControlDevice {
bool HasMappingsForShipDeviceIndex(ShipDeviceIndex lusIndex);
void MoveMappingsToDifferentController(std::shared_ptr<Controller> newController, ShipDeviceIndex lusIndex);

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

Expand All @@ -66,3 +66,16 @@ class Controller : public ControlDevice {
std::deque<OSContPad> mPadBuffer;
};
} // 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);
};
} // namespace LUS

0 comments on commit 2e01242

Please sign in to comment.