Move callback parameter of ui.begin* functions up

See 4f31f85b6b. This is better because the need to pad partial parameter lists with nils is avoided.
This commit is contained in:
Tamás Bálint Misius 2023-09-07 21:29:44 +02:00
parent 59b79e805f
commit 1b1ef99194
No known key found for this signature in database
GPG Key ID: 5B472A12F6ECA9F2

View File

@ -669,14 +669,37 @@ int LuaScriptInterface::tpt_newIndex(lua_State *l)
return 0; return 0;
} }
template<class Type>
struct PickIfTypeHelper;
template<>
struct PickIfTypeHelper<String>
{
static constexpr auto LuaType = LUA_TSTRING;
static String Get(lua_State *l, int index) { return tpt_lua_checkString(l, index); }
};
template<>
struct PickIfTypeHelper<bool>
{
static constexpr auto LuaType = LUA_TBOOLEAN;
static bool Get(lua_State *l, int index) { return lua_toboolean(l, index); }
};
template<class Type>
static Type PickIfType(lua_State *l, int index, Type defaultValue)
{
return lua_type(l, index) == PickIfTypeHelper<Type>::LuaType ? PickIfTypeHelper<Type>::Get(l, index) : defaultValue;
}
static int beginMessageBox(lua_State* l) static int beginMessageBox(lua_State* l)
{ {
String title = tpt_lua_optString(l, 1, "Title"); auto title = PickIfType(l, 1, String("Title"));
String message = tpt_lua_optString(l, 2, "Message"); auto message = PickIfType(l, 2, String("Message"));
int large = lua_toboolean(l, 3); auto large = PickIfType(l, 3, false);
auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface); auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface);
auto cb = std::make_shared<LuaSmartRef>(luacon_ci->l); // * Bind to main lua state (might be different from l). auto cb = std::make_shared<LuaSmartRef>(luacon_ci->l); // * Bind to main lua state (might be different from l).
cb->Assign(l, 4); cb->Assign(l, lua_gettop(l));
new InformationMessage(title, message, large, { [cb]() { new InformationMessage(title, message, large, { [cb]() {
auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface); auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface);
auto l = luacon_ci->l; auto l = luacon_ci->l;
@ -698,10 +721,10 @@ static int beginMessageBox(lua_State* l)
static int beginThrowError(lua_State* l) static int beginThrowError(lua_State* l)
{ {
String errorMessage = tpt_lua_optString(l, 1, "Error text"); auto errorMessage = PickIfType(l, 1, String("Error text"));
auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface); auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface);
auto cb = std::make_shared<LuaSmartRef>(luacon_ci->l); // * Bind to main lua state (might be different from l). auto cb = std::make_shared<LuaSmartRef>(luacon_ci->l); // * Bind to main lua state (might be different from l).
cb->Assign(l, 2); cb->Assign(l, lua_gettop(l));
new ErrorMessage("Error", errorMessage, { [cb]() { new ErrorMessage("Error", errorMessage, { [cb]() {
auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface); auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface);
auto l = luacon_ci->l; auto l = luacon_ci->l;
@ -723,13 +746,13 @@ static int beginThrowError(lua_State* l)
static int beginInput(lua_State* l) static int beginInput(lua_State* l)
{ {
String title = tpt_lua_optString(l, 1, "Title"); auto title = PickIfType(l, 1, String("Title"));
String prompt = tpt_lua_optString(l, 2, "Enter some text:"); auto prompt = PickIfType(l, 2, String("Enter some text:"));
String text = tpt_lua_optString(l, 3, ""); auto text = PickIfType(l, 3, String(""));
String shadow = tpt_lua_optString(l, 4, ""); auto shadow = PickIfType(l, 4, String(""));
auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface); auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface);
auto cb = std::make_shared<LuaSmartRef>(luacon_ci->l); // * Bind to main lua state (might be different from l). auto cb = std::make_shared<LuaSmartRef>(luacon_ci->l); // * Bind to main lua state (might be different from l).
cb->Assign(l, 5); cb->Assign(l, lua_gettop(l));
auto handle = [cb](std::optional<String> input) { auto handle = [cb](std::optional<String> input) {
auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface); auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface);
auto l = luacon_ci->l; auto l = luacon_ci->l;
@ -764,12 +787,12 @@ static int beginInput(lua_State* l)
static int beginConfirm(lua_State *l) static int beginConfirm(lua_State *l)
{ {
String title = tpt_lua_optString(l, 1, "Title"); auto title = PickIfType(l, 1, String("Title"));
String message = tpt_lua_optString(l, 2, "Message"); auto message = PickIfType(l, 2, String("Message"));
String buttonText = tpt_lua_optString(l, 3, "Confirm"); auto buttonText = PickIfType(l, 3, String("Confirm"));
auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface); auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface);
auto cb = std::make_shared<LuaSmartRef>(luacon_ci->l); // * Bind to main lua state (might be different from l). auto cb = std::make_shared<LuaSmartRef>(luacon_ci->l); // * Bind to main lua state (might be different from l).
cb->Assign(l, 4); cb->Assign(l, lua_gettop(l));
auto handle = [cb](int result) { auto handle = [cb](int result) {
auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface); auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface);
auto l = luacon_ci->l; auto l = luacon_ci->l;
@ -5083,10 +5106,10 @@ int LuaScriptInterface::luatpt_getscript(lua_State* l)
int scriptID = luaL_checkinteger(l, 1); int scriptID = luaL_checkinteger(l, 1);
auto filename = tpt_lua_checkByteString(l, 2); auto filename = tpt_lua_checkByteString(l, 2);
bool runScript = luaL_optint(l, 3, 0); auto runScript = PickIfType(l, 3, false);
auto cb = std::make_shared<LuaSmartRef>(luacon_ci->l); // * Bind to main lua state (might be different from l). auto cb = std::make_shared<LuaSmartRef>(luacon_ci->l); // * Bind to main lua state (might be different from l).
cb->Assign(l, 4); cb->Assign(l, lua_gettop(l));
luacon_ci->scriptDownloadComplete = [cb](const GetScriptStatus &status) { luacon_ci->scriptDownloadComplete = [cb](const GetScriptStatus &status) {
auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface); auto *luacon_ci = static_cast<LuaScriptInterface *>(commandInterface);
auto l = luacon_ci->l; auto l = luacon_ci->l;