merchandisectrl.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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 : WWUI *
  23. * *
  24. * $Archive:: /Commando/Code/wwui/merchandisectrl.cpp $*
  25. * *
  26. * Author:: Patrick Smith *
  27. * *
  28. * $Modtime:: 1/10/02 2:55p $*
  29. * *
  30. * $Revision:: 8 $*
  31. * *
  32. *---------------------------------------------------------------------------------------------*
  33. * Functions: *
  34. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  35. #include "merchandisectrl.h"
  36. #include "stylemgr.h"
  37. #include "mousemgr.h"
  38. #include "assetmgr.h"
  39. #include "texture.h"
  40. #include "dialogbase.h"
  41. #include "vector4.h"
  42. //////////////////////////////////////////////////////////////////////
  43. // Local constants
  44. //////////////////////////////////////////////////////////////////////
  45. static const float BUTTON_WIDTH = 16.0F;
  46. static const float BUTTON_HEIGHT = 16.0F;
  47. //////////////////////////////////////////////////////////////////////
  48. //
  49. // MerchandiseCtrlClass
  50. //
  51. //////////////////////////////////////////////////////////////////////
  52. MerchandiseCtrlClass::MerchandiseCtrlClass (void) :
  53. UserData (0),
  54. Cost (0),
  55. CurrentTextureIndex (0),
  56. Count (0)
  57. {
  58. //
  59. // Set the font for the text renderer
  60. //
  61. StyleMgrClass::Assign_Font (&NameTextRenderer, StyleMgrClass::FONT_TOOLTIPS);
  62. StyleMgrClass::Assign_Font (&CostTextRenderer, StyleMgrClass::FONT_HEADER);
  63. StyleMgrClass::Assign_Font (&CountTextRenderer, StyleMgrClass::FONT_TOOLTIPS);
  64. StyleMgrClass::Configure_Renderer (&ControlRenderer);
  65. StyleMgrClass::Configure_Renderer (&TextureRenderer);
  66. StyleMgrClass::Configure_Renderer (&ButtonRenderer);
  67. StyleMgrClass::Configure_Renderer (&HilightRenderer);
  68. StyleMgrClass::Configure_Hilighter (&HilightRenderer);
  69. TextureRenderer.Enable_Texturing (true);
  70. ButtonRenderer.Enable_Texturing (true);
  71. //
  72. // Pass the texture onto the renderer
  73. //
  74. TextureClass *texture = WW3DAssetManager::Get_Instance ()->Get_Texture ("hud_cnc_Button.tga", TextureClass::MIP_LEVELS_1);
  75. ButtonRenderer.Set_Texture (texture);
  76. REF_PTR_RELEASE (texture);
  77. return ;
  78. }
  79. //////////////////////////////////////////////////////////////////////
  80. //
  81. // ~MerchandiseCtrlClass
  82. //
  83. //////////////////////////////////////////////////////////////////////
  84. MerchandiseCtrlClass::~MerchandiseCtrlClass (void)
  85. {
  86. return ;
  87. }
  88. ////////////////////////////////////////////////////////////////
  89. //
  90. // Create_Text_Renderer
  91. //
  92. ////////////////////////////////////////////////////////////////
  93. void
  94. MerchandiseCtrlClass::Create_Text_Renderer (void)
  95. {
  96. //
  97. // Convert the cost to a string
  98. //
  99. WideStringClass cost_text;
  100. cost_text.Format (L"%d", Cost);
  101. //
  102. // Render the text
  103. //
  104. NameTextRenderer.Reset ();
  105. CostTextRenderer.Reset ();
  106. CountTextRenderer.Reset ();
  107. StyleMgrClass::Render_Text (Title, &NameTextRenderer, RGB_TO_INT32 (255, 255, 255), RGB_TO_INT32 (0, 0, 0), TextRect, true, true, StyleMgrClass::LEFT_JUSTIFY, true);
  108. StyleMgrClass::Render_Text (cost_text, &CostTextRenderer, RGB_TO_INT32 (255, 255, 255), RGB_TO_INT32 (0, 0, 0), CostRect, true, true, StyleMgrClass::LEFT_JUSTIFY, true);
  109. //
  110. // Render the counter (if necessary)
  111. //
  112. if (Count > 1) {
  113. //
  114. // Convert the count to a string
  115. //
  116. WideStringClass count_text;
  117. count_text.Format (L"%d", Count);
  118. StyleMgrClass::Render_Text (count_text, &CountTextRenderer, RGB_TO_INT32 (255, 255, 255), RGB_TO_INT32 (0, 0, 0), CountRect, true, true, StyleMgrClass::CENTER_JUSTIFY, true);
  119. }
  120. return ;
  121. }
  122. //////////////////////////////////////////////////////////////////////
  123. //
  124. // Render
  125. //
  126. //////////////////////////////////////////////////////////////////////
  127. void
  128. MerchandiseCtrlClass::Render (void)
  129. {
  130. //
  131. // Update the text renderer (if necessary)
  132. //
  133. if (IsDirty) {
  134. Create_Text_Renderer ();
  135. Create_Control_Renderer ();
  136. Create_Texture_Renderer ();
  137. }
  138. //
  139. // Render the parts of the control
  140. //
  141. TextureRenderer.Render ();
  142. ControlRenderer.Render ();
  143. NameTextRenderer.Render ();
  144. CostTextRenderer.Render ();
  145. CountTextRenderer.Render ();
  146. ButtonRenderer.Render ();
  147. HilightRenderer.Render ();
  148. DialogControlClass::Render ();
  149. return ;
  150. }
  151. ////////////////////////////////////////////////////////////////
  152. //
  153. // Update_Client_Rect
  154. //
  155. ////////////////////////////////////////////////////////////////
  156. void
  157. MerchandiseCtrlClass::Update_Client_Rect (void)
  158. {
  159. //
  160. // Set the client area
  161. //
  162. ClientRect = Rect;
  163. ClientRect.Inflate (Vector2 (-1, -1));
  164. //
  165. // Make the texture rectangle the 2/3 of the space
  166. //
  167. TextureRect = Rect;
  168. TextureRect.Bottom = int(TextureRect.Top + (Rect.Height () * 0.667F));
  169. //
  170. // Make the cost rect use up 60% of the remaining space
  171. //
  172. CostRect = Rect;
  173. CostRect.Top = int(TextureRect.Bottom + 1);
  174. CostRect.Bottom = CostRect.Top + int((Rect.Bottom - TextureRect.Bottom) * 0.6F);
  175. //
  176. // The rest of the space goes to the text rect
  177. //
  178. TextRect = Rect;
  179. TextRect.Top = CostRect.Bottom;
  180. //
  181. // The counter lives in the upper-right corner
  182. //
  183. CountRect = Rect;
  184. CountRect.Left = CountRect.Left + int(Rect.Width () * 0.75F);
  185. CountRect.Bottom = CountRect.Top + int(TextureRect.Height () * 0.25F);
  186. Set_Dirty ();
  187. return ;
  188. }
  189. ////////////////////////////////////////////////////////////////
  190. //
  191. // On_Set_Cursor
  192. //
  193. ////////////////////////////////////////////////////////////////
  194. void
  195. MerchandiseCtrlClass::On_Set_Cursor (const Vector2 &mouse_pos)
  196. {
  197. //
  198. // Change the mouse cursor
  199. //
  200. MouseMgrClass::Set_Cursor (MouseMgrClass::CURSOR_ACTION);
  201. return ;
  202. }
  203. ////////////////////////////////////////////////////////////////
  204. //
  205. // Create_Control_Renderer
  206. //
  207. ////////////////////////////////////////////////////////////////
  208. void
  209. MerchandiseCtrlClass::Create_Control_Renderer (void)
  210. {
  211. Render2DClass &renderer = ControlRenderer;
  212. //
  213. // Configure this renderer
  214. //
  215. renderer.Reset ();
  216. HilightRenderer.Reset ();
  217. ButtonRenderer.Reset ();
  218. renderer.Enable_Texturing (false);
  219. //
  220. // Determine which color to draw the outline in
  221. //
  222. int color = StyleMgrClass::Get_Line_Color ();
  223. int bkcolor = StyleMgrClass::Get_Bk_Color ();
  224. if (IsEnabled == false) {
  225. color = StyleMgrClass::Get_Disabled_Line_Color ();
  226. bkcolor = StyleMgrClass::Get_Disabled_Bk_Color ();
  227. }
  228. //
  229. // Draw the outlines
  230. //
  231. if (Count > 0) {
  232. StyleMgrClass::Render_Hilight (&HilightRenderer, Rect);
  233. }
  234. //
  235. // Draw the cycle button in the upper right corner...
  236. //
  237. if (TextureNameList.Count () > 1) {
  238. //
  239. // Draw the button
  240. //
  241. RectClass button_rect;
  242. button_rect.Right = Rect.Right - 1;
  243. button_rect.Top = Rect.Top + 2;
  244. button_rect.Left = int(button_rect.Right - (BUTTON_WIDTH * StyleMgrClass::Get_X_Scale ()));
  245. button_rect.Bottom = int(button_rect.Top + (BUTTON_HEIGHT * StyleMgrClass::Get_Y_Scale ()));
  246. ButtonRenderer.Add_Quad (button_rect);
  247. }
  248. //
  249. // Convert the cost to a string
  250. //
  251. WideStringClass cost_text;
  252. cost_text.Format (L"%d", Cost);
  253. //
  254. // Get the dimensions of the text
  255. //
  256. Vector2 name_extents = NameTextRenderer.Get_Text_Extents (Title);
  257. Vector2 cost_extents = CostTextRenderer.Get_Text_Extents (cost_text);
  258. //
  259. // Build rectangles for the backdrops of the cost and text
  260. //
  261. RectClass text_rect = TextRect;
  262. RectClass cost_rect = CostRect;
  263. text_rect.Right = int(text_rect.Left + name_extents.X + 4.0F);
  264. cost_rect.Right = int(cost_rect.Left + cost_extents.X + 4.0F);
  265. //
  266. // Clip the backdrop to the bounding rectangle
  267. //
  268. text_rect.Right = min (text_rect.Right, Rect.Right);
  269. cost_rect.Right = min (cost_rect.Right, Rect.Right);
  270. //
  271. // Render the text backdrops
  272. //
  273. Vector4 vector_color;
  274. INT32_TO_VRGBA (color, vector_color);
  275. vector_color.W = 0.0F;
  276. renderer.Add_Quad_HGradient (text_rect, color, VRGBA_TO_INT32 (vector_color));
  277. renderer.Add_Quad_HGradient (cost_rect, color, VRGBA_TO_INT32 (vector_color));
  278. return ;
  279. }
  280. ////////////////////////////////////////////////////////////////
  281. //
  282. // Create_Texture_Renderer
  283. //
  284. ////////////////////////////////////////////////////////////////
  285. void
  286. MerchandiseCtrlClass::Create_Texture_Renderer (void)
  287. {
  288. /*float smallest_dim = min (TextureRect.Width (), TextureRect.Height ());
  289. RectClass rect;
  290. rect.Left = int(TextureRect.Left + (TextureRect.Width () * 0.5F) - (smallest_dim * 0.5F));
  291. rect.Top = int(TextureRect.Top + (TextureRect.Height () * 0.5F) - (smallest_dim * 0.5F));
  292. rect.Right = int(rect.Left + smallest_dim);
  293. rect.Bottom = int(rect.Top + smallest_dim);*/
  294. //
  295. // Darken the bitmap if its disabled
  296. //
  297. int color = 0xFFFFFFFF;
  298. if (IsEnabled == false) {
  299. color = RGB_TO_INT32 (96, 96, 96);
  300. }
  301. //
  302. // Configure this renderer
  303. //
  304. TextureRenderer.Reset ();
  305. TextureRenderer.Add_Quad (Rect, color);
  306. return ;
  307. }
  308. ////////////////////////////////////////////////////////////////
  309. //
  310. // Set_Texture
  311. //
  312. ////////////////////////////////////////////////////////////////
  313. void
  314. MerchandiseCtrlClass::Set_Texture (const char *texture_name)
  315. {
  316. CurrentTextureIndex = 0;
  317. //
  318. // Cache the texture name
  319. //
  320. TextureNameList.Delete_All ();
  321. TextureNameList.Add (texture_name);
  322. //
  323. // Pass the texture onto the renderer
  324. //
  325. TextureClass *texture = WW3DAssetManager::Get_Instance ()->Get_Texture (texture_name, TextureClass::MIP_LEVELS_1);
  326. TextureRenderer.Set_Texture (texture);
  327. REF_PTR_RELEASE (texture);
  328. return ;
  329. }
  330. ////////////////////////////////////////////////////////////////
  331. //
  332. // Add_Alternate_Texture
  333. //
  334. ////////////////////////////////////////////////////////////////
  335. void
  336. MerchandiseCtrlClass::Add_Alternate_Texture (const char *texture_name)
  337. {
  338. TextureNameList.Add (texture_name);
  339. Set_Dirty ();
  340. return ;
  341. }
  342. ////////////////////////////////////////////////////////////////
  343. //
  344. // On_LButton_Down
  345. //
  346. ////////////////////////////////////////////////////////////////
  347. void
  348. MerchandiseCtrlClass::On_LButton_Down (const Vector2 &mouse_pos)
  349. {
  350. RectClass button_rect;
  351. button_rect.Right = Rect.Right - 1;
  352. button_rect.Top = Rect.Top + 2;
  353. button_rect.Left = int(button_rect.Right - (BUTTON_WIDTH * StyleMgrClass::Get_X_Scale ()));
  354. button_rect.Bottom = int(button_rect.Top + (BUTTON_HEIGHT * StyleMgrClass::Get_Y_Scale ()));
  355. //
  356. // Did the user click in the cycle button?
  357. //
  358. if (TextureNameList.Count () > 1 && button_rect.Contains (mouse_pos)) {
  359. //
  360. // Cycle to the next texture
  361. //
  362. CurrentTextureIndex ++;
  363. if (CurrentTextureIndex >= TextureNameList.Count ()) {
  364. CurrentTextureIndex = 0;
  365. }
  366. //
  367. // Pass the texture onto the renderer
  368. //
  369. TextureClass *texture = WW3DAssetManager::Get_Instance ()->Get_Texture (TextureNameList[CurrentTextureIndex], TextureClass::MIP_LEVELS_1);
  370. if (texture != NULL) {
  371. TextureRenderer.Set_Texture (texture);
  372. REF_PTR_RELEASE (texture);
  373. }
  374. Set_Dirty ();
  375. } else {
  376. //
  377. // Notify the owner that this item has been purchased
  378. //
  379. ADVISE_NOTIFY (On_Merchandise_Selected (this, Get_ID ()));
  380. }
  381. return ;
  382. }
  383. ////////////////////////////////////////////////////////////////
  384. //
  385. // On_LButton_DblClk
  386. //
  387. ////////////////////////////////////////////////////////////////
  388. void
  389. MerchandiseCtrlClass::On_LButton_DblClk (const Vector2 &mouse_pos)
  390. {
  391. RectClass button_rect;
  392. button_rect.Right = Rect.Right - 1;
  393. button_rect.Top = Rect.Top + 2;
  394. button_rect.Left = int(button_rect.Right - (BUTTON_WIDTH * StyleMgrClass::Get_X_Scale ()));
  395. button_rect.Bottom = int(button_rect.Top + (BUTTON_HEIGHT * StyleMgrClass::Get_Y_Scale ()));
  396. //
  397. // Did the user click in the cycle button?
  398. //
  399. if (TextureNameList.Count () == 0 || button_rect.Contains (mouse_pos) == false) {
  400. //
  401. // Notify the owner that this item has been double-clicked
  402. //
  403. ADVISE_NOTIFY (On_Merchandise_DblClk (this, Get_ID ()));
  404. }
  405. return ;
  406. }