aboutsummaryrefslogtreecommitdiff
path: root/mp/src/vgui2/vgui_controls/ScrollBarSlider.cpp
blob: 32df2fae66bfde8016134e1c7cf22945aad46a18 (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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//=============================================================================//

#define PROTECTED_THINGS_DISABLE

#include <vgui/IBorder.h>
#include <vgui/IInput.h>
#include <vgui/ISystem.h>
#include <vgui/IScheme.h>
#include <vgui/ISurface.h>
#include <vgui/MouseCode.h>
#include <KeyValues.h>

#include <vgui_controls/ScrollBarSlider.h>
#include <vgui_controls/Controls.h>

#include <math.h>

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

using namespace vgui;

//-----------------------------------------------------------------------------
// The ScrollBarSlider is the scroll bar nob that moves up and down in through a range.
//-----------------------------------------------------------------------------
ScrollBarSlider::ScrollBarSlider(Panel *parent, const char *panelName, bool vertical) : Panel(parent, panelName)
{
	_vertical=vertical;	
	_dragging=false;
	_value=0;
	_range[0]=0;
	_range[1]=0;
	_rangeWindow=0;
	_buttonOffset=0;
	_ScrollBarSliderBorder=NULL;
	RecomputeNobPosFromValue();
	SetBlockDragChaining( true );
}

//-----------------------------------------------------------------------------
// Purpose: Set the size of the ScrollBarSlider nob
//-----------------------------------------------------------------------------
void ScrollBarSlider::SetSize(int wide,int tall)
{
	BaseClass::SetSize(wide,tall);
	RecomputeNobPosFromValue();
}

//-----------------------------------------------------------------------------
// Purpose: Whether the scroll bar is vertical (true) or not (false)
//-----------------------------------------------------------------------------
bool ScrollBarSlider::IsVertical()
{
	return _vertical;
}

//-----------------------------------------------------------------------------
// Purpose: Set the ScrollBarSlider value of the nob.
//-----------------------------------------------------------------------------
void ScrollBarSlider::SetValue(int value)
{
	int oldValue = _value;

	if (value > _range[1] - _rangeWindow)
	{
		// note our scrolling range must take into acount _rangeWindow
		value = _range[1] - _rangeWindow;	
	}

	if (value < _range[0])
	{
		value = _range[0];
	}

	_value = value;
	RecomputeNobPosFromValue();

	if (_value != oldValue)
	{
		SendScrollBarSliderMovedMessage();
	}
}

//-----------------------------------------------------------------------------
// Purpose: Get the ScrollBarSlider value of the nob.
//-----------------------------------------------------------------------------
int ScrollBarSlider::GetValue()
{
	return _value;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void ScrollBarSlider::PerformLayout()
{
	RecomputeNobPosFromValue();
	BaseClass::PerformLayout();
}

//-----------------------------------------------------------------------------
// Purpose: Given the value of the ScrollBarSlider, adjust the ends of the nob.
//-----------------------------------------------------------------------------
void ScrollBarSlider::RecomputeNobPosFromValue()
{
	int wide, tall;
	GetPaintSize(wide, tall);

	float fwide = (float)( wide - 1 );
	float ftall = (float)( tall - 1 );
	float frange = (float)(_range[1] -_range[0]);
	float fvalue = (float)(_value - _range[0]);
	float frangewindow = (float)(_rangeWindow);
	float fper = ( frange != frangewindow ) ? fvalue / ( frange-frangewindow ) : 0;

//	Msg( "fwide: %f  ftall: %f  frange: %f  fvalue: %f  frangewindow: %f  fper: %f\n",
//		fwide, ftall, frange, fvalue, frangewindow, fper );

	if ( frangewindow > 0 )
	{
		if ( frange <= 0.0 )
		{
			frange = 1.0;
		}

		float width, length;
		if (_vertical)
		{
			width = fwide;
			length = ftall;
		}
		else
		{
			width = ftall;
			length = fwide;
		}
		
		// our size is proportional to frangewindow/frange
		// the scroll bar nob's length reflects the amount of stuff on the screen 
		// vs the total amount of stuff we could scroll through in window
		// so if a window showed half its contents and the other half is hidden the
		// scroll bar's length is half the window.
		// if everything is on the screen no nob is displayed
		// frange is how many 'lines' of stuff we can display
		// frangewindow is how many 'lines' are in the display window
		
		// proportion of whole window that is on screen
		float proportion = frangewindow / frange;
		float fnobsize = length * proportion;
		if ( fnobsize < width ) fnobsize = (float)width;
		
		float freepixels = length - fnobsize;
		
		float firstpixel = freepixels * fper;
		
		_nobPos[0] = (int)( firstpixel );
		_nobPos[1] = (int)( firstpixel + fnobsize );
		
		if ( _nobPos[1] > length )
		{
			_nobPos[0] = (int)( length - fnobsize );
			_nobPos[1] = (int)length;
		}
		
	}

	Repaint();
}

//-----------------------------------------------------------------------------
// Purpose: Get the ScrollBarSlider value using the location of the nob ends.
//-----------------------------------------------------------------------------
void ScrollBarSlider::RecomputeValueFromNobPos()
{
	int wide, tall;
	GetPaintSize(wide, tall);

	float fwide = (float)( wide - 1 );
	float ftall = (float)( tall - 1 );
	float frange = (float)( _range[1] - _range[0] );
	float fvalue = (float)( _value - _range[0] );
	float fnob = (float)_nobPos[0];
	float frangewindow = (float)(_rangeWindow);

	if ( frangewindow > 0 )
	{
		if ( frange <= 0.0 )
		{
			frange = 1.0;
		}

		// set local width and length
		float width, length;
		if ( _vertical )
		{
			width = fwide;
			length = ftall;
		}
		else
		{
			width = ftall;
			length = fwide;
		}
		
		// calculate the size of the nob
		float proportion = frangewindow / frange;
		float fnobsize = length * proportion;
		
		if ( fnobsize < width )
		{
			fnobsize = width;
		}
		
		// Our scroll bar actually doesnt scroll through all frange lines in the truerange, we
		// actually only scroll through frange-frangewindow number of lines so we must take that 
		// into account when we calculate the value
		// convert to our local size system

		// Make sure we don't divide by zero
		if ( length - fnobsize == 0 )
		{
			fvalue = 0.0f;
		}
		else
		{
			fvalue = (frange - frangewindow) * ( fnob / ( length - fnobsize ) );
		}
	}

	// check to see if we should just snap to the bottom
	if (fabs(fvalue + _rangeWindow - _range[1]) < (0.01f * frange))
	{
		// snap to the end
		_value = _range[1] - _rangeWindow;
	}
	else
	{
		// Take care of rounding issues.
		_value = (int)( fvalue + _range[0] + 0.5);
	}

	// Clamp final result
	_value = ( _value < (_range[1] - _rangeWindow) ) ? _value : (_range[1] - _rangeWindow);

	if (_value < _range[0])
	{
		_value = _range[0];
	}
}

//-----------------------------------------------------------------------------
// Purpose: Check if the ScrollBarSlider can move through one or more pixels per
//			unit of its range.
//-----------------------------------------------------------------------------
bool ScrollBarSlider::HasFullRange()
{
	int wide, tall;
	GetPaintSize(wide, tall);

	float frangewindow = (float)(_rangeWindow);

	float checkAgainst = 0;
	if(_vertical)
	{
		checkAgainst = (float)tall;
	}
	else
	{
		checkAgainst = (float)wide;
	}

	if ( frangewindow > 0 )
	{
		if( frangewindow <= ( checkAgainst + _buttonOffset ) )
		{
			return true;
		}
	}

	return false;
}
	
//-----------------------------------------------------------------------------
// Purpose: Inform other watchers that the ScrollBarSlider was moved
//-----------------------------------------------------------------------------
void ScrollBarSlider::SendScrollBarSliderMovedMessage()
{	
	// send a changed message
	PostActionSignal(new KeyValues("ScrollBarSliderMoved", "position", _value));
}

//-----------------------------------------------------------------------------
// Purpose: Return true if this slider is actually drawing itself
//-----------------------------------------------------------------------------
bool ScrollBarSlider::IsSliderVisible( void )
{
	int itemRange = _range[1] - _range[0];

	// Don't draw nob, no items in list
	if ( itemRange <= 0 )
		return false ;

	// Not enough range
	if ( itemRange <= _rangeWindow )
		return false;

	return true;
}

//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void ScrollBarSlider::ApplySchemeSettings(IScheme *pScheme)
{
	BaseClass::ApplySchemeSettings(pScheme);

	SetFgColor(GetSchemeColor("ScrollBarSlider.FgColor", pScheme));
	SetBgColor(GetSchemeColor("ScrollBarSlider.BgColor", pScheme));

	IBorder *newBorder = pScheme->GetBorder("ScrollBarSliderBorder");

	if ( newBorder )
	{
		_ScrollBarSliderBorder = newBorder;
	}
	else
	{
		_ScrollBarSliderBorder = pScheme->GetBorder("ButtonBorder");
	}
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void ScrollBarSlider::ApplySettings( KeyValues *pInResourceData )
{
	BaseClass::ApplySettings( pInResourceData );

	const char *pButtonBorderName = pInResourceData->GetString( "ButtonBorder", NULL );
	if ( pButtonBorderName )
	{
		_ScrollBarSliderBorder = vgui::scheme()->GetIScheme( GetScheme() )->GetBorder( pButtonBorderName );
	}
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void ScrollBarSlider::Paint()
{
	int wide,tall;
	GetPaintSize(wide,tall);

	if ( !IsSliderVisible() )	
		return;

	Color col = GetFgColor();
	surface()->DrawSetColor(col);

	if (_vertical)
	{
		if ( GetPaintBackgroundType() == 2 )
		{
			DrawBox( 1, _nobPos[0], wide - 2, _nobPos[1] - _nobPos[0], col, 1.0f );
		}
		else
		{
			// Nob
			surface()->DrawFilledRect(1, _nobPos[0], wide - 2, _nobPos[1]);
		}

		// border
		if (_ScrollBarSliderBorder)
		{
			_ScrollBarSliderBorder->Paint(0, _nobPos[0], wide, _nobPos[1]);
		}
	}
	else
	{
		// horizontal nob
		surface()->DrawFilledRect(_nobPos[0], 1, _nobPos[1], tall - 2 );

		// border
		if (_ScrollBarSliderBorder)
		{
			_ScrollBarSliderBorder->Paint(_nobPos[0] - 1, 1, _nobPos[1], tall );
		}
	}

}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void ScrollBarSlider::PaintBackground()
{
//	BaseClass::PaintBackground();
	
	int wide,tall;
	GetPaintSize(wide,tall);
	surface()->DrawSetColor(GetBgColor());
	surface()->DrawFilledRect(0, 0, wide-1, tall-1);
}

//-----------------------------------------------------------------------------
// Purpose: Set the range of the ScrollBarSlider
//-----------------------------------------------------------------------------
void ScrollBarSlider::SetRange(int min,int max)
{
	if(max<min)
	{
		max=min;
	}

	if(min>max)
	{
		min=max;
	}

	_range[0]=min;
	_range[1]=max;

	// update the value (forces it within the range)
	SetValue( _value );
	InvalidateLayout();
}

//-----------------------------------------------------------------------------
// Purpose: Get the range values of the ScrollBarSlider
//-----------------------------------------------------------------------------
void ScrollBarSlider::GetRange(int& min,int& max)
{
	min=_range[0];
	max=_range[1];
}

//-----------------------------------------------------------------------------
// Purpose: Respond to cursor movements, we only care about clicking and dragging
//-----------------------------------------------------------------------------
void ScrollBarSlider::OnCursorMoved(int x,int y)
{
	if (!_dragging)
	{
		return;
	}

//	input()->GetCursorPos(x, y);
//	ScreenToLocal(x, y);

	int wide, tall;
	GetPaintSize(wide, tall);

	if (_vertical)
	{
		_nobPos[0] = _nobDragStartPos[0] + (y - _dragStartPos[1]);
		_nobPos[1] = _nobDragStartPos[1] + (y - _dragStartPos[1]);
		
		if (_nobPos[1] > tall)
		{
			_nobPos[0] = tall - (_nobPos[1] - _nobPos[0]);
			_nobPos[1] = tall;
			SetValue( _range[1] - _rangeWindow );
		}
	}
	else
	{
		_nobPos[0] = _nobDragStartPos[0] + (x - _dragStartPos[0]);
		_nobPos[1] = _nobDragStartPos[1] + (x - _dragStartPos[0]);
		
		if (_nobPos[1] > wide)
		{
			_nobPos[0] = wide - (_nobPos[1] - _nobPos[0]);
			_nobPos[1] = wide;
		}
		
	}
	if (_nobPos[0] < 0)
	{
		_nobPos[1] = _nobPos[1] - _nobPos[0];
		_nobPos[0] = 0;
		SetValue(0);
	}
	
	InvalidateLayout();		// not invalidatelayout - because it won't draw while we're scrolling the slider
	RecomputeValueFromNobPos();
//	Repaint();
	SendScrollBarSliderMovedMessage();
}

//-----------------------------------------------------------------------------
// Purpose: Respond to mouse clicks on the ScrollBarSlider
//-----------------------------------------------------------------------------
void ScrollBarSlider::OnMousePressed(MouseCode code)
{
	int x,y;
	input()->GetCursorPos(x,y);
	ScreenToLocal(x,y);

	if (_vertical)
	{
		if ((y >= _nobPos[0]) && (y < _nobPos[1]))
		{
			_dragging = true;
			input()->SetMouseCapture(GetVPanel());
			_nobDragStartPos[0] = _nobPos[0];
			_nobDragStartPos[1] = _nobPos[1];
			_dragStartPos[0] = x;
			_dragStartPos[1] = y;
		}
		else if (y < _nobPos[0])
		{
			// jump the bar up by the range window
			int val = GetValue();
			val -= _rangeWindow;
			SetValue(val);
		}
		else if (y >= _nobPos[1])
		{
			// jump the bar down by the range window
			int val = GetValue();
			val += _rangeWindow;
			SetValue(val);
		}
	}
	else
	{
		if((x >= _nobPos[0]) && (x < _nobPos[1]))
		{
			_dragging = true;
			input()->SetMouseCapture(GetVPanel());
			_nobDragStartPos[0] = _nobPos[0];
			_nobDragStartPos[1] = _nobPos[1];
			_dragStartPos[0] = x;
			_dragStartPos[1] = y;
		}
		else if (x < _nobPos[0])
		{
			// jump the bar up by the range window
			int val = GetValue();
			val -= _rangeWindow;
			SetValue(val);
		}
		else if (x >= _nobPos[1])
		{
			// jump the bar down by the range window
			int val = GetValue();
			val += _rangeWindow;
			SetValue(val);
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: Treat double clicks as single clicks
//-----------------------------------------------------------------------------
void ScrollBarSlider::OnMouseDoublePressed(MouseCode code)
{
	OnMousePressed(code);
}

//-----------------------------------------------------------------------------
// Purpose: Stop looking for mouse events when mouse is up.
//-----------------------------------------------------------------------------
void ScrollBarSlider::OnMouseReleased(MouseCode code)
{
	_dragging = false;
	input()->SetMouseCapture(null);
}

//-----------------------------------------------------------------------------
// Purpose: Get the position of the ends of the ScrollBarSlider.
//-----------------------------------------------------------------------------
void ScrollBarSlider::GetNobPos(int& min, int& max)
{
	min=_nobPos[0];
	max=_nobPos[1];
}

//-----------------------------------------------------------------------------
// Purpose: Set the number of lines visible in the window the ScrollBarSlider is attached to
//-----------------------------------------------------------------------------
void ScrollBarSlider::SetRangeWindow(int rangeWindow)
{
	_rangeWindow = rangeWindow;
}

//-----------------------------------------------------------------------------
// Purpose: Get the number of lines visible in the window the ScrollBarSlider is attached to
//-----------------------------------------------------------------------------
int ScrollBarSlider::GetRangeWindow()
{
	return _rangeWindow;
}

//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void ScrollBarSlider::SetButtonOffset(int buttonOffset)
{
	_buttonOffset = buttonOffset;
}