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

Adding the ability to set pad analog mode to Lua #1452

Merged
merged 2 commits into from
Oct 6, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
158 changes: 78 additions & 80 deletions src/core/pad.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1238,51 +1238,6 @@ void PadsImpl::Pad::setDefaults(bool firstController) {
}

void PadsImpl::setLua(PCSX::Lua L) {
auto getButton = [this](PCSX::Lua L, unsigned pad) -> int {
int n = L.gettop();
if (n == 0) {
return L.error("Not enough arguments to getButton");
}
if (!L.isnumber(1)) {
return L.error("Invalid argument to getButton");
}
auto buttons = m_pads[pad].m_data.buttonStatus;
auto overrides = m_pads[pad].m_data.overrides;
unsigned button = L.checknumber(1);
L.push(((overrides & buttons) & (1 << button)) == 0);
return 1;
};

auto setOverride = [this](PCSX::Lua L, unsigned pad) -> int {
int n = L.gettop();
if (n == 0) {
return L.error("Not enough arguments to setOverride");
}
if (!L.isnumber(1)) {
return L.error("Invalid argument to setOverride");
}
auto& overrides = m_pads[pad].m_data.overrides;
unsigned button = L.checknumber(1);
button = 1 << button;
overrides &= ~button;
return 0;
};

auto clearOverride = [this](PCSX::Lua L, unsigned pad) -> int {
int n = L.gettop();
if (n == 0) {
return L.error("Not enough arguments to clearOverride");
}
if (!L.isnumber(1)) {
return L.error("Invalid argument to clearOverride");
}
auto& overrides = m_pads[pad].m_data.overrides;
unsigned button = L.checknumber(1);
button = 1 << button;
overrides |= button;
return 0;
};

L.getfieldtable("PCSX", LUA_GLOBALSINDEX);

// setting constants
Expand Down Expand Up @@ -1327,12 +1282,13 @@ void PadsImpl::setLua(PCSX::Lua L) {

L.getfieldtable("settings");
L.getfieldtable("pads");
L.push(lua_Number(1));
m_pads[0].m_settings.pushValue(L);
L.settable();
L.push(lua_Number(2));
m_pads[0].m_settings.pushValue(L);
L.settable();
auto pushSettings = [this, L](unsigned pad) mutable {
L.push(lua_Number(pad + 1));
m_pads[pad].m_settings.pushValue(L);
L.settable();
};
pushSettings(0);
pushSettings(1);
L.pop();
L.pop();

Expand All @@ -1341,37 +1297,79 @@ void PadsImpl::setLua(PCSX::Lua L) {

// pads callbacks

L.getfieldtable(1);
L.getfieldtable("pads");
L.getfieldtable(1);

// push first pad stuff here
L.declareFunc(
"getButton", [getButton](PCSX::Lua L) -> int { return getButton(L, 0); }, -1);
L.declareFunc(
"setOverride", [setOverride](PCSX::Lua L) -> int { return setOverride(L, 0); }, -1);
L.declareFunc(
"clearOverride", [clearOverride](PCSX::Lua L) -> int { return clearOverride(L, 0); }, -1);

L.pop();
L.pop();
L.pop();

L.getfieldtable(2);
L.getfieldtable("pads");
L.getfieldtable(1);
auto setCallbacks = [this, L](unsigned pad) mutable {
L.getfieldtable(pad + 1);
L.getfieldtable("pads");
L.getfieldtable(1);

// push second pad stuff here
L.declareFunc(
"getButton", [getButton](PCSX::Lua L) -> int { return getButton(L, 1); }, -1);
L.declareFunc(
"setOverride", [setOverride](PCSX::Lua L) -> int { return setOverride(L, 1); }, -1);
L.declareFunc(
"clearOverride", [clearOverride](PCSX::Lua L) -> int { return clearOverride(L, 1); }, -1);
L.declareFunc(
"getButton", [this, pad](PCSX::Lua L) -> int {
int n = L.gettop();
if (n == 0) {
return L.error("Not enough arguments to getButton");
}
if (!L.isnumber(1)) {
return L.error("Invalid argument to getButton");
}
auto buttons = m_pads[pad].m_data.buttonStatus;
auto overrides = m_pads[pad].m_data.overrides;
unsigned button = L.checknumber(1);
L.push(((overrides & buttons) & (1 << button)) == 0);
return 1;
}, -1);
L.declareFunc(
"setOverride", [this, pad](PCSX::Lua L) -> int {
int n = L.gettop();
if (n == 0) {
return L.error("Not enough arguments to setOverride");
}
if (!L.isnumber(1)) {
return L.error("Invalid argument to setOverride");
}
auto& overrides = m_pads[pad].m_data.overrides;
unsigned button = L.checknumber(1);
button = 1 << button;
overrides &= ~button;
return 0;
}, -1);
L.declareFunc(
"clearOverride", [this, pad](PCSX::Lua L) -> int {
int n = L.gettop();
if (n == 0) {
return L.error("Not enough arguments to clearOverride");
}
if (!L.isnumber(1)) {
return L.error("Invalid argument to clearOverride");
}
auto& overrides = m_pads[pad].m_data.overrides;
unsigned button = L.checknumber(1);
button = 1 << button;
overrides |= button;
return 0;
}, -1);
L.declareFunc(
"setAnalogMode", [this, pad](PCSX::Lua L) -> int {
int n = L.gettop();
if (n == 0) {
m_pads[pad].m_analogMode = false;
} else {
m_pads[pad].m_analogMode = L.toboolean();
}
return 0;
}, -1);
L.declareFunc(
"map", [this, pad](PCSX::Lua L) -> int {
m_pads[pad].map();
return 0;
}, -1);

L.pop();
L.pop();
L.pop();
};

L.pop();
L.pop();
L.pop();
setCallbacks(0);
setCallbacks(1);

L.pop();
L.pop();
Expand Down