summaryrefslogtreecommitdiff
path: root/game/client/cstrike/VGUI/buypreset_listbox.cpp
blob: add8dd75772e6d76f205f087ae9c3f2b74cff331 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
//=============================================================================

#include "cbase.h"

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

#include <vgui_controls/EditablePanel.h>
#include <vgui_controls/ScrollBar.h>
#include <vgui_controls/Label.h>
#include <vgui_controls/Button.h>
#include <vgui_controls/Controls.h>
#include "buypreset_listbox.h"

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

using namespace vgui;

#ifndef max
#define max(a,b)            (((a) > (b)) ? (a) : (b))
#endif


//--------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------
BuyPresetListBox::BuyPresetListBox( vgui::Panel *parent, char const *panelName ) : Panel( parent, panelName )
{
	m_visibleIndex = 0;
	m_lastSize = 0;

	SetBounds( 0, 0, 100, 100 );

	m_vbar = new ScrollBar(this, "PanelListPanelVScroll", true);
	m_vbar->SetBounds( 0, 0, 20, 20 );
	m_vbar->SetVisible(true);
	m_vbar->AddActionSignalTarget( this );

	m_pPanelEmbedded = new EditablePanel(this, "PanelListEmbedded");
	m_pPanelEmbedded->SetBounds(0, 0, 20, 20);
	m_pPanelEmbedded->SetPaintBackgroundEnabled( false );
	m_pPanelEmbedded->SetPaintBorderEnabled(false);

	if( IsProportional() )
	{
		int width, height;
		int sw,sh;
		surface()->GetProportionalBase( width, height );
		GetHudSize(sw, sh);

		// resize scrollbar, etc
		m_iScrollbarSize = static_cast<int>( static_cast<float>( SCROLLBAR_SIZE )*( static_cast<float>( sw )/ static_cast<float>( width )));
		m_iDefaultHeight = static_cast<int>( static_cast<float>( DEFAULT_HEIGHT )*( static_cast<float>( sw )/ static_cast<float>( width )));
		m_iPanelBuffer = static_cast<int>( static_cast<float>( PANELBUFFER )*( static_cast<float>( sw )/ static_cast<float>( width )));
	}
	else
	{
		m_iScrollbarSize = SCROLLBAR_SIZE;
		m_iDefaultHeight = DEFAULT_HEIGHT;
		m_iPanelBuffer = PANELBUFFER;
	}
}

