View Single Post
condolent
AlliedModders Donor
Join Date: Jan 2016
Location: gc_sLocation;
Old 06-09-2017 , 19:54   Re: Error when adding entry to admin menu
Reply With Quote #10

Quote:
Originally Posted by Fyren View Post
Here's a sample that makes a category and adds one item. I tried to explain what's going on.

PHP Code:
#include <sourcemod>
#undef REQUIRE_PLUGIN
#include <adminmenu>

#define MY_CATEGORY "My Custom Category" 
#define MY_CATEGORY_OVERRIDE "My Custom Category Override"
#define MY_ITEM "My Custom Item" 
#define MY_ITEM_OVERRIDE "My Custom Item Override"

TopMenuObject myCategory;
TopMenuObject myItem;

public 
void OnPluginStart() {
    
/* handle late loading */
    
TopMenu am;
    if (
LibraryExists("adminmenu") && ((am GetAdminTopMenu()) != null)) {
        
OnAdminMenuCreated(am);
        
OnAdminMenuReady(am);
    }
}
 
/*
    When the adminmenu plugin creates the menu, it calls this forward.

    Add categories here.
*/ 
public void OnAdminMenuCreated(Handle am)
{
    
TopMenu adminMenu view_as<TopMenu>(am);
    
myCategory adminMenu.AddCategory(MY_CATEGORYTMHandlerMY_CATEGORY_OVERRIDEADMFLAG_GENERIC);
}

/*
    This forward is called immediately after the above one is called for all plugins.

    If you want to add things to a different plugin's category, you have do it here to be sure the category
    was created.  I could have added items above since I know I just created the category, but I'll do it
    here anyway.
*/
public void OnAdminMenuReady(Handle am)
{
    
TopMenu adminMenu view_as<TopMenu>(am);
    
myItem adminMenu.AddItem(MY_ITEMTMHandlermyCategoryMY_ITEM_OVERRIDEADMFLAG_GENERIC); 
}

/* 
    You can have separate handlers for everything, if you want.  I'm using one for both the category and item
    I created above.

    The object will be the category or item object returned from AddCategory/AddItem.  Since I'm only using
    one handler, I have to check it against myCategory/myItem.  If you have separate handlers for everything
    you added, you wouldn't need to.

    Here's the list of possible actions: https://sm.alliedmods.net/new-api/topmenus/TopMenuAction

    param will be the client ID for most actions.

    I didn't handle translations, but you could make MY_CATEGORY/MY_ITEM translation names and use %T/%t
    in the Format calls below.
*/
public void TMHandler(TopMenu tmTopMenuAction actTopMenuObject objint paramchar[] bufint len)
{
    if (
obj == myCategory
        switch (
act) {
            case 
TopMenuAction_DisplayOption//put the string for the category name in buf
                
Format(buflenMY_CATEGORY);
            case 
TopMenuAction_DisplayTitle//put the string for the category header in buf
                
Format(buflenMY_CATEGORY);
        }
    else if (
obj == myItem)
        switch (
act) {
            case 
TopMenuAction_DisplayOption//put the string for the menu item in buf
                
Format(buflenMY_ITEM);
            case 
TopMenuAction_SelectOption//do something since the user picked the item
                
PrintToChat(param"Hello.");
        }

If you use separate handlers, you can get rid of the globals.
Thank you so much for this!!
__________________
condolent is offline