aboutsummaryrefslogtreecommitdiff
path: root/mp/src/vgui2/vgui_controls/RadioButton.cpp
blob: 8cb1e79314c08ade6d092f14e9a9f245b4796575 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#include <stdarg.h>
#include <stdio.h>

#include <vgui/IInput.h>
#include <vgui/IPanel.h>
#include <vgui/IScheme.h>
#include <vgui/ISystem.h>
#include <vgui/IVGui.h>
#include <vgui/KeyCode.h>
#include <KeyValues.h>

#include <vgui_controls/FocusNavGroup.h>
#include <vgui_controls/Image.h>
#include <vgui_controls/RadioButton.h>
#include <vgui_controls/TextImage.h>
#include <vgui_controls/Controls.h>

// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>

using namespace vgui;

enum direction
{
	UP = -1,
	DOWN = 1,
};

//-----------------------------------------------------------------------------
// Purpose: Check box image
//-----------------------------------------------------------------------------
void RadioImage::Paint()
{
	DrawSetTextFont(GetFont());

	// draw background
	if (_radioButton->IsEnabled())
	{
		DrawSetTextColor(_bgColor);
	}
	else
	{
		DrawSetTextColor(_radioButton->GetBgColor());
	}
	DrawPrintChar(0, 1, 'n');

	// draw border circl
	DrawSetTextColor(_borderColor1);
	DrawPrintChar(0, 1, 'j');
	DrawSetTextColor(_borderColor2);
	DrawPrintChar(0, 1, 'k');

	// draw selected check
	if (_radioButton->IsSelected())
	{
		DrawSetTextColor(_checkColor);
		DrawPrintChar(0, 1, 'h');
	}
}

DECLARE_BUILD_FACTORY_DEFAULT_TEXT( RadioButton, RadioButton );

//-----------------------------------------------------------------------------
// Purpose: Create a radio button.
//-----------------------------------------------------------------------------
RadioButton::RadioButton(Panel *parent, const char *panelName, const char *text) : ToggleButton(parent, panelName, text)
{
 	SetContentAlignment(a_west);

	// create the image
	_radioBoxImage = new RadioImage(this);

	_oldTabPosition = 0;
	_subTabPosition = 0;

	SetTextImageIndex(1);
	SetImageAtIndex(0, _radioBoxImage, 0);

	SetButtonActivationType(ACTIVATE_ONPRESSED);
}

//-----------------------------------------------------------------------------
// Purpose: Destructor
//-----------------------------------------------------------------------------
RadioButton::~RadioButton()
{
	delete _radioBoxImage;
}

//-----------------------------------------------------------------------------
// Purpose: Apply resource file scheme.
//-----------------------------------------------------------------------------
void RadioButton::ApplySchemeSettings(IScheme *pScheme)
{
	BaseClass::ApplySchemeSettings(pScheme);
	_radioBoxImage->_bgColor = GetSchemeColor("CheckButton.BgColor", Color(150, 150, 150, 0), pScheme);
	_radioBoxImage->_borderColor1 = GetSchemeColor("CheckButton.Border1", Color(20, 20, 20, 0), pScheme);
	_radioBoxImage->_borderColor2 = GetSchemeColor("CheckButton.Border2", Color(90, 90, 90, 0), pScheme);
	_radioBoxImage->_checkColor = GetSchemeColor("CheckButton.Check", Color(20, 20, 20, 0), pScheme);

	SetFgColor(GetSchemeColor("RadioButton.TextColor", pScheme));
	_selectedFgColor = GetSchemeColor("RadioButton.SelectedTextColor", GetSchemeColor("ControlText", pScheme), pScheme);

	SetDefaultColor( GetFgColor(), GetBgColor() );

	SetArmedColor( GetSchemeColor("RadioButton.ArmedTextColor", pScheme), GetButtonArmedBgColor() );

	SetContentAlignment(a_west);

	//  reloading the scheme wipes out lists of images
	HFont hFont = pScheme->GetFont("MarlettSmall", IsProportional());
	if ( hFont == INVALID_FONT )
	{
		// fallback to Marlett if MarlettSmall isn't found
		hFont = pScheme->GetFont("Marlett", IsProportional());
	}
	_radioBoxImage->SetFont( hFont );
	_radioBoxImage->ResizeImageToContent();
	SetImageAtIndex(0, _radioBoxImage, 0);

	// don't draw a background
	SetPaintBackgroundEnabled(false);
}

