CnC_Remastered_Collection

Command and Conquer: Red Alert
Log | Files | Refs | README | LICENSE

SLIDER.CPP (22420B)


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