easyDialog.inc 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. easyDialog.inc - Dialogs made easier!
  3. With this useful include, scripters can easily create
  4. dialogs and show them to players.
  5. This include will prevent dialog spoofing, ID collision
  6. and a lot more.
  7. Created by Emmet on Friday, January 24, 2014.
  8. */
  9. #if !defined isnull
  10. #define isnull(%1) \
  11. ((!(%1[0])) || (((%1[0]) == '\1') && (!(%1[1]))))
  12. #endif
  13. #define Dialog:%0(%1) \
  14. forward dialog_%0(%1); public dialog_%0(%1)
  15. #define Dialog_Show(%0,%1, \
  16. Dialog_Open(%0, #%1,
  17. #define Dialog_Opened(%0) \
  18. (CallRemoteFunction("Dialog_IsOpened", "i", (%0)))
  19. static
  20. s_DialogName[MAX_PLAYERS][32 char],
  21. s_DialogOpened[MAX_PLAYERS]
  22. ;
  23. forward OnDialogPerformed(playerid, dialog[], response, success);
  24. forward @dialog_format(); @dialog_format() {
  25. format("", 0, "");
  26. }
  27. forward Dialog_IsOpened(playerid);
  28. public Dialog_IsOpened(playerid)
  29. {
  30. return (s_DialogOpened[playerid]);
  31. }
  32. stock Dialog_Close(playerid)
  33. {
  34. s_DialogName[playerid]{0} = 0;
  35. s_DialogOpened[playerid] = 0;
  36. return ShowPlayerDialog(playerid, -1, DIALOG_STYLE_MSGBOX, " ", " ", " ", "");
  37. }
  38. stock Dialog_Open(playerid, function[], style, caption[], info[], button1[], button2[], {Float,_}:...)
  39. {
  40. static
  41. string[4096],
  42. args
  43. ;
  44. if (!strlen(info))
  45. {
  46. return 0;
  47. }
  48. if ((args = numargs()) > 7)
  49. {
  50. while (--args >= 7)
  51. {
  52. #emit LCTRL 5
  53. #emit LOAD.alt args
  54. #emit SHL.C.alt 2
  55. #emit ADD.C 12
  56. #emit ADD
  57. #emit LOAD.I
  58. #emit PUSH.pri
  59. }
  60. #emit PUSH.S info
  61. #emit PUSH.C 4096
  62. #emit PUSH.C string
  63. #emit LOAD.S.pri 8
  64. #emit CONST.alt 16
  65. #emit SUB
  66. #emit PUSH.pri
  67. #emit SYSREQ.C format
  68. #emit LCTRL 5
  69. #emit SCTRL 4
  70. ShowPlayerDialog(playerid, 32700, style, caption, string, button1, button2);
  71. }
  72. else
  73. {
  74. ShowPlayerDialog(playerid, 32700, style, caption, info, button1, button2);
  75. }
  76. s_DialogOpened[playerid] = 1;
  77. strpack(s_DialogName[playerid], function, 32 char);
  78. return 1;
  79. }
  80. public OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  81. {
  82. static
  83. s_Public = cellmax;
  84. if (s_Public == cellmax)
  85. {
  86. s_Public = funcidx("OnDialogPerformed");
  87. }
  88. // Sanitize inputs.
  89. for (new i = 0, l = strlen(inputtext); i < l; i ++)
  90. {
  91. if (inputtext[i] == '%')
  92. {
  93. inputtext[i] = '#';
  94. }
  95. }
  96. if (dialogid == 32700 && strlen(s_DialogName[playerid]) > 0)
  97. {
  98. new
  99. string[40];
  100. strcat(string, "dialog_");
  101. strcat(string, s_DialogName[playerid]);
  102. Dialog_Close(playerid);
  103. if ((s_Public == -1) || (CallLocalFunction("OnDialogPerformed", "dsdd", playerid, string[7], response, funcidx(string) != -1)))
  104. {
  105. CallLocalFunction(string, "ddds", playerid, response, listitem, (!inputtext[0]) ? ("\1") : (inputtext));
  106. }
  107. }
  108. #if defined DR_OnDialogResponse
  109. return DR_OnDialogResponse(playerid, dialogid, response, listitem, inputtext);
  110. #else
  111. return 0;
  112. #endif
  113. }
  114. #if defined _ALS_OnDialogResponse
  115. #undef OnDialogResponse
  116. #else
  117. #define _ALS_OnDialogResponse
  118. #endif
  119. #define OnDialogResponse DR_OnDialogResponse
  120. #if defined DR_OnDialogResponse
  121. forward DR_OnDialogResponse(playerid, dialogid, response, listitem, inputtext[]);
  122. #endif