summaryrefslogtreecommitdiff
path: root/hammer/editgroups.cpp
blob: d78331166ee73b69dd9d30856d5b4f2c2f39564b (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: A dialog for adding, deleting, and renaming visgroups.
//
//=============================================================================//

#include "stdafx.h"
#include "hammer.h"
#include "EditGroups.h"
#include "MainFrm.h"
#include "MapWorld.h"
#include "CustomMessages.h"
#include "GlobalFunctions.h"
#include "VisGroup.h"

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

static const unsigned int g_uSelChangeMsg = ::RegisterWindowMessage(GROUPLIST_MSG_SEL_CHANGE);


BEGIN_MESSAGE_MAP(CEditGroups, CDialog)
	//{{AFX_MSG_MAP(CEditGroups)
	ON_BN_CLICKED(IDC_COLOR, OnColor)
	ON_EN_CHANGE(IDC_NAME, OnChangeName)
	ON_BN_CLICKED(IDC_NEW, OnNew)
	ON_BN_CLICKED(IDC_REMOVE, OnRemove)
	ON_WM_CLOSE()
	ON_REGISTERED_MESSAGE(g_uSelChangeMsg, OnSelChangeGroupList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


//-----------------------------------------------------------------------------
// Purpose: Constructor.
// Input  : pParent - Parent window.
//-----------------------------------------------------------------------------
CEditGroups::CEditGroups(CWnd* pParent /*=NULL*/)
	: CDialog(CEditGroups::IDD, pParent)
{
	//{{AFX_DATA_INIT(CEditGroups)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
}


//-----------------------------------------------------------------------------
// Purpose: Exchanges data between controls and data members.
// Input  : pDX - 
//-----------------------------------------------------------------------------
void CEditGroups::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CEditGroups)
	DDX_Control(pDX, IDC_NAME, m_cName);
	//}}AFX_DATA_MAP
}


//-----------------------------------------------------------------------------
// Purpose: Sets the object's color as the visgroup color if the object belongs
//			to the given visgroup.
// Input  : pObject - Object to evaluate.
//			pGroup - Visgroup to check against.
// Output : Returns TRUE to continue enumerating.
//-----------------------------------------------------------------------------
static BOOL UpdateObjectColor(CMapClass *pObject, CVisGroup *pGroup)
{
	pObject->UpdateObjectColor();
	return(TRUE);
}


//-----------------------------------------------------------------------------
// Purpose: Invokes the color picker dialog to modify the selected visgroup.
//-----------------------------------------------------------------------------
void CEditGroups::OnColor(void) 
{
	CVisGroup *pGroup = m_cGroupList.GetSelectedVisGroup();

	if (pGroup != NULL)
	{
		color32 rgbColor = pGroup->GetColor();
		CColorDialog dlg(RGB(rgbColor.r, rgbColor.g, rgbColor.b), CC_FULLOPEN);

		if (dlg.DoModal() == IDOK)
		{
			// change group color
			pGroup->SetColor(GetRValue(dlg.m_cc.rgbResult), GetGValue(dlg.m_cc.rgbResult), GetBValue(dlg.m_cc.rgbResult));
			m_cColorBox.SetColor(dlg.m_cc.rgbResult, TRUE);

			// change all object colors
			GetActiveWorld()->EnumChildren(ENUMMAPCHILDRENPROC(UpdateObjectColor), DWORD(pGroup));

			CMapDoc::GetActiveMapDoc()->UpdateAllViews( MAPVIEW_UPDATE_COLOR );
		}
	}
}


//-----------------------------------------------------------------------------
// Purpose: Called when the contents of the name edit control changes. Renames
//			the currently selected group with the new edit control contents.
//-----------------------------------------------------------------------------
void CEditGroups::OnChangeName(void)
{
	CVisGroup *pGroup = m_cGroupList.GetSelectedVisGroup();

	CString szName;
	m_cName.GetWindowText(szName);
	pGroup->SetName(szName);
	m_cGroupList.UpdateVisGroup(pGroup);
}


//-----------------------------------------------------------------------------
// Purpose: Creates a new visgroup and adds it to the list.
//-----------------------------------------------------------------------------
void CEditGroups::OnNew(void)
{
	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
	CVisGroup *pGroup = pDoc->VisGroups_AddGroup("new group");
	pGroup->SetVisible(VISGROUP_SHOWN);
	UpdateGroupList();
	m_cGroupList.SelectVisGroup(pGroup);
	m_cName.EnableWindow(TRUE);
	m_cName.SetActiveWindow();
}


