summaryrefslogtreecommitdiff
path: root/public/dme_controls/dmecontrols_utils.h
blob: 5e3c17537b9564e4ca105462c8115987182393e6 (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//===========================================================================//

#ifndef DMECONTROLS_UTILS_H
#define DMECONTROLS_UTILS_H

#ifdef _WIN32
#pragma once
#endif

#include "tier1/KeyValues.h"
#include "datamodel/dmelement.h"
#include "datamodel/dmattribute.h"
#include "datamodel/dmattributevar.h"
#include "movieobjects/timeutils.h"

//-----------------------------------------------------------------------------
// Helper method to insert + extract DmElement handles into keyvalues
//-----------------------------------------------------------------------------
inline void SetElementKeyValue( KeyValues *pKeyValues, const char *pName, CDmElement *pElement )
{
	pKeyValues->SetInt( pName, pElement ? pElement->GetHandle() : DMELEMENT_HANDLE_INVALID );
}

template< class T >
T* GetElementKeyValue( KeyValues *pKeyValues, const char *pName )
{
	DmElementHandle_t h = (DmElementHandle_t)pKeyValues->GetInt( pName, DMELEMENT_HANDLE_INVALID );
	return GetElement<T>( h );
}

inline KeyValues *CreateElementKeyValues( const char *pName, const char *pKey, CDmElement *pElement )
{
	return new KeyValues( pName, pKey, pElement ? ( int )pElement->GetHandle() : DMELEMENT_HANDLE_INVALID );
}

inline void AddStandardElementKeys( KeyValues *pKeyValues, CDmElement *pElement )
{
	SetElementKeyValue( pKeyValues, "dmeelement", pElement );

	if ( pElement )
	{
		char buf[ 256 ];
		UniqueIdToString( pElement->GetId(), buf, sizeof( buf ) );
		pKeyValues->SetString( "text", buf );
		pKeyValues->SetString( "type", pElement->GetTypeString() );
	}
}


//-----------------------------------------------------------------------------
// Helper method to insert + extract DmeTime_t into keyvalues
//-----------------------------------------------------------------------------
inline void SetDmeTimeKeyValue( KeyValues *pKeyValues, const char *pName, DmeTime_t t )
{
	pKeyValues->SetInt( pName, t.GetTenthsOfMS() );
}

inline DmeTime_t GetDmeTimeKeyValue( KeyValues *pKeyValues, const char *pName, DmeTime_t defaultTime = DMETIME_ZERO )
{
	return DmeTime_t( pKeyValues->GetInt( pName, defaultTime.GetTenthsOfMS() ) );
}


inline bool ElementTree_IsArrayItem( KeyValues *itemData )
{
	return !itemData->IsEmpty( "arrayIndex" );
}

inline CDmAttribute *ElementTree_GetAttribute( KeyValues *itemData )
{
	CDmElement *pOwner = GetElementKeyValue< CDmElement >( itemData, "ownerelement" );
	if ( !pOwner )
		return NULL;

	const char *pAttributeName = itemData->GetString( "attributeName", "" );
	return pOwner->GetAttribute( pAttributeName );
}

inline DmAttributeType_t ElementTree_GetAttributeType( KeyValues *itemData )
{
	CDmElement *pOwner = GetElementKeyValue< CDmElement >( itemData, "ownerelement" );
	if ( !pOwner )
		return AT_UNKNOWN;

	const char *pAttributeName = itemData->GetString( "attributeName", "" );
	CDmAttribute *pAttribute = pOwner->GetAttribute( pAttributeName );
	if ( !pAttribute )
		return AT_UNKNOWN;

	return pAttribute->GetType();
}



inline bool ElementTree_GetDroppableItems( CUtlVector< KeyValues * >& msglist, const char *elementType, CUtlVector< CDmElement * >& list )
{
	int c = msglist.Count();
	for ( int i = 0; i < c; ++i )
	{	
		KeyValues *data = msglist[ i ];

		CDmElement *e = GetElementKeyValue<CDmElement>( data, "dmeelement" );
		if ( !e )
		{
			continue;
		}

		//if ( !e->IsA( elementType ) )
		//{
		//	continue;
		//}

		list.AddToTail( e );
	}

	return list.Count() != 0;
}

inline void ElementTree_RemoveListFromArray( CDmAttribute *pArrayAttribute, CUtlVector< CDmElement * >& list )
{
	CDmrElementArray<> array( pArrayAttribute );
	int c = array.Count();
	for ( int i = c - 1 ; i >= 0 ; --i )
	{
		CDmElement *element = array[ i ];
		if ( list.Find( element ) != list.InvalidIndex() )
		{
			array.Remove( i );
		}
	}
}

#endif // DMECONTROLS_UTILS_H