sliderctrl.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : Combat *
  23. * *
  24. * $Archive:: /Commando/Code/wwui/sliderctrl.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 1/09/02 2:36p $*
  29. * *
  30. * $Revision:: 11 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. // Disable warning about exception handling not being enabled. It's used as part of STL - in a part of STL we don't use.
  36. #pragma warning(disable : 4530)
  37. #include "sliderctrl.h"
  38. #include "assetmgr.h"
  39. #include "refcount.h"
  40. #include "mousemgr.h"
  41. #include "ww3d.h"
  42. #include "dialogmgr.h"
  43. #include "dialogbase.h"
  44. #include "stylemgr.h"
  45. ////////////////////////////////////////////////////////////////
  46. // Local constants
  47. ////////////////////////////////////////////////////////////////
  48. const float BAR_HEIGHT = 8.0F;
  49. ////////////////////////////////////////////////////////////////
  50. //
  51. // SliderCtrlClass
  52. //
  53. ////////////////////////////////////////////////////////////////
  54. SliderCtrlClass::SliderCtrlClass (void) :
  55. MinPos (0),
  56. MaxPos (100),
  57. CurrPos (0),
  58. IsDragging (false)
  59. {
  60. return ;
  61. }
  62. ////////////////////////////////////////////////////////////////
  63. //
  64. // ~SliderCtrlClass
  65. //
  66. ////////////////////////////////////////////////////////////////
  67. SliderCtrlClass::~SliderCtrlClass (void)
  68. {
  69. return ;
  70. }
  71. ////////////////////////////////////////////////////////////////
  72. //
  73. // Create_Control_Renderer
  74. //
  75. ////////////////////////////////////////////////////////////////
  76. void
  77. SliderCtrlClass::Create_Control_Renderer (void)
  78. {
  79. Render2DClass &renderer = ControlRenderer;
  80. //
  81. // Configure this renderer
  82. //
  83. renderer.Reset ();
  84. renderer.Enable_Texturing (false);
  85. renderer.Set_Coordinate_Range (Render2DClass::Get_Screen_Resolution());
  86. //
  87. // Determine which color to draw the outline in
  88. //
  89. int color = StyleMgrClass::Get_Line_Color ();
  90. int bkcolor = StyleMgrClass::Get_Bk_Color ();
  91. if (IsEnabled == false) {
  92. color = StyleMgrClass::Get_Disabled_Line_Color ();
  93. bkcolor = StyleMgrClass::Get_Disabled_Bk_Color ();
  94. }
  95. //
  96. // Calculate the thumb's position
  97. //
  98. float percent = float(CurrPos - MinPos) / float(MaxPos - MinPos);
  99. int thumb_height = ClientRect.Height () - 2;
  100. int thumb_width = thumb_height / 2;
  101. //
  102. // Calculate the rectangle for the thumb
  103. //
  104. RectClass rect;
  105. rect.Left = int (ClientRect.Left + (ClientRect.Width () * percent) - (thumb_width * 0.5F));
  106. rect.Right = rect.Left + thumb_width;
  107. rect.Top = int(ClientRect.Top + (ClientRect.Height () / 2) - (thumb_height / 2));
  108. rect.Bottom = int(ClientRect.Top + (ClientRect.Height () / 2) + (thumb_height / 2));
  109. //
  110. // Draw the thumb
  111. //
  112. renderer.Add_Rect (rect, 1.0F, color, HasFocus ? color : bkcolor);
  113. //
  114. // Determine what height to use for the bar
  115. //
  116. float bar_height = BAR_HEIGHT * StyleMgrClass::Get_Y_Scale ();
  117. //
  118. // Calculate the rectangle for the control
  119. //
  120. RectClass rect1;
  121. RectClass rect2;
  122. rect1.Left = ClientRect.Left;
  123. rect1.Right = rect.Left;
  124. rect2.Left = rect.Right;
  125. rect2.Right = ClientRect.Right;
  126. rect1.Top = int(ClientRect.Top + (ClientRect.Height () / 2) - (bar_height / 2));
  127. rect1.Bottom = int(ClientRect.Top + (ClientRect.Height () / 2) + (bar_height / 2));
  128. rect2.Top = rect1.Top;
  129. rect2.Bottom = rect1.Bottom;
  130. //
  131. // Draw the bar in two sections
  132. //
  133. if (rect1.Right > rect1.Left) {
  134. renderer.Add_Rect (rect1, 1.0F, color, bkcolor);
  135. }
  136. if (rect2.Right > rect2.Left) {
  137. renderer.Add_Rect (rect2, 1.0F, color, bkcolor);
  138. }
  139. return ;
  140. }
  141. ////////////////////////////////////////////////////////////////
  142. //
  143. // On_Set_Cursor
  144. //
  145. ////////////////////////////////////////////////////////////////
  146. void
  147. SliderCtrlClass::On_Set_Cursor (const Vector2 &mouse_pos)
  148. {
  149. //
  150. // Change the mouse cursor
  151. //
  152. MouseMgrClass::Set_Cursor (MouseMgrClass::CURSOR_ACTION);
  153. return ;
  154. }
  155. ////////////////////////////////////////////////////////////////
  156. //
  157. // Update_Client_Rect
  158. //
  159. ////////////////////////////////////////////////////////////////
  160. void
  161. SliderCtrlClass::Update_Client_Rect (void)
  162. {
  163. //
  164. // Set the client area
  165. //
  166. ClientRect = Rect;
  167. Set_Dirty ();
  168. return ;
  169. }
  170. ////////////////////////////////////////////////////////////////
  171. //
  172. // Render
  173. //
  174. ////////////////////////////////////////////////////////////////
  175. void
  176. SliderCtrlClass::Render (void)
  177. {
  178. //
  179. // Recreate the renderers (if necessary)
  180. //
  181. if (IsDirty) {
  182. Create_Control_Renderer ();
  183. }
  184. //
  185. // Render the background and text for the current state
  186. //
  187. ControlRenderer.Render ();
  188. DialogControlClass::Render ();
  189. return ;
  190. }
  191. ////////////////////////////////////////////////////////////////
  192. //
  193. // On_LButton_Down
  194. //
  195. ////////////////////////////////////////////////////////////////
  196. void
  197. SliderCtrlClass::On_LButton_Down (const Vector2 &mouse_pos)
  198. {
  199. Set_Capture ();
  200. IsDragging = true;
  201. //
  202. // Snap the slider to the mouse position
  203. //
  204. Set_Pos (Slider_Pos_From_Mouse_Pos (mouse_pos));
  205. return ;
  206. }
  207. ////////////////////////////////////////////////////////////////
  208. //
  209. // On_LButton_Up
  210. //
  211. ////////////////////////////////////////////////////////////////
  212. void
  213. SliderCtrlClass::On_LButton_Up (const Vector2 &mouse_pos)
  214. {
  215. Release_Capture ();
  216. IsDragging = false;
  217. return ;
  218. }
  219. ////////////////////////////////////////////////////////////////
  220. //
  221. // On_Mouse_Move
  222. //
  223. ////////////////////////////////////////////////////////////////
  224. void
  225. SliderCtrlClass::On_Mouse_Move (const Vector2 &mouse_pos)
  226. {
  227. if (IsDragging) {
  228. Set_Pos (Slider_Pos_From_Mouse_Pos (mouse_pos));
  229. }
  230. return ;
  231. }
  232. ////////////////////////////////////////////////////////////////
  233. //
  234. // On_Set_Focus
  235. //
  236. ////////////////////////////////////////////////////////////////
  237. void
  238. SliderCtrlClass::On_Set_Focus (void)
  239. {
  240. Set_Dirty ();
  241. DialogControlClass::On_Set_Focus ();
  242. return ;
  243. }
  244. ////////////////////////////////////////////////////////////////
  245. //
  246. // On_Kill_Focus
  247. //
  248. ////////////////////////////////////////////////////////////////
  249. void
  250. SliderCtrlClass::On_Kill_Focus (DialogControlClass *focus)
  251. {
  252. IsDragging = false;
  253. Set_Dirty ();
  254. DialogControlClass::On_Kill_Focus (focus);
  255. return ;
  256. }
  257. ////////////////////////////////////////////////////////////////
  258. //
  259. // On_Key_Down
  260. //
  261. ////////////////////////////////////////////////////////////////
  262. bool
  263. SliderCtrlClass::On_Key_Down (uint32 key_id, uint32 key_data)
  264. {
  265. bool handled = true;
  266. switch (key_id)
  267. {
  268. case VK_DOWN:
  269. case VK_LEFT:
  270. Set_Pos (CurrPos - 1);
  271. break;
  272. case VK_UP:
  273. case VK_RIGHT:
  274. Set_Pos (CurrPos + 1);
  275. break;
  276. case VK_HOME:
  277. Set_Pos (MinPos);
  278. break;
  279. case VK_END:
  280. Set_Pos (MaxPos);
  281. break;
  282. default:
  283. handled = false;
  284. break;
  285. }
  286. return handled;
  287. }
  288. ////////////////////////////////////////////////////////////////
  289. //
  290. // On_Create
  291. //
  292. ////////////////////////////////////////////////////////////////
  293. void
  294. SliderCtrlClass::On_Create (void)
  295. {
  296. return ;
  297. }
  298. ////////////////////////////////////////////////////////////////
  299. //
  300. // Slider_Pos_From_Mouse_Pos
  301. //
  302. ////////////////////////////////////////////////////////////////
  303. int
  304. SliderCtrlClass::Slider_Pos_From_Mouse_Pos (const Vector2 &mouse_pos)
  305. {
  306. int retval = 0;
  307. if (mouse_pos.X < ClientRect.Left) {
  308. //
  309. // Mouse is to the left of the control
  310. //
  311. retval = MinPos;
  312. } else if (mouse_pos.X >= ClientRect.Right) {
  313. //
  314. // Mouse is to the right of the control
  315. //
  316. retval = MaxPos;
  317. } else {
  318. int thumb_height = ClientRect.Height () - 2;
  319. int thumb_width = thumb_height / 2;
  320. //
  321. // Calculate where in the range the mouse is and return its position
  322. //
  323. float percent = (mouse_pos.X - (ClientRect.Left - (thumb_width * 0.5F))) / ClientRect.Width ();
  324. retval = MinPos + int(percent * (MaxPos - MinPos)) ;
  325. }
  326. return retval;
  327. }
  328. ////////////////////////////////////////////////////////////////
  329. //
  330. // Set_Range
  331. //
  332. ////////////////////////////////////////////////////////////////
  333. void
  334. SliderCtrlClass::Set_Range (int range_min, int range_max)
  335. {
  336. MinPos = range_min;
  337. MaxPos = range_max;
  338. Set_Pos (MinPos);
  339. return ;
  340. }
  341. ////////////////////////////////////////////////////////////////
  342. //
  343. // Set_Pos
  344. //
  345. ////////////////////////////////////////////////////////////////
  346. void
  347. SliderCtrlClass::Set_Pos (int pos, bool send_notification)
  348. {
  349. //
  350. // Bound the new position
  351. //
  352. if (pos < MinPos) {
  353. pos = MinPos;
  354. } else if (pos > MaxPos) {
  355. pos = MaxPos;
  356. }
  357. //
  358. // Update the current position
  359. //
  360. if (pos != CurrPos) {
  361. CurrPos = pos;
  362. Set_Dirty ();
  363. //
  364. // Notify the advise sinks if necessary
  365. //
  366. if (send_notification) {
  367. ADVISE_NOTIFY (On_SliderCtrl_Pos_Changed (this, Get_ID (), CurrPos));
  368. }
  369. }
  370. return ;
  371. }