CnC_Remastered_Collection

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

TOGGLE.CPP (11787B)


      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:   F:\projects\c&c\vcs\code\toggle.cpv   2.18   16 Oct 1995 16:50:56   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 : TOGGLE.CPP                                                   * 
     24  *                                                                                             * 
     25  *                   Programmer : Joe L. Bostic                                                * 
     26  *                                                                                             * 
     27  *                   Start Date : 01/15/95                                                     * 
     28  *                                                                                             * 
     29  *                  Last Update : February 2, 1995 [JLB]                                       * 
     30  *                                                                                             * 
     31  *---------------------------------------------------------------------------------------------* 
     32  * Functions:                                                                                  * 
     33  *   ToggleClass::ToggleClass -- Normal constructor for toggle button gadgets.                 * 
     34  *   ToggleClass::Turn_Off -- Turns the toggle button to the "OFF" state.                      * 
     35  *   ToggleClass::Turn_On -- Turns the toggle button to the "ON" state.                        * 
     36  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
     37 
     38 #include	"function.h"
     39 #include	"toggle.h"
     40 
     41 
     42 /*********************************************************************************************** 
     43  * ToggleClass::ToggleClass -- Normal constructor for toggle button gadgets.                   * 
     44  *                                                                                             * 
     45  *    This is the normal constructor for toggle buttons. A toggle button is one that most      * 
     46  *    closesly resembles the Windows style. It has an up and down state as well as an on       * 
     47  *    and off state.                                                                           * 
     48  *                                                                                             * 
     49  * INPUT:   id    -- ID number for this button.                                                * 
     50  *                                                                                             * 
     51  *          x,y   -- Pixel coordinate of upper left corner of this button.                     * 
     52  *                                                                                             * 
     53  *          w,h   -- Width and height of the button.                                           * 
     54  *                                                                                             * 
     55  * OUTPUT:  none                                                                               * 
     56  *                                                                                             * 
     57  * WARNINGS:   none                                                                            * 
     58  *                                                                                             * 
     59  * HISTORY:                                                                                    * 
     60  *   01/15/1995 JLB : Created.                                                                 * 
     61  *=============================================================================================*/
     62 ToggleClass::ToggleClass(unsigned id, int x, int y, int w, int h) 
     63 	: ControlClass(id, x, y, w, h, LEFTPRESS|LEFTRELEASE, true)
     64 {
     65 	IsPressed = false;
     66 	IsOn = false;
     67 	IsToggleType = false;
     68 }	
     69 
     70 
     71 /*********************************************************************************************** 
     72  * ToggleClass::Turn_On -- Turns the toggle button to the "ON" state.                          * 
     73  *                                                                                             * 
     74  *    This routine will turn the button on. The button will also be flagged to be redrawn      * 
     75  *    at the next opportunity.                                                                 * 
     76  *                                                                                             * 
     77  * INPUT:   none                                                                               * 
     78  *                                                                                             * 
     79  * OUTPUT:  none                                                                               * 
     80  *                                                                                             * 
     81  * WARNINGS:   none                                                                            * 
     82  *                                                                                             * 
     83  * HISTORY:                                                                                    * 
     84  *   01/15/1995 JLB : Created.                                                                 * 
     85  *=============================================================================================*/
     86 void ToggleClass::Turn_On(void)
     87 {
     88 	IsOn = true;
     89 	Flag_To_Redraw();
     90 }	
     91 
     92 
     93 /*********************************************************************************************** 
     94  * ToggleClass::Turn_Off -- Turns the toggle button to the "OFF" state.                        * 
     95  *                                                                                             * 
     96  *    This routine will turn the toggle button "off". It will also be flagged to be redrawn    * 
     97  *    at the next opportunity.                                                                 * 
     98  *                                                                                             * 
     99  * INPUT:   none                                                                               * 
    100  *                                                                                             * 
    101  * OUTPUT:  none                                                                               * 
    102  *                                                                                             * 
    103  * WARNINGS:   none                                                                            * 
    104  *                                                                                             * 
    105  * HISTORY:                                                                                    * 
    106  *   01/15/1995 JLB : Created.                                                                 * 
    107  *=============================================================================================*/
    108 void ToggleClass::Turn_Off(void)
    109 {
    110 	IsOn = false;	
    111 	Flag_To_Redraw();
    112 }	
    113 
    114 
    115 /*********************************************************************************************** 
    116  * ToggleClass::Action -- Handles mouse clicks on a text button.                               *
    117  *                                                                                             * 
    118  *    This routine will process any mouse or keyboard event that is associated with this       * 
    119  *    button object. It detects and flags the text button so that it will properly be drawn    * 
    120  *    in a pressed or raised state. It also handles any toggle state for the button.           * 
    121  *                                                                                             * 
    122  * INPUT:   flags -- The event flags that triggered this button.                               * 
    123  *                                                                                             * 
    124  *          key   -- The keyboard code associated with this event. Usually this is KN_LMOUSE   * 
    125  *                   or similar, but it could be a regular key if this text button is given    * 
    126  *                   a hotkey.                                                                 * 
    127  *                                                                                             * 
    128  * OUTPUT:  Returns whatever the lower level processing for buttons decides. This is usually   * 
    129  *          true.                                                                              * 
    130  *                                                                                             * 
    131  * WARNINGS:   The button is flagged to be redrawn by this routine.                            * 
    132  *                                                                                             * 
    133  * HISTORY:                                                                                    * 
    134  *   01/14/1995 JLB : Created.                                                                 * 
    135  *   02/02/1995 JLB : Left press doesn't get passed to other buttons now                       * 
    136  *=============================================================================================*/
    137 int ToggleClass::Action(unsigned flags, KeyNumType &key)
    138 {
    139 	/*
    140 	**	If there are no action flag bits set, then this must be a forced call. A forced call
    141 	**	must never actually function like a real call, but rather only performs any necessary
    142 	**	graphic updating.
    143 	*/
    144 	if (!flags) {
    145 		if ((unsigned)(Get_Mouse_X() - X) < (unsigned)Width && (unsigned)(Get_Mouse_Y() - Y) < (unsigned)Height ) {
    146 			if (!IsPressed) {
    147 				IsPressed = true;
    148 				Flag_To_Redraw();
    149 			}
    150 		} else {
    151 			if (IsPressed) {
    152 				IsPressed = false;
    153 				Flag_To_Redraw();
    154 			}
    155 		}
    156 	} 
    157 
    158 	/*
    159 	**	Handle the sticky state for this gadget. It must be processed here
    160 	**	because the event flags might be cleared before the action function
    161 	**	is called.
    162 	*/
    163 	Sticky_Process(flags);
    164 
    165 	/*
    166 	**	Flag the button to show the pressed down imagery if this mouse button
    167 	**	was pressed over this gadget.
    168 	*/
    169 	if (flags & LEFTPRESS) {
    170 		IsPressed = true;
    171 		Flag_To_Redraw();
    172 		flags &= ~LEFTPRESS;
    173 		ControlClass::Action(flags, key);
    174 		key = KN_NONE;				// erase the event
    175 		return(true);		// stop processing other buttons now
    176 	}
    177 
    178 	if (flags & LEFTRELEASE) {
    179 		if (IsPressed) {
    180 			if (IsToggleType) {
    181 				IsOn = (IsOn == false);
    182 			}
    183 			IsPressed = false;
    184 		} else {
    185 			flags &= ~LEFTRELEASE;
    186 		}
    187 	}
    188 
    189 	/*
    190 	**	Do normal button processing. This ends up causing the button's ID number to
    191 	**	be returned from the controlling Input() function.
    192 	*/
    193 	return(ControlClass::Action(flags, key));
    194 }	
    195 
    196