[INC] OldMenu (oldmenu menu wrapper)

For scripting examples for learning how to use AMXX.
User avatar
Archive-Poster
Posts: 1189
Gender:
United States of America

[INC] OldMenu (oldmenu menu wrapper)

Post by Archive-Poster »

A wrapper for show_menu() — oldmenu.inc

In my experience, menu_create can feel too verbose for a simple menu, and too rigid when you need full control over a dynamic menu. This include wraps the show_menu native to handle both cases cleanly.

For simple menus the boilerplate is minimal. For paginated dynamic lists, the pagination and page calculation helpers take care of the repetitive parts without hiding what's happening underneath.
/* using on oldmenu_set_color() */
enum MenuColor
{
MenuColor_Disabled=0, // \d
MenuColor_White, // \w
MenuColor_Red, // \r
MenuColor_Yellow, // \y
}

/**
* Must be called once in plugin_init().
* Registers the internal menu handler.
*/
stock oldmenu_register()

/**
* Sets the colors used by oldmenu_additem_ex and oldmenu_pagination.
* Defaults: (MenuColor_Red, MenuColor_White).
*
* - colorNumber: Color for the key number.
* - colorText: Color for the item label.
*/
stock oldmenu_set_color(MenuColor:colorNumber, MenuColor:colorText)

/**
* Sets the player index used for LANG translations in oldmenu_pagination and oldmenu_addexit.
* Defaults to server language (0).
*
* - lang_player: Player index, 0 = server language.
*/
stock oldmenu_set_lang(lang_player=0)

/**
* Starts building a new menu.
* Calls set_fail_state if handler is invalid.
*
* - handler: Name of a valid public function in the plugin.
* - title: Menu title, supports formatting.
*
* Handler:
* public my_handler(id, keynum, value, page)
* - id: Player index.
* - keynum: Key pressed (1-9, 0=exit).
* - value: Integer associated with the key via oldmenu_additem.
* - page: Page passed to oldmenu_display.
*/
stock oldmenu_create(const handler[], const title[], any:...)

/**
* Adds a selectable item to the menu.
*
* - keynum: Key to bind (1-9, 0=exit). Pass -1 for a non-selectable item.
* - value: Arbitrary integer forwarded to the handler when key is pressed.
* - item: Item text, supports formatting.
*/
stock oldmenu_additem(keynum, value, const item[], any:...)

/**
* Same as oldmenu_additem but automatically prepends the key number with the configured colors.
* Pass a negative keynum to render the item as disabled.
*
* - keynum: Key to bind (1-9). Negative value disables the item.
* - value: Arbitrary integer forwarded to the handler when key is pressed.
* - item: Item text without key number, supports formatting.
*/
stock oldmenu_additem_ex(keynum, value, const item[], any:...)

/**
* Adds a plain text line with no key binding.
*
* - item: Text to display, supports formatting.
*/
stock oldmenu_addtext(const item[], any:...)

/**
* Adds the exit item (key 0) using LANG key EXIT.
*
* - enable: If false, renders the exit item as disabled.
* - padding: If true, adds a blank line before the exit item.
*/
stock oldmenu_addexit(enable=true, padding=true)

/**
* Adds Back (8), Next (9), and Exit (0) items using LANG keys BACK, MORE, EXIT.
* Back and Next are disabled automatically when on first/last page.
*
* - page: Current page.
* - maxpages: Total number of pages.
* - padding: If true, adds a blank line when the menu has only one page.
*/
stock oldmenu_pagination(page, maxpages, padding=true)

/**
* Sends the built menu to a player or all players.
*
* - id: Player index. Pass 0 to broadcast to all connected players.
* - page: Page number stored and forwarded to the handler.
* - display_time: Menu timeout in seconds. -1 means no timeout.
*/
stock oldmenu_display(id, page=1, display_time=-1)

