SLIDER.CPP 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. /*
  2. ** Command & Conquer Red Alert(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. /* $Header: /CounterStrike/SLIDER.CPP 1 3/03/97 10:25a Joe_bostic $ */
  19. /***********************************************************************************************
  20. *** 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 ***
  21. ***********************************************************************************************
  22. * *
  23. * Project Name : Command & Conquer *
  24. * *
  25. * File Name : SLIDER.CPP *
  26. * *
  27. * Programmer : Joe L. Bostic *
  28. * *
  29. * Start Date : 01/15/95 *
  30. * *
  31. * Last Update : September 20, 1995 [JLB] *
  32. * *
  33. *---------------------------------------------------------------------------------------------*
  34. * Functions: *
  35. * SliderClass::Action -- Handles input processing for the slider. *
  36. * SliderClass::Bump -- Bumps the slider one "thumb size" up or down. *
  37. * SliderClass::Recalc_Thumb -- Recalculates the thumb pixel size and starting offset. *
  38. * SliderClass::Set_Maximum -- Sets the maximum value for this slider. *
  39. * SliderClass::Set_Thumb_Size -- Sets the size of the thumb in "slider units". *
  40. * SliderClass::Set_Value -- Sets the current thumb position for the slider. *
  41. * SliderClass::SliderClass -- Normal constructor for a slider (with thumb) gadget. *
  42. * SliderClass::Step -- Steps the slider one value up or down. *
  43. * SliderClass::Draw_Thumb -- Draws the "thumb" for this slider. *
  44. * SliderClass::~SliderClass -- Destructor for slider object. *
  45. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  46. #include "function.h"
  47. #include "slider.h"
  48. /***********************************************************************************************
  49. * SliderClass::SliderClass -- Normal constructor for a slider (with thumb) gadget. *
  50. * *
  51. * This is the normal constructor for the slider gadget. *
  52. * *
  53. * INPUT: id -- The ID number to assign to this gadget. *
  54. * x,y -- The pixel coordinate of the upper left corner for this gadget. *
  55. * w,h -- The width and height of the slider gadget. The slider automatically *
  56. * adapts for horizontal or vertical operation depending on which of these *
  57. * dimensions is greater. *
  58. * OUTPUT: none *
  59. * WARNINGS: none *
  60. * HISTORY: 01/15/1995 JLB : Created. *
  61. *=============================================================================================*/
  62. SliderClass::SliderClass(unsigned id, int x, int y, int w, int h, int belong_to_list)
  63. : GaugeClass(id, x, y, w, h)
  64. {
  65. BelongToList = belong_to_list ? true : false;
  66. PlusGadget = 0;
  67. MinusGadget = 0;
  68. if (!BelongToList) {
  69. PlusGadget = new ShapeButtonClass(id, MFCD::Retrieve("BTN-PLUS.SHP"), X+Width+2, Y);
  70. MinusGadget = new ShapeButtonClass(id, MFCD::Retrieve("BTN-MINS.SHP"), X-6, Y);
  71. if (PlusGadget) {
  72. PlusGadget->Make_Peer(*this);
  73. PlusGadget->Add(*this);
  74. PlusGadget->Flag_To_Redraw();
  75. }
  76. if (MinusGadget) {
  77. MinusGadget->Make_Peer(*this);
  78. MinusGadget->Add(*this);
  79. MinusGadget->Flag_To_Redraw();
  80. }
  81. }
  82. Set_Thumb_Size(1);
  83. Recalc_Thumb();
  84. /*
  85. ** Gauges have at least 2 colors, but sliders should only have one.
  86. */
  87. IsColorized = 0;
  88. }
  89. /***********************************************************************************************
  90. * SliderClass::~SliderClass -- Destructor for slider object. *
  91. * *
  92. * This cleans up the slider object in preparation for deletion. *
  93. * *
  94. * INPUT: none *
  95. * *
  96. * OUTPUT: none *
  97. * *
  98. * WARNINGS: none *
  99. * *
  100. * HISTORY: *
  101. * 09/20/1995 JLB : Created. *
  102. *=============================================================================================*/
  103. virtual SliderClass::~SliderClass(void)
  104. {
  105. if (PlusGadget) {
  106. delete PlusGadget;
  107. PlusGadget = 0;
  108. }
  109. if (MinusGadget) {
  110. delete MinusGadget;
  111. MinusGadget = 0;
  112. }
  113. }
  114. /***********************************************************************************************
  115. * SliderClass::Set_Maximum -- Sets the maximum value for this slider. *
  116. * *
  117. * This sets the maximum value that the slider can be set at. The maximum value controls *
  118. * the size of the thumb and the resolution of the thumb's movement. *
  119. * *
  120. * INPUT: value -- The value to set for the slider's maximum. *
  121. * OUTPUT: bool; Was the maximum value changed? A false indicates a set to the value it *
  122. * is currently set to already. *
  123. * WARNINGS: none *
  124. * HISTORY: 01/15/1995 JLB : Created. *
  125. *=============================================================================================*/
  126. int SliderClass::Set_Maximum(int value)
  127. {
  128. if (GaugeClass::Set_Maximum(value)) {
  129. Recalc_Thumb();
  130. return(true);
  131. }
  132. return(false);
  133. }
  134. /***********************************************************************************************
  135. * SliderClass::Set_Thumb_Size -- Sets the size of the thumb in "slider units". *
  136. * *
  137. * This routine will set the size of the thumb as it relates to the maximum value the *
  138. * slider can achieve. This serves to display a proportionally sized thumb as well as *
  139. * control how the slider "bumps" up or down. *
  140. * *
  141. * INPUT: value -- The new value of the thumb. It should never be larger than the slider *
  142. * maximum. *
  143. * OUTPUT: none *
  144. * WARNINGS: none *
  145. * HISTORY: 01/15/1995 JLB : Created. *
  146. *=============================================================================================*/
  147. void SliderClass::Set_Thumb_Size(int value)
  148. {
  149. Thumb = min(value, MaxValue);
  150. Thumb = max(Thumb, 1);
  151. Flag_To_Redraw();
  152. Recalc_Thumb();
  153. }
  154. /***********************************************************************************************
  155. * SliderClass::Set_Value -- Sets the current thumb position for the slider. *
  156. * *
  157. * This routine will set the thumb position for the slider. *
  158. * *
  159. * INPUT: value -- The position to set the slider. This position is relative to the maximum *
  160. * value for the slider. *
  161. * *
  162. * OUTPUT: bool; Was the slider thumb position changed at all? *
  163. * WARNINGS: none *
  164. * HISTORY: 01/15/1995 JLB : Created. *
  165. *=============================================================================================*/
  166. int SliderClass::Set_Value(int value)
  167. {
  168. value = min(value, MaxValue-Thumb);
  169. if (GaugeClass::Set_Value(value)) {
  170. Recalc_Thumb();
  171. return(true);
  172. }
  173. return(false);
  174. }
  175. /***********************************************************************************************
  176. * SliderClass::Recalc_Thumb -- Recalculates the thumb pixel size and starting offset. *
  177. * *
  178. * This takes the current thumb logical size and starting value and calculates the pixel *
  179. * size and starting offset accordingly. This function should be called whenever one of *
  180. * these elements has changed. *
  181. * *
  182. * INPUT: none *
  183. * OUTPUT: none *
  184. * WARNINGS: none *
  185. * HISTORY: 01/15/1995 JLB : Created. *
  186. *=============================================================================================*/
  187. void SliderClass::Recalc_Thumb(void)
  188. {
  189. int length = IsHorizontal ? Width : Height;
  190. int size = length * fixed(Thumb, MaxValue);
  191. // int size = Fixed_To_Cardinal(length, Cardinal_To_Fixed(MaxValue, Thumb));
  192. ThumbSize = max(size, 4);
  193. int start = length * fixed(CurValue, MaxValue);
  194. // int start = Fixed_To_Cardinal(length, Cardinal_To_Fixed(MaxValue, CurValue));
  195. ThumbStart = min(start, length-ThumbSize);
  196. }
  197. /***********************************************************************************************
  198. * SliderClass::Action -- Handles input processing for the slider. *
  199. * *
  200. * This routine is called when a qualifying input event has occurred. This routine will *
  201. * process that event and make any adjustments to the slider as necessary. *
  202. * *
  203. * INPUT: flags -- Flag bits that tell the input event that caused this function to *
  204. * be called. *
  205. * key -- Reference to the key that caused the input event. *
  206. * OUTPUT: bool; Was the event consumed and further processing of the gadget list should be *
  207. * aborted? *
  208. * WARNINGS: none *
  209. * HISTORY: 01/15/1995 JLB : Created. *
  210. *=============================================================================================*/
  211. int SliderClass::Action(unsigned flags, KeyNumType &key)
  212. {
  213. /*
  214. ** Handle the mouse click in a special way. If the click was not on the thumb, then
  215. ** jump the thumb position one "step" in the appropriate direction. Otherwise, let normal
  216. ** processing take place -- the slider then "sticks" and the thumb moves according to
  217. ** mouse position.
  218. */
  219. if (flags & LEFTPRESS) {
  220. int mouse; // Mouse pixel position.
  221. int edge; // Edge of slider.
  222. if (IsHorizontal) {
  223. mouse = Get_Mouse_X();
  224. edge = X;
  225. } else {
  226. mouse = Get_Mouse_Y();
  227. edge = Y;
  228. }
  229. edge += 1;
  230. /*
  231. ** Clicking outside the thumb: invoke parent's Action to process flags etc,
  232. ** but turn off the event & return true so processing stops at this button.
  233. */
  234. if (mouse < edge+ThumbStart) {
  235. Bump(true);
  236. GaugeClass::Action(0, key);
  237. key = KN_NONE;
  238. return(true);
  239. } else {
  240. if (mouse > edge+ThumbStart+ThumbSize) {
  241. Bump(false);
  242. GaugeClass::Action(0, key);
  243. key = KN_NONE;
  244. return(true);
  245. } else {
  246. GaugeClass::Action(flags, key);
  247. key = KN_NONE;
  248. return(true);
  249. }
  250. }
  251. }
  252. /*
  253. ** CHANGE GAUGECLASS::ACTION -- REMOVE (LEFTRELEASE) FROM IF STMT
  254. */
  255. return(GaugeClass::Action(flags, key));
  256. }
  257. /***********************************************************************************************
  258. * SliderClass::Bump -- Bumps the slider one "thumb size" up or down. *
  259. * *
  260. * This support function will bump the slider one "step" or the size of the thumb up or *
  261. * down as specified. It is typically called when the slider is clicked outside of the *
  262. * thumb region but still inside of the slider. *
  263. * *
  264. * INPUT: up -- Should the bump be to increase the current position? *
  265. * OUTPUT: bool; Was the slider changed at all? A false indicates that the slider is already *
  266. * at one end or the other. *
  267. * WARNINGS: none *
  268. * HISTORY: 01/15/1995 JLB : Created. *
  269. *=============================================================================================*/
  270. int SliderClass::Bump(int up)
  271. {
  272. if (up) {
  273. return(Set_Value(CurValue - Thumb));
  274. }
  275. return(Set_Value(CurValue + Thumb));
  276. }
  277. /***********************************************************************************************
  278. * SliderClass::Step -- Steps the slider one value up or down. *
  279. * *
  280. * This routine will move the slider thumb one step in the direction specified. *
  281. * *
  282. * INPUT: up -- Should the step be up (i.e., forward)? *
  283. * OUTPUT: bool; Was the slider changed at all? A false indicates that the slider is already *
  284. * at one end or the other. *
  285. * WARNINGS: none *
  286. * HISTORY: 01/15/1995 JLB : Created. *
  287. *=============================================================================================*/
  288. int SliderClass::Step(int up)
  289. {
  290. if (up) {
  291. return(Set_Value(CurValue - 1));
  292. }
  293. return(Set_Value(CurValue + 1));
  294. }
  295. /***********************************************************************************************
  296. * SliderClass::Draw_Thumb -- Draws the "thumb" for this slider. *
  297. * *
  298. * This will draw the thumb graphic for this slider. Sometimes the thumb requires special *
  299. * drawing, thus the need for this function separate from the normal Draw_Me function. *
  300. * *
  301. * INPUT: none *
  302. * OUTPUT: none *
  303. * WARNINGS: The mouse is guaranteed to be hidden when this routine is called. *
  304. * HISTORY: 01/16/1995 JLB : Created. *
  305. *=============================================================================================*/
  306. void SliderClass::Draw_Thumb(void)
  307. {
  308. if (IsHorizontal) {
  309. Draw_Box(X+ThumbStart, Y, ThumbSize, Height, BOXSTYLE_RAISED, true);
  310. } else {
  311. Draw_Box(X, Y+ThumbStart, Width, ThumbSize, BOXSTYLE_RAISED, true);
  312. }
  313. }
  314. /***********************************************************************************************
  315. * SliderClass::Draw_Me -- Draws the body of the gauge. *
  316. * *
  317. * This routine will draw the body of the gauge if necessary. *
  318. * *
  319. * INPUT: forced -- Should the gauge be redrawn regardless of the current redraw flag? *
  320. * OUTPUT: bool; Was the gauge redrawn? *
  321. * WARNINGS: none *
  322. * HISTORY: 01/16/1995 JLB : Created. *
  323. *=============================================================================================*/
  324. int SliderClass::Draw_Me(int forced)
  325. {
  326. if (BelongToList) {
  327. if (ControlClass::Draw_Me(forced)) {
  328. /*
  329. ** Hide the mouse.
  330. */
  331. if (LogicPage == &SeenBuff) {
  332. Conditional_Hide_Mouse(X, Y, X+Width, Y+Height);
  333. }
  334. /*
  335. ** Draw the body & set text color.
  336. */
  337. Draw_Box (X, Y, Width, Height, BOXSTYLE_DOWN, true);
  338. Draw_Thumb();
  339. /*
  340. ** Display the mouse.
  341. */
  342. if (LogicPage == &SeenBuff) {
  343. Conditional_Show_Mouse();
  344. }
  345. return(true);
  346. }
  347. }
  348. /*
  349. ** If it does not belong to a listbox...
  350. */
  351. return(GaugeClass::Draw_Me(forced));
  352. }
  353. /***********************************************************************************************
  354. * SliderClass::Peer_To_Peer -- A peer gadget was touched -- make adjustments. *
  355. * *
  356. * This routine is called when one of the peer gadgets (the scroll arrows or the slider) *
  357. * was touched in some fashion. This routine will sort out whom and why and then make *
  358. * any necessary adjustments to the list box. *
  359. * *
  360. * INPUT: flags -- The event flags that affected the peer gadget. *
  361. * key -- The key value at the time of the event. *
  362. * whom -- Which gadget is being touched. *
  363. * OUTPUT: none *
  364. * WARNINGS: none *
  365. * HISTORY: 01/16/1995 JLB : Created. *
  366. *=============================================================================================*/
  367. void SliderClass::Peer_To_Peer(unsigned flags, KeyNumType & , ControlClass & whom)
  368. {
  369. if (flags & LEFTRELEASE) {
  370. if (&whom == PlusGadget) {
  371. Step(false);
  372. }
  373. if (&whom == MinusGadget) {
  374. Step(true);
  375. }
  376. }
  377. }