//-----------------------------------------------------------------------------
// Purpose: Called when the remove button is pressed from the visgroup editor.
//			Deletes the selected visgroup and removes all references to it.
//-----------------------------------------------------------------------------
void CEditGroups::OnRemove(void) 
{
	CVisGroup *pGroup = m_cGroupList.GetSelectedVisGroup();
	if (!pGroup)
		return;
	//Don't allow user to delete autovisgroups.
	if ( pGroup->IsAutoVisGroup() )
		return;

	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
	if (pDoc != NULL)
	{
		pDoc->VisGroups_RemoveGroup(pGroup);

		if (pGroup->GetVisible() != VISGROUP_SHOWN)
		{
			pDoc->VisGroups_UpdateAll();
			pDoc->UpdateVisibilityAll();
			pDoc->UpdateAllViews( MAPVIEW_UPDATE_OBJECTS );
		}
		else
		{
			pDoc->UpdateAllViews( MAPVIEW_UPDATE_COLOR );
		}
	}

	UpdateGroupList();
}


//-----------------------------------------------------------------------------
// Purpose: Handles selection change in the visgroup list. Updates the static
//			text controls with the name and colot of the selected visgroup.
//-----------------------------------------------------------------------------
LRESULT CEditGroups::OnSelChangeGroupList(WPARAM wParam, LPARAM lParam)
{
	CVisGroup *pVisGroup = m_cGroupList.GetSelectedVisGroup();
	if (!pVisGroup)
		return 0;

	UpdateControlsForVisGroup(pVisGroup);
	return 0;
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CEditGroups::UpdateControlsForVisGroup(CVisGroup *pVisGroup)
{
	if (!pVisGroup)
		return;

	//
	// Update the name and color controls.
	//
	m_cName.SetWindowText(pVisGroup->GetName());
	color32 rgbColor = pVisGroup->GetColor();
	m_cColorBox.SetColor(RGB(rgbColor.r, rgbColor.g, rgbColor.b), TRUE);
}


//-----------------------------------------------------------------------------
// Purpose: Sets up initial state of dialog.
//-----------------------------------------------------------------------------
BOOL CEditGroups::OnInitDialog(void)
{
	CDialog::OnInitDialog();

	m_cGroupList.SubclassDlgItem(IDC_GROUPS, this);
	m_cColorBox.SubclassDlgItem(IDC_COLORBOX, this);

	//
	// Fill the listbox with the visgroup names.
	//
	UpdateGroupList();

	//
	// Disable the edit name window if there are no visgroups in the list.
	//
	if (m_cGroupList.GetVisGroupCount())
	{
		CVisGroup *pVisGroup = m_cGroupList.GetVisGroup(0);
		m_cGroupList.SelectVisGroup(pVisGroup);
		UpdateControlsForVisGroup(pVisGroup);
	}
	else
	{
		m_cName.EnableWindow(FALSE);
	}

	return(TRUE);
}


//-----------------------------------------------------------------------------
// Purpose: 
//-----------------------------------------------------------------------------
void CEditGroups::UpdateGroupList()
{
	if (!IsWindow(m_hWnd))
	{
		return;
	}

	m_cGroupList.SetRedraw(false);
	m_cGroupList.DeleteAllItems();

	CMapDoc *pDoc = CMapDoc::GetActiveMapDoc();
	if (pDoc != NULL)
	{
		int nCount = pDoc->VisGroups_GetCount();
		for (int i = 0; i < nCount; i++)
		{
			CVisGroup *pGroup = pDoc->VisGroups_GetVisGroup(i);
			if (!pGroup->GetParent())
			{
				m_cGroupList.AddVisGroup(pGroup);
			}
		}
	}

	m_cGroupList.ExpandAll();
	m_cGroupList.SetRedraw(true);
	m_cGroupList.Invalidate();
}


//-----------------------------------------------------------------------------
// Purpose: Called when the dialog is closing. Sends a message to update the
//			visgroup dialog bar in case any visgroup changes were made.
//-----------------------------------------------------------------------------
void CEditGroups::OnClose(void)
{
	GetMainWnd()->GlobalNotify(WM_MAPDOC_CHANGED);
	CDialog::OnClose();
}


//-----------------------------------------------------------------------------
// Purpose: Called when the dialog is closing. Sends a message to update the
//			visgroup dialog bar in case any visgroup changes were made.
//-----------------------------------------------------------------------------
BOOL CEditGroups::DestroyWindow(void)
{
	GetMainWnd()->GlobalNotify(WM_MAPDOC_CHANGED);
	return(CDialog::DestroyWindow());
}


BEGIN_MESSAGE_MAP(CColorBox, CStatic)
	ON_WM_PAINT()
END_MESSAGE_MAP()


//-----------------------------------------------------------------------------
// Purpose: Sets the color of the color box.
// Input  : c - RGB color to set.
//			bRedraw - TRUE repaints, FALSE does now.
//-----------------------------------------------------------------------------
void CColorBox::SetColor(COLORREF c, BOOL bRedraw)
{
	m_c = c;
	if (bRedraw)
	{
		RedrawWindow(NULL, NULL, RDW_INVALIDATE | RDW_UPDATENOW);
	}
}


//-----------------------------------------------------------------------------
// Purpose: Fills the colorbox window with the current color.
//-----------------------------------------------------------------------------
void CColorBox::OnPaint(void)
{
	CPaintDC dc(this);
	CRect r;
	GetClientRect(r);
	CBrush brush(m_c);
	dc.FillRect(r, &brush);
}