//--------------------------------------------------------------------------------------------------------------
BuyPresetListBox::~BuyPresetListBox()
{
	// free data from table
	DeleteAllItems();
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Passes commands up to the parent
 */
void BuyPresetListBox::OnCommand( const char *command )
{
	GetParent()->OnCommand( command );
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Scrolls the list according to the mouse wheel movement
 */
void BuyPresetListBox::OnMouseWheeled(int delta)
{
	int scale = 3;
	if ( m_items.Count() )
	{
		Panel *panel = m_items[0].panel;
		if ( panel )
		{
			scale = panel->GetTall() + m_iPanelBuffer;
		}
	}
	int val = m_vbar->GetValue();
	val -= (delta * scale);
	m_vbar->SetValue(val);
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Computes vertical pixels needed by listbox contents
 */
int	BuyPresetListBox::computeVPixelsNeeded( void )
{
	int pixels = 0;

	int i; 
	for ( i = 0; i < m_items.Count(); i++ )
	{
		Panel *panel = m_items[i].panel;
		if ( !panel )
			continue;

		int w, h;
		panel->GetSize( w, h );

		pixels += m_iPanelBuffer; // add in buffer. between items.
		pixels += h;	
	}

	pixels += m_iPanelBuffer; // add in buffer below last item

	return pixels;
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Adds an item to the end of the listbox.  UserData is assumed to be a pointer that can be freed by the listbox if non-NULL.
 */
int BuyPresetListBox::AddItem( vgui::Panel *panel, void * userData )
{
	assert(panel);

	DataItem item = { panel, userData };

	panel->SetParent( m_pPanelEmbedded );

	m_items.AddToTail( item );

	InvalidateLayout();
	return m_items.Count();
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Exchanges two items in the listbox
 */
void BuyPresetListBox::SwapItems( int index1, int index2 )
{
	if ( index1 < 0 || index2 < 0 || index1 >= m_items.Count() || index2 >= m_items.Count() )
	{
		return;
	}

	DataItem temp = m_items[index1];
	m_items[index1] = m_items[index2];
	m_items[index2] = temp;

	InvalidateLayout();
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Returns the number of items in the listbox
 */
int	BuyPresetListBox::GetItemCount( void ) const
{
	return m_items.Count();
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Returns the panel in the given index, or NULL
 */
Panel * BuyPresetListBox::GetItemPanel(int index) const
{
	if ( index < 0 || index >= m_items.Count() )
		return NULL;

	return m_items[index].panel;
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Returns the userData in the given index, or NULL
 */
void * BuyPresetListBox::GetItemUserData(int index)
{
	if ( index < 0 || index >= m_items.Count() )
	{
		return NULL;
	}

	return m_items[index].userData;
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Sets the userData in the given index
 */
void BuyPresetListBox::SetItemUserData( int index, void * userData )
{
	if ( index < 0 || index >= m_items.Count() )
		return;

	m_items[index].userData = userData;
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Removes an item from the table (changing the indices of all following items), deleting the panel and userData
 */
void BuyPresetListBox::RemoveItem(int index)
{
	if ( index < 0 || index >= m_items.Count() )
		return;

	DataItem item = m_items[index];
	if ( item.panel )
	{
		item.panel->MarkForDeletion();
	}
	if ( item.userData )
	{
		delete item.userData;
	}

	m_items.Remove( index );

	InvalidateLayout();
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  clears the listbox, deleting all panels and userData
 */
void BuyPresetListBox::DeleteAllItems()
{
	while ( m_items.Count() )
	{
		RemoveItem( 0 );
	}

	// move the scrollbar to the top of the list
	m_vbar->SetValue(0);
	InvalidateLayout();
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Handles Count changes
 */
void BuyPresetListBox::OnSizeChanged(int wide, int tall)
{
	BaseClass::OnSizeChanged(wide, tall);
	InvalidateLayout();
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Positions listbox items, etc after internal changes
 */
void BuyPresetListBox::PerformLayout()
{
	int wide, tall;
	GetSize( wide, tall );

	int vpixels = computeVPixelsNeeded();

	int visibleIndex = m_visibleIndex;

	//!! need to make it recalculate scroll positions
	m_vbar->SetVisible(true);
	m_vbar->SetEnabled(false);
	m_vbar->SetRange( 0, (MAX( 0, vpixels - tall + m_iDefaultHeight ))  );
	m_vbar->SetRangeWindow( m_iDefaultHeight );
	m_vbar->SetButtonPressedScrollValue( m_iDefaultHeight ); // standard height of labels/buttons etc.
	m_vbar->SetPos(wide - m_iScrollbarSize, 1);
	m_vbar->SetSize(m_iScrollbarSize, tall - 2);

	m_visibleIndex = visibleIndex;

	int top = MAX( 0, m_vbar->GetValue() );

	m_pPanelEmbedded->SetPos( 1, -top );
	m_pPanelEmbedded->SetSize( wide-m_iScrollbarSize -2, vpixels );

	// Now lay out the controls on the embedded panel
	int y = 0;
	int h = 0;
	int totalh = 0;
	
	int i;
	for ( i = 0; i < m_items.Count(); i++, y += h )
	{
		// add in a little buffer between panels
		y += m_iPanelBuffer;
		DataItem item = m_items[i];

		h = item.panel->GetTall();

		totalh += h;
		item.panel->SetBounds( 8, y, wide - m_iScrollbarSize - 8 - 8, h );
		item.panel->InvalidateLayout();
	}

	if ( m_visibleIndex >= 0 && m_visibleIndex < m_items.Count() )
	{

		int vpos = 0;

		int tempWide, tempTall;
		GetSize( tempWide, tempTall );

		int vtop, vbottom;
		m_vbar->GetRange( vtop, vbottom );

		int tempTop = MAX( 0, m_vbar->GetValue() ); // top pixel in the embedded panel
		int bottom = tempTop + tempTall - 2;

		int itemTop, itemLeft, itemBottom, itemRight;
		m_items[m_visibleIndex].panel->GetBounds( itemLeft, itemTop, itemRight, itemBottom );
		itemBottom += itemTop;
		itemRight += itemLeft;

		if ( itemTop < tempTop )
		{
			// item's top is too high
			vpos -= ( tempTop - itemTop );

			m_vbar->SetValue(vpos);
			OnSliderMoved(vpos);
		}
		else if ( itemBottom > bottom )
		{
			// item's bottom is too low
			vpos += ( itemBottom - bottom );

			m_vbar->SetValue(vpos);
			OnSliderMoved(vpos);
		}
	}

	if ( m_lastSize == vpixels )
	{
		m_visibleIndex = -1;
	}
	m_lastSize = vpixels;
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Try to ensure that the given index is visible
 */
void BuyPresetListBox::MakeItemVisible( int index )
{
	m_visibleIndex = index;
	m_lastSize = 0;
}

//--------------------------------------------------------------------------------------------------------------
/**
 *  Loads colors, fonts, etc
 */
void BuyPresetListBox::ApplySchemeSettings(IScheme *pScheme)
{
	BaseClass::ApplySchemeSettings(pScheme);

	SetBgColor(GetSchemeColor("BuyPresetListBox.BgColor", GetBgColor(), pScheme));

	SetBorder(pScheme->GetBorder("BrowserBorder"));
	m_vbar->SetBorder(pScheme->GetBorder("BrowserBorder"));

	PerformLayout();
}


//--------------------------------------------------------------------------------------------------------------
/**
 *  Handles slider being dragged
 */
void BuyPresetListBox::OnSliderMoved( int position )
{
	InvalidateLayout();
	Repaint();
}


//--------------------------------------------------------------------------------------------------------------
/**
 *  Moves slider to the top
 */
void BuyPresetListBox::MoveScrollBarToTop()
{
	m_vbar->SetValue(0);
	OnSliderMoved(0);
}