y_dialog.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /**--------------------------------------------------------------------------**\
  2. =================================
  3. y_dialog - Dialogs wrapper.
  4. =================================
  5. Description:
  6. Provides functions for dealing with dialogs, without needing to worry about
  7. IDs or huge "OnDialogResponse" callbacks.
  8. Legal:
  9. Version: MPL 1.1
  10. The contents of this file are subject to the Mozilla Public License Version
  11. 1.1 (the "License"); you may not use this file except in compliance with
  12. the License. You may obtain a copy of the License at
  13. http://www.mozilla.org/MPL/
  14. Software distributed under the License is distributed on an "AS IS" basis,
  15. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  16. for the specific language governing rights and limitations under the
  17. License.
  18. The Original Code is the YSI utils include.
  19. The Initial Developer of the Original Code is Alex "Y_Less" Cole.
  20. Portions created by the Initial Developer are Copyright (C) 2011
  21. the Initial Developer. All Rights Reserved.
  22. Contributors:
  23. ZeeX, koolk, JoeBullet/Google63, g_aSlice/Slice
  24. Thanks:
  25. JoeBullet/Google63 - Handy arbitrary ASM jump code using SCTRL.
  26. ZeeX - Very productive conversations.
  27. koolk - IsPlayerinAreaEx code.
  28. TheAlpha - Danish translation.
  29. breadfish - German translation.
  30. Fireburn - Dutch translation.
  31. yom - French translation.
  32. 50p - Polish translation.
  33. Zamaroht - Spanish translation.
  34. Dracoblue, sintax, mabako, Xtreme, other coders - Producing other modes
  35. for me to strive to better.
  36. Pixels^ - Running XScripters where the idea was born.
  37. Matite - Pestering me to release it and using it.
  38. Very special thanks to:
  39. Thiadmer - PAWN, whose limits continue to amaze me!
  40. Kye/Kalcor - SA:MP.
  41. SA:MP Team past, present and future - SA:MP.
  42. Version:
  43. 0.1
  44. Changelog:
  45. 25/02/12:
  46. First version.
  47. Functions:
  48. Stock:
  49. -
  50. Inline:
  51. -
  52. Variables:
  53. Global:
  54. -
  55. </remarks>
  56. \**--------------------------------------------------------------------------**/
  57. // y_dialog - does stuff with dialogs.
  58. #include "internal\y_version"
  59. #include "y_bit"
  60. #include "y_remote"
  61. #include "y_inline"
  62. #include "y_iterate"
  63. #include "y_hooks"
  64. #include "y_amx"
  65. #define YSIM_U_DISABLE
  66. #include "y_master"
  67. #if !defined MAX_DIALOGS
  68. #define MAX_DIALOGS (2000)
  69. #endif
  70. // This is actually QUITE A LOT of data, I should maybe sort that out.
  71. static stock
  72. BitArray:YSI_g_sIDs<MAX_DIALOGS> = {Bit:-1, ...},
  73. BitArray:YSI_g_sFree<MAX_DIALOGS>,
  74. YSI_g_sPlayerDialog[MAX_PLAYERS] = {-1, ...},
  75. YSI_g_sDialogPlayers[MAX_DIALOGS],
  76. YSI_g_sDialogMasters[MAX_DIALOGS] = {-1, ...},
  77. YSI_g_sDialogInfo[MAX_DIALOGS][E_CALLBACK_DATA];
  78. /*stock Dialog_ObtainID()
  79. {
  80. new
  81. data = Dialog_DoObtainID();
  82. YSI_g_sDialogInfo[data][E_CALLBACK_DATA_POINTER] = 0;
  83. return data;
  84. }*/
  85. foreign Dialog_ObtainID();
  86. global Dialog_ObtainID()
  87. {
  88. static const
  89. scDeBruijn[] =
  90. {
  91. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  92. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  93. };
  94. for (new i = 0; i != sizeof (YSI_g_sIDs); ++i)
  95. {
  96. if (YSI_g_sIDs[i])
  97. {
  98. new
  99. Bit:b = (YSI_g_sIDs[i] & -YSI_g_sIDs[i]),
  100. data = scDeBruijn[(_:b * 0x077CB531) >>> 27];
  101. YSI_g_sIDs[i] &= ~b;
  102. data += (i * 32);
  103. YSI_g_sDialogPlayers[data] = 0;
  104. YSI_g_sDialogMasters[data] = -1;
  105. //YSI_g_sDialogInfo[data][E_CALLBACK_DATA_POINTER] = 0;
  106. return data;
  107. }
  108. }
  109. return -1;
  110. // http://supertech.csail.mit.edu/papers/debruijn.pdf
  111. }
  112. foreign Dialog_TryObtainID(id);
  113. global Dialog_TryObtainID(id)
  114. {
  115. if (Bit_Get(YSI_g_sIDs, id))
  116. {
  117. Bit_Vet(YSI_g_sIDs, id);
  118. YSI_g_sDialogPlayers[id] = 0;
  119. YSI_g_sDialogMasters[id] = -1;
  120. //YSI_g_sDialogInfo[data][E_CALLBACK_DATA_POINTER] = 0;
  121. return id;
  122. }
  123. P:C(else P:E("Dialog ID %d already in use", id););
  124. return -1;
  125. }
  126. foreign Dialog_Get(playerid);
  127. global Dialog_Get(playerid)
  128. {
  129. return YSI_g_sPlayerDialog[playerid];
  130. }
  131. foreign void:Dialog_Garbage(dialogid);
  132. global void:Dialog_Garbage(dialogid)
  133. {
  134. Bit_Let(YSI_g_sFree, dialogid);
  135. //return 1;
  136. }
  137. foreign void:Dialog_Free(dialogid);
  138. global void:Dialog_Free(dialogid)
  139. {
  140. Bit_Vet(YSI_g_sFree, dialogid);
  141. Bit_Let(YSI_g_sIDs, dialogid);
  142. if (YSI_g_sDialogPlayers[dialogid])
  143. {
  144. YSI_g_sDialogPlayers[dialogid] = 0;
  145. foreach (new i : Player)
  146. {
  147. if (YSI_g_sPlayerDialog[i] == dialogid)
  148. {
  149. ShowPlayerDialog(i, -1, 0, NULL, NULL, NULL, NULL);
  150. YSI_g_sPlayerDialog[i] = -1;
  151. }
  152. }
  153. }
  154. //return 1;
  155. }
  156. foreign Dialog_Set(playerid, dialogid);
  157. global Dialog_Set(playerid, dialogid)
  158. {
  159. new
  160. old = YSI_g_sPlayerDialog[playerid];
  161. if (dialogid != old)
  162. {
  163. if (dialogid != -1)
  164. {
  165. ++YSI_g_sDialogPlayers[dialogid];
  166. }
  167. YSI_g_sPlayerDialog[playerid] = dialogid;
  168. if (old != -1)
  169. {
  170. if (--YSI_g_sDialogPlayers[old] == 0)
  171. {
  172. if (Bit_Get(YSI_g_sFree, old))
  173. {
  174. YSI_g_sDialogMasters[old] = -1;
  175. Bit_Vet(YSI_g_sFree, old);
  176. Bit_Let(YSI_g_sIDs, old);
  177. // "old" destroyed.
  178. return 0;
  179. }
  180. }
  181. }
  182. }
  183. // "old" still exists.
  184. return 1;
  185. }
  186. stock Dialog_ShowCallback(playerid, callback:callback, style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  187. {
  188. new
  189. ret = Dialog_Show(playerid, style, title, caption, button1, button2, dialog),
  190. data[E_CALLBACK_DATA];
  191. if (Callback_Get(callback, data, _F<iiiis>))
  192. {
  193. Dialog_SetCallbackData(ret, data);
  194. }
  195. return ret;
  196. }
  197. stock Dialog_ShowCallbackData(playerid, callback[E_CALLBACK_DATA], style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  198. {
  199. new
  200. ret = Dialog_Show(playerid, style, title, caption, button1, button2, dialog);
  201. Dialog_SetCallbackData(ret, callback);
  202. return ret;
  203. }
  204. stock Dialog_Show(playerid, style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  205. {
  206. if (dialog == -1)
  207. {
  208. dialog = Dialog_ObtainID();
  209. Dialog_Garbage(dialog);
  210. }
  211. ShowPlayerDialog(playerid, dialog, style, title, caption, button1, button2);
  212. Dialog_Set(playerid, dialog);
  213. return dialog;
  214. }
  215. stock Dialog_Hide(playerid)
  216. {
  217. // This almost looks like a Windows API function call!
  218. ShowPlayerDialog(playerid, -1, 0, NULL, NULL, NULL, NULL);
  219. return Dialog_Set(playerid, -1);
  220. }
  221. hook OnPlayerDisconnect(playerid, reason)
  222. {
  223. #pragma unused reason
  224. Dialog_Hide(playerid);
  225. return 1;
  226. }
  227. remotefunc void:Dialog_SetMaster(dialogid, master)
  228. {
  229. //printf("SETTING %d MASTER: %d", dialogid, master);
  230. YSI_g_sDialogMasters[dialogid] = master;
  231. //YSI_g_sDialogInfo[dialogid][E_CALLBACK_DATA_POINTER] = 0;
  232. }
  233. stock Dialog_GetMaster(dialogid)
  234. {
  235. return YSI_g_sDialogMasters[dialogid];
  236. }
  237. stock Dialog_SetCallback(dialogid, callback:callback)
  238. {
  239. new
  240. data[E_CALLBACK_DATA];
  241. if (Callback_Get(callback, data, _F<iiiis>))
  242. {
  243. Dialog_SetCallbackData(dialogid, data);
  244. }
  245. P:C(else P:E("Could not find dialog callback %s", _:callback););
  246. }
  247. stock Dialog_SetCallbackData(dialogid, callback[E_CALLBACK_DATA])
  248. {
  249. broadcastfunc Dialog_SetMaster(dialogid, _@);
  250. YSI_g_sDialogInfo[dialogid] = callback;
  251. }
  252. // Need to somehow integrate this new dialogs library with the text library.
  253. hook OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  254. {
  255. P:1("Dialog_OnDialogResponse called: %d %d %d %d %s", playerid, dialogid, response, listitem, inputtext);
  256. // Apparently there's a hack to alter this.
  257. dialogid = Dialog_Get(playerid);
  258. P:5("Dialog_OnDialogResponse: dialog = %d", dialogid);
  259. if (dialogid == -1)
  260. {
  261. return 0;
  262. }
  263. P:5("Dialog_OnDialogResponse: master = %d, %d", YSI_g_sDialogMasters[dialogid], _@);
  264. if (YSI_g_sDialogMasters[dialogid] != _@)
  265. {
  266. return 0;
  267. }
  268. // Dialogs close automaticaly.
  269. if (Dialog_Set(playerid, -1))
  270. {
  271. P:5("OnDialogResponse: Not free");
  272. if (YSI_g_sDialogInfo[dialogid][E_CALLBACK_DATA_POINTER])
  273. {
  274. //printf("Calling function at 0x%08x", YSI_g_sDialogInfo[dialogid][E_CALLBACK_DATA_POINTER]);
  275. Callback_Call(YSI_g_sDialogInfo[dialogid], playerid, dialogid, response, listitem, inputtext);
  276. return 1;
  277. }
  278. }
  279. else
  280. {
  281. // Or it will screw up when you show a new dialog from a dialog.
  282. P:5("OnDialogResponse: Already freed");
  283. new
  284. dat[E_CALLBACK_DATA];
  285. dat = YSI_g_sDialogInfo[dialogid];
  286. // Callback no longer needed.
  287. if (dat[E_CALLBACK_DATA_POINTER])
  288. {
  289. //printf("Calling function at 0x%08x", dat[E_CALLBACK_DATA_POINTER]);
  290. Callback_Call(dat, playerid, dialogid, response, listitem, inputtext);
  291. Callback_Release(dat);
  292. return 1;
  293. }
  294. }
  295. //Dialog_Set(playerid, -1);
  296. return 0;
  297. }
  298. /*stock _ShowPlayerDialog(playerid, dialog, style, string:title[], string:caption[], string:button1[], string:button2[])
  299. {
  300. Dialog_TryObtainID(dialog);
  301. YSI_g_sDialogInfo[dialog][E_CALLBACK_DATA_POINTER] = 0;
  302. ShowPlayerDialog(playerid, dialog, style, title, caption, button1, button2);
  303. return 0;
  304. }
  305. #if defined _ALS_ShowPlayerDialog
  306. #undef ShowPlayerDialog
  307. #else
  308. #define _ALS_ShowPlayerDialog
  309. #endif
  310. #define ShowPlayerDialog _ShowPlayerDialog
  311. #define HidePlayerDialog Dialog_Hide*/
  312. /*stock HidePlayerDialog(playerid)
  313. {
  314. ShowPlayerDialog(playerid, -1, 0, NULL, NULL, NULL, NULL);
  315. }*/
  316. //#define YSI_SET_LAST_GROUP 19
  317. #include "internal\y_grouprevert"