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

Add menu_getprop() function #507

Open
wants to merge 21 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 17 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
107 changes: 107 additions & 0 deletions amxmodx/newmenus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,112 @@ static cell AMX_NATIVE_CALL menu_setprop(AMX *amx, cell *params)
return 1;
}

// Gets a menu property
// native menu_getprop(menu, prop, ...);
static cell AMX_NATIVE_CALL menu_getprop(AMX *amx, cell *params)
{
enum args { arg_numargs, arg_menu, arg_prop, arg_buffer, arg_bufferlen };

GETMENU(params[arg_menu]);

int paramsNum = params[arg_numargs] / sizeof(cell);

switch (params[arg_prop])
{
case MPROP_PAGE_CALLBACK: return pMenu->pageCallback;
case MPROP_SHOWPAGE: return pMenu->showPageNumber;
case MPROP_SET_NUMBER_COLOR:
{
if (paramsNum < arg_bufferlen)
{
LogError(amx, AMX_ERR_NATIVE, "Expected 4 parameters");
return 0;
}

set_amxstring(amx, params[arg_buffer], pMenu->m_ItemColor.chars(), params[arg_bufferlen]);
break;
}
case MPROP_PERPAGE: return static_cast<cell>(pMenu->items_per_page);
case MPROP_BACKNAME:
{
if (paramsNum < arg_bufferlen)
{
LogError(amx, AMX_ERR_NATIVE, "Expected 4 parameters");
return 0;
}

set_amxstring(amx, params[arg_buffer], pMenu->m_OptNames[abs(MENU_BACK)].chars(), params[arg_bufferlen]);
break;
}
case MPROP_NEXTNAME:
{
if (paramsNum < arg_bufferlen)
{
LogError(amx, AMX_ERR_NATIVE, "Expected 4 parameters");
return 0;
}

set_amxstring(amx, params[arg_buffer], pMenu->m_OptNames[abs(MENU_MORE)].chars(), params[arg_bufferlen]);
break;
}
case MPROP_EXITNAME:
{
if (paramsNum < arg_bufferlen)
{
LogError(amx, AMX_ERR_NATIVE, "Expected 4 parameters");
return 0;
}

set_amxstring(amx, params[arg_buffer], pMenu->m_OptNames[abs(MENU_EXIT)].chars(), params[arg_bufferlen]);
break;
}
case MPROP_TITLE:
{
if (paramsNum < arg_bufferlen)
{
LogError(amx, AMX_ERR_NATIVE, "Expected 4 parameters");
return 0;
}

set_amxstring(amx, params[arg_buffer], pMenu->m_Title.chars(), params[arg_bufferlen]);
break;
}
case MPROP_EXITALL:
{
if (!pMenu->m_NeverExit && !pMenu->m_ForceExit)
{
return 1;
}
else if (!pMenu->m_NeverExit && pMenu->m_ForceExit)
{
return 2;
}
else if (pMenu->m_NeverExit && !pMenu->m_ForceExit)
{
return -1;
}

return 0;
}
case MPROP_ORDER:
{
/* Ignored as of 1.8.0 */
break;
}
case MPROP_NOCOLORS:
{
return static_cast<cell>(pMenu->m_AutoColors);
}
default:
{
LogError(amx, AMX_ERR_NATIVE, "Invalid menu setting: %d", params[arg_prop]);
return 0;
}
}

return 1;
}

#define GETMENU_R(p) Menu *pMenu = get_menu_by_id(p); \
if (pMenu == NULL) { \
LogError(amx, AMX_ERR_NATIVE, "Invalid menu id %d(%d)", p, g_NewMenus.length()); \
Expand Down Expand Up @@ -1185,6 +1291,7 @@ AMX_NATIVE_INFO g_NewMenuNatives[] =
{"menu_item_setname", menu_item_setname},
{"menu_destroy", menu_destroy},
{"menu_setprop", menu_setprop},
{"menu_getprop", menu_getprop},
{"menu_cancel", menu_cancel},
{"player_menu_info", player_menu_info},
{"menu_addblank2", menu_addblank2},
Expand Down
18 changes: 18 additions & 0 deletions plugins/include/newmenus.inc
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,24 @@ native menu_addtext2( menu, const text[] );
*/
native menu_setprop(menu, prop, ...);

/**
* Gets a menu property.
*
* @note If the value of the property is a string, you should pass
* two additional parameters - one for the buffer and another one
* for the buffer length. Integer values are directly returned by
* the function, so no extra parameters are required.
* Example #1: menu_getprop(iMenu, MPROP_TITLE, szTitle, charsmax(szTitle))
* Example #2: new iPerPage = menu_getprop(iMenu, MPROP_PERPAGE)
*
* @param menu Menu resource identifier.
* @param prop MPROP_ constant.
* @param ... Property parameters.
* @return Menu property if the property type is an integer.
* @error Invalid menu resource, invalid property or invalid amount of parameters.
*/
native menu_getprop(menu, prop, ...);

/**
* Cancels a player's menu, effectively forcing the player to select MENU_EXIT.
* The menu will still exist on their screen but any results are invalidated,
Expand Down