Y_Less

YSI_Menu

Apr 14th, 2008
351
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.87 KB | None | 0 0
  1. /*----------------------------------------------------------------------------*-
  2.                     ============================
  3.                     Y Sever Includes - Menu Core
  4.                     ============================
  5. Description:
  6.     Allows you to define menus with as many rows as you like and handles the
  7.     pages internally, allowing you to simply process the rows selected as if it
  8.     was one huge long menu.
  9. Legal:
  10.     Copyright (C) 2007 Alex "Y_Less" Cole
  11.  
  12.     This program is free software; you can redistribute it and/or
  13.     modify it under the terms of the GNU General Public License
  14.     as published by the Free Software Foundation; either version 2
  15.     of the License, or (at your option) any later version.
  16.  
  17.     This program is distributed in the hope that it will be useful,
  18.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  19.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20.     GNU General Public License for more details.
  21.  
  22.     You should have received a copy of the GNU General Public License
  23.     along with this program; if not, write to the Free Software
  24.     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  25.     MA 02110-1301, USA.
  26. Version:
  27.     0.0.1
  28. Changelog:
  29.     23/11/07:
  30.         First version.
  31. Functions:
  32.     Public:
  33.         -
  34.     Core:
  35.         -
  36.     Stock:
  37.         -
  38.     Static:
  39.         -
  40.     Inline:
  41.         -
  42.     API:
  43.         -
  44. Callbacks:
  45.     -
  46. Definitions:
  47.     -
  48. Enums:
  49.     -
  50. Macros:
  51.     -
  52. Tags:
  53.     -
  54. Variables:
  55.     Global:
  56.         -
  57.     Static:
  58.         -
  59. Commands:
  60.     -
  61. Compile options:
  62.     -
  63. -*----------------------------------------------------------------------------*/
  64.  
  65. #if !defined MAX_MENU_LINE
  66.     #define MAX_MENU_LINE (32)
  67. #endif
  68.  
  69. #define NO_MENU     (-1)
  70. #define NO_MENU_ROW (-1)
  71.  
  72. enum e_MENU_FLAGS (<<= 1)
  73. {
  74.     e_MENU_FLAGS_NONE,
  75.     e_MENU_FLAGS_TWO = 1,
  76.     e_MENU_FLAGS_ACTIVE,
  77.     e_MENU_FLAGS_SHOWN
  78. }
  79.  
  80. enum E_MENU_DATA
  81. {
  82.     E_MENU_DATA_TITLE[MAX_MENU_LINE],
  83.     E_MENU_COL_HEAD0[MAX_MENU_LINE],
  84.     E_MENU_COL_HEAD1[MAX_MENU_LINE],
  85.     E_MENU_DATA_FIRST,
  86.     E_MENU_DATA_ROWS,
  87.     e_MENU_FLAGS:E_MENU_DATA_DATA,
  88.     Float:E_MENU_DATA_X,
  89.     Float:E_MENU_DATA_Y,
  90.     Float:E_MENU_DATA_C1,
  91.     Float:E_MENU_DATA_C2
  92. }
  93.  
  94. enum E_MENU_ROW
  95. {
  96.     E_MENU_ROW_NEXT,
  97.     e_MENU_FLAGS:E_MENU_ROW_DATA,
  98.     E_MENU_ROW_COL1[MAX_MENU_LINE],
  99.     E_MENU_ROW_COL2[MAX_MENU_LINE],
  100. }
  101.  
  102. enum E_PLAYER_MENU
  103. {
  104.     Menu:E_PLAYER_MENU_REAL,
  105.     E_PLAYER_MENU_MENU,
  106.     E_PLAYER_MENU_PAGE
  107. }
  108.  
  109. static
  110.     YSI_g_sMenuData[MAX_DYN_MENUS][E_MENU_DATA],
  111.     YSI_g_sMenuItems[MAX_MENU_ROWS][E_MENU_ROW],
  112.     YSI_g_sPlayerMenus[MAX_PLAYERS][E_PLAYER_MENU],
  113.     YSI_g_sUnusedRows;
  114.  
  115. #define Menu_IsValid(%1) \
  116.     ((%1) >= 0 && (%1) < MAX_DYN_MENUS && YSI_g_sMenuData[(%1)][E_MENU_ROW_DATA] & e_MENU_FLAGS_ACTIVE)
  117.  
  118. Menu_Menu()
  119. {
  120.     YSI_g_sUnusedRows = 0
  121.     for (new i = 0; i < MAX_MENU_ROWS - 1; i++)
  122.     {
  123.         YSI_g_sMenuItems[i][E_MENU_ROW_NEXT] = i + 1;
  124.     }
  125.     YSI_g_sMenuItems[MAX_MENU_ROWS - 1][E_MENU_ROW_NEXT] = NO_MENU_ROW;
  126. }
  127.  
  128. stock Menu_Create(title[], cols, Float:x, Float:y, Float:col1wirdth, Float:col2width = 0.0)
  129. {
  130.     new
  131.         i;
  132.     for (i = 0; i < MAX_DYN_MENUS; i++)
  133.     {
  134.         if (!(YSI_g_sMenuData[i][E_MENU_DATA_DATA] & e_MENU_FLAGS_ACTIVE)) break;
  135.     }
  136.     if (i == MAX_DYN_MENUS) return NO_MENU;
  137.     YSI_g_sMenuData[i][E_MENU_DATA_DATA] = e_MENU_FLAGS_ACTIVE | ((cols == 2) ? e_MENU_FLAGS_TWO : e_MENU_FLAGS_NONE);
  138.     YSI_g_sMenuData[i][E_MENU_DATA_FIRST] = NO_MENU_ROW;
  139.     YSI_g_sMenuData[i][E_MENU_DATA_ROWS] = 0;
  140.     YSI_g_sMenuData[i][E_MENU_DATA_X] = x;
  141.     YSI_g_sMenuData[i][E_MENU_DATA_Y] = y;
  142.     YSI_g_sMenuData[i][E_MENU_COL_HEAD0][0] = '\0';
  143.     YSI_g_sMenuData[i][E_MENU_COL_HEAD1][0] = '\0';
  144.     YSI_g_sMenuData[i][E_MENU_DATA_C1] = col1width;
  145.     YSI_g_sMenuData[i][E_MENU_DATA_C2] = col2width;
  146.     strcpy(YSI_g_sMenuData[i][E_MENU_DATA_TITLE], title, MAX_MENU_LINE);
  147.     return i;
  148. }
  149.  
  150. stock Menu_AddRow(menuid, col1[], col2[] = "")
  151. {
  152.     if (!Menu_IsValid(menuid)) return NO_MENU_ROW;
  153.     if (YSI_g_sUnusedRows == NO_MENU_ROW) return NO_MENU_ROW;
  154.     new
  155.         row = YSI_g_sUnusedRows;
  156.     YSI_g_sUnusedRows = YSI_g_sMenuItems[row][E_MENU_ROW_NEXT];
  157.     strcpy(YSI_g_sMenuItems[row][E_MENU_ROW_COL1], col1, MAX_MENU_LINE);
  158.     strcpy(YSI_g_sMenuItems[row][E_MENU_ROW_COL2], col2, MAX_MENU_LINE);
  159.     YSI_g_sMenuItems[row][E_MENU_ROW_DATA] = e_MENU_FLAGS_ACTIVE | ((col2[0]) ? e_MENU_FLAGS_TWO : e_MENU_FLAGS_NONE);
  160.     new
  161.         last = NO_MENU_ROW,
  162.         cur = YSI_g_sMenuData[menu][E_MENU_DATA_FIRST];
  163.     while (cur != NO_MENU_ROW)
  164.     {
  165.         last = cur;
  166.         cur = YSI_g_sMenuItems[last][E_MENU_ROW_NEXT];
  167.     }
  168.     if (last == NO_MENU_ROW)
  169.     {
  170.         YSI_g_sMenuData[menu][E_MENU_DATA_FIRST] = row;
  171.     }
  172.     else
  173.     {
  174.         YSI_g_sMenuItems[last][E_MENU_ROW_NEXT] = row;
  175.     }
  176.     YSI_g_sMenuItems[cur][E_MENU_ROW_NEXT] = NO_MENU_ROW;
  177.     return YSI_g_sMenuData[i][E_MENU_DATA_ROWS]++;
  178. }
  179.  
  180. stock Menu_RemoveRow(menuid, rowid)
  181. {
  182.    
  183. }
  184.  
  185. stock Menu_SetColumnHeader(menuid, colid, header[])
  186. {
  187.     if (!Menu_IsValid(menuid))
  188.     {
  189.         return 0;
  190.     }
  191.     if (colid == 0)
  192.     {
  193.         strcpy(YSI_g_sMenuData[menuid][E_MENU_COL_HEAD0], header, MAX_MENU_LINE);
  194.     }
  195.     else if (colid == 1)
  196.     {
  197.         strcpy(YSI_g_sMenuData[menuid][E_MENU_COL_HEAD1], header, MAX_MENU_LINE);
  198.     }
  199.     else
  200.     {
  201.         return 0;
  202.     }
  203.     return 1;
  204. }
  205.  
  206. static stock Menu:Menu_CreateHeader(menu)
  207. {
  208.     new
  209.         e_MENU_FLAGS:flags = YSI_g_sMenuData[menu][E_MENU_DATA_DATA],
  210.         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]);
  211.     if (rMenu == INVALID_MENU)
  212.     {
  213.         return INVALID_MENU;
  214.     }
  215.     SetMenuColumnHeader(rMenu, 0, YSI_g_sMenuData[menu][E_MENU_COL_HEAD0]);
  216.     if (flags & e_MENU_FLAGS_TWO)
  217.     {
  218.         SetMenuColumnHeader(rMenu, 1, YSI_g_sMenuData[menu][E_MENU_COL_HEAD1]);
  219.     }
  220.     return rMenu;
  221. }
  222.  
  223. static stock Menu_FindRow(start, row)
  224. {
  225.     new
  226.         count = 0;
  227.     while (count < row && start != NO_MENU_ROW)
  228.     {
  229.         start = YSI_g_sMenuItems[start][E_MENU_ROW_NEXT];
  230.         count++;
  231.     }
  232.     return start;
  233. }
  234.  
  235. static stock Menu_Generate(menu, start, type)
  236. {
  237.     new
  238.         e_MENU_FLAGS:flags = YSI_g_sMenuData[menu][E_MENU_DATA_DATA] & e_MENU_FLAGS_TWO,
  239.         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]);
  240.     if (rMenu == INVALID_MENU)
  241.     {
  242.         return INVALID_MENU;
  243.     }
  244.     SetMenuColumnHeader(rMenu, 0, YSI_g_sMenuData[menu][E_MENU_COL_HEAD0]);
  245.     if (flags)
  246.     {
  247.         SetMenuColumnHeader(rMenu, 1, YSI_g_sMenuData[menu][E_MENU_COL_HEAD1]);
  248.     }
  249.     new
  250.         row = Menu_FindRow(YSI_g_sMenuData[i][E_MENU_DATA_FIRST], start);
  251.     if (row == NO_MENU_ROW)
  252.     {
  253.         DestroyMenu(rMenu);
  254.         return INVALID_MENU;
  255.     }
  256.     new
  257.         end = start + 10 + type;
  258.     do
  259.     {
  260.         AddMenuItem(rMenu, 0, YSI_g_sMenuItems[row][E_MENU_ROW_COL1]);
  261.         if (flags)
  262.         {
  263.             AddMenuItem(rMenu, 1, YSI_g_sMenuItems[row][E_MENU_ROW_COL2]);
  264.         }
  265.         ;
  266.     }
  267.     while (++start < end && (row = YSI_g_sMenuItems[row][E_MENU_ROW_NEXT]) != NO_MENU_ROW);
  268.     switch (type)
  269.     {
  270.         case 0:
  271.         {
  272.             AddMenuItem(rMenu, 0, "<- Back");
  273.             if (flags)
  274.             {
  275.                 AddMenuItem(rMenu, 1, "");
  276.             }
  277.             AddMenuItem(rMenu, 0, "Next ->");
  278.             if (flags)
  279.             {
  280.                 AddMenuItem(rMenu, 1, "");
  281.             }
  282.         }
  283.         case 1:
  284.         {
  285.             AddMenuItem(rMenu, 0, "More");
  286.             if (flags)
  287.             {
  288.                 AddMenuItem(rMenu, 1, "");
  289.             }
  290.         }
  291.     }
  292. }
  293.  
  294. stock Menu_ShowForPlayer(playerid, menuid)
  295. {
  296.     if (!Menu_IsValid(menuid) || !IsPlayerConnected(playerid)) return 0;
  297.     new
  298.         page = 0,
  299.         Menu:rMenu,
  300.         count = YSI_g_sMenuData[menuid][E_MENU_DATA_ROWS];
  301.     if (count > 22)
  302.     {
  303.         rMenu = Menu_Generate(menuid, 0, 0);
  304.     }
  305.     else if (count > 12)
  306.     {
  307.         rMenu = Menu_Generate(menuid, 0, 1);
  308.     }
  309.     else
  310.     {
  311.         rMenu = Menu_Generate(menuid, 0, 2);
  312.     }
  313.     if (rMenu != INVALID_MENU)
  314.     {
  315.         ShowMenuForPlayer(rMenu, playerid);
  316.     }
  317.     return 1;
  318. }
  319.  
  320. Menu_OnPlayerSelectMenuRow(playerid, row)
  321. {
  322.     // TO-DO
  323.     // Determine if they selected a real row or back/next from the system
  324.     // If back/next alter page
  325.     // If real row call OnPlayerSelectMenuRowEx with the row
  326. }
  327.  
Advertisement
Add Comment
Please, Sign In to add comment