1
0

y_dialog.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. \*----------------------------------------------------------------------------*/
  56. // y_dialog - does stuff with dialogs.
  57. #include "internal\y_version"
  58. #include "y_bit"
  59. #include "y_remote"
  60. #include "y_inline"
  61. #include "y_iterate"
  62. #include "y_hooks"
  63. #include "y_amx"
  64. #define YSIM_U_DISABLE
  65. #include "y_master"
  66. #if !defined MAX_DIALOGS
  67. #define MAX_DIALOGS (2000)
  68. #endif
  69. // This is actually QUITE A LOT of data, I should maybe sort that out.
  70. static stock
  71. BitArray:YSI_g_sIDs<MAX_DIALOGS> = {Bit:-1, ...},
  72. BitArray:YSI_g_sFree<MAX_DIALOGS>,
  73. YSI_g_sPlayerDialog[MAX_PLAYERS] = {-1, ...},
  74. YSI_g_sDialogPlayers[MAX_DIALOGS],
  75. YSI_g_sDialogMasters[MAX_DIALOGS] = {-1, ...},
  76. YSI_g_sDialogInfo[MAX_DIALOGS][E_CALLBACK_DATA];
  77. /*stock Dialog_ObtainID()
  78. {
  79. new
  80. data = Dialog_DoObtainID();
  81. YSI_g_sDialogInfo[data][E_CALLBACK_DATA_POINTER] = 0;
  82. return data;
  83. }*/
  84. foreign Dialog_ObtainID();
  85. global Dialog_ObtainID()
  86. {
  87. static const
  88. scDeBruijn[] =
  89. {
  90. 0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
  91. 31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
  92. };
  93. for (new i = 0; i != sizeof (YSI_g_sIDs); ++i)
  94. {
  95. if (YSI_g_sIDs[i])
  96. {
  97. new
  98. Bit:b = (YSI_g_sIDs[i] & -YSI_g_sIDs[i]),
  99. data = scDeBruijn[(_:b * 0x077CB531) >>> 27];
  100. YSI_g_sIDs[i] &= ~b;
  101. data += (i * 32);
  102. YSI_g_sDialogPlayers[data] = 0;
  103. YSI_g_sDialogMasters[data] = -1;
  104. //YSI_g_sDialogInfo[data][E_CALLBACK_DATA_POINTER] = 0;
  105. return data;
  106. }
  107. }
  108. return -1;
  109. // http://supertech.csail.mit.edu/papers/debruijn.pdf
  110. }
  111. foreign Dialog_TryObtainID(id);
  112. global Dialog_TryObtainID(id)
  113. {
  114. if (Bit_Get(YSI_g_sIDs, id))
  115. {
  116. Bit_Vet(YSI_g_sIDs, id);
  117. YSI_g_sDialogPlayers[id] = 0;
  118. YSI_g_sDialogMasters[id] = -1;
  119. //YSI_g_sDialogInfo[data][E_CALLBACK_DATA_POINTER] = 0;
  120. return id;
  121. }
  122. P:C(else P:E("Dialog ID %d already in use", id););
  123. return -1;
  124. }
  125. foreign Dialog_Get(playerid);
  126. global Dialog_Get(playerid)
  127. {
  128. return YSI_g_sPlayerDialog[playerid];
  129. }
  130. foreign void:Dialog_Garbage(dialogid);
  131. global void:Dialog_Garbage(dialogid)
  132. {
  133. Bit_Let(YSI_g_sFree, dialogid);
  134. //return 1;
  135. }
  136. foreign void:Dialog_Free(dialogid);
  137. global void:Dialog_Free(dialogid)
  138. {
  139. Bit_Vet(YSI_g_sFree, dialogid);
  140. Bit_Let(YSI_g_sIDs, dialogid);
  141. if (YSI_g_sDialogPlayers[dialogid])
  142. {
  143. YSI_g_sDialogPlayers[dialogid] = 0;
  144. foreach (new i : Player)
  145. {
  146. if (YSI_g_sPlayerDialog[i] == dialogid)
  147. {
  148. ShowPlayerDialog(i, -1, 0, NULL, NULL, NULL, NULL);
  149. YSI_g_sPlayerDialog[i] = -1;
  150. }
  151. }
  152. }
  153. //return 1;
  154. }
  155. foreign Dialog_Set(playerid, dialogid);
  156. global Dialog_Set(playerid, dialogid)
  157. {
  158. new
  159. old = YSI_g_sPlayerDialog[playerid];
  160. if (dialogid != old)
  161. {
  162. if (dialogid != -1)
  163. {
  164. ++YSI_g_sDialogPlayers[dialogid];
  165. }
  166. YSI_g_sPlayerDialog[playerid] = dialogid;
  167. if (old != -1)
  168. {
  169. if (--YSI_g_sDialogPlayers[old] == 0)
  170. {
  171. if (Bit_Get(YSI_g_sFree, old))
  172. {
  173. YSI_g_sDialogMasters[old] = -1;
  174. Bit_Vet(YSI_g_sFree, old);
  175. Bit_Let(YSI_g_sIDs, old);
  176. // "old" destroyed.
  177. return 0;
  178. }
  179. }
  180. }
  181. }
  182. // "old" still exists.
  183. return 1;
  184. }
  185. stock Dialog_ShowCallback(playerid, callback:callback, style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  186. {
  187. new
  188. ret = Dialog_Show(playerid, style, title, caption, button1, button2, dialog),
  189. data[E_CALLBACK_DATA];
  190. if (Callback_Get(callback, data, _F<iiiis>))
  191. {
  192. Dialog_SetCallbackData(ret, data);
  193. }
  194. return ret;
  195. }
  196. stock Dialog_ShowCallbackData(playerid, callback[E_CALLBACK_DATA], style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  197. {
  198. new
  199. ret = Dialog_Show(playerid, style, title, caption, button1, button2, dialog);
  200. Dialog_SetCallbackData(ret, callback);
  201. return ret;
  202. }
  203. stock Dialog_Show(playerid, style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  204. {
  205. if (dialog == -1)
  206. {
  207. dialog = Dialog_ObtainID();
  208. Dialog_Garbage(dialog);
  209. }
  210. ShowPlayerDialog(playerid, dialog, style, title, caption, button1, button2);
  211. Dialog_Set(playerid, dialog);
  212. return dialog;
  213. }
  214. stock Dialog_Hide(playerid)
  215. {
  216. // This almost looks like a Windows API function call!
  217. ShowPlayerDialog(playerid, -1, 0, NULL, NULL, NULL, NULL);
  218. return Dialog_Set(playerid, -1);
  219. }
  220. hook OnPlayerDisconnect(playerid, reason)
  221. {
  222. #pragma unused reason
  223. Dialog_Hide(playerid);
  224. return 1;
  225. }
  226. remotefunc void:Dialog_SetMaster(dialogid, master)
  227. {
  228. //printf("SETTING %d MASTER: %d", dialogid, master);
  229. YSI_g_sDialogMasters[dialogid] = master;
  230. //YSI_g_sDialogInfo[dialogid][E_CALLBACK_DATA_POINTER] = 0;
  231. }
  232. stock Dialog_GetMaster(dialogid)
  233. {
  234. return YSI_g_sDialogMasters[dialogid];
  235. }
  236. stock Dialog_SetCallback(dialogid, callback:callback)
  237. {
  238. new
  239. data[E_CALLBACK_DATA];
  240. if (Callback_Get(callback, data, _F<iiiis>))
  241. {
  242. Dialog_SetCallbackData(dialogid, data);
  243. }
  244. P:C(else P:E("Could not find dialog callback %s", _:callback););
  245. }
  246. stock Dialog_SetCallbackData(dialogid, callback[E_CALLBACK_DATA])
  247. {
  248. broadcastfunc Dialog_SetMaster(dialogid, _@);
  249. YSI_g_sDialogInfo[dialogid] = callback;
  250. }
  251. // Need to somehow integrate this new dialogs library with the text library.
  252. hook OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  253. {
  254. P:1("Dialog_OnDialogResponse called: %d %d %d %d %s", playerid, dialogid, response, listitem, inputtext);
  255. // Apparently there's a hack to alter this.
  256. dialogid = Dialog_Get(playerid);
  257. P:5("Dialog_OnDialogResponse: dialog = %d", dialogid);
  258. if (dialogid == -1)
  259. {
  260. return 0;
  261. }
  262. P:5("Dialog_OnDialogResponse: master = %d, %d", YSI_g_sDialogMasters[dialogid], _@);
  263. if (YSI_g_sDialogMasters[dialogid] != _@)
  264. {
  265. return 0;
  266. }
  267. // Dialogs close automaticaly.
  268. if (Dialog_Set(playerid, -1))
  269. {
  270. P:5("OnDialogResponse: Not free");
  271. if (YSI_g_sDialogInfo[dialogid][E_CALLBACK_DATA_POINTER])
  272. {
  273. //printf("Calling function at 0x%08x", YSI_g_sDialogInfo[dialogid][E_CALLBACK_DATA_POINTER]);
  274. Callback_Call(YSI_g_sDialogInfo[dialogid], playerid, dialogid, response, listitem, inputtext);
  275. return 1;
  276. }
  277. }
  278. else
  279. {
  280. // Or it will screw up when you show a new dialog from a dialog.
  281. P:5("OnDialogResponse: Already freed");
  282. new
  283. dat[E_CALLBACK_DATA];
  284. dat = YSI_g_sDialogInfo[dialogid];
  285. // Callback no longer needed.
  286. if (dat[E_CALLBACK_DATA_POINTER])
  287. {
  288. //printf("Calling function at 0x%08x", dat[E_CALLBACK_DATA_POINTER]);
  289. Callback_Call(dat, playerid, dialogid, response, listitem, inputtext);
  290. Callback_Release(dat);
  291. return 1;
  292. }
  293. }
  294. //Dialog_Set(playerid, -1);
  295. return 0;
  296. }
  297. /*stock _ShowPlayerDialog(playerid, dialog, style, string:title[], string:caption[], string:button1[], string:button2[])
  298. {
  299. Dialog_TryObtainID(dialog);
  300. YSI_g_sDialogInfo[dialog][E_CALLBACK_DATA_POINTER] = 0;
  301. ShowPlayerDialog(playerid, dialog, style, title, caption, button1, button2);
  302. return 0;
  303. }
  304. #if defined _ALS_ShowPlayerDialog
  305. #undef ShowPlayerDialog
  306. #else
  307. #define _ALS_ShowPlayerDialog
  308. #endif
  309. #define ShowPlayerDialog _ShowPlayerDialog
  310. #define HidePlayerDialog Dialog_Hide*/
  311. /*stock HidePlayerDialog(playerid)
  312. {
  313. ShowPlayerDialog(playerid, -1, 0, NULL, NULL, NULL, NULL);
  314. }*/
  315. //#define YSI_SET_LAST_GROUP 19
  316. #include "internal\y_grouprevert"