//-----------------------------------------------------------------------------
// Purpose: Get the border style of the button, Radio buttons have no border			
//-----------------------------------------------------------------------------
IBorder *RadioButton::GetBorder(bool depressed, bool armed, bool selected, bool keyfocus)
{
	return NULL;
}

//-----------------------------------------------------------------------------
// Purpose: Get the tab position of the radio button with the set of radio buttons
//			A group of RadioButtons must have the same TabPosition, with [1, n] subtabpositions
//-----------------------------------------------------------------------------
int RadioButton::GetSubTabPosition()
{
	return _subTabPosition;
}

//-----------------------------------------------------------------------------
// Purpose: Get the tab position of the radio button with the set of radio buttons
//			A group of RadioButtons must have the same TabPosition, with [1, n] subtabpositions
//-----------------------------------------------------------------------------
void RadioButton::SetSubTabPosition(int position)
{
	_subTabPosition = position;
}

//-----------------------------------------------------------------------------
// Purpose: Return the RadioButton's real tab position (its Panel one changes)
//-----------------------------------------------------------------------------
int RadioButton::GetRadioTabPosition()
{
	return _oldTabPosition;
}

//-----------------------------------------------------------------------------
// Purpose: Set the radio button checked. When a radio button is checked, a 
//			message is sent to all other radio buttons in the same group so
//			they will become unchecked.
//-----------------------------------------------------------------------------
void RadioButton::SetSelected(bool state)
{
	InternalSetSelected( state, true );
}

void RadioButton::InternalSetSelected(bool state, bool bFireEvents)
{
	if (state == true)
	{
		if (!IsEnabled())
			return;

		// restore our tab position
		SetTabPosition(_oldTabPosition);

		// Should we send notifications?
		if ( bFireEvents )
		{
			// send a message
			KeyValues *msg = new KeyValues("RadioButtonChecked");
			msg->SetPtr("panel", this);
			msg->SetInt("tabposition", _oldTabPosition);

			// send a message to all other panels on the same level as heirarchy, 
			// so that other radio buttons know to shut off
			VPANEL radioParent = GetVParent();
			if (radioParent)
			{
				for (int i = 0; i < ipanel()->GetChildCount(radioParent); i++)
				{
					VPANEL child = ipanel()->GetChild(radioParent, i);
					if (child != GetVPanel())
					{
						ivgui()->PostMessage(child, msg->MakeCopy(), GetVPanel());
					}
				}
			}

			RequestFocus();
			PostActionSignal(msg);
		}
	}
	else
	{
		// remove ourselves from the tab order
		if (GetTabPosition())
		{
			_oldTabPosition = GetTabPosition();
		}
		SetTabPosition(0);
	}

	InvalidateLayout();
	Repaint();

	ToggleButton::SetSelected(state);
}

//-----------------------------------------------------------------------------
// Purpose: Set the selection state without firing any events
//-----------------------------------------------------------------------------
void RadioButton::SilentSetSelected(bool state)
{
	InternalSetSelected( state, false );
}

//-----------------------------------------------------------------------------
// Purpose: Set up the text color before doing normal layout
//-----------------------------------------------------------------------------
void RadioButton::PerformLayout()
{
	if (IsSelected())
	{
		SetFgColor(_selectedFgColor);
	}
	else
	{
		SetFgColor(GetButtonFgColor());
	}

	BaseClass::PerformLayout();
}

//-----------------------------------------------------------------------------
// Purpose: Apply resource settings including button state and text color.
//-----------------------------------------------------------------------------
void RadioButton::ApplySettings(KeyValues *inResourceData)
{
	ToggleButton::ApplySettings(inResourceData);
	SetTextColorState(CS_NORMAL);

	_subTabPosition = inResourceData->GetInt("SubTabPosition");
	_oldTabPosition = inResourceData->GetInt("TabPosition");
	SetImageAtIndex(0, _radioBoxImage, 0);
}

