implemented amb17

expanded menu tests
deprecated MEXIT_NORMAL, MPROP_ORDER, MPROP_PADMENU
fixed various bugs in PagekeyToItem
This commit is contained in:
David Anderson 2007-04-16 06:58:28 +00:00
parent 8dc310f1bb
commit 5d4669d52e
4 changed files with 134 additions and 107 deletions

View File

@ -90,17 +90,11 @@ Menu::Menu(const char *title, int mid, int tid)
m_OptNames[abs(MENU_MORE)].assign("More");
m_OptNames[abs(MENU_EXIT)].assign("Exit");
m_OptOrders[0] = MENU_BACK;
m_OptOrders[1] = MENU_MORE;
m_OptOrders[2] = MENU_EXIT;
m_AlwaysExit = false;
m_NeverExit = false;
m_AutoColors = g_coloredmenus;
items_per_page = 7;
func = 0;
padding = 0;
isDestroying = false;
}
@ -164,9 +158,11 @@ int Menu::PagekeyToItem(page_t page, item_t key)
if (num_pages == 1 || !items_per_page)
{
if (key > m_Items.size())
{
return MENU_EXIT;
else
} else {
return key-1;
}
} else {
//first page
if (page == 0)
@ -192,10 +188,10 @@ int Menu::PagekeyToItem(page_t page, item_t key)
}
}
key = new_key;
if (key == items_per_page + 1)
if (key == items_per_page + 2)
{
return MENU_MORE;
} else if (key == items_per_page + 2) {
} else if (key == items_per_page + 3) {
return MENU_EXIT;
} else {
return (start + key - 1);
@ -214,10 +210,10 @@ int Menu::PagekeyToItem(page_t page, item_t key)
}
}
}
if (key == remaining + 1)
if (key == items_per_page + 1)
{
return MENU_BACK;
} else if (key == remaining + 2) {
} else if (key == items_per_page + 3) {
return MENU_EXIT;
} else {
return (start + key - 1);
@ -227,7 +223,7 @@ int Menu::PagekeyToItem(page_t page, item_t key)
* one from the key for each space we find along the way.
*/
item_t new_key = key;
for (size_t i=start; i<(start+key-1) && i<m_Items.size(); i++)
for (size_t i=start; i<(start+items_per_page-1) && i<m_Items.size(); i++)
{
for (size_t j=0; j<m_Items[i]->blanks.size(); j++)
{
@ -246,9 +242,11 @@ int Menu::PagekeyToItem(page_t page, item_t key)
}
}
key = new_key;
if (key > items_per_page && (key-items_per_page<=3))
if (key > items_per_page && (key-items_per_page<3))
{
return m_OptOrders[key-items_per_page-1];
unsigned int num = key - items_per_page - 1;
static int map[] = {MENU_BACK, MENU_MORE, MENU_EXIT};
return map[num];
} else {
return (start + key - 1);
}
@ -309,10 +307,10 @@ const char *Menu::GetTextString(int player, page_t page, int &keys)
{
Display_Back = (1<<0),
Display_Next = (1<<1),
Display_Exit = (1<<2),
};
int flags = Display_Back|Display_Next;
item_t start = page * items_per_page;
item_t end = 0;
if (items_per_page)
@ -324,17 +322,18 @@ const char *Menu::GetTextString(int player, page_t page, int &keys)
} else {
end = start + items_per_page - 1;
}
if (!m_NeverExit && (m_AlwaysExit || (page == 0 || page == pages-1)))
flags |= Display_Exit;
} else {
end = numItems - 1;
if (end > 10)
{
end = 10;
flags = 0;
}
}
if (page == 0)
{
flags &= ~Display_Back;
}
menuitem *pItem = NULL;
@ -424,33 +423,19 @@ const char *Menu::GetTextString(int player, page_t page, int &keys)
}
}
if (padding == 1 && items_per_page)
if (items_per_page)
{
int pad = items_per_page;
if (flags & Display_Back)
{
pad--;
}
if (flags & Display_Next)
{
pad--;
}
if (flags & Display_Exit)
{
pad--;
}
for (int i=slots+1; i<=pad; i++)
/* Pad spaces until we reach the end of the max possible items */
for (unsigned int i=(unsigned)slots; i<items_per_page; i++)
{
m_Text.append("\n");
option++;
}
}
/* Make sure there is at least one visual pad */
m_Text.append("\n");
for (int i=0; i<3; i++)
{
switch (m_OptOrders[i])
{
case MENU_BACK:
/* Don't bother if there is only one page */
if (pages > 1)
{
if (flags & Display_Back)
{
@ -459,14 +444,22 @@ const char *Menu::GetTextString(int player, page_t page, int &keys)
sizeof(buffer)-1,
m_AutoColors ? "\\r%d. \\w%s\n" : "%d. %s\n",
option == 10 ? 0 : option,
m_OptNames[abs(MENU_BACK)].c_str()
);
m_Text.append(buffer);
}
break;
}
case MENU_MORE:
m_OptNames[abs(MENU_BACK)].c_str());
} else {
option++;
if (m_AutoColors)
{
_snprintf(buffer,
sizeof(buffer)-1,
"\\d%d. %s\n\\w",
option == 10 ? 0 : option,
m_OptNames[abs(MENU_BACK)].c_str());
} else {
_snprintf(buffer, sizeof(buffer)-1, "#. %s\n", m_OptNames[abs(MENU_BACK)].c_str());
}
}
m_Text.append(buffer);
if (flags & Display_Next)
{
keys |= (1<<option++);
@ -474,28 +467,36 @@ const char *Menu::GetTextString(int player, page_t page, int &keys)
sizeof(buffer)-1,
m_AutoColors ? "\\r%d. \\w%s\n" : "%d. %s\n",
option == 10 ? 0 : option,
m_OptNames[abs(MENU_MORE)].c_str()
);
m_Text.append(buffer);
}
break;
}
case MENU_EXIT:
m_OptNames[abs(MENU_MORE)].c_str());
} else {
option++;
if (m_AutoColors)
{
if (flags & Display_Exit)
_snprintf(buffer,
sizeof(buffer)-1,
"\\d%d. %s\n\\w",
option == 10 ? 0 : option,
m_OptNames[abs(MENU_MORE)].c_str());
} else {
_snprintf(buffer, sizeof(buffer)-1, "#. %s\n", m_OptNames[abs(MENU_MORE)].c_str());
}
}
m_Text.append(buffer);
} else {
/* Keep padding */
option += 2;
}
if (!m_NeverExit)
{
keys |= (1<<option++);
_snprintf(buffer,
sizeof(buffer)-1,
m_AutoColors ? "\\r%d. \\w%s\n" : "%d. %s\n",
option == 10 ? 0 : option,
m_OptNames[abs(MENU_EXIT)].c_str()
);
m_OptNames[abs(MENU_EXIT)].c_str());
m_Text.append(buffer);
}
break;
}
}
}
return m_Text.c_str();
@ -810,25 +811,17 @@ static cell AMX_NATIVE_CALL menu_setprop(AMX *amx, cell *params)
case MPROP_EXITALL:
{
cell ans = *get_amxaddr(amx, params[3]);
if (ans == 1)
if (ans == 1 || ans == 0)
{
pMenu->m_AlwaysExit = true;
pMenu->m_NeverExit = false;
} else if (ans == 0) {
pMenu->m_AlwaysExit = false;
pMenu->m_NeverExit = false;
} else if (ans == -1) {
pMenu->m_NeverExit = true;
pMenu->m_AlwaysExit = false;
}
break;
}
case MPROP_ORDER:
{
cell *addr = get_amxaddr(amx, params[3]);
pMenu->m_OptOrders[0] = addr[0];
pMenu->m_OptOrders[1] = addr[1];
pMenu->m_OptOrders[2] = addr[2];
/* Ignored as of 1.8.0 */
break;
}
case MPROP_NOCOLORS:
@ -838,7 +831,7 @@ static cell AMX_NATIVE_CALL menu_setprop(AMX *amx, cell *params)
}
case MPROP_PADMENU:
{
pMenu->padding = *get_amxaddr(amx, params[3]);
/* Ignored as of 1.8.0 */
break;
}
default:

