Raised This Month: $ Target: $400
 0% 

Solved how to add item for the category number 8 of the menu


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CrazY.
Veteran Member
Join Date: May 2015
Location: SP, Brazil
Old 04-24-2022 , 10:58   Re: how to add item for the category number 8 of the menu
Reply With Quote #1

Just renaming the "back" option won't work as MENU_BACK and MENU_NEXT don't trigger the menu handler.

Not sure what you mean by "I don't trust that code", if you are not aware, newmenus is just a oldmenu but made with the intent to make the developer's life easier whenever one needs a menu with dynamic items/pagination.

Newmenus does store the items in a vector, as I did with arrays

Code:
menuitem *Menu::AddItem(const char *name, const char *cmd, int access)
{
	menuitem *pItem = new menuitem;

	pItem->name = name;
	pItem->cmd = cmd;
	pItem->access = access;
	pItem->id = m_Items.length();
	pItem->handler = -1;
	pItem->isBlank = false;
	pItem->pfn = NULL;

	m_Items.append(pItem);

	return pItem;
}
If you check the source code of newmenus, you will clearly see that it does pretty much the same thing under the hood.

https://github.com/alliedmodders/amx...menus.cpp#L350

So again, I don't get your point, maybe you should provide a working solution, otherwise whatever you say is irrelevant.

-----------------

menu_additem doesn't actually add an item to the menu, it just stores it to a list of items to be used later when you call menu_display.

The problem with the oldmenu approach is that it assumes all items to be available in the menu when calculating the pagination, no matter the location, for that reason, skipping an item from being added won't behave as newmenus.

To get that done, you first have to define which items should be skipped. A solution would be to loop through all items and store the indexes of the ones to be added in a temporary array and use that array to create the menu instead of the g_menuItems one.

Working solution:

Code:
#include <amxmodx>
#include <amxmisc>

new Array:g_menuItems

enum _:ArrayTest
{
	Location,
	Item[ 32 ]
}

new g_playerMenuPage[33]
new g_menuloction[33]
new g_playerMenuItemData[33][10]
new eData[ ArrayTest ]

public plugin_init()
{
	g_menuItems = ArrayCreate(ArrayTest)

	for (new i = 1; i <= 5; i++)
	{
		eData[ Location ] = 1;
		copy( eData[ Item ], charsmax( eData[ Item ] ), fmt( "Item %d", i ) )
		ArrayPushArray(g_menuItems, eData )
	}
	
	for (new i = 1; i <= 5; i++)
	{
		eData[ Location ] = 2;
		copy( eData[ Item ], charsmax( eData[ Item ] ), fmt( "ItemPlus %d", i ) )
		ArrayPushArray(g_menuItems, eData )
	}

	register_clcmd("say /menu", "@TestMenu")

	register_menu("Cool Menu", -1, "MenuHandler")
}

@TestMenu( id )
{
	new iMenu = menu_create( "\yTest Menu:", "@TestHandler" );
	
	menu_additem( iMenu, "Location One", "1" );
	menu_additem( iMenu, "Location Two", "2" );
	
	menu_display(id, iMenu)
	return PLUGIN_HANDLED;
}

@TestHandler( id, iMenu, iItem ) 
{
	if(iItem != MENU_EXIT) 
	{
		new szData[ 10 ], iUnused;
		menu_item_getinfo( iMenu, iItem, iUnused, szData, charsmax( szData ), .callback = iUnused )
		
		new iItemID = str_to_num( szData ); 
		
		ShowMenu(id, iItemID )
	}
	menu_destroy(iMenu);
	return PLUGIN_HANDLED;

}


public ShowMenu(index, iLocation )
{
	new Array:itemsToAdd = ArrayCreate()

	for (new i = 0, data[ArrayTest], count = ArraySize(g_menuItems); i < count; i++)
	{
		// Add any item skipping logic here

		ArrayGetArray(g_menuItems, i, data)

		if (data[Location] && data[Location] != iLocation)
		{
			continue
		}

		ArrayPushCell(itemsToAdd, i)
	}

	const itemsPerPage = 7
	new itemCount = ArraySize(itemsToAdd)

	if (itemCount == 0)
	{
		client_print(index, print_chat, "No items to display.")
		return
	}

	new pageCount = floatround(itemCount / float(itemsPerPage), floatround_ceil)
	new page = clamp(g_playerMenuPage[index], 0, pageCount - 1)
	new offset = page * itemsPerPage
	g_menuloction[ index ] = iLocation
	new menu[MAX_MENU_LENGTH], len, key

	len = copy(menu, charsmax(menu), "\yCool Menu")

	if (pageCount > 1)
	{
		len += formatex(menu[len], charsmax(menu) - len, " %d/%d", page + 1, pageCount)
	}

	len += copy(menu[len], charsmax(menu) - len, "^n^n")

	arrayset(g_playerMenuItemData[index], -1, sizeof g_playerMenuItemData[])

	for (new i = offset, itemId, limit = min(itemCount, offset + itemsPerPage); i < limit; i++)
	{
		itemId = ArrayGetCell(itemsToAdd, i)
		
		ArrayGetArray(g_menuItems, itemId, eData )
		
		g_playerMenuItemData[index][key] = itemId
		
		len += formatex(menu[len], charsmax(menu) - len, "\r%d. \w%s^n", ++key, eData[ Item ])
	}

	ArrayDestroy(itemsToAdd)

	for (new i = key; i <= itemsPerPage; i++)
	{
		len += copy(menu[len], charsmax(menu) - len, "^n")
	}

	if (pageCount > 1)
	{
		if (page > 0)
		{
			len += copy(menu[len], charsmax(menu) - len, "\r8. \wBack^n")
		}
		else
		{
			len += copy(menu[len], charsmax(menu) - len, "\r8. \dBack^n")
		}
		
		if (page < pageCount - 1)
		{
			len += copy(menu[len], charsmax(menu) - len, "\r9. \wMore^n")
		}
		else
		{
			len += copy(menu[len], charsmax(menu) - len, "\r9. \dMore^n")
		}
	}

	len += copy(menu[len], charsmax(menu) - len, "\r0. \wExit")

	g_playerMenuPage[index] = page

	show_menu(index, -1, menu, -1, "Cool Menu")
}

public MenuHandler(index, key)
{
	switch (key)
	{
		case 7:
		{
			// Back
			g_playerMenuPage[index] -= 1
			ShowMenu(index, g_menuloction[ index ])
		}
		case 8:
		{
			// More
			g_playerMenuPage[index] += 1
			ShowMenu(index, g_menuloction[ index ])
		}
		case 9:
		{
			// Exit
		}
		default:
		{
			new item = g_playerMenuItemData[index][key]

			if (0 <= item < ArraySize(g_menuItems))
			{
				new buffer[32]
				ArrayGetString(g_menuItems, item, buffer, charsmax(buffer))
				client_print(index, print_chat, "You've selected ^"%s^"", buffer)
			}

			ShowMenu(index, g_menuloction[ index ])
		}
	}

	return PLUGIN_HANDLED
}
__________________








CrazY. is offline
Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 20:28.


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