y_dialog.inc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. #if defined _INC_y_dialog
  2. #endinput
  3. #endif
  4. #define _INC_y_dialog
  5. /**
  6. * <library name="y_dialog">
  7. * <section>
  8. * Description
  9. * </section>
  10. * Provides functions for dealing with dialogs, without needing to worry about
  11. * IDs or huge "OnDialogResponse" callbacks.
  12. * <section>
  13. * Version
  14. * </section>
  15. * 0.1
  16. * </library>
  17. *//** *//*
  18. Legal:
  19. Version: MPL 1.1
  20. The contents of this file are subject to the Mozilla Public License Version
  21. 1.1 the "License"; you may not use this file except in compliance with
  22. the License. You may obtain a copy of the License at
  23. http://www.mozilla.org/MPL/
  24. Software distributed under the License is distributed on an "AS IS" basis,
  25. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  26. for the specific language governing rights and limitations under the
  27. License.
  28. The Original Code is the YSI framework.
  29. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  30. Portions created by the Initial Developer are Copyright C 2011
  31. the Initial Developer. All Rights Reserved.
  32. Contributors:
  33. Y_Less
  34. koolk
  35. JoeBullet/Google63
  36. g_aSlice/Slice
  37. Misiur
  38. samphunter
  39. tianmeta
  40. maddinat0r
  41. spacemud
  42. Crayder
  43. Dayvison
  44. Ahmad45123
  45. Zeex
  46. irinel1996
  47. Yiin-
  48. Chaprnks
  49. Konstantinos
  50. Masterchen09
  51. Southclaws
  52. PatchwerkQWER
  53. m0k1
  54. paulommu
  55. udan111
  56. Thanks:
  57. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  58. ZeeX - Very productive conversations.
  59. koolk - IsPlayerinAreaEx code.
  60. TheAlpha - Danish translation.
  61. breadfish - German translation.
  62. Fireburn - Dutch translation.
  63. yom - French translation.
  64. 50p - Polish translation.
  65. Zamaroht - Spanish translation.
  66. Los - Portuguese translation.
  67. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes for
  68. me to strive to better.
  69. Pixels^ - Running XScripters where the idea was born.
  70. Matite - Pestering me to release it and using it.
  71. Very special thanks to:
  72. Thiadmer - PAWN, whose limits continue to amaze me!
  73. Kye/Kalcor - SA:MP.
  74. SA:MP Team past, present and future - SA:MP.
  75. Optional plugins:
  76. Gamer_Z - GPS.
  77. Incognito - Streamer.
  78. Me - sscanf2, fixes2, Whirlpool.
  79. */
  80. // y_dialog - does stuff with dialogs.
  81. #include "..\YSI_Internal\y_version"
  82. #include "..\YSI_Data\y_bit"
  83. #include "..\YSI_Coding\y_remote"
  84. #include "..\YSI_Coding\y_inline"
  85. #include "..\YSI_Data\y_iterate"
  86. #include "..\YSI_Coding\y_hooks"
  87. #include "..\YSI_Storage\y_amx"
  88. #define YSIM_U_DISABLE
  89. #define MASTER 54
  90. #include "..\YSI_Core\y_master"
  91. #define YSI_DIALOG_ID _A<_yD@>
  92. static stock
  93. YSI_g_sPlayerDialog[MAX_PLAYERS] = {-1, ...},
  94. bool:YSI_g_sPlayerMaster[MAX_PLAYERS] = {false, ...},
  95. YSI_g_sPlayerCallback[MAX_PLAYERS][E_CALLBACK_DATA];
  96. stock Dialog_Get(playerid)
  97. {
  98. return YSI_g_sPlayerDialog[playerid];
  99. }
  100. remotefunc void:Dialog_Set(playerid, dialogid)
  101. {
  102. // If they could already see a dialog shown by this script, get rid of the
  103. // data from that one.
  104. YSI_g_sPlayerDialog[playerid] = dialogid;
  105. if (YSI_g_sPlayerMaster[playerid])
  106. {
  107. Callback_Release(YSI_g_sPlayerCallback[playerid]),
  108. YSI_g_sPlayerMaster[playerid] = false;
  109. }
  110. }
  111. stock Dialog_ShowCallback(playerid, callback:callback, style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  112. {
  113. #pragma unused dialog
  114. new
  115. ret = Dialog_Show(playerid, style, title, caption, button1, button2),
  116. data[E_CALLBACK_DATA];
  117. if (Callback_Get(callback, data, _F<iiiis>))
  118. YSI_g_sPlayerCallback[playerid] = data;
  119. return ret;
  120. }
  121. stock Dialog_ShowCallbackData(playerid, callback[E_CALLBACK_DATA], style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  122. {
  123. #pragma unused dialog
  124. new
  125. ret = Dialog_Show(playerid, style, title, caption, button1, button2);
  126. YSI_g_sPlayerCallback[playerid] = callback;
  127. return ret;
  128. }
  129. stock Dialog_Show(playerid, style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  130. {
  131. #pragma unused dialog
  132. broadcastfunc Dialog_Set(playerid, YSI_DIALOG_ID);
  133. YSI_g_sPlayerMaster[playerid] = true;
  134. return ShowPlayerDialog(playerid, YSI_DIALOG_ID, style, title, caption, button1, button2);
  135. }
  136. stock Dialog_Hide(playerid)
  137. {
  138. // This almost looks like a Windows API function call!
  139. broadcastfunc Dialog_Set(playerid, -1);
  140. return ShowPlayerDialog(playerid, -1, 0, NULL, NULL, NULL, NULL);
  141. }
  142. hook OnPlayerDisconnect(playerid, reason)
  143. {
  144. #pragma unused reason
  145. // If this is a filterscript that has the currently displayed dialog in it,
  146. // then we need all scripts to know that this is no longer the case. If
  147. // this script is not in charge of the current dialog then do not broadcast
  148. // the reset, just do it locally.
  149. if (YSI_g_sPlayerMaster[playerid])
  150. Dialog_Hide(playerid);
  151. else
  152. YSI_g_sPlayerDialog[playerid] = -1;
  153. return 1;
  154. }
  155. hook OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  156. {
  157. // Apparently there's a hack to alter this.
  158. dialogid = YSI_g_sPlayerDialog[playerid];
  159. P:1("Dialog_OnDialogResponse called: %d %d %d %d %s", playerid, dialogid, response, listitem, inputtext);
  160. if (dialogid == YSI_DIALOG_ID)
  161. {
  162. if (YSI_g_sPlayerMaster[playerid])
  163. {
  164. static
  165. reset[E_CALLBACK_DATA];
  166. new
  167. callback[E_CALLBACK_DATA];
  168. callback = YSI_g_sPlayerCallback[playerid];
  169. // Totally blank the stored dialog information, after making a local
  170. // copy of it for the sake of calling the callback. This is so that
  171. // we don't free the memory for the callback prematurely.
  172. YSI_g_sPlayerCallback[playerid] = reset;
  173. broadcastfunc Dialog_Set(playerid, -1);
  174. // Call the callback.
  175. Callback_Call(callback, playerid, YSI_DIALOG_ID, response, listitem, inputtext);
  176. // Free the data.
  177. Callback_Release(callback);
  178. return Y_HOOKS_BREAK_RETURN_1;
  179. }
  180. return Y_HOOKS_BREAK_RETURN_0;
  181. }
  182. else
  183. return Y_HOOKS_CONTINUE_RETURN_0;
  184. }
  185. stock _ShowPlayerDialog(playerid, dialog, style, string:title[], string:caption[], string:button1[], string:button2[])
  186. {
  187. // Fail.
  188. if (dialog == YSI_DIALOG_ID)
  189. return 0;
  190. broadcastfunc Dialog_Set(playerid, dialog);
  191. YSI_g_sPlayerMaster[playerid] = true;
  192. return ShowPlayerDialog(playerid, dialog, style, title, caption, button1, button2);
  193. }
  194. #if defined _ALS_ShowPlayerDialog
  195. #undef ShowPlayerDialog
  196. #else
  197. #define _ALS_ShowPlayerDialog
  198. #endif
  199. #define ShowPlayerDialog _ShowPlayerDialog
  200. #define _ALS_HidePlayerDialog
  201. #define HidePlayerDialog Dialog_Hide
  202. #include "..\YSI_Core\y_master"