1
0

messagebox.inc 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. //
  2. // #LB_TDBox
  3. // Text Draw Message Box
  4. // ---------------------
  5. // Sends a message box to a playerid. If the input text is to long the function
  6. // automaticly stops, creates a new line and continues writing. Also supports
  7. // ~n~ to not exceed the background box.
  8. //
  9. // If you wish to send the text without the background box just use an invisible
  10. // color for the box (e.g. 0xFFAAFF00). If you want to change the text color use
  11. // as following ~y~, ~r~ ...
  12. //
  13. // 'padding' defines the space between text and box while 'BaseX' and 'BaseY'
  14. // set the actual text and box position. Note that the function checks whether
  15. // the text would exceed the screen and aligns the position of the box!
  16. //
  17. // If 'hide_in' is set to 0 or anything below the text box will not disappear
  18. // after time. You then have to use 'TD_HideMessage()' or the box will be hidden
  19. // when the function gets used a second time for a player!
  20. //
  21. // !!! Max amount of Lines that can be used is 10 !!!
  22. //
  23. // Version 1.0
  24. //
  25. // ~ LeBoyce 2013
  26. //
  27. #if defined _LBYC_TDBox_included
  28. #endinput
  29. #endif
  30. #define _LBYC_TDBox_included
  31. /*
  32. native TD_SendMessage(playerid, const string_[], header[], hide_in = 5000, bgcolor = 12840, Float:padding = 24.0000, Float:BaseX = 319.000000, Float:BaseY = 231.000000, length = 90);
  33. native TD_HideMessage(playerid);
  34. */
  35. #define LIMIT_LEFT 2.0000
  36. #define LIMIT_RIGHT 634.0000
  37. #define LIMIT_BOTTOM 436.0000
  38. #define LIMIT_TOP 2.0000
  39. // Callbacks
  40. forward TD_HideAll(playerid, hide_in);
  41. // settings player lists
  42. new
  43. Text:G_TEXTDRAW[MAX_PLAYERS][12],
  44. G_TEXTDRAW_MESSAGE[MAX_PLAYERS][12][128],
  45. P_TDRAWS[MAX_PLAYERS],
  46. bool:P_HEADER[MAX_PLAYERS],
  47. E_TD_TIMER[MAX_PLAYERS],
  48. P_TEXTDRAW_SIZE[MAX_PLAYERS],
  49. P_TEXTDRAW_SIZE_LONGEST[MAX_PLAYERS],
  50. E_LAST[MAX_PLAYERS],
  51. Text:Td_Header[MAX_PLAYERS]
  52. ;
  53. //------------------------------------------------
  54. TD_CreateBgBox(playerid, bgcolor, Float:startX, Float:BaseY, Float:padding, Float:length)
  55. {
  56. G_TEXTDRAW[playerid][0] = TextDrawCreate((startX - padding), BaseY + (P_HEADER[playerid] ? 1.0 : 0.0), "_");TextDrawLetterSize(G_TEXTDRAW[playerid][0], 0.169999, 0.899999+float(P_TDRAWS[playerid]));
  57. TextDrawBoxColor(G_TEXTDRAW[playerid][0], bgcolor);TextDrawTextSize(G_TEXTDRAW[playerid][0], (startX + length + padding + 3.0000), 24.000000);
  58. TextDrawBackgroundColor(G_TEXTDRAW[playerid][0], 255);TextDrawFont(G_TEXTDRAW[playerid][0], 1);TextDrawColor(G_TEXTDRAW[playerid][0], -1);
  59. TextDrawSetOutline(G_TEXTDRAW[playerid][0], 0);TextDrawSetProportional(G_TEXTDRAW[playerid][0], 1);TextDrawSetShadow(G_TEXTDRAW[playerid][0], 1);
  60. TextDrawUseBox(G_TEXTDRAW[playerid][0], 1);TextDrawShowForPlayer(playerid, G_TEXTDRAW[playerid][0]);
  61. }
  62. //------------------------------------------------
  63. TD_CreateTextMessages(playerid, Float:startX, Float:BaseY)
  64. {
  65. for(new i = 1; i != P_TDRAWS[playerid]; i++)
  66. {
  67. G_TEXTDRAW[playerid][i] = TextDrawCreate(startX, BaseY+(9.0*float(i)), G_TEXTDRAW_MESSAGE[playerid][i]);
  68. TextDrawBackgroundColor(G_TEXTDRAW[playerid][i], 255);TextDrawFont(G_TEXTDRAW[playerid][i], 2);TextDrawLetterSize(G_TEXTDRAW[playerid][i], 0.170000, 1.000000);
  69. TextDrawColor(G_TEXTDRAW[playerid][i], -1);TextDrawSetOutline(G_TEXTDRAW[playerid][i], 0);TextDrawSetProportional(G_TEXTDRAW[playerid][i], 1);
  70. TextDrawSetShadow(G_TEXTDRAW[playerid][i], 0);TextDrawShowForPlayer(playerid, G_TEXTDRAW[playerid][i]);format(G_TEXTDRAW_MESSAGE[playerid][i], 128, "0");
  71. }
  72. }
  73. //------------------------------------------------
  74. TD_CreateHeader(playerid, header[], Float:startX, Float:BaseY, Float:padding)
  75. {
  76. if(header[0] == '\0' || header[0] == ' ') header[0] = '_', P_HEADER[playerid] = true;
  77. else P_HEADER[playerid] = false;
  78. Td_Header[playerid] = TextDrawCreate(startX - 1.0 - padding, BaseY - 7.0, header);TextDrawBackgroundColor(Td_Header[playerid], 255);TextDrawFont(Td_Header[playerid], 2);
  79. TextDrawLetterSize(Td_Header[playerid], 0.260000, 1.000000);TextDrawColor(Td_Header[playerid], -1);TextDrawSetOutline(Td_Header[playerid], 0);
  80. TextDrawSetProportional(Td_Header[playerid], 1);TextDrawSetShadow(Td_Header[playerid], 1);TextDrawShowForPlayer(playerid, Td_Header[playerid]);
  81. }
  82. //------------------------------------------------
  83. stock TD_HideMessage(playerid)
  84. {
  85. if(P_TDRAWS[playerid] != 0) { KillTimer(E_TD_TIMER[playerid]);TD_HideAll(playerid, 0); }
  86. return 1;
  87. }
  88. //------------------------------------------------
  89. stock require_LetterLength(player_id, string[])
  90. {
  91. new
  92. var[1]
  93. ;
  94. var[0] = tolower(string[0]);
  95. switch(var[0])
  96. {
  97. case 'a', 'c', 'f', 'h', 'l', 'm', 'o', 'p', 'r', 's', 'u', 'v', 'x', 'z', /**/'b', 'd', 'e', 'g', 'j', 'k', 'n', 'q', 't', 'y'/**/, 0..9, '!', '.', ',', ':', ';':
  98. {
  99. if(E_LAST[player_id] == 4)
  100. {
  101. E_LAST[player_id] = 5;
  102. return 5;
  103. }
  104. else
  105. {
  106. E_LAST[player_id] = 4;
  107. return 4;
  108. }
  109. }
  110. case 'w': return 6;
  111. }
  112. return 2;
  113. }
  114. //------------------------------------------------
  115. stock TD_SendMessage(playerid, const string_[], header[], hide_in = 5000, bgcolor = 12840, Float:padding = 24.0000, Float:BaseX = 319.000000, Float:BaseY = 231.000000, length = 90)
  116. {
  117. P_TEXTDRAW_SIZE_LONGEST[playerid] = 0;
  118. if(P_TDRAWS[playerid] != 0) { KillTimer(E_TD_TIMER[playerid]); TD_HideAll(playerid, 0); }
  119. if(length > 90) length = 90;
  120. new
  121. count,
  122. pos,
  123. td = 1,
  124. string[1024]
  125. ;
  126. do { string[pos] = string_[pos]; pos++; } while(string_[pos] != '\0');
  127. pos = 0;
  128. do
  129. {
  130. new
  131. bMessage[128]
  132. ;
  133. do
  134. {
  135. while(!count && !!pos && string[++pos] == ' ') continue;
  136. if(string[pos] == '~' && string[pos+1] == 'n' && string[pos+2] == '~') { string[pos] = '_'; string[pos+1] = '_'; pos += 2; string[pos] = ' '; break; }
  137. bMessage[count] = string[pos];
  138. count++;
  139. pos++;
  140. }
  141. while(count < length && string[pos] != '\0' || count > length - 1 && string[pos] != ' ' && string[pos] != '\0');
  142. new
  143. stringPos = 0
  144. ;
  145. P_TEXTDRAW_SIZE[playerid] = 0;
  146. while(bMessage[stringPos++] != '\0') { P_TEXTDRAW_SIZE[playerid] += require_LetterLength(playerid, bMessage[stringPos]); }
  147. if(P_TEXTDRAW_SIZE[playerid] > P_TEXTDRAW_SIZE_LONGEST[playerid]) { P_TEXTDRAW_SIZE_LONGEST[playerid] = P_TEXTDRAW_SIZE[playerid]; }
  148. format(G_TEXTDRAW_MESSAGE[playerid][td], 128, "%s", bMessage);
  149. count = 0;
  150. td++;
  151. }
  152. while(string[pos] != '\0');
  153. P_TDRAWS[playerid] = (td);
  154. new
  155. Float:Length = float(P_TEXTDRAW_SIZE_LONGEST[playerid]),
  156. Float:X = BaseX - Length / 2,
  157. Float:Y = BaseY,
  158. Rows = P_TDRAWS[playerid]
  159. ;
  160. if(X - padding < LIMIT_LEFT) {
  161. X += (LIMIT_LEFT - (X - padding));
  162. }
  163. else if(X + Length + padding > LIMIT_RIGHT) {
  164. X += (LIMIT_RIGHT - (X + Length + padding));
  165. }
  166. if(Y < LIMIT_TOP) {
  167. Y = LIMIT_TOP;
  168. }
  169. else if (Y + float(Rows * 9) > LIMIT_BOTTOM) {
  170. Y = LIMIT_BOTTOM - (float(Rows * 9));
  171. }
  172. TD_CreateHeader(playerid, header, X, Y, padding);
  173. TD_CreateBgBox(playerid, bgcolor, X, Y, padding, Length);
  174. TD_CreateTextMessages(playerid, X, Y);
  175. if(hide_in != 0) SetTimerEx("TD_HideAll", 100, false, "ii", playerid, hide_in);
  176. return 1;
  177. }
  178. //------------------------------------------------
  179. public TD_HideAll(playerid, hide_in)
  180. {
  181. if(hide_in != 0) { E_TD_TIMER[playerid] = SetTimerEx("TD_HideAll", hide_in, false, "ii", playerid, 0); }
  182. else {
  183. for(new i = 0; i != P_TDRAWS[playerid]; i++) { TextDrawHideForPlayer(playerid, G_TEXTDRAW[playerid][i]);TextDrawDestroy(G_TEXTDRAW[playerid][i]); }
  184. TextDrawHideForPlayer(playerid, Td_Header[playerid]);TextDrawDestroy(Td_Header[playerid]);P_TDRAWS[playerid] = 0;
  185. }
  186. return 1;
  187. }