View File

@ -39,6 +39,7 @@
#define ITEM_ENABLED 1
#define ITEM_DISABLED 2
#define MAX_MENU_ITEMS 10
#define MPROP_PERPAGE 1
#define MPROP_BACKNAME 2
@ -92,16 +93,13 @@ public:
String m_Text;
String m_OptNames[4];
int m_OptOrders[3];
bool m_AlwaysExit;
bool m_NeverExit;
bool m_AutoColors;
int menuId;
int thisId;
int func;
int padding;
bool isDestroying;
public:
unsigned int items_per_page;

View File

@ -280,12 +280,12 @@ enum {
#define FORWARD_ONLY_NEW 2
#define FORWARD_ALL 3
#define MEXIT_ALL 1
#define MEXIT_NORMAL 0
#define MEXIT_NEVER -1
#define MEXIT_ALL 1 /* (default) Menu will have an exit option */
#define MEXIT_NORMAL 0 /* Deprecated, has no effect */
#define MEXIT_NEVER -1 /* Menu will not have an exit option */
#define MENUPAD_NONE 0 //no padding (default)
#define MENUPAD_PAGE 1 //pads based on items per page
#define MENUPAD_NONE 0 /* Deprecated, not used */
#define MENUPAD_PAGE 1 /* Deprecated, not used */
#define MPROP_PERPAGE 1 //NUM - items per page. 0=no paginating, 7=default
#define MPROP_BACKNAME 2 //STRING - name of "back" button
@ -293,12 +293,9 @@ enum {
#define MPROP_EXITNAME 4 //STRING - name of "exit" button
#define MPROP_TITLE 5 //STRING - sets menu title
#define MPROP_EXIT 6 //NUM - sets how "Exit" appears on the menu
#define MPROP_ORDER 7 //Sets the back/next/exit order
//Send an 3-cell array with MENU_EXIT etc in the order
//(from 0-2), 0 being first
#define MPROP_ORDER 7 /* Deprecated, has no effect */
#define MPROP_NOCOLORS 8 //NUM - sets whether colors are not auto (0=default)
#define MPROP_PADMENU 9 //NUM - sets how the ending portions of a menu are padded
//i.e. 2 items, 7 per page, with endings at slots 5/6/7.
#define MPROP_PADMENU 9 /* Deprecated, has no effect */
#define SND_SPAWNING (1<<8) // we're spawing, used in some cases for ambients
#define SND_STOP (1<<5) // stop sound

View File

@ -8,6 +8,7 @@ public plugin_init()
register_clcmd("menu_test2", "Test_Menu2")
register_clcmd("menu_test3", "Test_Menu3")
register_clcmd("menu_test4", "Test_Menu4")
register_clcmd("menu_test5", "Test_Menu5")
}
public Test_Menu1(id, level, cid)
@ -20,7 +21,8 @@ public Test_Menu1(id, level, cid)
menu_additem(menu, "Gabezilla 5", "5", 0)
menu_additem(menu, "Gabezilla 6", "6", 0)
menu_addblank(menu, 7)
menu_setprop(menu, MPROP_EXIT, MEXIT_ALL)
menu_additem(menu, "Gabezilla 7", "7", 0)
menu_setprop(menu, MPROP_EXIT, MEXIT_NEVER)
menu_display(id, menu, 0)
return PLUGIN_HANDLED
}
@ -71,6 +73,7 @@ public Test_Menu3(id)
menu_additem(mHandleID, "test10", "10", 0)
menu_additem(mHandleID, "test11", "11", 0)
menu_addblank(mHandleID, 1) // add blank got problem
menu_setprop(mHandleID, MPROP_PERPAGE, 5)
menu_display(id, mHandleID, 0)
@ -105,7 +108,6 @@ public Test_Menu4(id)
menu_additem(mHandleID, "test7", "7", 0)
menu_additem(mHandleID, "test8", "8", 0)
menu_additem(mHandleID, "test9", "9", 0)
menu_additem(mHandleID, "test10", "10", 0)
menu_display(id, mHandleID, 0)
@ -126,3 +128,40 @@ public Test_Menu4_Handler(id, menu, item)
return PLUGIN_HANDLED
}
public Test_Menu5(id)
{
new mHandleID = menu_create("Test Menu 5", "Test_Menu5_Handler")
menu_additem(mHandleID, "test1", "1", 0)
menu_additem(mHandleID, "test2", "2", 0)
menu_additem(mHandleID, "test3", "3", 0)
menu_additem(mHandleID, "test4", "4", 0)
menu_additem(mHandleID, "test5", "5", 0)
menu_additem(mHandleID, "test6", "6", 0)
menu_additem(mHandleID, "test7", "7", 0)
menu_additem(mHandleID, "test8", "8", 0)
menu_additem(mHandleID, "test9", "9", 0)
menu_additem(mHandleID, "test10", "10", 0)
menu_additem(mHandleID, "test11", "11", 0)
menu_addblank(mHandleID, 1) // add blank got problem
menu_setprop(mHandleID, MPROP_EXIT, MEXIT_NEVER)
menu_display(id, mHandleID, 0)
return PLUGIN_HANDLED
}
public Test_Menu5_Handler(id, menu, item)
{
if (item == MENU_EXIT)
{
menu_destroy(menu)
return PLUGIN_HANDLED
}
client_print(id, print_chat, "item = %d", item)
menu_destroy(menu)
return PLUGIN_HANDLED
}