Advertisement
bondowocopz

XMenu SAMP

Mar 1st, 2016
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 12.47 KB | None | 0 0
  1. /*
  2.     XMenu by Bondowocopz(X337)
  3.     Example code :
  4.    
  5.     ShowPlayerMenu(playerid, Menu1, "XMenu Title", "Option 1\nOption 2\nOption 3", "XMenu Example");
  6.  
  7.     Response :
  8.     Menu:Menu1(playerid, listitem)
  9.     {
  10.         switch(listitem)
  11.         {
  12.             case 0:
  13.             {
  14.                 SendClientMessage(playerid, -1, "You've choosed option 1");
  15.             }
  16.             case 1:
  17.             {
  18.                 SendClientMessage(playerid, -1, "You've choosed option 2");
  19.             }
  20.             case 2:
  21.             {
  22.                 SendClientMessage(playerid, -1, "You've choosed option 3");
  23.             }
  24.         }
  25.     }
  26.  
  27.     Credits:
  28.     - SA-MP Team - a_samp
  29.     - Slice - strlib
  30. */
  31. #define MAX_MENU_ITEM_TEXT      64
  32.  
  33. #define Menu:%0(%1) \
  34.     forward tdmenu_%0(%1); public tdmenu_%0(%1)
  35.  
  36. #define ShowPlayerMenu(%0,%1, \
  37.     __ShowMenu(%0, #%1,
  38.  
  39. stock x_isnumeric(str[])
  40. {
  41.     new
  42.         ch,
  43.         i;
  44.     while ((ch = str[i++])) if (!('0' <= ch <= '9')) return 0;
  45.     return 1;
  46. }
  47.  
  48. stock bool:x_isempty(const string[]) {
  49.     if (ispacked(string))
  50.         return string{0} == '\0';
  51.     else
  52.         return string[0] == '\0';
  53. }
  54.  
  55. stock x_strexplode(output[][], const input[], const delimiter[] = !",", limit = cellmax, bool:trim = true, bool:ignorecase = false, size1 = sizeof(output), size2 = sizeof(output[])) {
  56.     if (!size1 || !size2) {
  57.         printf("(x_strexplode) ERROR: size1 = %d, size2 = %d. Can't be 0.", size1, size2);
  58.        
  59.         return 0;
  60.     }
  61.    
  62.     if (x_isempty(delimiter)) {
  63.         print(!"(x_strexplode) ERROR: delimiter is empty.");
  64.        
  65.         return 0;
  66.     }
  67.    
  68.     if (trim) {
  69.         new i = -1;
  70.        
  71.         if (ispacked(input)) {
  72.             while (input{++i}) {
  73.                 if (input{i} > ' ') {
  74.                     i = -1;
  75.                    
  76.                     break;
  77.                 }
  78.             }
  79.         } else {
  80.             while (input[++i]) {
  81.                 if (input[i] > ' ') {
  82.                     i = -1;
  83.                    
  84.                     break;
  85.                 }
  86.             }
  87.         }
  88.        
  89.         if (i != -1)
  90.             return 0;
  91.     } else if (x_isempty(input)) {
  92.         return 0;
  93.     }
  94.    
  95.     if (limit == 0) {
  96.         return 0;
  97.     } else if (limit == cellmax) {
  98.         limit = 0;
  99.     }
  100.    
  101.     new
  102.              pos = 0,
  103.              next,
  104.         bool:packed = ispacked(input),
  105.              dlen = strlen(delimiter),
  106.              count = 0,
  107.              end
  108.     ;
  109.    
  110.     while (pos != -1) {
  111.         ++count;
  112.        
  113.         if (limit > 0 && count >= limit) {
  114.             next = -1;
  115.         } else {
  116.             next = strfind(input, delimiter, ignorecase, pos);
  117.         }
  118.        
  119.         end = (next == -1) ? cellmax : next;
  120.        
  121.         if (trim) {
  122.             if (end == cellmax)
  123.                 end = strlen(input);
  124.            
  125.             if (packed) {
  126.                 while (0 < input{pos} <= ' ') pos++;
  127.                 while (end > 0 && input{end - 1} <= ' ') end--;
  128.             } else {
  129.                 while (0 < input[pos] <= ' ') pos++;
  130.                 while (end > 0 && input[end - 1] <= ' ') end--;
  131.             }
  132.         }
  133.        
  134.         strmid(output[count - 1], input, pos, end, size2);
  135.        
  136.         if (count >= size1 || next == -1 || (limit < 0 && count >= -limit))
  137.             break;
  138.        
  139.         pos = next + dlen;
  140.     }
  141.    
  142.     return count;
  143. }
  144.  
  145. static
  146.     Text:tdmnu_MenuTextdrawBG,
  147.     PlayerText:tdmnu_MenuTextdrawTitle[MAX_PLAYERS],
  148.     PlayerText:tdmnu_MenuTextdrawDesc[MAX_PLAYERS],
  149.     PlayerText:tdmnu_MenuTextdrawOpts[MAX_PLAYERS],
  150.     PlayerText:tdmnu_MenuTextdrawClose[MAX_PLAYERS],
  151.     tdmnu_MenuFunction[MAX_PLAYERS][32 char],
  152.     bool:tdmnu_MenuOpened[MAX_PLAYERS],
  153.     tdmnu_TotalOpts[MAX_PLAYERS]
  154. ;
  155.  
  156. #if !defined isnull
  157.     #define isnull(%1) \
  158.         ((%1[0] == 0) || (%1[0] == 1 && %1[1] == 0))
  159. #endif
  160.  
  161. public OnGameModeInit()
  162. {
  163.     tdmnu_MenuTextdrawBG = TextDrawCreate(26.525003, 147.573745, "box");
  164.     TextDrawLetterSize(tdmnu_MenuTextdrawBG, 0.000000, 18.312500);
  165.     TextDrawTextSize(tdmnu_MenuTextdrawBG, 147.000000, 0.000000);
  166.     TextDrawAlignment(tdmnu_MenuTextdrawBG, 1);
  167.     TextDrawColor(tdmnu_MenuTextdrawBG, -1);
  168.     TextDrawUseBox(tdmnu_MenuTextdrawBG, 1);
  169.     TextDrawBoxColor(tdmnu_MenuTextdrawBG, 144);
  170.     TextDrawSetShadow(tdmnu_MenuTextdrawBG, 0);
  171.     TextDrawSetOutline(tdmnu_MenuTextdrawBG, 0);
  172.     TextDrawBackgroundColor(tdmnu_MenuTextdrawBG, 255);
  173.     TextDrawFont(tdmnu_MenuTextdrawBG, 1);
  174.     TextDrawSetProportional(tdmnu_MenuTextdrawBG, 1);
  175.     TextDrawSetShadow(tdmnu_MenuTextdrawBG, 0);
  176.  
  177.     #if defined Tdmnu_OnGameModeInit
  178.         return Tdmnu_OnGameModeInit();
  179.     #else
  180.         return 1;
  181.     #endif
  182. }
  183. #if defined _ALS_OnGameModeInit
  184.     #undef OnGameModeInit
  185. #else
  186.     #define _ALS_OnGameModeInit
  187. #endif
  188.  
  189. #define OnGameModeInit Tdmnu_OnGameModeInit
  190. #if defined Tdmnu_OnGameModeInit
  191.     forward Tdmnu_OnGameModeInit();
  192. #endif
  193.  
  194. public OnPlayerConnect(playerid)
  195. {
  196.     tdmnu_MenuTextdrawTitle[playerid] = CreatePlayerTextDraw(playerid, 83.974967, 147.486572, "AMMUNATION");
  197.     PlayerTextDrawLetterSize(playerid, tdmnu_MenuTextdrawTitle[playerid], 0.303750, 1.502933);
  198.     PlayerTextDrawAlignment(playerid, tdmnu_MenuTextdrawTitle[playerid], 2);
  199.     PlayerTextDrawColor(playerid, tdmnu_MenuTextdrawTitle[playerid], -1);
  200.     PlayerTextDrawSetShadow(playerid, tdmnu_MenuTextdrawTitle[playerid], 0);
  201.     PlayerTextDrawSetOutline(playerid, tdmnu_MenuTextdrawTitle[playerid], 0);
  202.     PlayerTextDrawBackgroundColor(playerid, tdmnu_MenuTextdrawTitle[playerid], 255);
  203.     PlayerTextDrawFont(playerid, tdmnu_MenuTextdrawTitle[playerid], 2);
  204.     PlayerTextDrawSetProportional(playerid, tdmnu_MenuTextdrawTitle[playerid], 1);
  205.     PlayerTextDrawSetShadow(playerid, tdmnu_MenuTextdrawTitle[playerid], 0);
  206.  
  207.     tdmnu_MenuTextdrawDesc[playerid] = CreatePlayerTextDraw(playerid, 86.949989, 165.860000, "Lorem ipsum dolor sit amet conclusion snis");
  208.     PlayerTextDrawLetterSize(playerid, tdmnu_MenuTextdrawDesc[playerid], 0.169999, 1.039999);
  209.     PlayerTextDrawTextSize(playerid, tdmnu_MenuTextdrawDesc[playerid], 0.000000, 119.070014);
  210.     PlayerTextDrawAlignment(playerid, tdmnu_MenuTextdrawDesc[playerid], 2);
  211.     PlayerTextDrawColor(playerid, tdmnu_MenuTextdrawDesc[playerid], -1061109505);
  212.     PlayerTextDrawUseBox(playerid, tdmnu_MenuTextdrawDesc[playerid], 1);
  213.     PlayerTextDrawBoxColor(playerid, tdmnu_MenuTextdrawDesc[playerid], 255);
  214.     PlayerTextDrawSetShadow(playerid, tdmnu_MenuTextdrawDesc[playerid], 0);
  215.     PlayerTextDrawSetOutline(playerid, tdmnu_MenuTextdrawDesc[playerid], 0);
  216.     PlayerTextDrawBackgroundColor(playerid, tdmnu_MenuTextdrawDesc[playerid], 255);
  217.     PlayerTextDrawFont(playerid, tdmnu_MenuTextdrawDesc[playerid], 1);
  218.     PlayerTextDrawSetProportional(playerid, tdmnu_MenuTextdrawDesc[playerid], 1);
  219.     PlayerTextDrawSetShadow(playerid, tdmnu_MenuTextdrawDesc[playerid], 0);
  220.  
  221.     tdmnu_MenuTextdrawOpts[playerid] = CreatePlayerTextDraw(playerid, 30.300010, 180.346633, "1. Option 1~n~2. Option 2~n~3. Option 3~n~4. Option 4~n~5. Option 5~n~6. Option 6~n~7. Option 7~n~8. Option 8~n~9. Option 9~n~1");
  222.     PlayerTextDrawLetterSize(playerid, tdmnu_MenuTextdrawOpts[playerid], 0.211245, 1.002665);
  223.     PlayerTextDrawTextSize(playerid, tdmnu_MenuTextdrawOpts[playerid], 146.000000, 0.000000);
  224.     PlayerTextDrawAlignment(playerid, tdmnu_MenuTextdrawOpts[playerid], 1);
  225.     PlayerTextDrawColor(playerid, tdmnu_MenuTextdrawOpts[playerid], -5963521);
  226.     PlayerTextDrawUseBox(playerid, tdmnu_MenuTextdrawOpts[playerid], 1);
  227.     PlayerTextDrawBoxColor(playerid, tdmnu_MenuTextdrawOpts[playerid], 0);
  228.     PlayerTextDrawSetShadow(playerid, tdmnu_MenuTextdrawOpts[playerid], 0);
  229.     PlayerTextDrawSetOutline(playerid, tdmnu_MenuTextdrawOpts[playerid], 0);
  230.     PlayerTextDrawBackgroundColor(playerid, tdmnu_MenuTextdrawOpts[playerid], 255);
  231.     PlayerTextDrawFont(playerid, tdmnu_MenuTextdrawOpts[playerid], 1);
  232.     PlayerTextDrawSetProportional(playerid, tdmnu_MenuTextdrawOpts[playerid], 1);
  233.     PlayerTextDrawSetShadow(playerid, tdmnu_MenuTextdrawOpts[playerid], 0);
  234.  
  235.     tdmnu_MenuTextdrawClose[playerid] = CreatePlayerTextDraw(playerid, 86.624984, 298.667541, "Press (~b~T~w~) or (~b~F6~w~) to enter number~n~Press (~b~CAPSLOCK~w~) to close");
  236.     PlayerTextDrawLetterSize(playerid, tdmnu_MenuTextdrawClose[playerid], 0.195695, 1.129464);
  237.     PlayerTextDrawTextSize(playerid, tdmnu_MenuTextdrawClose[playerid], 0.000000, 119.890190);
  238.     PlayerTextDrawAlignment(playerid, tdmnu_MenuTextdrawClose[playerid], 2);
  239.     PlayerTextDrawColor(playerid, tdmnu_MenuTextdrawClose[playerid], -1);
  240.     PlayerTextDrawUseBox(playerid, tdmnu_MenuTextdrawClose[playerid], 1);
  241.     PlayerTextDrawBoxColor(playerid, tdmnu_MenuTextdrawClose[playerid], 255);
  242.     PlayerTextDrawSetShadow(playerid, tdmnu_MenuTextdrawClose[playerid], 0);
  243.     PlayerTextDrawSetOutline(playerid, tdmnu_MenuTextdrawClose[playerid], 0);
  244.     PlayerTextDrawBackgroundColor(playerid, tdmnu_MenuTextdrawClose[playerid], 255);
  245.     PlayerTextDrawFont(playerid, tdmnu_MenuTextdrawClose[playerid], 1);
  246.     PlayerTextDrawSetProportional(playerid, tdmnu_MenuTextdrawClose[playerid], 1);
  247.     PlayerTextDrawSetShadow(playerid, tdmnu_MenuTextdrawClose[playerid], 0);
  248.  
  249.     tdmnu_MenuOpened[playerid] = false;
  250.  
  251.     #if defined Tdmnu_OnPlayerConnect
  252.         return Tdmnu_OnPlayerConnect(playerid);
  253.     #else
  254.         return 1;
  255.     #endif
  256. }
  257. #if defined _ALS_OnPlayerConnect
  258.     #undef OnPlayerConnect
  259. #else
  260.     #define _ALS_OnPlayerConnect
  261. #endif
  262.  
  263. #define OnPlayerConnect Tdmnu_OnPlayerConnect
  264. #if defined Tdmnu_OnPlayerConnect
  265.     forward Tdmnu_OnPlayerConnect(playerid);
  266. #endif
  267.  
  268. stock __ShowMenu(playerid, function[], title[], options[], desc[] = "")
  269. {
  270.     new
  271.         items[10][MAX_MENU_ITEM_TEXT],
  272.         len = 10 * MAX_MENU_ITEM_TEXT,
  273.         text[10 * MAX_MENU_ITEM_TEXT],
  274.         total
  275.     ;
  276.     x_strexplode(items, options, "\n");
  277.     for(new i = 0; i < 10; i++)
  278.     {
  279.         if(isnull(items[i]))
  280.             continue;
  281.  
  282.         format(text, len, "%s~n~~y~%d. ~w~%s", text, i + 1, items[i]);
  283.         total++;
  284.     }
  285.     if(!IsPlayerInAnyVehicle(playerid))
  286.         PlayerTextDrawSetString(playerid, tdmnu_MenuTextdrawClose[playerid], "Press ~b~T~w~ or ~b~F6~w~ to enter number~n~Press ~b~LMB~w~ to close");
  287.     else
  288.         PlayerTextDrawSetString(playerid, tdmnu_MenuTextdrawClose[playerid], "Press ~b~T~w~ or ~b~F6~w~ to enter number~n~Press ~b~~k~~VEHICLE_HORN~~w~ to close");
  289.     if(strlen(desc) < 3)
  290.         format(desc,  18, "(No Description)");
  291.     PlayerTextDrawSetString(playerid, tdmnu_MenuTextdrawOpts[playerid], text);
  292.     PlayerTextDrawSetString(playerid, tdmnu_MenuTextdrawDesc[playerid], desc);
  293.     PlayerTextDrawSetString(playerid, tdmnu_MenuTextdrawTitle[playerid], title);
  294.     TextDrawShowForPlayer(playerid, tdmnu_MenuTextdrawBG);
  295.     PlayerTextDrawShow(playerid, tdmnu_MenuTextdrawTitle[playerid]);
  296.     PlayerTextDrawShow(playerid, tdmnu_MenuTextdrawOpts[playerid]);
  297.     PlayerTextDrawShow(playerid, tdmnu_MenuTextdrawDesc[playerid]);
  298.     PlayerTextDrawShow(playerid, tdmnu_MenuTextdrawClose[playerid]);
  299.  
  300.     strpack(tdmnu_MenuFunction[playerid], function, 32 char);
  301.     tdmnu_MenuOpened[playerid] = true;
  302.     tdmnu_TotalOpts[playerid] = ++total;
  303.     return 1;
  304. }
  305.  
  306. stock HidePlayerMenu(playerid)
  307. {
  308.     TextDrawHideForPlayer(playerid, tdmnu_MenuTextdrawBG);
  309.     PlayerTextDrawHide(playerid, tdmnu_MenuTextdrawTitle[playerid]);
  310.     PlayerTextDrawHide(playerid, tdmnu_MenuTextdrawOpts[playerid]);
  311.     PlayerTextDrawHide(playerid, tdmnu_MenuTextdrawDesc[playerid]);
  312.     PlayerTextDrawHide(playerid, tdmnu_MenuTextdrawClose[playerid]);
  313.  
  314.     tdmnu_MenuOpened[playerid] = false;
  315. }
  316.  
  317. stock IsMenuOpened(playerid)
  318. {
  319.     return tdmnu_MenuOpened[playerid];
  320. }
  321.  
  322. public OnPlayerText(playerid, text[])
  323. {
  324.     if(tdmnu_MenuOpened[playerid])
  325.     {
  326.         if(x_isnumeric(text))
  327.         {
  328.             new option = strval(text);
  329.             if(option > 0 && option <= tdmnu_TotalOpts[playerid])
  330.             {
  331.                 new str[64];
  332.                 strcat(str, "tdmenu_");
  333.                 strcat(str, tdmnu_MenuFunction[playerid]);
  334.                 HidePlayerMenu(playerid);
  335.                 CallLocalFunction(str, "dd", playerid, option - 1);
  336.                 return 0;
  337.             }
  338.         }
  339.     }
  340.     #if defined tdmn_OnPlayerText
  341.         return tdmn_OnPlayerText(playerid, text);
  342.     #else
  343.         return 1;
  344.     #endif
  345. }
  346. #if defined _ALS_OnPlayerText
  347.     #undef OnPlayerText
  348. #else
  349.     #define _ALS_OnPlayerText
  350. #endif
  351.  
  352. #define OnPlayerText tdmn_OnPlayerText
  353. #if defined tdmn_OnPlayerText
  354.     forward tdmn_OnPlayerText(playerid, text[]);
  355. #endif
  356.  
  357. public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
  358. {
  359.     if(tdmnu_MenuOpened[playerid])
  360.     {
  361.         new bool = IsPlayerInAnyVehicle(playerid);
  362.         if((newkeys & KEY_FIRE && !bool) || (newkeys & KEY_CROUCH && bool))
  363.         {
  364.             HidePlayerMenu(playerid);
  365.             return 0;
  366.         }
  367.     }
  368.     #if defined Tdmnu_OnPlayerKeyStateChange
  369.         return Tdmnu_OnPlayerKeyStateChange(playerid, newkeys, oldkeys);
  370.     #else
  371.         return 1;
  372.     #endif
  373. }
  374. #if defined _ALS_OnPlayerKeyStateChange
  375.     #undef OnPlayerKeyStateChange
  376. #else
  377.     #define _ALS_OnPlayerKeyStateChange
  378. #endif
  379.  
  380. #define OnPlayerKeyStateChange Tdmnu_OnPlayerKeyStateChange
  381. #if defined Tdmnu_OnPlayerKeyStateChange
  382.     forward Tdmnu_OnPlayerKeyStateChange(playerid, newkeys, oldkeys);
  383. #endif
  384.  
  385. public OnPlayerDeath(playerid, killerid, reason)
  386. {
  387.     tdmnu_MenuOpened[playerid] = false;
  388.     HidePlayerMenu(playerid);
  389.  
  390.     #if defined TDMenu_OnPlayerDeath
  391.         return TDMenu_OnPlayerDeath(playerid, killerid, reason);
  392.     #else
  393.         return 1;
  394.     #endif
  395. }
  396. #if defined _ALS_OnPlayerDeath
  397.     #undef OnPlayerDeath
  398. #else
  399.     #define _ALS_OnPlayerDeath
  400. #endif
  401.  
  402. #define OnPlayerDeath TDMenu_OnPlayerDeath
  403. #if defined TDMenu_OnPlayerDeath
  404.     forward TDMenu_OnPlayerDeath(playerid, killerid, reason);
  405. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement