Functions to update Mod Menu elements (#385)

Three new functions have been added, to be able to change the respective mod menu elements' values:
- update_mod_menu_element_checkbox
- update_mod_menu_element_slider
- update_mod_menu_element_inputbox
This commit is contained in:
Motoo Chhotoo Chintoo 2024-10-22 12:15:19 +05:30 committed by GitHub
parent 418b1201a0
commit e967e2cd1e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 102 additions and 0 deletions

View file

@ -192,6 +192,30 @@ function update_mod_menu_element_name(index, name)
-- ... -- ...
end end
--- @param index integer The index of the element in the order in which they were hooked
--- @param value boolean The boolean value to change to
--- Updates a mod menu checkbox element's boolean value
--- - NOTE: `index` is zero-indexed
function update_mod_menu_element_checkbox(index, value)
-- ...
end
--- @param index integer The index of the element in the order in which they were hooked
--- @param value number The number value to change to
--- Updates a mod menu slider element's numerical value
--- - NOTE: `index` is zero-indexed
function update_mod_menu_element_slider(index, value)
-- ...
end
--- @param index integer The index of the element in the order in which they were hooked
--- @param value string The text to change to
--- Updates a mod menu inputbox element's string value
--- - NOTE: `index` is zero-indexed
function update_mod_menu_element_inputbox(index, value)
-- ...
end
--------------- ---------------
-- functions -- -- functions --
--------------- ---------------

View file

@ -2180,6 +2180,81 @@ int smlua_update_mod_menu_element_name(lua_State* L) {
return 1; return 1;
} }
int smlua_update_mod_menu_element_checkbox(lua_State* L) {
if (L == NULL) { return 0; }
if (!smlua_functions_valid_param_count(L, 2)) { return 0; }
int index = smlua_to_integer(L, 1);
if (index >= gHookedModMenuElementsCount || !gSmLuaConvertSuccess) {
LOG_LUA_LINE("Update mod menu element: tried to update invalid element");
return 0;
}
if (gHookedModMenuElements[index].element != MOD_MENU_ELEMENT_CHECKBOX) {
LOG_LUA_LINE("Update mod menu element: element is not a checkbox.");
return 0;
}
bool boolValue = smlua_to_boolean(L, 2);
if (!gSmLuaConvertSuccess) {
LOG_LUA_LINE("Update mod menu element: tried to update invalid element");
return 0;
}
gHookedModMenuElements[index].boolValue = boolValue;
return 1;
}
int smlua_update_mod_menu_element_slider(lua_State* L) {
if (L == NULL) { return 0; }
if (!smlua_functions_valid_param_count(L, 2)) { return 0; }
int index = smlua_to_integer(L, 1);
if (index >= gHookedModMenuElementsCount || !gSmLuaConvertSuccess) {
LOG_LUA_LINE("Update mod menu element: tried to update invalid element");
return 0;
}
if (gHookedModMenuElements[index].element != MOD_MENU_ELEMENT_SLIDER) {
LOG_LUA_LINE("Update mod menu element: element is not a slider.");
return 0;
}
u32 uintValue = smlua_to_integer(L, 2);
if (!gSmLuaConvertSuccess) {
LOG_LUA_LINE("Update mod menu element: tried to update invalid element");
return 0;
}
gHookedModMenuElements[index].uintValue = uintValue;
return 1;
}
int smlua_update_mod_menu_element_inputbox(lua_State* L) {
if (L == NULL) { return 0; }
if (!smlua_functions_valid_param_count(L, 2)) { return 0; }
int index = smlua_to_integer(L, 1);
if (index >= gHookedModMenuElementsCount || !gSmLuaConvertSuccess) {
LOG_LUA_LINE("Update mod menu element: tried to update invalid element");
return 0;
}
if (gHookedModMenuElements[index].element != MOD_MENU_ELEMENT_INPUTBOX) {
LOG_LUA_LINE("Update mod menu element: element is not an inputbox.");
return 0;
}
const char* stringValue = smlua_to_string(L, 2);
if (stringValue == NULL || strlen(stringValue) == 0 || !gSmLuaConvertSuccess) {
LOG_LUA_LINE("Update mod menu element: tried to update invalid element string");
return 0;
}
snprintf(gHookedModMenuElements[index].stringValue, gHookedModMenuElements[index].length, "%s", stringValue);
return 1;
}
void smlua_call_mod_menu_element_hook(struct LuaHookedModMenuElement* hooked, int index) { void smlua_call_mod_menu_element_hook(struct LuaHookedModMenuElement* hooked, int index) {
lua_State* L = gLuaState; lua_State* L = gLuaState;
if (L == NULL) { return; } if (L == NULL) { return; }
@ -2310,4 +2385,7 @@ void smlua_bind_hooks(void) {
smlua_bind_function(L, "hook_mod_menu_inputbox", smlua_hook_mod_menu_inputbox); smlua_bind_function(L, "hook_mod_menu_inputbox", smlua_hook_mod_menu_inputbox);
smlua_bind_function(L, "update_chat_command_description", smlua_update_chat_command_description); smlua_bind_function(L, "update_chat_command_description", smlua_update_chat_command_description);
smlua_bind_function(L, "update_mod_menu_element_name", smlua_update_mod_menu_element_name); smlua_bind_function(L, "update_mod_menu_element_name", smlua_update_mod_menu_element_name);
smlua_bind_function(L, "update_mod_menu_element_checkbox", smlua_update_mod_menu_element_checkbox);
smlua_bind_function(L, "update_mod_menu_element_slider", smlua_update_mod_menu_element_slider);
smlua_bind_function(L, "update_mod_menu_element_inputbox", smlua_update_mod_menu_element_inputbox);
} }