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
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Implements an autoselection combo box that color codes the text
// based on whether the current selection represents a single entity,
// multiple entities, or an unresolved entity targetname.
//
// The fonts are as follows:
//
// Single entity black, normal weight
// Multiple entities black, bold
// Unresolved red, normal weight
//
//=============================================================================//
#include "stdafx.h"
#include "MapEntity.h"
#include "TargetNameCombo.h"
// memdbgon must be the last include file in a .cpp file!!!
#include <tier0/memdbgon.h>
#pragma warning( disable : 4355 )
BEGIN_MESSAGE_MAP(CTargetNameComboBox, CFilteredComboBox)
//{{AFX_MSG_MAP(CTargetNameComboBox)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
CTargetNameComboBox::CTargetNameComboBox( CFilteredComboBox::ICallbacks *pPassThru ) :
BaseClass( this )
{
m_pEntityList = NULL;
m_pPassThru = pPassThru;
}
//-----------------------------------------------------------------------------
// Purpose: Frees allocated memory.
//-----------------------------------------------------------------------------
CTargetNameComboBox::~CTargetNameComboBox(void)
{
FreeSubLists();
}
//-----------------------------------------------------------------------------
// Purpose:
//-----------------------------------------------------------------------------
void CTargetNameComboBox::FreeSubLists(void)
{
POSITION pos = m_SubLists.GetHeadPosition();
while (pos != NULL)
{
CMapEntityList *pList = m_SubLists.GetNext(pos);
delete pList;
}
m_SubLists.RemoveAll();
}
void CTargetNameComboBox::CreateFonts()
{
//
// Create a normal and bold font.
//
if (!m_BoldFont.m_hObject)
{
CFont &nf = GetNormalFont();
if ( nf.m_hObject )
{
LOGFONT LogFont;
nf.GetLogFont(&LogFont);
LogFont.lfWeight = FW_BOLD;
m_BoldFont.CreateFontIndirect(&LogFont);
}
}
}
CTargetNameComboBox* CTargetNameComboBox::Create( CFilteredComboBox::ICallbacks *pCallbacks, DWORD dwStyle, RECT rect, CWnd *pParentWnd, UINT nID )
{
CTargetNameComboBox *pRet = new CTargetNameComboBox( pCallbacks );
pRet->BaseClass::Create( dwStyle, rect, pParentWnd, nID );
return pRet;
}
//-----------------------------------------------------------------------------
// Purpose: Attaches an entity list to the combo box. This list will be used
// for matching targetnames to entities in the world.
// Input : pEntityList - The beauty of Hungarian notation and meaningful naming
// makes this comment utterly unnecessary.
//-----------------------------------------------------------------------------
void CTargetNameComboBox::SetEntityList(const CMapEntityList *pEntityList)
{
// We want all notifications, even if the current text doesn't match an exact entity name.
SetOnlyProvideSuggestions( false );
// Setup the list.
m_pEntityList = pEntityList;
FreeSubLists();
m_EntityLists.RemoveAll();
if (m_pEntityList != NULL)
{
FOR_EACH_OBJ( *m_pEntityList, pos )
{
CMapEntity *pEntity = m_pEntityList->Element(pos);
const char *pszTargetName = pEntity->GetKeyValue("targetname");
if (pszTargetName != NULL)
{
//
// If the targetname is not in the combo box, add it to the combo as the
// first entry in an entity list. The list is necessary because there
// may be several entities in the map with the same targetname.
//
int nIndex = m_EntityLists.Find( pszTargetName );
if (nIndex == m_EntityLists.InvalidIndex())
{
CMapEntityList *pList = new CMapEntityList;
pList->AddToTail(pEntity);
m_EntityLists.Insert( pszTargetName, pList );
//
// Keep track of all the sub lists so we can delete them later.
//
m_SubLists.AddTail(pList);
}
//
// Else append the entity to the given targetname's list.
//
else
{
CMapEntityList *pList = m_EntityLists[nIndex];
pList->AddToTail(pEntity);
}
}
}
}
// Setup the suggestions.
CUtlVector<CString> suggestions;
for ( int i=m_EntityLists.First(); i != m_EntityLists.InvalidIndex(); i=m_EntityLists.Next( i ) )
{
suggestions.AddToTail( m_EntityLists.GetElementName( i ) );
}
SetSuggestions( suggestions );
}
CMapEntityList* CTargetNameComboBox::GetSubEntityList( const char *pName )
{
int testIndex = m_EntityLists.Find( pName );
if ( testIndex != m_EntityLists.InvalidIndex() )
{
return m_EntityLists[testIndex];
}
return NULL;
}
void CTargetNameComboBox::OnTextChanged( const char *pText )
{
// Make sure our fonts are created.
CreateFonts();
// Update the fonts.
int nCount = 0;
CMapEntityList *pList = GetSubEntityList( pText );
if ( pList )
nCount = pList->Count();
// Figure out the font and color that we want.
CFont *pWantedFont = &m_BoldFont;
if ( (nCount == 0) || (nCount == 1) )
pWantedFont = &GetNormalFont();
COLORREF clrWanted = RGB(255,0,0);
if ( nCount > 0 )
clrWanted = RGB(0,0,0);
SetEditControlFont( *pWantedFont );
SetEditControlTextColor( clrWanted );
// Pass it through to the owner if they want notification.
if ( m_pPassThru )
m_pPassThru->OnTextChanged( pText );
}
|