aboutsummaryrefslogtreecommitdiff
path: root/mp/src/vgui2/vgui_controls/BuildFactoryHelper.cpp
blob: 46f38cd1367251eac0abaf125486a8bacebf882f (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Helper for the CHudElement class to add themselves to the list of hud elements
//
// $NoKeywords: $
//=============================================================================//
#include "vgui/IVGui.h"
#include "vgui_controls/MessageMap.h"

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

using namespace vgui;

// Start with empty list
CBuildFactoryHelper *CBuildFactoryHelper::m_sHelpers = NULL;

//-----------------------------------------------------------------------------
// Purpose: Constructs a panel  factory
// Input  : pfnCreate - fn Ptr to a function which generates a panel
//-----------------------------------------------------------------------------
CBuildFactoryHelper::CBuildFactoryHelper( char const *className, PANELCREATEFUNC func )
{
	// Make this fatal
	if ( HasFactory( className ) )
	{
		Error( "CBuildFactoryHelper:  Factory for '%s' already exists!!!!\n", className );
	}

	//List is empty, or element belongs at front, insert here
	m_pNext			= m_sHelpers;
	m_sHelpers		= this;

	Assert( func );
	m_CreateFunc = func;
	Assert( className );
	m_pClassName = className;
}

//-----------------------------------------------------------------------------
// Purpose: Returns next object in list
// Output : CBuildFactoryHelper
//-----------------------------------------------------------------------------
CBuildFactoryHelper *CBuildFactoryHelper::GetNext( void )
{ 
	return m_pNext;
}

char const *CBuildFactoryHelper::GetClassName() const
{
	return m_pClassName;
}

vgui::Panel *CBuildFactoryHelper::CreatePanel()
{
	if ( !m_CreateFunc )
		return NULL;

	return ( *m_CreateFunc )();
}

// private static meethod
bool CBuildFactoryHelper::HasFactory( char const *className )
{
	CBuildFactoryHelper *p = m_sHelpers;
	while ( p )
	{
		if ( !Q_stricmp( className, p->GetClassName() ) )
			return true;

		p = p->GetNext();
	}
	return false;
}

// static method
vgui::Panel *CBuildFactoryHelper::InstancePanel( char const *className )
{
	CBuildFactoryHelper *p = m_sHelpers;
	while ( p )
	{
		if ( !Q_stricmp( className, p->GetClassName() ) )
			return p->CreatePanel();

		p = p->GetNext();
	}
	return NULL;
}

// static method
void CBuildFactoryHelper::GetFactoryNames( CUtlVector< char const * >& list )
{
	list.RemoveAll();

	CBuildFactoryHelper *p = m_sHelpers;
	while ( p )
	{
		list.AddToTail( p->GetClassName() );
		p = p->GetNext();
	}
}