checkboxctrl.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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/checkboxctrl.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 12/05/01 4:22p $*
  29. * *
  30. * $Revision:: 18 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "checkboxctrl.h"
  36. #include "assetmgr.h"
  37. #include "refcount.h"
  38. #include "font3d.h"
  39. #include "mousemgr.h"
  40. #include "ww3d.h"
  41. #include "dialogmgr.h"
  42. #include "dialogbase.h"
  43. #include "stylemgr.h"
  44. ////////////////////////////////////////////////////////////////
  45. //
  46. // CheckBoxCtrlClass
  47. //
  48. ////////////////////////////////////////////////////////////////
  49. CheckBoxCtrlClass::CheckBoxCtrlClass (void) :
  50. TextRect (0, 0, 0, 0),
  51. ButtonRect (0, 0, 0, 0),
  52. MaxRect (0, 0, 0, 0),
  53. IsChecked (false)
  54. {
  55. //
  56. // Set the font for the text renderers
  57. //
  58. StyleMgrClass::Assign_Font (&TextRenderer, StyleMgrClass::FONT_CONTROLS);
  59. StyleMgrClass::Configure_Renderer (&ControlRenderer);
  60. return ;
  61. }
  62. ////////////////////////////////////////////////////////////////
  63. //
  64. // ~CheckBoxCtrlClass
  65. //
  66. ////////////////////////////////////////////////////////////////
  67. CheckBoxCtrlClass::~CheckBoxCtrlClass (void)
  68. {
  69. return ;
  70. }
  71. ////////////////////////////////////////////////////////////////
  72. //
  73. // Create_Text_Renderers
  74. //
  75. ////////////////////////////////////////////////////////////////
  76. void
  77. CheckBoxCtrlClass::Create_Text_Renderers (void)
  78. {
  79. const float TEXT_SPACING = 8.0F;
  80. //
  81. // Configure the renderers
  82. //
  83. HilightRenderer.Reset ();
  84. HilightRenderer.Set_Coordinate_Range (Render2DClass::Get_Screen_Resolution());
  85. StyleMgrClass::Configure_Hilighter (&HilightRenderer);
  86. //
  87. // Determine how wide to make the text area
  88. //
  89. Vector2 extents = TextRenderer.Get_Text_Extents (Title);
  90. float width = extents.X + (StyleMgrClass::Get_X_Scale () * TEXT_SPACING);
  91. //
  92. // Resize the text area to fit the text
  93. //
  94. TextRect.Right = TextRect.Left + width;
  95. TextRect.Right = min (TextRect.Right, MaxRect.Right);
  96. Rect.Right = TextRect.Right;
  97. //
  98. // Start fresh
  99. //
  100. TextRenderer.Reset ();
  101. //
  102. // Draw the text
  103. //
  104. StyleMgrClass::Render_Text (Title, &TextRenderer, TextRect, true, true,
  105. StyleMgrClass::LEFT_JUSTIFY, IsEnabled);
  106. //
  107. // Do the hilight
  108. //
  109. if (HasFocus) {
  110. StyleMgrClass::Render_Hilight (&HilightRenderer, TextRect);
  111. }
  112. return ;
  113. }
  114. ////////////////////////////////////////////////////////////////
  115. //
  116. // Create_Control_Renderers
  117. //
  118. ////////////////////////////////////////////////////////////////
  119. void
  120. CheckBoxCtrlClass::Create_Control_Renderers (void)
  121. {
  122. Render2DClass &renderer = ControlRenderer;
  123. //
  124. // Configure this renderer
  125. //
  126. renderer.Reset ();
  127. renderer.Enable_Texturing (false);
  128. //
  129. // Determine which color to draw the outline in
  130. //
  131. int color = StyleMgrClass::Get_Line_Color ();
  132. int bkcolor = StyleMgrClass::Get_Bk_Color ();
  133. if (IsEnabled == false) {
  134. color = StyleMgrClass::Get_Disabled_Line_Color ();
  135. bkcolor = StyleMgrClass::Get_Disabled_Bk_Color ();
  136. }
  137. //
  138. // Draw the button outline
  139. //
  140. RectClass rect = ButtonRect;
  141. renderer.Add_Outline (ButtonRect, 1.0F, color);
  142. //
  143. // Now draw the background
  144. //
  145. rect.Right -= 1;
  146. rect.Bottom -= 1;
  147. renderer.Add_Quad (rect, bkcolor);
  148. //
  149. // Draw the check (if necessary)
  150. //
  151. if (IsChecked) {
  152. int text_color = StyleMgrClass::Get_Text_Color ();
  153. int shadow_color = StyleMgrClass::Get_Text_Shadow_Color ();
  154. if (IsEnabled == false) {
  155. text_color = StyleMgrClass::Get_Disabled_Text_Color ();
  156. shadow_color = StyleMgrClass::Get_Disabled_Text_Shadow_Color ();
  157. }
  158. rect = ButtonRect;
  159. rect.Inflate (Vector2 (-5, -5));
  160. renderer.Add_Line (Vector2 (rect.Left-1, rect.Top+1), Vector2 (rect.Right-2, rect.Bottom+1), 2, shadow_color);
  161. renderer.Add_Line (Vector2 (rect.Left-1, rect.Bottom+1), Vector2 (rect.Right-2, rect.Top+1), 2, shadow_color);
  162. renderer.Add_Line (Vector2 (rect.Left, rect.Top), Vector2 (rect.Right-1, rect.Bottom), 2, text_color);
  163. renderer.Add_Line (Vector2 (rect.Left, rect.Bottom), Vector2 (rect.Right-1, rect.Top), 2, text_color);
  164. }
  165. return ;
  166. }
  167. ////////////////////////////////////////////////////////////////
  168. //
  169. // On_Set_Cursor
  170. //
  171. ////////////////////////////////////////////////////////////////
  172. void
  173. CheckBoxCtrlClass::On_Set_Cursor (const Vector2 &mouse_pos)
  174. {
  175. //
  176. // Change the mouse cursor
  177. //
  178. if (Rect.Contains (mouse_pos) && mouse_pos.X <= TextRect.Right) {
  179. MouseMgrClass::Set_Cursor (MouseMgrClass::CURSOR_ACTION);
  180. } else {
  181. MouseMgrClass::Set_Cursor (MouseMgrClass::CURSOR_ARROW);
  182. }
  183. return ;
  184. }
  185. ////////////////////////////////////////////////////////////////
  186. //
  187. // Update_Client_Rect
  188. //
  189. ////////////////////////////////////////////////////////////////
  190. void
  191. CheckBoxCtrlClass::Update_Client_Rect (void)
  192. {
  193. //
  194. // Determine what one character spacing would be
  195. //
  196. Vector2 char_size = TextRenderer.Get_Text_Extents (L"W");
  197. //
  198. // Set the client area
  199. //
  200. MaxRect = Rect;
  201. ClientRect = Rect;
  202. //
  203. // Determine what size the button should be
  204. //
  205. float button_width = int(char_size.X * 1.5F);
  206. float button_height = button_width;
  207. //
  208. // Calculate the button rectangle
  209. //
  210. ButtonRect.Left = ClientRect.Left;
  211. ButtonRect.Top = ClientRect.Top + int((ClientRect.Height () / 2.0F) - (button_height / 2.0F));
  212. ButtonRect.Right = ButtonRect.Left + button_width;
  213. ButtonRect.Bottom = ClientRect.Top + int((ClientRect.Height () / 2.0F) + (button_height / 2.0F));
  214. //
  215. // Calculate the text rectangle
  216. //
  217. TextRect.Left = int(ButtonRect.Right + (char_size.X * 0.5F));
  218. TextRect.Top = ClientRect.Top;
  219. TextRect.Right = ClientRect.Right;
  220. TextRect.Bottom = ClientRect.Bottom;
  221. Set_Dirty ();
  222. return ;
  223. }
  224. ////////////////////////////////////////////////////////////////
  225. //
  226. // Render
  227. //
  228. ////////////////////////////////////////////////////////////////
  229. void
  230. CheckBoxCtrlClass::Render (void)
  231. {
  232. //
  233. // Recreate the renderers (if necessary)
  234. //
  235. if (IsDirty) {
  236. Create_Control_Renderers ();
  237. Create_Text_Renderers ();
  238. }
  239. //
  240. // Render the background and text for the current state
  241. //
  242. ControlRenderer.Render ();
  243. TextRenderer.Render ();
  244. HilightRenderer.Render ();
  245. DialogControlClass::Render ();
  246. return ;
  247. }
  248. ////////////////////////////////////////////////////////////////
  249. //
  250. // Set_Check
  251. //
  252. ////////////////////////////////////////////////////////////////
  253. void CheckBoxCtrlClass::Set_Check (bool onoff)
  254. {
  255. Internal_Set_Check(onoff, false);
  256. }
  257. void CheckBoxCtrlClass::Internal_Set_Check(bool onoff, bool notify)
  258. {
  259. IsChecked = onoff;
  260. Set_Dirty ();
  261. if (notify) {
  262. Parent->On_Command (Get_ID (), 0, onoff);
  263. }
  264. }
  265. ////////////////////////////////////////////////////////////////
  266. //
  267. // On_LButton_Down
  268. //
  269. ////////////////////////////////////////////////////////////////
  270. void
  271. CheckBoxCtrlClass::On_LButton_Down (const Vector2 &mouse_pos)
  272. {
  273. Internal_Set_Check (!IsChecked, true);
  274. return ;
  275. }
  276. ////////////////////////////////////////////////////////////////
  277. //
  278. // On_LButton_Up
  279. //
  280. ////////////////////////////////////////////////////////////////
  281. void
  282. CheckBoxCtrlClass::On_LButton_Up (const Vector2 &mouse_pos)
  283. {
  284. return ;
  285. }
  286. ////////////////////////////////////////////////////////////////
  287. //
  288. // On_Set_Focus
  289. //
  290. ////////////////////////////////////////////////////////////////
  291. void
  292. CheckBoxCtrlClass::On_Set_Focus (void)
  293. {
  294. Set_Dirty ();
  295. DialogControlClass::On_Set_Focus ();
  296. return ;
  297. }
  298. ////////////////////////////////////////////////////////////////
  299. //
  300. // On_Kill_Focus
  301. //
  302. ////////////////////////////////////////////////////////////////
  303. void
  304. CheckBoxCtrlClass::On_Kill_Focus (DialogControlClass *focus)
  305. {
  306. Set_Dirty ();
  307. DialogControlClass::On_Kill_Focus (focus);
  308. return ;
  309. }
  310. ////////////////////////////////////////////////////////////////
  311. //
  312. // On_Key_Down
  313. //
  314. ////////////////////////////////////////////////////////////////
  315. bool
  316. CheckBoxCtrlClass::On_Key_Down (uint32 key_id, uint32 key_data)
  317. {
  318. bool handled = false;
  319. bool is_dirty = true;
  320. switch (key_id)
  321. {
  322. case VK_SPACE:
  323. Internal_Set_Check (!IsChecked, true);
  324. handled = true;
  325. break;
  326. default:
  327. is_dirty = false;
  328. break;
  329. }
  330. if (is_dirty) {
  331. Set_Dirty ();
  332. }
  333. return handled;
  334. }
  335. ////////////////////////////////////////////////////////////////
  336. //
  337. // On_Create
  338. //
  339. ////////////////////////////////////////////////////////////////
  340. void
  341. CheckBoxCtrlClass::On_Create (void)
  342. {
  343. return ;
  344. }