2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-12-14 22:06:10 +00:00

Input: Add onControllerAdded/Removed callbacks and also display the controller name in the input viewer

This commit is contained in:
2022-02-09 00:54:53 -08:00
parent 52fd54bc3e
commit a6b2d66e1e
12 changed files with 175 additions and 59 deletions

View File

@@ -94,19 +94,22 @@ CFinalInput::CFinalInput(int cIdx, float dt, const SAuroraControllerState& data,
, x2e_b28_PDPRight(DDPRight() && !prevInput.DDPRight())
, x2e_b29_PDPDown(DDPDown() && !prevInput.DDPDown())
, x2e_b30_PDPLeft(DDPLeft() && !prevInput.DDPLeft())
, x2e_b31_PStart(DStart() && !prevInput.DStart()) {
if (x2c_b29_L) {
x18_anaLeftTrigger = 150.f * 0.007f;
}
if (x2c_b30_R) {
x1c_anaRightTrigger = 150.f * 0.007f;
}
, x2e_b31_PStart(DStart() && !prevInput.DStart())
, m_which(data.m_which){
if (!data.m_isGamecube) {
if (x2c_b29_L && x18_anaLeftTrigger <= 0.f) {
x18_anaLeftTrigger = 150.f * 0.007f;
}
if (x2c_b30_R && x1c_anaRightTrigger <= 0.f) {
x1c_anaRightTrigger = 150.f * 0.007f;
}
if (x18_anaLeftTrigger > (150.f * 0.007f) && !x2c_b29_L) {
x2c_b29_L = true;
}
if (x1c_anaRightTrigger > (150.f * 0.007f) && !x2c_b30_R) {
x2c_b30_R = true;
if (x18_anaLeftTrigger >= (150.f * 0.007f) && !x2c_b29_L) {
x2c_b29_L = true;
}
if (x1c_anaRightTrigger >= (150.f * 0.007f) && !x2c_b30_R) {
x2c_b30_R = true;
}
}
}
@@ -208,6 +211,7 @@ CFinalInput& CFinalInput::operator|=(const CFinalInput& other) {
m_PSpecialKeys = other.m_PSpecialKeys;
m_PMouseButtons = other.m_PMouseButtons;
}
m_which = other.m_which;
return *this;
}

View File

@@ -12,14 +12,19 @@
namespace metaforce {
struct SAuroraControllerState {
u32 m_which = -1;
bool m_isGamecube = false;
std::array<int16_t, size_t(aurora::ControllerAxis::MAX)> m_axes{};
std::bitset<size_t(aurora::ControllerButton::MAX)> m_btns{};
SAuroraControllerState() = default;
SAuroraControllerState(uint32_t which, bool isGamecube) : m_which(which), m_isGamecube(isGamecube) {}
void clamp();
};
struct CFinalInput {
float x0_dt = 0.0f;
u32 x4_controllerIdx = 0;
u32 x4_controllerIdx = -1;
float x8_anaLeftX = 0.0f;
float xc_anaLeftY = 0.0f;
float x10_anaRightX = 0.0f;

View File

@@ -14,33 +14,61 @@ void CInputGenerator::Update(float dt, CArchitectureQueue& queue) {
const CFinalInput& kbInput = getFinalInput(0, dt);
queue.Push(MakeMsg::CreateUserInput(EArchMsgTarget::Game, kbInput));
/* Dolphin controllers next */
// for (int i = 0; i < 4; ++i) {
// bool connected;
// EStatusChange change = m_dolphinCb.getStatusChange(i, connected);
// if (change != EStatusChange::NoChange)
// queue.Push(MakeMsg::CreateControllerStatus(EArchMsgTarget::Game, i, connected));
// if (connected) {
// CFinalInput input = m_dolphinCb.getFinalInput(i, dt, m_leftDiv, m_rightDiv);
// if (i == 0) /* Merge KB input with first controller */
// {
// input |= kbInput;
// kbUsed = true;
// }
// m_lastUpdate = input;
// queue.Push(MakeMsg::CreateUserInput(EArchMsgTarget::Game, input));
// }
// }
/* Dolphin controllers next */
// for (int i = 0; i < 4; ++i) {
// bool connected;
// EStatusChange change = m_dolphinCb.getStatusChange(i, connected);
// if (change != EStatusChange::NoChange)
// queue.Push(MakeMsg::CreateControllerStatus(EArchMsgTarget::Game, i, connected));
// if (connected) {
// CFinalInput input = m_dolphinCb.getFinalInput(i, dt, m_leftDiv, m_rightDiv);
// if (i == 0) /* Merge KB input with first controller */
// {
// input |= kbInput;
// kbUsed = true;
// }
// m_lastUpdate = input;
// queue.Push(MakeMsg::CreateUserInput(EArchMsgTarget::Game, input));
// }
// }
// /* Send straight keyboard input if no first controller present */
// if (!kbUsed) {
// m_lastUpdate = kbInput;
// }
// /* Send straight keyboard input if no first controller present */
// if (!kbUsed) {
// m_lastUpdate = kbInput;
// }
}
void CInputGenerator::controllerAdded(uint32_t which) noexcept {
s32 player = aurora::get_controller_player_index(which);
if (player < 0) {
player = 0;
aurora::set_controller_player_index(which, 0);
}
m_state[player] = SAuroraControllerState(which, aurora::is_controller_gamecube(which));
}
void CInputGenerator::controllerRemoved(uint32_t which) noexcept {
auto* it =
std::find_if(m_state.begin(), m_state.end(), [&which](const auto& s) { return s.m_which == which; });
if (it == m_state.end()) {
return;
}
(*it) = SAuroraControllerState();
}
void CInputGenerator::controllerButton(uint32_t which, aurora::ControllerButton button, bool pressed) noexcept {
s32 player = aurora::get_controller_player_index(which);
if (player < 0) {
return;
}
m_state[player].m_btns.set(size_t(button), pressed);
}
void CInputGenerator::controllerAxis(uint32_t which, aurora::ControllerAxis axis, int16_t value) noexcept {
s32 idx = aurora::get_controller_player_index(which);
if (idx < 0) {
s32 player = aurora::get_controller_player_index(which);
if (player < 0) {
return;
}
@@ -62,7 +90,7 @@ void CInputGenerator::controllerAxis(uint32_t which, aurora::ControllerAxis axis
break;
}
m_state[idx].m_axes[size_t(axis)] = value;
m_state[player].m_axes[size_t(axis)] = value;
}
const CFinalInput& CInputGenerator::getFinalInput(unsigned int idx, float dt) {

View File

@@ -25,7 +25,7 @@ class CInputGenerator /*: public boo::DeviceFinder*/ {
float m_leftDiv;
float m_rightDiv;
CKeyboardMouseControllerData m_data;
SAuroraControllerState m_state[4];
std::array<SAuroraControllerState, 4> m_state;
CFinalInput m_lastUpdate;
const CFinalInput& getFinalInput(unsigned idx, float dt);
@@ -43,14 +43,14 @@ public:
// }
// }
void controllerButton(uint32_t idx, aurora::ControllerButton button, bool pressed) noexcept {
s32 player = aurora::get_controller_player_index(idx);
if (player < 0) {
return;
}
m_state[player].m_btns.set(size_t(button), pressed);
}
void controllerAxis(uint32_t idx, aurora::ControllerAxis axis, int16_t value) noexcept;
void controllerAdded(uint32_t which) noexcept;
void controllerRemoved(uint32_t which) noexcept;
void controllerButton(uint32_t which, aurora::ControllerButton button, bool pressed) noexcept;
void controllerAxis(uint32_t which, aurora::ControllerAxis axis, int16_t value) noexcept;
/* Keyboard and mouse events are delivered on the main game
* thread as part of the app's main event loop. The OS is responsible