SLIDER.CPP 22 KB

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