CnC_Remastered_Collection

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

TOGGLE.CPP (11695B)


      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/TOGGLE.CPP 1     3/03/97 10:26a 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  *    closely 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 	IsPressed(false),
     65 	IsOn(false),
     66 	IsToggleType(false)
     67 {
     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 	bool overbutton = ((Get_Mouse_X() - X) < Width && (Get_Mouse_Y() - Y) < Height );
    145 	if (!flags) {
    146 		if (overbutton) {
    147 			if (!IsPressed) {
    148 				IsPressed = true;
    149 				Flag_To_Redraw();
    150 			}
    151 		} else {
    152 			if (IsPressed) {
    153 				IsPressed = false;
    154 				Flag_To_Redraw();
    155 			}
    156 		}
    157 	}
    158 
    159 	/*
    160 	**	Handle the sticky state for this gadget. It must be processed here
    161 	**	because the event flags might be cleared before the action function
    162 	**	is called.
    163 	*/
    164 	Sticky_Process(flags);
    165 
    166 	/*
    167 	**	Flag the button to show the pressed down imagery if this mouse button
    168 	**	was pressed over this gadget.
    169 	*/
    170 	if (flags & LEFTPRESS) {
    171 		IsPressed = true;
    172 		Flag_To_Redraw();
    173 		flags &= ~LEFTPRESS;
    174 		ControlClass::Action(flags, key);
    175 		key = KN_NONE;				// erase the event
    176 		return(true);		// stop processing other buttons now
    177 	}
    178 
    179 	if (flags & LEFTRELEASE) {
    180 		if (IsPressed) {
    181 			if (IsToggleType && overbutton) {
    182 				IsOn = (IsOn == false);
    183 			}
    184 			IsPressed = false;
    185 			Flag_To_Redraw();
    186 		} else {
    187 			flags &= ~LEFTRELEASE;
    188 		}
    189 	}
    190 
    191 	/*
    192 	**	Do normal button processing. This ends up causing the button's ID number to
    193 	**	be returned from the controlling Input() function.
    194 	*/
    195 	return(ControlClass::Action(flags, key));
    196 }
    197 
    198