1
0

progress.inc 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #if defined _progress_included
  2. #endinput
  3. #endif
  4. #if !defined _samp_included
  5. #tryinclude <a_samp>
  6. #if !defined _samp_included
  7. #error could not locate a_samp.inc file, please check your server includes
  8. #endif
  9. #endif
  10. #tryinclude <foreach>
  11. #define _progress_included
  12. #define _progress_version 0x1310
  13. #define MAX_BARS (MAX_TEXT_DRAWS / 3)
  14. #define INVALID_BAR_VALUE (Float:0xFFFFFFFF)
  15. #define INVALID_BAR_ID (Bar:-1)
  16. #define pb_percent(%1,%2,%3,%4) ((%1 - 6.0) + ((((%1 + 6.0 + %2 - 2.0) - %1) / %3) * %4))
  17. forward Bar:CreateProgressBar(Float:x, Float:y, Float:width=55.5, Float:height=3.2, color, Float:max=100.0);
  18. forward Float:GetProgressBarValue(Bar:barid);
  19. enum e_bar
  20. {
  21. Float:pb_x,
  22. Float:pb_y,
  23. Float:pb_w,
  24. Float:pb_h,
  25. Float:pb_m,
  26. Float:pb_v,
  27. Text:pb_t1,
  28. Text:pb_t2,
  29. Text:pb_t3,
  30. pb_color,
  31. bool:pb_created
  32. }
  33. static Bars[MAX_BARS][e_bar];
  34. stock Bar:CreateProgressBar(Float:x, Float:y, Float:width=55.5, Float:height=3.2, color, Float:max=100.0)
  35. {
  36. new
  37. barid;
  38. for(barid = 0; barid < sizeof Bars; ++barid)
  39. if(!Bars[barid][pb_created]) break;
  40. if(Bars[barid][pb_created] || barid == sizeof Bars)
  41. return INVALID_BAR_ID;
  42. new Text:in_t = Bars[barid][pb_t1] = TextDrawCreate(x, y, "_");
  43. TextDrawUseBox (in_t, 1);
  44. TextDrawTextSize (in_t, x + width, 0.0);
  45. TextDrawLetterSize (in_t, 1.0, height / 10);
  46. TextDrawBoxColor (in_t, 0x00000000 | (color & 0x000000FF));
  47. in_t = Bars[barid][pb_t2] = TextDrawCreate(x + 1.2, y + 2.15, "_");
  48. TextDrawUseBox (in_t, 1);
  49. TextDrawTextSize (in_t, x + width - 2.0, 0.0);
  50. TextDrawLetterSize (in_t, 1.0, height / 10 - 0.35);
  51. TextDrawBoxColor (in_t, (color & 0xFFFFFF00) | (0x66 & ((color & 0x000000FF) / 2)));
  52. in_t = Bars[barid][pb_t3] = TextDrawCreate(x + 1.2, y + 2.15, "_");
  53. TextDrawTextSize (in_t, pb_percent(x, width, max, 1.0), 0.0);
  54. TextDrawLetterSize (in_t, 1.0, height / 10 - 0.35);
  55. TextDrawBoxColor (in_t, color);
  56. Bars[barid][pb_x] = x;
  57. Bars[barid][pb_y] = y;
  58. Bars[barid][pb_w] = width;
  59. Bars[barid][pb_h] = height;
  60. Bars[barid][pb_m] = max;
  61. Bars[barid][pb_color] = color;
  62. Bars[barid][pb_created] = true;
  63. return Bar:barid;
  64. }
  65. stock DestroyProgressBar(Bar:barid)
  66. {
  67. if(barid != INVALID_BAR_ID && Bar:-1 < barid < Bar:MAX_BARS)
  68. {
  69. if(!Bars[_:barid][pb_created])
  70. return 0;
  71. TextDrawDestroy(Bars[_:barid][pb_t1]);
  72. TextDrawDestroy(Bars[_:barid][pb_t2]);
  73. TextDrawDestroy(Bars[_:barid][pb_t3]);
  74. Bars[_:barid][pb_t1] = Text:0;
  75. Bars[_:barid][pb_t2] = Text:0;
  76. Bars[_:barid][pb_t3] = Text:0;
  77. Bars[_:barid][pb_x] = 0.0;
  78. Bars[_:barid][pb_y] = 0.0;
  79. Bars[_:barid][pb_w] = 0.0;
  80. Bars[_:barid][pb_h] = 0.0;
  81. Bars[_:barid][pb_m] = 0.0;
  82. Bars[_:barid][pb_v] = 0.0;
  83. Bars[_:barid][pb_color] = 0;
  84. Bars[_:barid][pb_created] = false;
  85. return 1;
  86. }
  87. return 0;
  88. }
  89. stock ShowProgressBarForPlayer(playerid, Bar:barid)
  90. {
  91. if(IsPlayerConnected(playerid) && barid != INVALID_BAR_ID && Bar:-1 < barid < Bar:MAX_BARS)
  92. {
  93. if(!Bars[_:barid][pb_created])
  94. return 0;
  95. TextDrawShowForPlayer(playerid, Bars[_:barid][pb_t1]);
  96. TextDrawShowForPlayer(playerid, Bars[_:barid][pb_t2]);
  97. TextDrawShowForPlayer(playerid, Bars[_:barid][pb_t3]);
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. stock HideProgressBarForPlayer(playerid, Bar:barid)
  103. {
  104. if(IsPlayerConnected(playerid) && barid != INVALID_BAR_ID && Bar:-1 < barid < Bar:MAX_BARS)
  105. {
  106. if(!Bars[_:barid][pb_created])
  107. return 0;
  108. TextDrawHideForPlayer(playerid, Bars[_:barid][pb_t1]);
  109. TextDrawHideForPlayer(playerid, Bars[_:barid][pb_t2]);
  110. TextDrawHideForPlayer(playerid, Bars[_:barid][pb_t3]);
  111. return 1;
  112. }
  113. return 0;
  114. }
  115. stock SetProgressBarValue(Bar:barid, Float:value)
  116. {
  117. if(barid == INVALID_BAR_ID || Bar:MAX_BARS < barid < Bar:-1)
  118. return 0;
  119. if(Bars[_:barid][pb_created])
  120. {
  121. value =
  122. (value < 0.0) ? (0.0) : (value > Bars[_:barid][pb_m]) ? (Bars[_:barid][pb_m]) : (value);
  123. TextDrawUseBox(Bars[_:barid][pb_t3], value > 0.0);
  124. Bars[_:barid][pb_v] = value;
  125. TextDrawTextSize(Bars[_:barid][pb_t3],
  126. pb_percent(Bars[_:barid][pb_x], Bars[_:barid][pb_w], Bars[_:barid][pb_m], value), 0.0);
  127. return 1;
  128. }
  129. return 0;
  130. }
  131. stock Float:GetProgressBarValue(Bar:barid)
  132. {
  133. if(barid == INVALID_BAR_ID || Bar:MAX_BARS < barid < Bar:-1)
  134. return INVALID_BAR_VALUE;
  135. if(Bars[_:barid][pb_created])
  136. return Bars[_:barid][pb_v];
  137. return INVALID_BAR_VALUE;
  138. }
  139. stock SetProgressBarMaxValue(Bar:barid, Float:max)
  140. {
  141. if(barid == INVALID_BAR_ID || Bar:MAX_BARS < barid < Bar:-1)
  142. return 0;
  143. if(Bars[_:barid][pb_created])
  144. {
  145. Bars[_:barid][pb_m] = max;
  146. SetProgressBarValue(barid, Bars[_:barid][pb_v]);
  147. return 1;
  148. }
  149. return 0;
  150. }
  151. stock SetProgressBarColor(Bar:barid, color)
  152. {
  153. if(barid == INVALID_BAR_ID || Bar:MAX_BARS < barid < Bar:-1)
  154. return 0;
  155. if(Bars[_:barid][pb_created])
  156. {
  157. Bars[_:barid][pb_color] = color;
  158. TextDrawBoxColor(Bars[_:barid][pb_t1], 0x00000000 | (color & 0x000000FF));
  159. TextDrawBoxColor(Bars[_:barid][pb_t2],
  160. (color & 0xFFFFFF00) | (0x66 & ((color & 0x000000FF) / 2)));
  161. TextDrawBoxColor(Bars[_:barid][pb_t3], color);
  162. return 1;
  163. }
  164. return 0;
  165. }
  166. stock ShowProgressBarForAll(Bar:barid)
  167. {
  168. #if defined _foreach_included
  169. foreach(Player, i)
  170. #else
  171. for(new i = 0; i < MAX_PLAYERS; ++i)
  172. if(IsPlayerConnected(i))
  173. #endif
  174. #if defined IsPlayerNPC
  175. if(!IsPlayerNPC(i))
  176. #endif
  177. {
  178. ShowProgressBarForPlayer(i, barid);
  179. }
  180. return 1;
  181. }
  182. stock HideProgressBarForAll(Bar:barid)
  183. {
  184. #if defined _foreach_included
  185. foreach(Player, i)
  186. #else
  187. for(new i = 0; i < MAX_PLAYERS; ++i)
  188. if(IsPlayerConnected(i))
  189. #endif
  190. #if defined IsPlayerNPC
  191. if(!IsPlayerNPC(i))
  192. #endif
  193. {
  194. HideProgressBarForPlayer(i, barid);
  195. }
  196. return 1;
  197. }
  198. stock UpdateProgressBar(Bar:barid, playerid=INVALID_PLAYER_ID)
  199. {
  200. if(playerid == INVALID_PLAYER_ID)
  201. {
  202. return ShowProgressBarForAll(barid);
  203. } else {
  204. return ShowProgressBarForPlayer(playerid, barid);
  205. }
  206. }