ProgressCtrl.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. * $Archive: /Commando/Code/wwui/ProgressCtrl.cpp $
  22. *
  23. * DESCRIPTION
  24. * Progress bar control
  25. *
  26. * PROGRAMMER
  27. * Denzil E. Long, Jr.
  28. * $Author: Denzil_l $
  29. *
  30. * VERSION INFO
  31. * $Revision: 4 $
  32. * $Modtime: 1/12/02 9:44p $
  33. *
  34. ******************************************************************************/
  35. #include "ProgressCtrl.h"
  36. #include "StyleMgr.h"
  37. #define BAR_INSET 3
  38. /******************************************************************************
  39. *
  40. * NAME
  41. * ProgressCtrlClass::ProgressCtrlClass
  42. *
  43. * DESCRIPTION
  44. * Constructor
  45. *
  46. * INPUTS
  47. * NONE
  48. *
  49. * RESULT
  50. * NONE
  51. *
  52. ******************************************************************************/
  53. ProgressCtrlClass::ProgressCtrlClass(void) :
  54. mBarRect(0, 0, 0, 0),
  55. mMinLimit(0),
  56. mMaxLimit(100),
  57. mPosition(0),
  58. mStep(10)
  59. {
  60. StyleMgrClass::Configure_Renderer(&mControlRenderer);
  61. }
  62. /******************************************************************************
  63. *
  64. * NAME
  65. * ProgressCtrlClass::~ProgressCtrlClass
  66. *
  67. * DESCRIPTION
  68. * Destructor
  69. *
  70. * INPUTS
  71. * NONE
  72. *
  73. * RESULT
  74. * NONE
  75. *
  76. ******************************************************************************/
  77. ProgressCtrlClass::~ProgressCtrlClass()
  78. {
  79. }
  80. /******************************************************************************
  81. *
  82. * NAME
  83. * ProgressCtrlClass::Create_Control_Renderers
  84. *
  85. * DESCRIPTION
  86. * Create the renderers for the control
  87. *
  88. * INPUTS
  89. * NONE
  90. *
  91. * RESULT
  92. * NONE
  93. *
  94. ******************************************************************************/
  95. void ProgressCtrlClass::Create_Control_Renderers(void)
  96. {
  97. Render2DClass& renderer = mControlRenderer;
  98. // Configure this renderer
  99. renderer.Reset();
  100. renderer.Enable_Texturing(false);
  101. // Determine which color to draw the outline in
  102. int color = StyleMgrClass::Get_Line_Color();
  103. int bkColor = StyleMgrClass::Get_Bk_Color();
  104. if (IsEnabled == false)
  105. {
  106. color = StyleMgrClass::Get_Disabled_Line_Color();
  107. bkColor = StyleMgrClass::Get_Disabled_Bk_Color();
  108. }
  109. // Draw the control outline
  110. RectClass rect = Rect;
  111. renderer.Add_Outline(rect, 1.0F, color);
  112. // Now draw the background
  113. rect.Right -= 1;
  114. rect.Bottom -= 1;
  115. renderer.Add_Quad(rect, bkColor);
  116. // Draw the bar
  117. if (mPosition > mMinLimit)
  118. {
  119. int barColor = StyleMgrClass::Get_Text_Color();
  120. if (IsEnabled == false)
  121. {
  122. barColor = StyleMgrClass::Get_Disabled_Text_Color();
  123. }
  124. renderer.Add_Quad(mBarRect, barColor);
  125. }
  126. }
  127. /******************************************************************************
  128. *
  129. * NAME
  130. * ProgressCtrlClass::Calculate_Bar_Width
  131. *
  132. * DESCRIPTION
  133. * Calculate the new bar width.
  134. *
  135. * INPUTS
  136. * Position - Position to calculate bar width for.
  137. *
  138. * RESULT
  139. * NONE
  140. *
  141. ******************************************************************************/
  142. float ProgressCtrlClass::Calculate_Bar_Width(unsigned int position)
  143. {
  144. // Adjust the bar to reflect the new position
  145. float maxBarWidth = (Rect.Width() - (float)(2 * BAR_INSET));
  146. float scaler = (maxBarWidth / (float)(mMaxLimit - mMinLimit));
  147. float barWidth = (scaler * (float)(position - mMinLimit));
  148. return barWidth;
  149. }
  150. /******************************************************************************
  151. *
  152. * NAME
  153. * ProgressCtrlClass::Update_Client_Rect
  154. *
  155. * DESCRIPTION
  156. *
  157. * INPUTS
  158. * NONE
  159. *
  160. * RESULT
  161. * NONE
  162. *
  163. ******************************************************************************/
  164. void ProgressCtrlClass::Update_Client_Rect(void)
  165. {
  166. // Calculate the new bar rectangle
  167. mBarRect.Left = Rect.Left + BAR_INSET;
  168. mBarRect.Top = Rect.Top + BAR_INSET;
  169. mBarRect.Bottom = Rect.Bottom - BAR_INSET;
  170. mBarRect.Right = (mBarRect.Left + Calculate_Bar_Width(mPosition));
  171. Set_Dirty();
  172. }
  173. /******************************************************************************
  174. *
  175. * NAME
  176. * ProgressCtrlClass::Render
  177. *
  178. * DESCRIPTION
  179. * Render the control
  180. *
  181. * INPUTS
  182. * NONE
  183. *
  184. * RESULT
  185. * NONE
  186. *
  187. ******************************************************************************/
  188. void ProgressCtrlClass::Render(void)
  189. {
  190. // Recreate the renderers (if necessary)
  191. if (IsDirty)
  192. {
  193. Create_Control_Renderers();
  194. }
  195. // Render the progress bar's current state
  196. mControlRenderer.Render();
  197. DialogControlClass::Render();
  198. }
  199. /******************************************************************************
  200. *
  201. * NAME
  202. * ProgressCtrlClass::Set_Range
  203. *
  204. * DESCRIPTION
  205. * Sets the minimum and maximum values for the progress bar and redraws the
  206. * bar to reflect the new range.
  207. *
  208. * INPUTS
  209. * Min - Minimum range value
  210. * Max - Maximum range value
  211. *
  212. * RESULT
  213. * NONE
  214. *
  215. ******************************************************************************/
  216. void ProgressCtrlClass::Set_Range(unsigned int min, unsigned int max)
  217. {
  218. WWASSERT(min < max);
  219. // Scale current position to new range
  220. if (mPosition > mMinLimit)
  221. {
  222. float oldDelta = (mMaxLimit - mMinLimit);
  223. float newDelta = (max - min);
  224. float scaler = (oldDelta / newDelta);
  225. unsigned int position = (unsigned int)((float)mPosition * scaler);
  226. Set_Position(position);
  227. }
  228. mMinLimit = min;
  229. mMaxLimit = max;
  230. Set_Dirty();
  231. }
  232. /******************************************************************************
  233. *
  234. * NAME
  235. * ProgressCtrlClass::Get_Range
  236. *
  237. * DESCRIPTION
  238. * Get the minimun and maximum values for the progress bar.
  239. *
  240. * INPUTS
  241. * Min - On return; Minimum range value
  242. * Max - On return; Maximum range value
  243. *
  244. * RESULT
  245. * NONE
  246. *
  247. ******************************************************************************/
  248. void ProgressCtrlClass::Get_Range(unsigned int& min, unsigned int& max)
  249. {
  250. min = mMinLimit;
  251. max = mMaxLimit;
  252. }
  253. /******************************************************************************
  254. *
  255. * NAME
  256. * ProgressCtrlClass::Set_Position
  257. *
  258. * DESCRIPTION
  259. * Set the current position for the progress bar.
  260. *
  261. * INPUTS
  262. * Position - New position
  263. *
  264. * RESULT
  265. * NONE
  266. *
  267. ******************************************************************************/
  268. void ProgressCtrlClass::Set_Position(unsigned int position)
  269. {
  270. unsigned int oldPosition = mPosition;
  271. mPosition = min<unsigned int>(mMaxLimit, position);
  272. mPosition = max<unsigned int>(mMinLimit, mPosition);
  273. if (oldPosition != mPosition)
  274. {
  275. mBarRect.Right = (mBarRect.Left + Calculate_Bar_Width(mPosition));
  276. Set_Dirty();
  277. }
  278. }
  279. /******************************************************************************
  280. *
  281. * NAME
  282. * ProgressCtrlClass::Delta_Position
  283. *
  284. * DESCRIPTION
  285. * Advance the position of the progress bar by a specified increment.
  286. *
  287. * INPUTS
  288. * Delta - Amount to advance the position by.
  289. *
  290. * RESULT
  291. * NONE
  292. *
  293. ******************************************************************************/
  294. void ProgressCtrlClass::Delta_Position(int delta)
  295. {
  296. Set_Position(mPosition + delta);
  297. }
  298. /******************************************************************************
  299. *
  300. * NAME
  301. * ProgressCtrlClass::Get_Position
  302. *
  303. * DESCRIPTION
  304. * Get the current position of the progress bar.
  305. *
  306. * INPUTS
  307. * NONE
  308. *
  309. * RESULT
  310. * Position - Progress position
  311. *
  312. ******************************************************************************/
  313. unsigned int ProgressCtrlClass::Get_Position(void) const
  314. {
  315. return mPosition;
  316. }
  317. /******************************************************************************
  318. *
  319. * NAME
  320. * ProgressCtrlClass::Set_Step
  321. *
  322. * DESCRIPTION
  323. * Specify the step increment for the progress bar. This is the amount
  324. * the progress bar increases its current position whenever Step_Position()
  325. * is called.
  326. *
  327. * INPUTS
  328. * Step - New step increment.
  329. *
  330. * RESULT
  331. * NONE
  332. *
  333. ******************************************************************************/
  334. void ProgressCtrlClass::Set_Step(unsigned int step)
  335. {
  336. mStep = min<unsigned int>((mMaxLimit - mMinLimit), step);
  337. mStep = max<unsigned int>(1, mStep);
  338. }
  339. /******************************************************************************
  340. *
  341. * NAME
  342. * ProgressCtrlClass::Step_Position
  343. *
  344. * DESCRIPTION
  345. * Advance the position for the progress bar by the step increment.
  346. *
  347. * INPUTS
  348. * NONE
  349. *
  350. * RESULT
  351. * NONE
  352. *
  353. ******************************************************************************/
  354. void ProgressCtrlClass::Step_Position(void)
  355. {
  356. float stepping = ((float)(mMaxLimit - mMinLimit) / (float)mStep);
  357. float stepPos = (floor((float)mPosition / stepping) * stepping);
  358. Set_Position(stepPos + mStep);
  359. }