//-----------------------------------------------------------------------------
// Purpose: Get resource settings including button state, text color, and subTabPosition
//-----------------------------------------------------------------------------
void RadioButton::GetSettings(KeyValues *outResourceData)
{
	ToggleButton::GetSettings(outResourceData);
	outResourceData->SetInt("SubTabPosition", _subTabPosition);
	outResourceData->SetInt("TabPosition", GetRadioTabPosition());
}

//-----------------------------------------------------------------------------
// Purpose: Describe editing details
//-----------------------------------------------------------------------------
const char *RadioButton::GetDescription( void )
{
	static char buf[1024];
	Q_snprintf(buf, sizeof(buf), "%s, int SubTabPosition", BaseClass::GetDescription());
	return buf;
}

//-----------------------------------------------------------------------------
// Purpose: When a radio button is checked, all other radio buttons
//			in the same group become unchecked.
//-----------------------------------------------------------------------------
void RadioButton::OnRadioButtonChecked(int tabPosition)
{
	// make sure we're in the same tab group
	if (tabPosition != _oldTabPosition)
		return;

	// wouldn't be sent to us from ourselves, so another radio button has taken over
	SetSelected(false);
}

//-----------------------------------------------------------------------------
// Purpose: Draws the selection rectangle
//-----------------------------------------------------------------------------
void RadioButton::Paint()
{
	BaseClass::Paint();

	/*
	if (HasFocus())
	{
		int tx0, ty0, tx1, ty1;
		_textImage->GetPos(tx0, ty0);
		_textImage->GetSize(tx1, ty1);
		DrawFocusBorder(tx0, ty0, tx0 + tx1, ty0 + ty1);
	}
	*/
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void RadioButton::DoClick()
{
	SetSelected(true);
}

//-----------------------------------------------------------------------------
// Purpose: Handle arrow key movement
//-----------------------------------------------------------------------------
void RadioButton::OnKeyCodeTyped(KeyCode code)
{
	switch (code)
	{
		case KEY_ENTER:
		case KEY_SPACE:
		{
			if (!IsSelected())
			{
				SetSelected(true);
			}
			else
			{
				Panel::OnKeyCodeTyped(code);
			}
		}
		break;

		case KEY_DOWN:
		case KEY_RIGHT:
			{
				RadioButton *bestRadio = FindBestRadioButton( DOWN );
				if (bestRadio)
				{
					bestRadio->SetSelected(true);
				}	
			}
			break;
		case KEY_UP:
		case KEY_LEFT:
			{
				RadioButton *bestRadio = FindBestRadioButton( UP );
				if (bestRadio)
				{
					bestRadio->SetSelected(true);
				}	
			}
			break;

		default:
			BaseClass::OnKeyCodeTyped(code);
			break;
	}
}

//-----------------------------------------------------------------------------
// Purpose: Find the correct radio button to move to.
// Input  : direction - the direction we are moving, up or down. 
//-----------------------------------------------------------------------------
RadioButton *RadioButton::FindBestRadioButton(int direction)
{
	RadioButton *bestRadio = NULL;
	int highestRadio = 0;
	Panel *pr = GetParent();
	if (pr)
	{
		// find the radio button to go to next
		for (int i = 0; i < pr->GetChildCount(); i++)
		{
			RadioButton *child = dynamic_cast<RadioButton *>(pr->GetChild(i));
			if (child && child->GetRadioTabPosition() == _oldTabPosition)
			{
				if (child->GetSubTabPosition() == _subTabPosition + direction)
				{
					bestRadio = child;
					break;
				}
				if ( (child->GetSubTabPosition() == 0)  && (direction == DOWN) )
				{
					bestRadio = child;
					continue;
				}
				else if ( (child->GetSubTabPosition() > highestRadio) && (direction == UP) )
				{
					bestRadio = child;
					highestRadio = bestRadio->GetSubTabPosition();
					continue;
				}
				if (!bestRadio)
				{
					bestRadio = child;
				}
			}
		}
		
		if (bestRadio)
		{
			bestRadio->RequestFocus();
		}
		
		InvalidateLayout();
		Repaint();
	}

	return bestRadio;
}