SHARE
TWEET

[INC] KeyPad.inc

Chip7 Apr 28th, 2012 1,289 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3. Easy-to-use interactive clickable textdraw keypad
  4. by Mike (MP2)
  5. © 2012
  6. VISITE NOSSO SITE: http://www.sampknd.com/
  7. SAMP KND MELHOR BLOG DE SAMP DO BRASIL
  8. */
  9.  
  10.  
  11. /* >= CHANGELOG <= */
  12. /*
  13. 1.0 (27th of March 2011)
  14. - Initial release
  15.  
  16. 1.1 (7th of April 2011)
  17. - Textdraws only created when needed, and not per-player (only ever creates 25 textdraws)
  18. - Improved variable names
  19.  
  20.  
  21. ### TO-DO ###
  22.  
  23. - Add 'retry' parameter to ShowPlayerKeypad to allow multiple attempts (+ 'attempts' in OnPlayerKeypadInput)
  24.  
  25. */
  26.  
  27. /* ===[CONFIG]=== */
  28.  
  29. #define MAX_KEYPAD_KEY_LENGTH 10 // Max length for keypad key
  30. #define KEYPAD_STAY_TIME 1000 // Time in miliseconds the keypad should stay on screen for after input (to show green/red light)
  31.  
  32. // Input types (for OnPlayerKeypadInput)
  33. #define KEYPAD_INPUT_BAD 0 // Failed authorization
  34. #define KEYPAD_INPUT_GOOD 1 // Successful authorization
  35. #define KEYPAD_INPUT_CANCEL 2 // Called when ESC press to cancel keypad
  36.  
  37. // Macros
  38. #define GetPlayerKeypad(%0) KEYPAD_pKeypad[%0][keypad_id]
  39.  
  40.  
  41. /* ===[VARIABLES, ARRAYS ETC.]=== */
  42.  
  43. enum E_PLAYER_KEYPAD
  44. {
  45. keypad_id,
  46. keypad_state,
  47. keypad_string[MAX_KEYPAD_KEY_LENGTH+1],
  48. keypad_key[MAX_KEYPAD_KEY_LENGTH+1]
  49. }
  50.  
  51. new KEYPAD_pKeypad[MAX_PLAYERS][E_PLAYER_KEYPAD]; // The keypad data for a player
  52.  
  53. enum E_KEYPAD_TD
  54. {
  55. Text:keypad_textdraw_num[10],
  56. Text:keypad_textdraw_box[14], // 10 numbers, RED_OFF, GREEN_OFF, RED_ON, GREEN_ON
  57. Text:keypad_textdraw_bg // The grey background box
  58. }
  59.  
  60. new KEYPAD_gKeypadTextdraws[E_KEYPAD_TD]; // An enum array to store the textdraw IDs in
  61.  
  62. #define KEYPAD_TD_GREEN_OFF 10
  63. #define KEYPAD_TD_RED_OFF 11
  64. #define KEYPAD_TD_GREEN_ON 12
  65. #define KEYPAD_TD_RED_ON 13
  66.  
  67. new bool:KEYPAD_gTextDrawsCreated; // Are the textdraws for the keypad created?
  68.  
  69.  
  70. // Return codes for ShowPlayerKeypad
  71. #define CREATEKEYPAD_RETURN_INVALID_ID 0 // ID must be 1+
  72. #define CREATEKEYPAD_RETURN_NUM -1 // String is not a number sequence
  73. #define CREATEKEYPAD_RETURN_INVALID_PLAYER -2 // 'playerid' not connected
  74.  
  75. /*
  76. native ShowPlayerKeypad(playerid, keypadID, key[]);
  77. native HideKeypad(playerid);
  78. */
  79.  
  80. // Forwards
  81. forward OnPlayerKeypadInput(playerid, keypadID, type, key);
  82.  
  83. stock DestroyKeypadTextdraws()
  84. {
  85.         if(!KEYPAD_gTextDrawsCreated) return 1;
  86.         KEYPAD_gTextDrawsCreated = false;
  87.        
  88.         TextDrawDestroy(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg]);
  89.        
  90.         for(new i=0; i<14; i++) // Destroy the 14 boxes (10 numbers, 4 green/red boxes (two lit up two not))
  91.         {
  92.                 TextDrawDestroy(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i]);
  93.         }
  94.         for(new i=0; i<10; i++) // Destroy the numbers
  95.         {
  96.                 TextDrawDestroy(KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i]);
  97.         }
  98.         return 1;
  99. }
  100.  
  101. stock CreateKeypadTextdraws()
  102. {
  103.         if(KEYPAD_gTextDrawsCreated) return 1; // If the textdraws are already created, don't continue
  104.         KEYPAD_gTextDrawsCreated = true; // Signify that the textdraws have been created
  105.  
  106.         // Create the grey background box
  107.         KEYPAD_gKeypadTextdraws[keypad_textdraw_bg] = TextDrawCreate(570.000000, 280.000000, "_");
  108.         TextDrawAlignment(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg], 2);
  109.         TextDrawBackgroundColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg], 255);
  110.         TextDrawFont(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg], 2);
  111.         TextDrawLetterSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg], 0.319999, 14.799995);
  112.         TextDrawColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg], -1);
  113.         TextDrawSetOutline(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg], 0);
  114.         TextDrawSetProportional(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg], 1);
  115.         TextDrawSetShadow(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg], 0);
  116.         TextDrawUseBox(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg], 1);
  117.         TextDrawBoxColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg], -1852730422);
  118.         TextDrawTextSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_bg], 0.000000, 113.000000);
  119.        
  120.         // The coordinates for the boxes
  121.         new keypad_box_coords[][] = {
  122.         {540, 290},
  123.         {570, 320},
  124.         {570, 290},
  125.         {600, 290},
  126.         {540, 320},
  127.         {600, 320},
  128.         {600, 350},
  129.         {570, 350},
  130.         {540, 350},
  131.         {570, 380}
  132.         };
  133.  
  134.         for(new i=0; i<10; i++) // Create the black boxes for the numbers
  135.         {
  136.                 KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i] = TextDrawCreate(keypad_box_coords[i][0], keypad_box_coords[i][1], "~N~~N~~N~");
  137.                 TextDrawAlignment(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i], 2);
  138.                 TextDrawBackgroundColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i], 255);
  139.                 TextDrawFont(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i], 2);
  140.                 TextDrawLetterSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i], 0.319999, 0.899999);
  141.                 TextDrawColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i], -1);
  142.                 TextDrawSetOutline(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i], 0);
  143.                 TextDrawSetProportional(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i], 1);
  144.                 TextDrawSetShadow(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i], 0);
  145.                 TextDrawUseBox(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i], 1);
  146.                 TextDrawBoxColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i], 255);
  147.                 TextDrawTextSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i], 100.000000, 23.000000);
  148.         }
  149.        
  150.         // The coordinates for the numbers
  151.         new keypad_num_coords[][] = {
  152.         {570, 379},
  153.         {540, 289},
  154.         {570, 289},
  155.         {600, 289},
  156.         {540, 319},
  157.         {570, 319},
  158.         {600, 319},
  159.         {540, 349},
  160.         {570, 349},
  161.         {600, 349}
  162.         };
  163.  
  164.         for(new i=0; i<10; i++) // Create the number textdraws
  165.         {
  166.                 new numstr[2];
  167.                 format(numstr, 2, "%i", i);
  168.                 KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i] = TextDrawCreate(keypad_num_coords[i][0], keypad_num_coords[i][1], numstr);
  169.                 TextDrawAlignment(KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i], 2);
  170.                 TextDrawBackgroundColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i], 255);
  171.                 TextDrawFont(KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i], 2);
  172.                 TextDrawLetterSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i], 0.550000, 2.599998);
  173.                 TextDrawColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i], -1);
  174.                 TextDrawSetOutline(KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i], 0);
  175.                 TextDrawSetProportional(KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i], 1);
  176.                 TextDrawSetShadow(KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i], 0);
  177.                 TextDrawTextSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i], 23.000000, 23.000000);
  178.                 TextDrawSetSelectable(KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i], 1);
  179.         }
  180.        
  181.         // Green Off
  182.         KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF] = TextDrawCreate(600.000000, 380.000000, "~N~~N~~N~");
  183.         TextDrawAlignment(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF], 2);
  184.         TextDrawBackgroundColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF], 255);
  185.         TextDrawFont(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF], 2);
  186.         TextDrawLetterSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF], 0.319999, 0.899999);
  187.         TextDrawColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF], -1);
  188.         TextDrawSetOutline(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF], 0);
  189.         TextDrawSetProportional(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF], 1);
  190.         TextDrawSetShadow(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF], 0);
  191.         TextDrawUseBox(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF], 1);
  192.         TextDrawBoxColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF], 5374207);
  193.         TextDrawTextSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF], 23.000000, 23.000000);
  194.        
  195.         // Red Off
  196.         KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF] = TextDrawCreate(540.000000, 380.000000, "~N~~N~~N~");
  197.         TextDrawAlignment(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF], 2);
  198.         TextDrawBackgroundColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF], 255);
  199.         TextDrawFont(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF], 2);
  200.         TextDrawLetterSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF], 0.319999, 0.899999);
  201.         TextDrawColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF], -1);
  202.         TextDrawSetOutline(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF], 0);
  203.         TextDrawSetProportional(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF], 1);
  204.         TextDrawSetShadow(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF], 0);
  205.         TextDrawUseBox(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF], 1);
  206.         TextDrawBoxColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF], 1375731967);
  207.         TextDrawTextSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF], 0.000000, 23.000000);
  208.        
  209.         // Green On
  210.         KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON] = TextDrawCreate(600.000000, 380.000000, "~N~~N~~N~");
  211.         TextDrawAlignment(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON], 2);
  212.         TextDrawBackgroundColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON], 255);
  213.         TextDrawFont(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON], 2);
  214.         TextDrawLetterSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON], 0.319999, 0.899999);
  215.         TextDrawColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON], -1);
  216.         TextDrawSetOutline(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON], 0);
  217.         TextDrawSetProportional(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON], 1);
  218.         TextDrawSetShadow(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON], 0);
  219.         TextDrawUseBox(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON], 1);
  220.         TextDrawBoxColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON], 0x00FF00FF);
  221.         TextDrawTextSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON], 23.000000, 23.000000);
  222.        
  223.         // Red On
  224.         KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON] = TextDrawCreate(540.000000, 380.000000, "~N~~N~~N~");
  225.         TextDrawAlignment(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON], 2);
  226.         TextDrawBackgroundColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON], 255);
  227.         TextDrawFont(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON], 2);
  228.         TextDrawLetterSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON], 0.319999, 0.899999);
  229.         TextDrawColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON], -1);
  230.         TextDrawSetOutline(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON], 0);
  231.         TextDrawSetProportional(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON], 1);
  232.         TextDrawSetShadow(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON], 0);
  233.         TextDrawUseBox(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON], 1);
  234.         TextDrawTextSize(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON], 0.000000, 23.000000);
  235.         TextDrawBoxColor(KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON], 0xFF0000FF);
  236.         return 1;
  237. }
  238.  
  239. stock ShowPlayerKeypad(playerid, keypadID, key[]) // Show the keypad to a player. keypadID should be a unique ID like for dialogs to handle response code. key[] is the key they must enter to pass.
  240. {
  241.         if(!IsPlayerConnected(playerid)) return CREATEKEYPAD_RETURN_INVALID_PLAYER; // Just in-case the player isn't connected
  242.  
  243.         if(keypadID < 1) return CREATEKEYPAD_RETURN_INVALID_ID; // Returns CREATEKEYPAD_RETURN_INVALID_ID if invalid keypad ID. Must be 1+
  244.         if(!keypad_IsNumeric(key)) return CREATEKEYPAD_RETURN_NUM; // Returns CREATEKEYPAD_RETURN_NUM if invalid key. Must be numbers only
  245.        
  246.         if(!KEYPAD_gTextDrawsCreated) CreateKeypadTextdraws(); // If textdraws aren't created, create them now
  247.  
  248.         format(KEYPAD_pKeypad[playerid][keypad_key], MAX_KEYPAD_KEY_LENGTH, "%s", key); // Save the key[] in to the player's variable
  249.         KEYPAD_pKeypad[playerid][keypad_id] = keypadID; // Store which keypad the player has open
  250.        
  251.         for(new i=0; i<10; i++) // Show boxes
  252.         {
  253.                 TextDrawShowForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_box][i]);
  254.         }
  255.         for(new i=0; i<10; i++) // Show numbers
  256.         {
  257.                 TextDrawShowForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i]);
  258.         }
  259.        
  260.         TextDrawShowForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF]); // Show the dark red light
  261.         TextDrawShowForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF]); // Show the dark green light
  262.         TextDrawShowForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_bg]); // Show the grey background box
  263.         SelectTextDraw(playerid, 0x00FF00FF); // Allow the player to select textdraws with the mouse cursor
  264.        
  265.         KEYPAD_pKeypad[playerid][keypad_state] = 1; // State 1 means they are inputting a code
  266.  
  267.         strdel(KEYPAD_pKeypad[playerid][keypad_string], 0, strlen(KEYPAD_pKeypad[playerid][keypad_string])); // Wipe the string that stores what they have entered so far
  268.         return 1;
  269. }
  270.  
  271. stock HideKeypad(playerid) // Hide the keypad for a player.
  272. {
  273.         if(!IsPlayerConnected(playerid)) return CREATEKEYPAD_RETURN_INVALID_PLAYER; // Just in-case they aren't connected
  274.        
  275.         KEYPAD_pKeypad[playerid][keypad_state] = 0; // State 0 means they have no keypad open
  276.        
  277.         if(!KEYPAD_gTextDrawsCreated) return 0; // Textdraws aren't even created, so stop.
  278.        
  279.         new KEYPAD_playersWithKeypadOpen; // A variable to store whether anyone else has a keypad open
  280.         for(new i=0; i<MAX_PLAYERS; i++) // Loop through all players
  281.         {
  282.                 if(KEYPAD_pKeypad[i][keypad_state] && i != playerid) KEYPAD_playersWithKeypadOpen = 1; // If any other players have a keypad open, set KEYPAD_playersWithKeypadOpen to 1.
  283.         }
  284.         if(!KEYPAD_playersWithKeypadOpen) return DestroyKeypadTextdraws(); // If nobody else has a keypad open, we can destroy the textdraws instead of just hiding them
  285.        
  286.         // Other players have keypads open, just hide the textdraws for this player
  287.         TextDrawHideForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_bg]); // Hide the grey background box
  288.         for(new e=0; e<10; e++) TextDrawHideForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_num][e]); // Hide numbers
  289.         for(new e=0; e<14; e++) TextDrawHideForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_box][e]); // Hide boxes
  290.         CancelSelectTextDraw(playerid); // Hide the player's mouse cursor
  291.         return 1;
  292. }
  293.  
  294. public OnPlayerConnect(playerid) // When a player connects
  295. {
  296.         KEYPAD_pKeypad[playerid][keypad_state] = 0; // Make sure the script doesn't think they have a keypad open
  297.         return CallLocalFunction("keypad_OnPlayerConnect", "i", playerid); // Hooking method
  298. }
  299.  
  300. #if defined _ALS_OnPlayerConnect
  301.     #undef OnPlayerConnect
  302. #else
  303.     #define _ALS_OnPlayerConnect
  304. #endif
  305. #define OnPlayerConnect keypad_OnPlayerConnect
  306. forward keypad_OnPlayerConnect(playerid);
  307.  
  308. public OnPlayerDisconnect(playerid, reason) // When a player disconnects
  309. {
  310.         if(KEYPAD_pKeypad[playerid][keypad_state]) HideKeypad(playerid); // If they have a keypad open, close it
  311.         return CallLocalFunction("keypad_OnPlayerDisconnect", "ii", playerid, reason);
  312. }
  313.  
  314. #if defined _ALS_OnPlayerDisconnect
  315.     #undef OnPlayerDisconnect
  316. #else
  317.     #define _ALS_OnPlayerDisconnect
  318. #endif
  319. #define OnPlayerDisconnect keypad_OnPlayerDisconnect
  320. forward keypad_OnPlayerDisconnect(playerid, reason);
  321.  
  322. public OnPlayerClickTextDraw(playerid, Text:clickedid) // When a player clicks a textdraw
  323. {
  324.         if(KEYPAD_pKeypad[playerid][keypad_state] == 1) // If they are inputting a code in a keypad, check if they clicked one of the numbers
  325.         {
  326.             if(clickedid == Text:INVALID_TEXT_DRAW) // They pressed ESC to cancel the keypad, so close it and call OnPlayerKeypadInput with 'type'  KEYPAD_INPUT_CANCEL
  327.                 {
  328.                     PlayerPlaySound(playerid, 21001, 0, 0, 0);
  329.                         CallLocalFunction("OnPlayerKeypadInput", "iiii", playerid, KEYPAD_pKeypad[playerid][keypad_id], KEYPAD_INPUT_CANCEL, strval(KEYPAD_pKeypad[playerid][keypad_key]));
  330.                         return HideKeypad(playerid);
  331.                 }
  332.             for(new i=0; i<10; i++) // Loop through the 10 numbers, see if they clicked one
  333.             {
  334.                 if(clickedid == KEYPAD_gKeypadTextdraws[keypad_textdraw_num][i]) // We found the number they clicked.
  335.                 {
  336.                     new numstr[2]; // Store the number in a string
  337.                     format(numstr, 2, "%i", i);
  338.                     strins(KEYPAD_pKeypad[playerid][keypad_string], numstr, strlen(KEYPAD_pKeypad[playerid][keypad_string])); // Add the number to the end of the string that stores what they have entered so far
  339.                     if(strlen(KEYPAD_pKeypad[playerid][keypad_string]) == strlen(KEYPAD_pKeypad[playerid][keypad_key])) // Reached length of required key, check if it is correct
  340.                     {
  341.                         if(strcmp(KEYPAD_pKeypad[playerid][keypad_string], KEYPAD_pKeypad[playerid][keypad_key]) != 0) // Not the correct key
  342.                         {
  343.                                         PlayerPlaySound(playerid, 21001, 0, 0, 0); // Play the 'fail' sound
  344.                                                
  345.                                                 new keypad_memory = KEYPAD_pKeypad[playerid][keypad_id]; // Store what keypad they have open, because we need to reset it before calling OnPlayerKeypadInput
  346.                                                 KEYPAD_pKeypad[playerid][keypad_id] = 0; // Reset what keypad they have open
  347.                                                
  348.                                         TextDrawHideForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_OFF]); // Hide the DARK red box
  349.                                                 TextDrawShowForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_RED_ON]);  // and show the LIT UP red box
  350.                                         KEYPAD_pKeypad[playerid][keypad_state] = 2; // State 2 means they have entered a key, and this will not allow them to click more numbers
  351.                                         CancelSelectTextDraw(playerid); // Hide the mouse cursor
  352.                                        
  353.                                         SetTimerEx("keypad_hide", KEYPAD_STAY_TIME, false, "i", playerid); // Hide the keypad after so long. This is defined by KEYPAD_STAY_TIME at the top of this include. You should leave time for them to see the red/green box light up.
  354.                                                 CallLocalFunction("OnPlayerKeypadInput", "iiii", playerid, keypad_memory, KEYPAD_INPUT_BAD, strval(KEYPAD_pKeypad[playerid][keypad_string])); // Call OnPlayerKeypadInput with 'type' KEYPAD_INPUT_BAD
  355.                                                 return 1;
  356.                                         }
  357.                                         else // Success, they entered the correct key
  358.                                         {
  359.                                         PlayerPlaySound(playerid, 21002, 0, 0, 0); // Play the 'success' sound
  360.                                         TextDrawHideForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_OFF]); // Hide the DARK green box
  361.                                                 TextDrawShowForPlayer(playerid, KEYPAD_gKeypadTextdraws[keypad_textdraw_box][KEYPAD_TD_GREEN_ON]);  // and show the LIT UP green box
  362.                                         KEYPAD_pKeypad[playerid][keypad_state] = 2; // State 2 means they have entered a key, and this will not allow them to click more numbers
  363.                                         CancelSelectTextDraw(playerid); // Hide the mouse cursor
  364.                                                 new keypad_memory = KEYPAD_pKeypad[playerid][keypad_id]; // Store what keypad they have open, because we need to reset it before calling OnPlayerKeypadInput
  365.                                                 KEYPAD_pKeypad[playerid][keypad_id] = 0; // Reset what keypad they have open
  366.                                         SetTimerEx("keypad_hide", KEYPAD_STAY_TIME, false, "i", playerid); // Hide the keypad after so long. This is defined by KEYPAD_STAY_TIME at the top of this include. You should leave time for them to see the red/green box light up.
  367.                                         CallLocalFunction("OnPlayerKeypadInput", "iiii", playerid, keypad_memory, KEYPAD_INPUT_GOOD, strval(KEYPAD_pKeypad[playerid][keypad_string])); // Call OnPlayerKeypadInput with 'type' KEYPAD_INPUT_BAD
  368.                                                 return 1;
  369.                                         }
  370.                                 }
  371.                     PlayerPlaySound(playerid, 21000, 0, 0, 0); // This is the beep sound which will be played when they click a number
  372.                     return 1;
  373.                 }
  374.             }
  375.         }
  376.         return CallLocalFunction("myinc_OnPlayerClickTextDraw", "ii", playerid, _:clickedid); // Hooking method
  377. }
  378.  
  379. #if defined _ALS_OnPlayerClickTextDraw
  380.     #undef OnPlayerClickTextDraw
  381. #else
  382.     #define _ALS_OnPlayerClickTextDraw
  383. #endif
  384. #define OnPlayerClickTextDraw myinc_OnPlayerClickTextDraw
  385. forward myinc_OnPlayerClickTextDraw(playerid, Text:clickedid);
  386.  
  387. // keypad_hide is called after so long, and will hide the keypad
  388. forward keypad_hide(playerid);
  389. public keypad_hide(playerid)
  390. {
  391.         HideKeypad(playerid); // Hide the keypad for the player
  392.         return 1;
  393. }
  394.  
  395. // This is used to determine whether a string is just numbers, as there are no letters on the keypad so the string MUST be all numbers!
  396. stock keypad_IsNumeric(const isthisnumeric[])
  397. {
  398.         if(!strlen(isthisnumeric)) return 0;
  399.         for(new i = 0; i < strlen(isthisnumeric); i++)
  400.         {
  401.                 if(isthisnumeric[i] > '9' || isthisnumeric[i] < '0') return 0;
  402.         }
  403.         return 1;
  404. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top