Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*----------------------------------------------------------------------------*-
- ============================
- Y Sever Includes - Menu Core
- ============================
- Description:
- Allows you to define menus with as many rows as you like and handles the
- pages internally, allowing you to simply process the rows selected as if it
- was one huge long menu.
- Legal:
- Copyright (C) 2007 Alex "Y_Less" Cole
- This program is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License
- as published by the Free Software Foundation; either version 2
- of the License, or (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- MA 02110-1301, USA.
- Version:
- 0.0.1
- Changelog:
- 23/11/07:
- First version.
- Functions:
- Public:
- -
- Core:
- -
- Stock:
- -
- Static:
- -
- Inline:
- -
- API:
- -
- Callbacks:
- -
- Definitions:
- -
- Enums:
- -
- Macros:
- -
- Tags:
- -
- Variables:
- Global:
- -
- Static:
- -
- Commands:
- -
- Compile options:
- -
- -*----------------------------------------------------------------------------*/
- #if !defined MAX_MENU_LINE
- #define MAX_MENU_LINE (32)
- #endif
- #define NO_MENU (-1)
- #define NO_MENU_ROW (-1)
- enum e_MENU_FLAGS (<<= 1)
- {
- e_MENU_FLAGS_NONE,
- e_MENU_FLAGS_TWO = 1,
- e_MENU_FLAGS_ACTIVE,
- e_MENU_FLAGS_SHOWN
- }
- enum E_MENU_DATA
- {
- E_MENU_DATA_TITLE[MAX_MENU_LINE],
- E_MENU_COL_HEAD0[MAX_MENU_LINE],
- E_MENU_COL_HEAD1[MAX_MENU_LINE],
- E_MENU_DATA_FIRST,
- E_MENU_DATA_ROWS,
- e_MENU_FLAGS:E_MENU_DATA_DATA,
- Float:E_MENU_DATA_X,
- Float:E_MENU_DATA_Y,
- Float:E_MENU_DATA_C1,
- Float:E_MENU_DATA_C2
- }
- enum E_MENU_ROW
- {
- E_MENU_ROW_NEXT,
- e_MENU_FLAGS:E_MENU_ROW_DATA,
- E_MENU_ROW_COL1[MAX_MENU_LINE],
- E_MENU_ROW_COL2[MAX_MENU_LINE],
- }
- enum E_PLAYER_MENU
- {
- Menu:E_PLAYER_MENU_REAL,
- E_PLAYER_MENU_MENU,
- E_PLAYER_MENU_PAGE
- }
- static
- YSI_g_sMenuData[MAX_DYN_MENUS][E_MENU_DATA],
- YSI_g_sMenuItems[MAX_MENU_ROWS][E_MENU_ROW],
- YSI_g_sPlayerMenus[MAX_PLAYERS][E_PLAYER_MENU],
- YSI_g_sUnusedRows;
- #define Menu_IsValid(%1) \
- ((%1) >= 0 && (%1) < MAX_DYN_MENUS && YSI_g_sMenuData[(%1)][E_MENU_ROW_DATA] & e_MENU_FLAGS_ACTIVE)
- Menu_Menu()
- {
- YSI_g_sUnusedRows = 0
- for (new i = 0; i < MAX_MENU_ROWS - 1; i++)
- {
- YSI_g_sMenuItems[i][E_MENU_ROW_NEXT] = i + 1;
- }
- YSI_g_sMenuItems[MAX_MENU_ROWS - 1][E_MENU_ROW_NEXT] = NO_MENU_ROW;
- }
- stock Menu_Create(title[], cols, Float:x, Float:y, Float:col1wirdth, Float:col2width = 0.0)
- {
- new
- i;
- for (i = 0; i < MAX_DYN_MENUS; i++)
- {
- if (!(YSI_g_sMenuData[i][E_MENU_DATA_DATA] & e_MENU_FLAGS_ACTIVE)) break;
- }
- if (i == MAX_DYN_MENUS) return NO_MENU;
- YSI_g_sMenuData[i][E_MENU_DATA_DATA] = e_MENU_FLAGS_ACTIVE | ((cols == 2) ? e_MENU_FLAGS_TWO : e_MENU_FLAGS_NONE);
- YSI_g_sMenuData[i][E_MENU_DATA_FIRST] = NO_MENU_ROW;
- YSI_g_sMenuData[i][E_MENU_DATA_ROWS] = 0;
- YSI_g_sMenuData[i][E_MENU_DATA_X] = x;
- YSI_g_sMenuData[i][E_MENU_DATA_Y] = y;
- YSI_g_sMenuData[i][E_MENU_COL_HEAD0][0] = '\0';
- YSI_g_sMenuData[i][E_MENU_COL_HEAD1][0] = '\0';
- YSI_g_sMenuData[i][E_MENU_DATA_C1] = col1width;
- YSI_g_sMenuData[i][E_MENU_DATA_C2] = col2width;
- strcpy(YSI_g_sMenuData[i][E_MENU_DATA_TITLE], title, MAX_MENU_LINE);
- return i;
- }
- stock Menu_AddRow(menuid, col1[], col2[] = "")
- {
- if (!Menu_IsValid(menuid)) return NO_MENU_ROW;
- if (YSI_g_sUnusedRows == NO_MENU_ROW) return NO_MENU_ROW;
- new
- row = YSI_g_sUnusedRows;
- YSI_g_sUnusedRows = YSI_g_sMenuItems[row][E_MENU_ROW_NEXT];
- strcpy(YSI_g_sMenuItems[row][E_MENU_ROW_COL1], col1, MAX_MENU_LINE);
- strcpy(YSI_g_sMenuItems[row][E_MENU_ROW_COL2], col2, MAX_MENU_LINE);
- YSI_g_sMenuItems[row][E_MENU_ROW_DATA] = e_MENU_FLAGS_ACTIVE | ((col2[0]) ? e_MENU_FLAGS_TWO : e_MENU_FLAGS_NONE);
- new
- last = NO_MENU_ROW,
- cur = YSI_g_sMenuData[menu][E_MENU_DATA_FIRST];
- while (cur != NO_MENU_ROW)
- {
- last = cur;
- cur = YSI_g_sMenuItems[last][E_MENU_ROW_NEXT];
- }
- if (last == NO_MENU_ROW)
- {
- YSI_g_sMenuData[menu][E_MENU_DATA_FIRST] = row;
- }
- else
- {
- YSI_g_sMenuItems[last][E_MENU_ROW_NEXT] = row;
- }
- YSI_g_sMenuItems[cur][E_MENU_ROW_NEXT] = NO_MENU_ROW;
- return YSI_g_sMenuData[i][E_MENU_DATA_ROWS]++;
- }
- stock Menu_RemoveRow(menuid, rowid)
- {
- }
- stock Menu_SetColumnHeader(menuid, colid, header[])
- {
- if (!Menu_IsValid(menuid))
- {
- return 0;
- }
- if (colid == 0)
- {
- strcpy(YSI_g_sMenuData[menuid][E_MENU_COL_HEAD0], header, MAX_MENU_LINE);
- }
- else if (colid == 1)
- {
- strcpy(YSI_g_sMenuData[menuid][E_MENU_COL_HEAD1], header, MAX_MENU_LINE);
- }
- else
- {
- return 0;
- }
- return 1;
- }
- static stock Menu:Menu_CreateHeader(menu)
- {
- new
- e_MENU_FLAGS:flags = YSI_g_sMenuData[menu][E_MENU_DATA_DATA],
- Menu:rMenu = CreateMenu(YSI_g_sMenuData[menu][E_MENU_DATA_TITLE], ((flags & e_MENU_FLAGS_TWO) ? (2) : (1)), YSI_g_sMenuData[menu][E_MENU_DATA_X], YSI_g_sMenuData[menu][E_MENU_DATA_Y], YSI_g_sMenuData[menu][E_MENU_DATA_C1], YSI_g_sMenuData[menu][E_MENU_DATA_C2]);
- if (rMenu == INVALID_MENU)
- {
- return INVALID_MENU;
- }
- SetMenuColumnHeader(rMenu, 0, YSI_g_sMenuData[menu][E_MENU_COL_HEAD0]);
- if (flags & e_MENU_FLAGS_TWO)
- {
- SetMenuColumnHeader(rMenu, 1, YSI_g_sMenuData[menu][E_MENU_COL_HEAD1]);
- }
- return rMenu;
- }
- static stock Menu_FindRow(start, row)
- {
- new
- count = 0;
- while (count < row && start != NO_MENU_ROW)
- {
- start = YSI_g_sMenuItems[start][E_MENU_ROW_NEXT];
- count++;
- }
- return start;
- }
- static stock Menu_Generate(menu, start, type)
- {
- new
- e_MENU_FLAGS:flags = YSI_g_sMenuData[menu][E_MENU_DATA_DATA] & e_MENU_FLAGS_TWO,
- Menu:rMenu = CreateMenu(YSI_g_sMenuData[menu][E_MENU_DATA_TITLE], ((flags) ? (2) : (1)), YSI_g_sMenuData[menu][E_MENU_DATA_X], YSI_g_sMenuData[menu][E_MENU_DATA_Y], YSI_g_sMenuData[menu][E_MENU_DATA_C1], YSI_g_sMenuData[menu][E_MENU_DATA_C2]);
- if (rMenu == INVALID_MENU)
- {
- return INVALID_MENU;
- }
- SetMenuColumnHeader(rMenu, 0, YSI_g_sMenuData[menu][E_MENU_COL_HEAD0]);
- if (flags)
- {
- SetMenuColumnHeader(rMenu, 1, YSI_g_sMenuData[menu][E_MENU_COL_HEAD1]);
- }
- new
- row = Menu_FindRow(YSI_g_sMenuData[i][E_MENU_DATA_FIRST], start);
- if (row == NO_MENU_ROW)
- {
- DestroyMenu(rMenu);
- return INVALID_MENU;
- }
- new
- end = start + 10 + type;
- do
- {
- AddMenuItem(rMenu, 0, YSI_g_sMenuItems[row][E_MENU_ROW_COL1]);
- if (flags)
- {
- AddMenuItem(rMenu, 1, YSI_g_sMenuItems[row][E_MENU_ROW_COL2]);
- }
- ;
- }
- while (++start < end && (row = YSI_g_sMenuItems[row][E_MENU_ROW_NEXT]) != NO_MENU_ROW);
- switch (type)
- {
- case 0:
- {
- AddMenuItem(rMenu, 0, "<- Back");
- if (flags)
- {
- AddMenuItem(rMenu, 1, "");
- }
- AddMenuItem(rMenu, 0, "Next ->");
- if (flags)
- {
- AddMenuItem(rMenu, 1, "");
- }
- }
- case 1:
- {
- AddMenuItem(rMenu, 0, "More");
- if (flags)
- {
- AddMenuItem(rMenu, 1, "");
- }
- }
- }
- }
- stock Menu_ShowForPlayer(playerid, menuid)
- {
- if (!Menu_IsValid(menuid) || !IsPlayerConnected(playerid)) return 0;
- new
- page = 0,
- Menu:rMenu,
- count = YSI_g_sMenuData[menuid][E_MENU_DATA_ROWS];
- if (count > 22)
- {
- rMenu = Menu_Generate(menuid, 0, 0);
- }
- else if (count > 12)
- {
- rMenu = Menu_Generate(menuid, 0, 1);
- }
- else
- {
- rMenu = Menu_Generate(menuid, 0, 2);
- }
- if (rMenu != INVALID_MENU)
- {
- ShowMenuForPlayer(rMenu, playerid);
- }
- return 1;
- }
- Menu_OnPlayerSelectMenuRow(playerid, row)
- {
- // TO-DO
- // Determine if they selected a real row or back/next from the system
- // If back/next alter page
- // If real row call OnPlayerSelectMenuRowEx with the row
- }
Advertisement
Add Comment
Please, Sign In to add comment