partially added analog controls

This commit is contained in:
georgemoralis 2024-06-17 12:52:25 +03:00
parent 57c65cadb8
commit 48d6ccd9bb
4 changed files with 73 additions and 26 deletions

View File

@ -250,12 +250,12 @@ int PS4_SYSV_ABI scePadRead(s32 handle, OrbisPadData* pData, s32 num) {
for (int i = 0; i < ret_num; i++) {
pData[i].buttons = states[i].buttonsState;
pData[i].leftStick.x = 128; // dummy
pData[i].leftStick.y = 128; // dummy
pData[i].rightStick.x = 0; // dummy
pData[i].rightStick.y = 0; // dummy
pData[i].analogButtons.l2 = 0; // dummy
pData[i].analogButtons.r2 = 0; // dummy
pData[i].leftStick.x = states[i].axes[static_cast<int>(Input::Axis::LeftX)];
pData[i].leftStick.y = states[i].axes[static_cast<int>(Input::Axis::LeftY)];
pData[i].rightStick.x = states[i].axes[static_cast<int>(Input::Axis::RightX)];
pData[i].rightStick.y = states[i].axes[static_cast<int>(Input::Axis::RightY)];
pData[i].analogButtons.l2 = states[i].axes[static_cast<int>(Input::Axis::TriggerLeft)];
pData[i].analogButtons.r2 = states[i].axes[static_cast<int>(Input::Axis::TriggerRight)];
pData[i].orientation.x = 0.0f;
pData[i].orientation.y = 0.0f;
pData[i].orientation.z = 0.0f;
@ -309,14 +309,14 @@ int PS4_SYSV_ABI scePadReadState(s32 handle, OrbisPadData* pData) {
bool isConnected = false;
Input::State state;
controller->readState(&state, &isConnected, &connectedCount);
controller->ReadState(&state, &isConnected, &connectedCount);
pData->buttons = state.buttonsState;
pData->leftStick.x = 128; // dummy
pData->leftStick.y = 128; // dummy
pData->rightStick.x = 0; // dummy
pData->rightStick.y = 0; // dummy
pData->analogButtons.r2 = 0; // dummy
pData->analogButtons.l2 = 0; // dummy
pData->leftStick.x = state.axes[static_cast<int>(Input::Axis::LeftX)];
pData->leftStick.y = state.axes[static_cast<int>(Input::Axis::LeftY)];
pData->rightStick.x = state.axes[static_cast<int>(Input::Axis::RightX)];
pData->rightStick.y = state.axes[static_cast<int>(Input::Axis::RightY)];
pData->analogButtons.l2 = state.axes[static_cast<int>(Input::Axis::TriggerLeft)];
pData->analogButtons.r2 = state.axes[static_cast<int>(Input::Axis::TriggerRight)];
pData->orientation.x = 0;
pData->orientation.y = 0;
pData->orientation.z = 0;

View File

@ -3,7 +3,7 @@
#include "core/libraries/kernel/time_management.h"
#include "input/controller.h"
#include "core/libraries/pad/pad.h"
namespace Input {
GameController::GameController() {
@ -11,12 +11,12 @@ GameController::GameController() {
m_last_state = State();
}
void GameController::readState(State* state, bool* isConnected, int* connectedCount) {
void GameController::ReadState(State* state, bool* isConnected, int* connectedCount) {
std::scoped_lock lock{m_mutex};
*isConnected = m_connected;
*connectedCount = m_connected_count;
*state = getLastState();
*state = GetLastState();
}
int GameController::ReadStates(State* states, int states_num, bool* isConnected,
@ -50,7 +50,7 @@ int GameController::ReadStates(State* states, int states_num, bool* isConnected,
return ret_num;
}
State GameController::getLastState() const {
State GameController::GetLastState() const {
if (m_states_num == 0) {
return m_last_state;
}
@ -60,7 +60,7 @@ State GameController::getLastState() const {
return m_states[last];
}
void GameController::addState(const State& state) {
void GameController::AddState(const State& state) {
if (m_states_num >= MAX_STATES) {
m_states_num = MAX_STATES - 1;
m_first_state = (m_first_state + 1) % MAX_STATES;
@ -74,9 +74,9 @@ void GameController::addState(const State& state) {
m_states_num++;
}
void GameController::checkButton(int id, u32 button, bool isPressed) {
void GameController::CheckButton(int id, u32 button, bool isPressed) {
std::scoped_lock lock{m_mutex};
auto state = getLastState();
auto state = GetLastState();
state.time = Libraries::Kernel::sceKernelGetProcessTime();
if (isPressed) {
state.buttonsState |= button;
@ -84,7 +84,36 @@ void GameController::checkButton(int id, u32 button, bool isPressed) {
state.buttonsState &= ~button;
}
addState(state);
AddState(state);
}
void GameController::Axis(int id, Input::Axis axis, int value) {
std::scoped_lock lock{m_mutex};
auto state = GetLastState();
state.time = Libraries::Kernel::sceKernelGetProcessTime();
int axis_id = static_cast<int>(axis);
state.axes[axis_id] = value;
if (axis == Input::Axis::TriggerLeft) {
if (value > 0) {
state.buttonsState |= Libraries::Pad::OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_L2;
} else {
state.buttonsState &= ~Libraries::Pad::OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_L2;
}
}
if (axis == Input::Axis::TriggerRight) {
if (value > 0) {
state.buttonsState |= Libraries::Pad::OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_R2;
} else {
state.buttonsState &= ~Libraries::Pad::OrbisPadButtonDataOffset::ORBIS_PAD_BUTTON_R2;
}
}
AddState(state);
}
} // namespace Input

View File

@ -8,11 +8,28 @@
namespace Input {
enum class Axis {
LeftX = 0,
LeftY = 1,
RightX = 2,
RightY = 3,
TriggerLeft = 4,
TriggerRight = 5,
AxisMax
};
struct State {
u32 buttonsState = 0;
u64 time = 0;
int axes[static_cast<int>(Axis::AxisMax)] = {128, 128, 128, 128, 0, 0};
};
inline int GetAxis(int min, int max, int value) {
int v = (255 * (value - min)) / (max - min);
return (v < 0 ? 0 : (v > 255 ? 255 : v));
}
constexpr u32 MAX_STATES = 64;
class GameController {
@ -20,11 +37,12 @@ public:
GameController();
virtual ~GameController() = default;
void readState(State* state, bool* isConnected, int* connectedCount);
void ReadState(State* state, bool* isConnected, int* connectedCount);
int ReadStates(State* states, int states_num, bool* isConnected, int* connectedCount);
State getLastState() const;
void checkButton(int id, u32 button, bool isPressed);
void addState(const State& state);
State GetLastState() const;
void CheckButton(int id, u32 button, bool isPressed);
void AddState(const State& state);
void Axis(int id, Input::Axis axis, int value);
private:
struct StateInternal {

View File

@ -131,7 +131,7 @@ void WindowSDL::onKeyPress(const SDL_Event* event) {
break;
}
if (button != 0) {
controller->checkButton(0, button, event->type == SDL_EVENT_KEY_DOWN);
controller->CheckButton(0, button, event->type == SDL_EVENT_KEY_DOWN);
}
}