/**
* Calculates pagination values from a total item count.
* Clamps page to valid range.
*
* - maxpages: Output: total number of pages.
* - start: Output: start index for the current page slice.
* - end: Output: end index for the current page slice.
* - page: Input/Output: current page, clamped to valid range.
* - maxitems: Total number of items.
* - perpage: Items per page.
* - start_offset: Optional offset applied to the start index.
*/
stock oldmenu_calculate_pages(&maxpages, &start, &end, &page, const maxitems, const perpage=7, const start_offset=0)

/**
* Returns the current oldmenu state for a player.
*
* - id: Player index.
* - return: 0 if no menu is open, -1 if a different menu is open,
* positive value if an oldmenu is currently open.
*/
stock oldmenu_get_player_menu(id)
Basic Example:

Code: Select all

#include <amxmodx>
#include <oldmenu>

public plugin_init()
{
    register_clcmd("say /yesno", "show_menu_basic_yesno")

    oldmenu_register()
}

public show_menu_basic_yesno(id)
{
    oldmenu_create("menu_basic_yesno", "\rBasic Menus Yes/No")

    oldmenu_additem_ex(1, 0, "Yes")
    oldmenu_additem_ex(2, 0, "No")

    oldmenu_display(id)
}

public menu_basic_yesno(id, keynum)
{
    if(keynum == 1)
        client_print(id, print_chat, "You press YES!")

}
Extended:

Code: Select all

public show_menu_test(id)
{
    // Create menu:
    oldmenu_create("menu_test", "\rTest %s:", "OldMenu")

    // Manual method: full control over formatting
    oldmenu_additem(1, 111, "\r%d. \wItem 1", 1)
    oldmenu_additem(2, 222, "\r%s. \wItem 2", "2")
    oldmenu_additem(3, 333, "\r3. \wItem 3")
    oldmenu_additem(-1, 0, "\d4. Item 4 DISABLED")
    oldmenu_addtext("Only Text line!!")

    // Easy method: key number and colors handled automatically
    oldmenu_additem_ex(6, 666, "Item 6")
    oldmenu_additem_ex(-7, 777, "Item 7 DISABLED") // negative keynum = disabled

    // Exit item
    oldmenu_additem(0, 0, "^n\r0. \wSalir")
    
    /* OR Easy:
    oldmenu_set_lang(id) // optional, defaults to server language
    oldmenu_addexit()
    */

    oldmenu_display(id)
}

public menu_test(id, keynum, value, page)
{
    client_print(id, print_chat, "keynum: %d - value: %d - page: %d", keynum, value, page)
}
Player menu and pagination:

Code: Select all

public show_menu_players(id, page)
{
    new players[MAX_PLAYERS], num
    get_players(players, num, "h")

    new maxpages, start, end
    oldmenu_calculate_pages(maxpages, start, end, page, num)

    oldmenu_create("menu_players", "\rPlayers: %d/%d", page, maxpages)

    new name[32]
    for(new i=start, count=1; i < end; i++, count++)
    {
        get_user_name(players[i], name, 31)
        oldmenu_additem(count, players[i], "\r%d. \w%s", count, name)
    }

    // oldmenu_set_lang(id) // optional, defaults to server language
    oldmenu_pagination(page, maxpages)

    oldmenu_display(id, page)
}

public menu_players(id, keynum, value, page)
{
    if(keynum == 0) // Exit
        return

    if(keynum >= 8) // Back (8) or Next (9): value is -1 or 1
    {
        show_menu_players(id, page + value)
        return
    }

    // value = player index of the selected item
    new name[32]
    get_user_name(value, name, 31)
    client_print(id, print_chat, "PlayerID: %d - Name: %s", value, name)
}
Attachments
oldmenu.inc.zip
(3.38 KiB) Downloaded 6 times
test_oldmenu.sma.zip
(1.15 KiB) Downloaded 6 times

Who is online

Users browsing this forum: No registered users and 1 guest