AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Snippets and Tutorials (https://forums.alliedmods.net/forumdisplay.php?f=112)
-   -   [Clientprefs] Easy way to add submenu to the cookie settings menu. (https://forums.alliedmods.net/showthread.php?t=309889)

Bijleveldje 08-10-2018 18:08

[Clientprefs] Easy way to add submenu to the cookie settings menu.
 
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 clientbool 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 cookieCookieMenu type, const char[] displayCookiemenuCallback callback) {
    
DataPack pack = new DataPack();
    
pack.WriteCell(view_as<int>(cookie));
    
pack.WriteFunction(callback);

    switch(
type) {
        case 
CookieMenu_YesNoCookieMenu_YesNo_Int: {
            
SetCookieMenuItem(CookieHandler_YesNopackdisplay);
        }

        case 
CookieMenu_OnOffCookieMenu_OnOff_Int: {
            
SetCookieMenuItem(CookieHandler_OnOffpackdisplay);
        }
    }
}

static 
void CookieHandler_YesNo(int clientCookieMenuAction actionany infochar[] bufferint maxlen) {
    
Menu menu = new Menu(MenuHandler_CookieMenu);

    
DataPack pack view_as<DataPack>(info);
    
pack.Reset(false);

    
char sPack[64];
    
IntToString(view_as<int>(pack), sPacksizeof(sPack));

    
menu.AddItem(sPack"Yes");
    
menu.AddItem(sPack"No");
    
menu.ExitButton true;

    
menu.Display(client10);
}

static 
void CookieHandler_OnOff(int clientCookieMenuAction actionany infochar[] bufferint maxlen) {
    
Menu menu = new Menu(MenuHandler_CookieMenu);

    
char sPack[64];
    
IntToString(view_as<int>(info), sPacksizeof(sPack));

    
menu.AddItem(sPack"On");
    
menu.AddItem(sPack"Off");
    
menu.ExitButton true;

    
menu.Display(client10);
}

static 
int MenuHandler_CookieMenu(Menu menuMenuAction actionint param1int param2) {
    if(
action == MenuAction_Select) {
        
char info[64];
        
menu.GetItem(param2infosizeof(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(param1cookie"1");
            }
            
//No/Off
            
case 1: {
                
SetClientCookie(param1cookie"0");
            }
        }

        
Call_StartFunction(nullpack.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.


All times are GMT -4. The time now is 10:06.

Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.