Advertisement
Eduardo_AC

e_dialog.inc

Jul 21st, 2022 (edited)
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pawn 8.68 KB | None | 0 0
  1. /*
  2.     e_dialog by Eduardo AC © 2022
  3.    
  4.     OnOpenPlayerDialog(playerid, response_callback[])
  5.     OnClosePlayerDialog(playerid, response_callback[], delay)
  6.    
  7.     ShowPlayerDialog(playerid, const response_callback[], style, const caption[], const info[], const button1[], const button2[])
  8.     ShowDialogForPlayer(playerid, style, const caption[], const info[], const button1[], const button2[], const response_callback[])
  9.     HasPlayerOpenDialog(playerid)
  10.     GetPlayerDialogResponseName(playerid)
  11.     GetLastPlayerDialogResponseName(playerid)
  12.     ClosePlayerDialog(playerid, bool:blank_dialog = false)
  13.     OpenLastPlayerDialog(playerid)
  14. */
  15.  
  16. #if defined _e_dialog_included
  17.     #endinput
  18. #endif
  19. #define _e_dialog_included
  20. #pragma library e_dialog
  21.  
  22. #define E_DIALOG_HOOK                   false
  23. #define E_DIALOG_ID                     32760
  24. #define E_DIALOG_CLOSE_CHECK            1500
  25.  
  26. static __playerDialogResponse[MAX_PLAYERS][32];
  27. static __playerTimerCheckDialogClose[MAX_PLAYERS] = {-1, ...};
  28.  
  29. // Last dialog
  30. static __playerLastDialogStyle[MAX_PLAYERS] = {-1, ...};
  31. static __playerLastDialogCaption[MAX_PLAYERS][128];
  32. static __playerLastDialogInfo[MAX_PLAYERS][4096];
  33. static __playerLastDialogButton1[MAX_PLAYERS][20];
  34. static __playerLastDialogButton2[MAX_PLAYERS][20];
  35. static __playerLastDialogResponse[MAX_PLAYERS][32];
  36.  
  37. forward OnOpenPlayerDialog(playerid, response_callback[]);
  38. forward OnClosePlayerDialog(playerid, response_callback[], delay);
  39.  
  40. public OnPlayerDisconnect(playerid, reason)
  41. {
  42.     KillTimer(__playerTimerCheckDialogClose[playerid]);
  43.     __playerTimerCheckDialogClose[playerid] = -1;
  44.     __playerDialogResponse[playerid][0] = '\0';
  45.    
  46.     __playerLastDialogStyle[playerid] = -1;
  47.     __playerLastDialogCaption[playerid][0] = '\0';
  48.     __playerLastDialogInfo[playerid][0] = '\0';
  49.     __playerLastDialogButton1[playerid][0] = '\0';
  50.     __playerLastDialogButton2[playerid][0] = '\0';
  51.     __playerLastDialogResponse[playerid][0] = '\0';
  52.  
  53.     #if defined e_dialog_OnPlayerDisconnect
  54.         e_dialog_OnPlayerDisconnect(playerid, reason);
  55.     #endif
  56.     return true;
  57. }
  58.  
  59. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  60. {
  61.     if(dialogid == E_DIALOG_ID)
  62.     {
  63.         if(__playerDialogResponse[playerid][0] != '\0')
  64.         {
  65.             new response_callback[32];
  66.             strcat(response_callback, __playerDialogResponse[playerid]);
  67.             __playerDialogResponse[playerid][0] = '\0';
  68.            
  69.             if(inputtext[0] != '\0')
  70.             {
  71.                 for(new i = 0, len = strlen(inputtext); i < len; i ++)
  72.                 {
  73.                     if(inputtext[i] == '%')
  74.                         inputtext[i] = '#';
  75.                 }
  76.                
  77.                 CallLocalFunction(response_callback, "iiis", playerid, response, listitem, inputtext);
  78.             }
  79.             else
  80.                 CallLocalFunction(response_callback, "iii", playerid, response, listitem);
  81.         }
  82.         return false;
  83.     }
  84.    
  85.     #if defined e_dialog_OnDialogResponse
  86.         e_dialog_OnDialogResponse(playerid, dialogid, response, listitem, inputtext);
  87.     #endif
  88.     return true;
  89. }
  90.  
  91. forward Timer_CheckDialogClose(playerid, response_callback[], tick);
  92. public Timer_CheckDialogClose(playerid, response_callback[], tick)
  93. {
  94.     if(__playerDialogResponse[playerid][0] == '\0')
  95.     {
  96.         KillTimer(__playerTimerCheckDialogClose[playerid]);
  97.         __playerTimerCheckDialogClose[playerid] = -1;
  98.         CallLocalFunction("OnClosePlayerDialog", "isi", playerid, response_callback, (gettime() - tick));
  99.     }
  100.     return true;
  101. }
  102.  
  103. /// <summary>
  104. /// Função interna para abrir uma caixa de diálogo.
  105. /// </summary>
  106. /// <param name="playerid">ID do jogador</param>
  107. /// <param name="style">ID do estilo de caixa</param>
  108. /// <param name="caption">Título da caixa</param>
  109. /// <param name="info">Informação da caixa</param>
  110. /// <param name="button1">Texto do botão 1</param>
  111. /// <param name="button2">Texto do botão 2</param>
  112. /// <param name="response_callback">Callback de resposta dos botões</param>
  113. /// <returns>True para sucesso ou False para erro</returns>
  114. static stock __dialogContent(playerid, style, const caption[], const info[], const button1[], const button2[], const response_callback[])
  115. {
  116.     if(!IsPlayerConnected(playerid))
  117.         return false;
  118.    
  119.     __playerLastDialogStyle[playerid] = style;
  120.     format(__playerLastDialogCaption[playerid], 128, "%s", caption);
  121.     format(__playerLastDialogInfo[playerid], 4096, "%s", info);
  122.     format(__playerLastDialogButton1[playerid], 20, "%s", button1);
  123.     format(__playerLastDialogButton2[playerid], 20, "%s", button2);
  124.     format(__playerLastDialogResponse[playerid], 32, "%s", response_callback);
  125.    
  126.     if(response_callback[0] != '\0')
  127.     {
  128.         strcat((__playerDialogResponse[playerid][0] = '\0', __playerDialogResponse[playerid]), response_callback);
  129.         KillTimer(__playerTimerCheckDialogClose[playerid]);
  130.         __playerTimerCheckDialogClose[playerid] = SetTimerEx("Timer_CheckDialogClose", E_DIALOG_CLOSE_CHECK, true, "isi", playerid, response_callback, gettime());
  131.     }
  132.    
  133.     ShowPlayerDialog(playerid, E_DIALOG_ID, style, caption, info, button1, button2);
  134.     CallLocalFunction("OnOpenPlayerDialog", "is", playerid, !strlen(response_callback) ? ("<null>") : response_callback);
  135.     return true;
  136. }
  137.  
  138. #if E_DIALOG_HOOK
  139.     stock e_dialog_ShowPlayerDialog(playerid, const response_callback[], style, const caption[], const info[], const button1[], const button2[])
  140.     {
  141.         return __dialogContent(playerid, style, caption, info, button1, button2, response_callback);
  142.     }
  143.     #if defined _ALS_ShowPlayerDialog
  144.         #undef ShowPlayerDialog
  145.     #else
  146.         #define _ALS_ShowPlayerDialog
  147.     #endif
  148.     #define ShowPlayerDialog e_dialog_ShowPlayerDialog
  149. #endif
  150.  
  151. stock ShowDialogForPlayer(playerid, style, const caption[], const info[], const button1[], const button2[], const response_callback[])
  152. {
  153.     return __dialogContent(playerid, style, caption, info, button1, button2, response_callback);
  154. }
  155.  
  156. /// <summary>
  157. /// Retorna se o jogador possui uma caixa de diálogo (e_dialog) aberta.
  158. /// </summary>
  159. /// <param name="playerid">ID do jogador</param>
  160. /// <returns>True para aberta ou False para fechada</returns>
  161. stock HasPlayerOpenDialog(playerid)
  162. {
  163.     if(!IsPlayerConnected(playerid))
  164.         return false;
  165.    
  166.     return __playerTimerCheckDialogClose[playerid] > -1;
  167. }
  168.  
  169. /// <summary>
  170. /// Retorna o nome da callback de resposta que está ativa na tela do jogador.
  171. /// </summary>
  172. /// <param name="playerid">ID do jogador</param>
  173. /// <returns>Nome da callback de resposta</returns>
  174. stock GetPlayerDialogResponseName(playerid)
  175. {
  176.     new dname[32];
  177.     if(!IsPlayerConnected(playerid))
  178.         return dname;
  179.    
  180.     if(__playerTimerCheckDialogClose[playerid] == -1)
  181.         return dname;
  182.    
  183.     strcat(dname, __playerLastDialogResponse[playerid]);
  184.     return dname;
  185. }
  186.  
  187. /// <summary>
  188. /// Retorna o nome da última callback de resposta chamada para o jogador (ativa ou não).
  189. /// </summary>
  190. /// <param name="playerid">ID do jogador</param>
  191. /// <returns>Nome da última callback de resposta</returns>
  192. stock GetLastPlayerDialogResponseName(playerid)
  193. {
  194.     new dname[32];
  195.     if(!IsPlayerConnected(playerid))
  196.         return dname;
  197.    
  198.     strcat(dname, __playerLastDialogResponse[playerid]);
  199.     return dname;
  200. }
  201.  
  202. /// <summary>
  203. /// Força a chamada da função "OnClosePlayerDialog" e remove o suporte a respostas da caixa de diálogo aberta.
  204. /// </summary>
  205. /// <param name="playerid">ID do jogador</param>
  206. /// <param name="blank_dialog">Quando verdadeiro abre uma caixa de diálogo vazia no lugar da anterior.</param>
  207. /// <returns>True para sucesso ou False para erro</returns>
  208. stock ClosePlayerDialog(playerid, bool:blank_dialog = false)
  209. {
  210.     if(!IsPlayerConnected(playerid))
  211.         return false;
  212.    
  213.     __playerDialogResponse[playerid][0] = '\0';
  214.    
  215.     if(blank_dialog && __playerTimerCheckDialogClose[playerid] > -1)
  216.         __dialogContent(playerid, DIALOG_STYLE_MSGBOX, " ", " ", " ", "", "");
  217.     return true;
  218. }
  219.  
  220. /// <summary>
  221. /// Abre a última caixa de diálogo fechada.
  222. /// </summary>
  223. /// <param name="playerid">ID do jogador</param>
  224. /// <returns>True para sucesso ou False para erro</returns>
  225. stock OpenLastPlayerDialog(playerid)
  226. {
  227.     if(!IsPlayerConnected(playerid))
  228.         return false;
  229.    
  230.     __dialogContent(playerid,
  231.         __playerLastDialogStyle[playerid],
  232.         __playerLastDialogCaption[playerid],
  233.         __playerLastDialogInfo[playerid],
  234.         __playerLastDialogButton1[playerid],
  235.         __playerLastDialogButton2[playerid],
  236.         __playerLastDialogResponse[playerid]);
  237.     return true;
  238. }
  239.  
  240. // ---------------- HOOK ----------------
  241.  
  242. #if defined _ALS_OnPlayerDisconnect
  243.     #undef OnPlayerDisconnect
  244. #else
  245.     #define _ALS_OnPlayerDisconnect
  246. #endif
  247. #define OnPlayerDisconnect e_dialog_OnPlayerDisconnect
  248. #if defined e_dialog_OnPlayerDisconnect
  249.     forward e_dialog_OnPlayerDisconnect(playerid, reason);
  250. #endif
  251.  
  252. #if defined _ALS_OnDialogResponse
  253.     #undef OnDialogResponse
  254. #else
  255.     #define _ALS_OnDialogResponse
  256. #endif
  257. #define OnDialogResponse e_dialog_OnDialogResponse
  258. #if defined e_dialog_OnDialogResponse
  259.     forward e_dialog_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]);
  260. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement