summaryrefslogtreecommitdiff
path: root/hammer/op_flags.cpp
blob: 642c3d695fe07ef38d14c5ba07d144a2121320c5 (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: Implements the spawnflags page of the Entity Properties dialog.
//
//=============================================================================//

#include "stdafx.h"
#include "hammer.h"
#include "OP_Flags.h"
#include "OP_Entity.h"
#include "ObjectProperties.h"

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

/////////////////////////////////////////////////////////////////////////////
// COP_Flags property page

IMPLEMENT_DYNCREATE(COP_Flags, CObjectPage)

COP_Flags::COP_Flags() : CObjectPage(COP_Flags::IDD)
{
	//{{AFX_DATA_INIT(COP_Flags)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	m_pEditObjectRuntimeClass = RUNTIME_CLASS(editCEditGameClass);
	m_nNumSelectedObjects = 0;
	m_pEntityPage = NULL;
}

COP_Flags::~COP_Flags()
{
}

void COP_Flags::SetEntityPage( COP_Entity *pPage )
{
	m_pEntityPage = pPage;
}

void COP_Flags::DoDataExchange(CDataExchange* pDX)
{
	CObjectPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(COP_Flags)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(COP_Flags, CObjectPage)
	//{{AFX_MSG_MAP(COP_Flags)
	ON_CLBN_CHKCHANGE(IDC_CHECKLIST, OnCheckListChange)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// COP_Flags message handlers

void COP_Flags::UpdateData( int Mode, PVOID pData, bool bCanEdit )
{
	__super::UpdateData( Mode, pData, bCanEdit );

	if(!IsWindow(m_hWnd) || !pData)
	{
		return;
	}
	
	CEditGameClass *pObj = (CEditGameClass*) pData;

	if (Mode == LoadFirstData)
	{
		UpdateForClass(pObj);
		
	}
	else if (Mode == LoadData)
	{
		MergeForClass(pObj);
	}
    CreateCheckList();

	m_CheckList.EnableWindow( m_bCanEdit ? TRUE : FALSE );
}


//-----------------------------------------------------------------------------
// Purpose: 
// Output : Returns true on success, false on failure.
//-----------------------------------------------------------------------------
bool COP_Flags::SaveData(void)
{
	if (!IsWindow(m_hWnd))
	{
		return(false);
	}

	//
	// Apply the dialog data to all the objects being edited.
	//
	FOR_EACH_OBJ( *m_pObjectList, pos )
	{
		CMapClass *pObject = m_pObjectList->Element(pos);
		CEditGameClass *pEdit = dynamic_cast <CEditGameClass *>(pObject);
		Assert(pEdit != NULL);

		if ( pEdit != NULL )
		{
			for ( int i = 0; i < m_CheckListItems.Count(); i++ )
			{
				CheckListItem currentItem = m_CheckListItems.Element( i );
				// don't save tri-stated bit
				if ( m_CheckList.GetCheck(i) != 2 )
				{
					pEdit->SetSpawnFlag( currentItem.nItemBit, m_CheckList.GetCheck(i) ? TRUE : FALSE );
				}
			}
		}
	}

	return(true);
}

//-----------------------------------------------------------------------------
// Purpose: This function is used to initialize the flag checklist.
//			It is called to place all the flags belonging to the first
//			selected object into the temporary CheckListItems vector
//-----------------------------------------------------------------------------

void COP_Flags::UpdateForClass(CEditGameClass* pObj)
{
	extern GameData *pGD;

	GDclass * pClass = pGD->ClassForName(pObj->GetClassName());

	if(!IsWindow(m_hWnd))
		return;

	m_nNumSelectedObjects = 1;

	m_CheckListItems.RemoveAll();

	if(pClass)
	{
		GDinputvariable *pVar = pClass->VarForName("spawnflags");

		if (pVar)
		{
			int nItems = pVar->GetFlagCount();		

			for ( int i = 0; i < nItems; i++ )
			{
				CheckListItem newItem;
				newItem.nItemBit = pVar->GetFlagMask( i );
				newItem.pszItemString = pVar->GetFlagCaption( i );
				newItem.state = pObj->GetSpawnFlag( newItem.nItemBit ) ? 1 : 0;
				m_CheckListItems.AddToTail( newItem );			
			}
		}
	}

	Assert( m_CheckListItems.Count() <= 32 );

	for ( int i = 0; i < 32; i++ )
	{
		int nBitPattern = 1 << i;
		// is spawnflag for this bit set?
		if ( pObj->GetSpawnFlag(nBitPattern) )
		{
			int j;
			// then see if its allowed to be
			for ( j = 0; j < m_CheckListItems.Count(); j ++ )
			{
				int nCheckListPattern = m_CheckListItems.Element(j).nItemBit;
				if ( nCheckListPattern == nBitPattern )
					break;
			}
			// we fail to find it?
			if ( j == m_CheckListItems.Count() )
			{
				CheckListItem newItem;
				newItem.nItemBit = nBitPattern;
				newItem.pszItemString = "????";
				newItem.state = 1;
				m_CheckListItems.AddToTail( newItem );
			}				
		}
	}
}

//-----------------------------------------------------------------------------
// Purpose: This function is called to combine flags when multiple objects are selected
//			It removes flags from the CheckListItem vector that are not present in all selected objects
//-----------------------------------------------------------------------------
void COP_Flags::MergeForClass(CEditGameClass* pObj)
{
	extern GameData *pGD;
	GDclass * pClass = pGD->ClassForName(pObj->GetClassName());

	if( !IsWindow(m_hWnd) )
		return;

	m_nNumSelectedObjects++;

	if( pClass )
	{
		GDinputvariable *pVar = pClass->VarForName("spawnflags");

		for ( int i = m_CheckListItems.Count() - 1; i >= 0; i-- )
		{	
			bool bFound = false;
			CheckListItem currentItem = m_CheckListItems.Element( i ); 
			if ( pVar )
			{
				for ( int j = 0; j < pVar->GetFlagCount(); j++ )
				{
					CheckListItem newItem;
					newItem.nItemBit = pVar->GetFlagMask(j);
					newItem.pszItemString = pVar->GetFlagCaption(j);
					if ( newItem == currentItem )
					{
						bFound = true;
						int nNewState = pObj->GetSpawnFlag( newItem.nItemBit ) ? 1 : 0;
						if ( currentItem.state != nNewState )
						{
							m_CheckListItems.Element( i ).state = 2;
						}
						break;
					}
				}
			}
			if ( !bFound )
			{
				m_CheckListItems.FastRemove( i );
			}
		}
	}
	Assert( m_CheckListItems.Count() <= 32 );	
}

//-----------------------------------------------------------------------------
// Purpose: Creates the checklist by stepping through the CheckListItems vector that
//			was created during Update/MergeForClass
//-----------------------------------------------------------------------------
void COP_Flags::CreateCheckList()
{
	m_CheckList.ResetContent();
	
	if ( m_nNumSelectedObjects > 1 )
	{
		m_CheckList.SetCheckStyle(BS_AUTO3STATE);
	}

	for ( int i = 0; i < m_CheckListItems.Count(); i++ )
	{
		CheckListItem newItem = m_CheckListItems.Element(i);
		m_CheckList.InsertString(i, newItem.pszItemString);
		m_CheckList.SetCheck(i, newItem.state);
	}
}

void COP_Flags::OnUpdateSpawnFlags( unsigned long value )
{
	for ( int i=0; i < m_CheckListItems.Count(); i++ )
	{
		CheckListItem &item = m_CheckListItems[i];
		m_CheckList.SetCheck( i, (value & item.nItemBit) != 0 );
	}
}

BOOL COP_Flags::OnInitDialog() 
{	
	CObjectPage::OnInitDialog();

	m_nNumSelectedObjects = 0;

	// Subclass checklistbox
	m_CheckList.SubclassDlgItem(IDC_CHECKLIST, this);
	m_CheckList.SetCheckStyle(BS_AUTOCHECKBOX);
	m_CheckList.ResetContent();

	CAnchorDef anchorDefs[] =
	{
		CAnchorDef( IDC_CHECKLIST, k_eSimpleAnchorAllSides )
	};
	m_AnchorMgr.Init( GetSafeHwnd(), anchorDefs, ARRAYSIZE( anchorDefs ) );
	
	return TRUE;	             
}

void COP_Flags::OnCheckListChange() 
{
	if ( !m_pEntityPage )
		return;

	unsigned long bitsSet = 0;
	unsigned long triStateMask = 0;

	// This is just like SaveData.. collect the state of all the checks.	
	for ( int i = 0; i < m_CheckListItems.Count(); i++ )
	{
		CheckListItem currentItem = m_CheckListItems.Element( i );
		
		// If multiple of the selected entities have a different value for this flag,
		// note that. The entity page will use triStateMask to denote flags that
		// it should leave alone.
		if ( m_CheckList.GetCheck(i) == 2 )
			triStateMask |= currentItem.nItemBit;
		else if ( m_CheckList.GetCheck( i ) )
			bitsSet |= currentItem.nItemBit;
	}

	m_pEntityPage->OnUpdateSpawnFlags( triStateMask, bitsSet );    
}

void COP_Flags::OnSize( UINT nType, int cx, int cy )
{
	m_AnchorMgr.OnSize();
}