progress.inc 6.7 KB

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