This is a variant for the SetCookiePrefabMenu function, because i hate the way that function works.
The difference with this function is that the callback is called after the cookie value has changed, and the SetCookiePrefabMenu is called when only the submenu is selected.
PHP Code:
#if defined _cookiemenu_included_
#endinput
#endif
#define _cookiemenu_included_
/**
* Defines a cookiemenu callback.
*
* @param client Client that changed his cookie value
* @param selection Bool of the client value
* @noreturn
*/
typedef CookiemenuCallback = function void(int client, bool selection);
/**
* Registers a cookie menu item, with a callback that is fired after the cookie value is changes
*
* @param cookie Cookie handle
* @param type Cookiemenu type
* @param display String to display in the menu
* @param callback CookiemenuCallback, callback function
* @noreturn
*/
public void SetCookieMenu(Handle cookie, CookieMenu type, const char[] display, CookiemenuCallback callback) {
DataPack pack = new DataPack();
pack.WriteCell(view_as<int>(cookie));
pack.WriteFunction(callback);
switch(type) {
case CookieMenu_YesNo, CookieMenu_YesNo_Int: {
SetCookieMenuItem(CookieHandler_YesNo, pack, display);
}
case CookieMenu_OnOff, CookieMenu_OnOff_Int: {
SetCookieMenuItem(CookieHandler_OnOff, pack, display);
}
}
}
static void CookieHandler_YesNo(int client, CookieMenuAction action, any info, char[] buffer, int maxlen) {
Menu menu = new Menu(MenuHandler_CookieMenu);
DataPack pack = view_as<DataPack>(info);
pack.Reset(false);
char sPack[64];
IntToString(view_as<int>(pack), sPack, sizeof(sPack));
menu.AddItem(sPack, "Yes");
menu.AddItem(sPack, "No");
menu.ExitButton = true;
menu.Display(client, 10);
}
static void CookieHandler_OnOff(int client, CookieMenuAction action, any info, char[] buffer, int maxlen) {
Menu menu = new Menu(MenuHandler_CookieMenu);
char sPack[64];
IntToString(view_as<int>(info), sPack, sizeof(sPack));
menu.AddItem(sPack, "On");
menu.AddItem(sPack, "Off");
menu.ExitButton = true;
menu.Display(client, 10);
}
static int MenuHandler_CookieMenu(Menu menu, MenuAction action, int param1, int param2) {
if(action == MenuAction_Select) {
char info[64];
menu.GetItem(param2, info, sizeof(info));
DataPack pack = view_as<DataPack>(StringToInt(info));
pack.Reset(false);
Handle cookie = view_as<Handle>(pack.ReadCell());
switch (param2) {
//Yes/On
case 0: {
SetClientCookie(param1, cookie, "1");
}
//No/Off
case 1: {
SetClientCookie(param1, cookie, "0");
}
}
Call_StartFunction(null, pack.ReadFunction());
Call_PushCell(param1);
Call_PushCell(view_as<bool>(!param2));
int result;
Call_Finish(result);
}
if(action == MenuAction_End) {
delete menu;
}
}
If you have any suggestions to make this better, please post them below.