1
0

DialogCenter.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. /*
  2. ---------------------------------------
  3. Dialog Center Text
  4. @author: Ino
  5. @version: 1.0.2
  6. @release: 27/12/2016
  7. @build: 3
  8. @download: github.com/Ino42O/Dialog-Center-Text
  9. ---------------------------------------
  10. Changelog:
  11. 27-12-2016:
  12. * Initial release
  13. 28-12-2016:
  14. * Fixed space char
  15. * Added new characters
  16. 10-01-2017:
  17. * Added support for caption (title) [beta]
  18. Thanks to:
  19. [uL]Kanada42O - Consultations
  20. ---------------------------------------
  21. */
  22. #include <a_samp>
  23. #define _INC_CENTER_DIALOG // For other includes
  24. static const stock letters[ ] [ ] =
  25. {
  26. // Uppercase
  27. { 'A' , 12},
  28. { 'B' , 11},
  29. { 'C' , 11},
  30. { 'D' , 11},
  31. { 'E' , 10},
  32. { 'F' , 9},
  33. { 'G' , 12},
  34. { 'H' , 11},
  35. { 'I' , 4},
  36. { 'J' , 9},
  37. { 'K' , 12},
  38. { 'L' , 9},
  39. { 'M' , 13},
  40. { 'N' , 11},
  41. { 'O' , 12},
  42. { 'P' , 10},
  43. { 'Q' , 12},
  44. { 'R' , 12},
  45. { 'S' , 10},
  46. { 'T' , 10},
  47. { 'U' , 11},
  48. { 'V' , 11},
  49. { 'W' , 15},
  50. { 'X' , 12},
  51. { 'Y' , 12},
  52. { 'Z' , 10},
  53. // Lowercase
  54. { 'a' , 9},
  55. { 'b' , 9},
  56. { 'c' , 8},
  57. { 'd' , 9},
  58. { 'e' , 9},
  59. { 'f' , 8},
  60. { 'g' , 9},
  61. { 'h' , 9},
  62. { 'i' , 4},
  63. { 'j' , 5},
  64. { 'k' , 9},
  65. { 'l' , 4},
  66. { 'm' , 12},
  67. { 'n' , 9},
  68. { 'o' , 9},
  69. { 'p' , 9},
  70. { 'q' , 9},
  71. { 'r' , 7},
  72. { 's' , 8},
  73. { 't' , 7},
  74. { 'u' , 9},
  75. { 'v' , 11},
  76. { 'w' , 13},
  77. { 'x' , 11},
  78. { 'y' , 9},
  79. { 'z' , 8},
  80. // Numbers
  81. { '1' , 7},
  82. { '2' , 9},
  83. { '3' , 9},
  84. { '4' , 9},
  85. { '5' , 9},
  86. { '6' , 9},
  87. { '7' , 9},
  88. { '8' , 9},
  89. { '9' , 9},
  90. { '0' , 9},
  91. // Special characters
  92. { '!' , 4},
  93. { '"' , 8},
  94. { '$' , 9},
  95. { '%' , 14},
  96. { '&' , 12},
  97. { '/' , 6},
  98. { '(' , 6},
  99. { ')' , 6},
  100. { '=' , 10},
  101. { '?' , 10},
  102. { '+' , 10},
  103. { '\'', 4},
  104. { '[' , 6},
  105. { ']' , 6},
  106. { '-' , 6},
  107. { '.' , 4},
  108. { ',' , 4},
  109. { '|' , 4},
  110. { '_' , 10},
  111. { '#' , 9},
  112. { '^' , 10},
  113. { '~' , 10},
  114. { ' ' , 3}
  115. };
  116. #define MAX_LINES (100)
  117. static
  118. centerBuffer[0xF06],
  119. centerTitle [0xFA],
  120. bool:use = false
  121. ;
  122. stock dcenter_ShowPlayerDialog(playerid, dialogid, style, caption[], info[], button1[], button2[])
  123. {
  124. new defaultSize;
  125. switch (style)
  126. {
  127. case DIALOG_STYLE_MSGBOX: { defaultSize = 0xC8; }
  128. case DIALOG_STYLE_INPUT: { defaultSize = 0xC8; }
  129. case DIALOG_STYLE_LIST: { return ShowPlayerDialog(playerid, dialogid, style, caption, info, button1, button2); } // Still in progress
  130. case DIALOG_STYLE_PASSWORD: { defaultSize = 0xC8; }
  131. case DIALOG_STYLE_TABLIST: { return ShowPlayerDialog(playerid, dialogid, style, caption, info, button1, button2); } // Still in progress
  132. case DIALOG_STYLE_TABLIST_HEADERS: { return ShowPlayerDialog(playerid, dialogid, style, caption, info, button1, button2); } // Still in progress
  133. default: { return ShowPlayerDialog(playerid, dialogid, style, caption, info, button1, button2); }
  134. }
  135. if (use) return dcenter_ShowPlayerDialog(playerid, dialogid, style, caption, info, button1, button2);
  136. use = true;
  137. centerBuffer[0] = '\0';
  138. centerTitle [0] = '\0';
  139. new
  140. lineInfo [MAX_LINES],
  141. lineSize [MAX_LINES],
  142. lineSpace [MAX_LINES],
  143. bool:lineCenter [MAX_LINES],
  144. bool:titleCenter,
  145. centered,
  146. biggest,
  147. biggestID,
  148. line = 0,
  149. // Find centers
  150. cpos = -2,
  151. tpos = -2,
  152. // Count lines
  153. pos = -2,
  154. lines = 0
  155. ;
  156. while (-1 != ( pos = strfind(info, "\n", false, pos + 2) ) ) lines++;
  157. if (lines != 0)
  158. lines++;
  159. if (lines > 0)
  160. {
  161. // Find positions for new line
  162. for (new i = 0, c = 0; i < strlen(info); i++)
  163. {
  164. if (info[i] == '\n')
  165. lineInfo[c++] = i;
  166. if (i == strlen(info) - 1) lineInfo[c] = strlen(info);
  167. }
  168. for (new i = 0; i < lines; i++)
  169. {
  170. if (i == 0) lineSize[i] = lineInfo[i];
  171. else if (0 < i < lines) lineSize[i] = (lineInfo[i] - lineInfo[i - 1] - 1);
  172. }
  173. }
  174. new tmp[3];
  175. // Center title
  176. strmid(tmp, caption, 0, 3);
  177. if (strfind(tmp, "\\c", false) != -1)
  178. titleCenter = true;
  179. if (lines > 0)
  180. {
  181. for (new i = 0; i < lines; i++)
  182. {
  183. if (i == 0)
  184. {
  185. strmid(tmp, info, 0, 3);
  186. if (strfind(tmp, "\\c", false) != -1)
  187. {
  188. lineCenter[i] = true;
  189. centered++;
  190. }
  191. }
  192. else
  193. {
  194. strmid(tmp, info, (lineInfo[i - 1] + 1), (lineInfo[i - 1] + 4));
  195. if (strfind(tmp, "\\c", false) != -1)
  196. {
  197. lineCenter[i] = true;
  198. centered++;
  199. }
  200. }
  201. }
  202. }
  203. else
  204. {
  205. strmid(tmp, info, 0, 3);
  206. if (strfind(tmp, "\\c", false) != -1)
  207. {
  208. lineCenter[0] = true;
  209. centered++;
  210. }
  211. }
  212. format (centerBuffer, sizeof(centerBuffer), "%s", info);
  213. format (centerTitle, sizeof(centerTitle), "%s", caption);
  214. // Find biggest line
  215. if (lines > 0)
  216. {
  217. for (new i = 0; i < lines; i++)
  218. {
  219. new t1 = ( (lines > 0) ? ( (i == 0) ? (0) : (lineInfo[i - 1] + 1) ) : (0) );
  220. new t2 = ( (lines > 0) ? (lineInfo[i]) : (strlen(info)) );
  221. for (new u = t1; u < t2; u++)
  222. {
  223. if (centerBuffer[u] == '{')
  224. {
  225. u += 7;
  226. continue;
  227. }
  228. for (new j = 0; j < sizeof(letters); j++)
  229. {
  230. if (centerBuffer[u] == letters[j][0])
  231. {
  232. lineSpace[i] += letters[j][1];
  233. break;
  234. }
  235. }
  236. }
  237. }
  238. biggest = 0;
  239. for (new i = 0; i < lines; i++)
  240. {
  241. if (lineSpace[i] > biggest)
  242. {
  243. biggest = lineSpace[i];
  244. biggestID = i;
  245. }
  246. }
  247. }
  248. // Title
  249. if (!titleCenter) goto skip_title;
  250. new titleSpace;
  251. for (new i = 0; i < strlen(caption); i++)
  252. {
  253. if (centerTitle[i] == '{')
  254. {
  255. i += 7;
  256. continue;
  257. }
  258. for (new j = 0; j < sizeof(letters); j++)
  259. {
  260. if (centerTitle[i] == letters[j][0])
  261. {
  262. if (centerTitle[i] == ' ') titleSpace += letters[j][1] + 3;
  263. else titleSpace += letters[j][1] - 2;
  264. break;
  265. }
  266. }
  267. }
  268. if (titleSpace < defaultSize)
  269. {
  270. new loop;
  271. if (biggest < defaultSize) loop = ( ( ( (defaultSize - titleSpace) / 4) / 2) + 4) - ( (centered == 0) ? (1) : (0));
  272. else
  273. {
  274. if (0xC8 < biggest < 0x15E) loop = ( ( ( (biggest - titleSpace) / 4) - 2) / 2) + 3 + ( (centered == 0) ? (1) : (0));
  275. else if (0x15E < biggest < 0x17C) loop = ( ( ( (biggest - titleSpace) / 4) - 2) / 2) + 4 + ( (centered == 0) ? (2) : (0));
  276. else if (0x17C < biggest < 0x258) loop = ( ( ( (biggest - titleSpace) / 4) - 4) / 2) + 3 + ( (centered == 0) ? (1) : (0));
  277. else if (0x258 < biggest < 0x384) loop = ( ( ( (biggest - titleSpace) / 4) - 5) / 2) + 3 + ( (centered == 0) ? (2) : (0));
  278. else loop = ( ( ( (biggest - titleSpace) / 4) - 7) / 2) + 1 + ( (centered == 0) ? (1) : (0));
  279. }
  280. for (new i = 0; i < loop; i++)
  281. {
  282. strins(centerTitle, " ", i);
  283. }
  284. }
  285. skip_title:
  286. generate_line:
  287. if (!lineCenter[line])
  288. goto generate_fail;
  289. new space = 0;
  290. for (new i = ( (lines > 0) ? ( (line == 0) ? (0) : (lineInfo[line - 1] + 1) ) : (0) ); i < ( (lines > 0) ? (lineInfo[line]) : (strlen(info)) ); i++)
  291. {
  292. if (centerBuffer[i] == '{')
  293. {
  294. i += 7;
  295. continue;
  296. }
  297. for (new j = 0; j < sizeof(letters); j++)
  298. {
  299. if (centerBuffer[i] == letters[j][0])
  300. {
  301. space += letters[j][1];
  302. break;
  303. }
  304. }
  305. }
  306. if (lines == 0)
  307. {
  308. if (space < defaultSize)
  309. {
  310. new loop = ( ( (defaultSize - space) / 4) / 2) + 1;
  311. new start = ( (lines > 0) ? ( (line == 0) ? (0) : (lineInfo[line - 1] + 1) ) : (0) );
  312. for (new i = start; i < loop + start; i++)
  313. {
  314. strins(centerBuffer, " ", i);
  315. space += 3;
  316. for (new j = 0; j < lines; j++)
  317. lineInfo[j]++;
  318. }
  319. }
  320. else
  321. {
  322. strins(centerBuffer, " ", ( (lines > 0) ? ( (line == 0) ? (0) : (lineInfo[line - 1] + 1) ) : (0) ));
  323. for (new j = 0; j < lines; j++)
  324. lineInfo[j] += 2;
  325. }
  326. }
  327. else if (lines > 0)
  328. {
  329. new loop = ( ( ( ( (biggest < defaultSize) ? (defaultSize) : (biggest) ) - space) / 4) / 2) - ( ( (biggest < defaultSize) ? (-1) : ( (space < defaultSize) ? ( (lineCenter[biggestID]) ? (4) : (2) ) : (1) ) ) );
  330. new start = ( (lines > 0) ? ( (line == 0) ? (0) : (lineInfo[line - 1] + 1) ) : (0) );
  331. for (new i = start; i < loop + start; i++)
  332. {
  333. strins(centerBuffer, " ", i);
  334. space += 3;
  335. for (new j = 0; j < lines; j++)
  336. lineInfo[j]++;
  337. }
  338. }
  339. generate_fail:
  340. if (lines > 0)
  341. {
  342. if (line++ < lines - 1)
  343. goto generate_line;
  344. }
  345. while (-1 != ( cpos = strfind(centerBuffer, "\\c", false, cpos + 2) ) )
  346. strdel(centerBuffer, cpos, cpos + 2);
  347. while (-1 != ( tpos = strfind(centerTitle, "\\c", false, tpos + 2) ) )
  348. strdel(centerTitle, tpos, tpos + 2);
  349. use = false;
  350. return ShowPlayerDialog(playerid, dialogid, style, centerTitle, centerBuffer, button1, button2);
  351. }
  352. #if defined _ALS_ShowPlayerDialog
  353. #undef ShowPlayerDialog
  354. #else
  355. #define _ALS_ShowPlayerDialog
  356. #endif
  357. #define ShowPlayerDialog dcenter_ShowPlayerDialog