y_dialog.inc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #if !defined MAX_DIALOGS
  92. #define MAX_DIALOGS (2000)
  93. #endif
  94. // This is actually QUITE A LOT of data, I should maybe sort that out.
  95. static stock
  96. BitArray:YSI_g_sIDs<MAX_DIALOGS> = {Bit:-1, ...},
  97. BitArray:YSI_g_sFree<MAX_DIALOGS>,
  98. YSI_g_sPlayerDialog[MAX_PLAYERS] = {-1, ...},
  99. YSI_g_sDialogPlayers[MAX_DIALOGS],
  100. YSI_g_sDialogMasters[MAX_DIALOGS] = {-1, ...},
  101. YSI_g_sDialogInfo[MAX_DIALOGS][E_CALLBACK_DATA];
  102. /*stock Dialog_ObtainID()
  103. {
  104. new
  105. data = Dialog_DoObtainID();
  106. YSI_g_sDialogInfo[data][E_CALLBACK_DATA_POINTER] = 0;
  107. return data;
  108. }*/
  109. foreign Dialog_ObtainID();
  110. global Dialog_ObtainID()
  111. {
  112. for (new i = 0; i != sizeof (YSI_g_sIDs); ++i)
  113. {
  114. if (YSI_g_sIDs[i])
  115. {
  116. new
  117. Bit:b = YSI_g_sIDs[i],
  118. data = Cell_GetLowestBit(b);
  119. YSI_g_sIDs[i] ^= Bit:Cell_GetLowestComponent(b);
  120. data += (i * 32);
  121. YSI_g_sDialogPlayers[data] = 0;
  122. YSI_g_sDialogMasters[data] = -1;
  123. //YSI_g_sDialogInfo[data][E_CALLBACK_DATA_POINTER] = 0;
  124. return data;
  125. }
  126. }
  127. return -1;
  128. // <a href="http://supertech.csail.mit.edu/papers/debruijn.pdf" />
  129. }
  130. foreign Dialog_TryObtainID(id);
  131. global Dialog_TryObtainID(id)
  132. {
  133. if (Bit_Get(YSI_g_sIDs, id))
  134. {
  135. Bit_Vet(YSI_g_sIDs, id);
  136. YSI_g_sDialogPlayers[id] = 0;
  137. YSI_g_sDialogMasters[id] = -1;
  138. //YSI_g_sDialogInfo[data][E_CALLBACK_DATA_POINTER] = 0;
  139. return id;
  140. }
  141. P:C(else P:E("Dialog ID %d already in use", id););
  142. return -1;
  143. }
  144. foreign Dialog_Get(playerid);
  145. global Dialog_Get(playerid)
  146. {
  147. return YSI_g_sPlayerDialog[playerid];
  148. }
  149. foreign void:Dialog_Garbage(dialogid);
  150. global void:Dialog_Garbage(dialogid)
  151. {
  152. Bit_Let(YSI_g_sFree, dialogid);
  153. //return 1;
  154. }
  155. foreign void:Dialog_Free(dialogid);
  156. global void:Dialog_Free(dialogid)
  157. {
  158. Bit_Vet(YSI_g_sFree, dialogid);
  159. Bit_Let(YSI_g_sIDs, dialogid);
  160. if (YSI_g_sDialogPlayers[dialogid])
  161. {
  162. YSI_g_sDialogPlayers[dialogid] = 0;
  163. foreach (new i : Player)
  164. {
  165. if (YSI_g_sPlayerDialog[i] == dialogid)
  166. {
  167. ShowPlayerDialog(i, -1, 0, NULL, NULL, NULL, NULL);
  168. YSI_g_sPlayerDialog[i] = -1;
  169. }
  170. }
  171. }
  172. //return 1;
  173. }
  174. foreign Dialog_Set(playerid, dialogid);
  175. global Dialog_Set(playerid, dialogid)
  176. {
  177. new
  178. old = YSI_g_sPlayerDialog[playerid];
  179. if (dialogid != old)
  180. {
  181. if (dialogid != -1)
  182. {
  183. ++YSI_g_sDialogPlayers[dialogid];
  184. }
  185. YSI_g_sPlayerDialog[playerid] = dialogid;
  186. if (old != -1)
  187. {
  188. if (--YSI_g_sDialogPlayers[old] == 0)
  189. {
  190. if (Bit_Get(YSI_g_sFree, old))
  191. {
  192. YSI_g_sDialogMasters[old] = -1;
  193. Bit_Vet(YSI_g_sFree, old);
  194. Bit_Let(YSI_g_sIDs, old);
  195. // "old" destroyed.
  196. return 0;
  197. }
  198. }
  199. }
  200. }
  201. // "old" still exists.
  202. return 1;
  203. }
  204. stock Dialog_ShowCallback(playerid, callback:callback, style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  205. {
  206. new
  207. ret = Dialog_Show(playerid, style, title, caption, button1, button2, dialog),
  208. data[E_CALLBACK_DATA];
  209. if (Callback_Get(callback, data, _F<iiiis>))
  210. {
  211. Dialog_SetCallbackData(ret, data);
  212. }
  213. return ret;
  214. }
  215. stock Dialog_ShowCallbackData(playerid, callback[E_CALLBACK_DATA], style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  216. {
  217. new
  218. ret = Dialog_Show(playerid, style, title, caption, button1, button2, dialog);
  219. Dialog_SetCallbackData(ret, callback);
  220. return ret;
  221. }
  222. stock Dialog_Show(playerid, style, string:title[], string:caption[], string:button1[], string:button2[] = "", dialog = -1)
  223. {
  224. if (dialog == -1)
  225. {
  226. dialog = Dialog_ObtainID();
  227. Dialog_Garbage(dialog);
  228. }
  229. ShowPlayerDialog(playerid, dialog, style, title, caption, button1, button2);
  230. Dialog_Set(playerid, dialog);
  231. return dialog;
  232. }
  233. stock Dialog_Hide(playerid)
  234. {
  235. // This almost looks like a Windows API function call!
  236. ShowPlayerDialog(playerid, -1, 0, NULL, NULL, NULL, NULL);
  237. return Dialog_Set(playerid, -1);
  238. }
  239. hook OnPlayerDisconnect(playerid, reason)
  240. {
  241. #pragma unused reason
  242. Dialog_Hide(playerid);
  243. return 1;
  244. }
  245. remotefunc void:Dialog_SetMaster(dialogid, master)
  246. {
  247. //printf("SETTING %d MASTER: %d", dialogid, master);
  248. YSI_g_sDialogMasters[dialogid] = master;
  249. //YSI_g_sDialogInfo[dialogid][E_CALLBACK_DATA_POINTER] = 0;
  250. }
  251. stock Dialog_GetMaster(dialogid)
  252. {
  253. return YSI_g_sDialogMasters[dialogid];
  254. }
  255. stock Dialog_SetCallback(dialogid, callback:callback)
  256. {
  257. new
  258. data[E_CALLBACK_DATA];
  259. if (Callback_Get(callback, data, _F<iiiis>))
  260. {
  261. Dialog_SetCallbackData(dialogid, data);
  262. }
  263. P:C(else P:E("Could not find dialog callback %s", _:callback););
  264. }
  265. stock Dialog_SetCallbackData(dialogid, callback[E_CALLBACK_DATA])
  266. {
  267. broadcastfunc Dialog_SetMaster(dialogid, _@);
  268. YSI_g_sDialogInfo[dialogid] = callback;
  269. }
  270. // xhunterx editing all day
  271. hook OnPlayerConnect(playerid) {
  272. YSI_g_sPlayerDialog[playerid] = -1;
  273. return 1;
  274. }
  275. // Need to somehow integrate this new dialogs library with the text library.
  276. hook OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
  277. {
  278. P:1("Dialog_OnDialogResponse called: %d %d %d %d %s", playerid, dialogid, response, listitem, inputtext);
  279. // Apparently there's a hack to alter this.
  280. dialogid = Dialog_Get(playerid);
  281. P:5("Dialog_OnDialogResponse: dialog = %d", dialogid);
  282. if (dialogid == -1)
  283. {
  284. return 0;
  285. }
  286. P:5("Dialog_OnDialogResponse: master = %d, %d", YSI_g_sDialogMasters[dialogid], _@);
  287. if(YSI_g_sDialogMasters[dialogid] == -1) {
  288. if(Master_Caller() == _@) {
  289. SetTimerEx("DialogSetToNone", 0, false, "i", playerid);
  290. }
  291. return 0;
  292. }
  293. if (YSI_g_sDialogMasters[dialogid] != _@)
  294. {
  295. return 0;
  296. }
  297. // Dialogs close automaticaly.
  298. if (Dialog_Set(playerid, -1))
  299. {
  300. P:5("OnDialogResponse: Not free");
  301. if (YSI_g_sDialogInfo[dialogid][E_CALLBACK_DATA_POINTER])
  302. {
  303. //printf("Calling function at 0x%08x", YSI_g_sDialogInfo[dialogid][E_CALLBACK_DATA_POINTER]);
  304. Callback_Call(YSI_g_sDialogInfo[dialogid], playerid, dialogid, response, listitem, inputtext);
  305. return 1;
  306. }
  307. }
  308. else
  309. {
  310. // Or it will screw up when you show a new dialog from a dialog.
  311. P:5("OnDialogResponse: Already freed");
  312. new
  313. dat[E_CALLBACK_DATA];
  314. dat = YSI_g_sDialogInfo[dialogid];
  315. // Callback no longer needed.
  316. if (dat[E_CALLBACK_DATA_POINTER])
  317. {
  318. //printf("Calling function at 0x%08x", dat[E_CALLBACK_DATA_POINTER]);
  319. Callback_Call(dat, playerid, dialogid, response, listitem, inputtext);
  320. Callback_Release(dat);
  321. return 1;
  322. }
  323. }
  324. //Dialog_Set(playerid, -1);
  325. return 0;
  326. }
  327. forward DialogSetToNone(playerid);
  328. public DialogSetToNone(playerid) {
  329. Dialog_Set(playerid, -1);
  330. return 1;
  331. }
  332. /*stock _ShowPlayerDialog(playerid, dialog, style, string:title[], string:caption[], string:button1[], string:button2[])
  333. {
  334. Dialog_TryObtainID(dialog);
  335. YSI_g_sDialogInfo[dialog][E_CALLBACK_DATA_POINTER] = 0;
  336. ShowPlayerDialog(playerid, dialog, style, title, caption, button1, button2);
  337. return 0;
  338. }
  339. #if defined _ALS_ShowPlayerDialog
  340. #undef ShowPlayerDialog
  341. #else
  342. #define _ALS_ShowPlayerDialog
  343. #endif
  344. #define ShowPlayerDialog _ShowPlayerDialog
  345. #define HidePlayerDialog Dialog_Hide*/
  346. /*stock HidePlayerDialog(playerid)
  347. {
  348. ShowPlayerDialog(playerid, -1, 0, NULL, NULL, NULL, NULL);
  349. }*/
  350. #include "..\YSI_Core\y_master"