summaryrefslogtreecommitdiff
path: root/vgui2/dme_controls/AttributeElementPickerPanel.cpp
blob: 44725d96b6376bf9003581847cdd584555b9f39e (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
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: 
//
// $NoKeywords: $
//
//=============================================================================//

#include "dme_controls/AttributeElementPickerPanel.h"
#include "dme_controls/AttributeTextEntry.h"
#include "dme_controls/AttributeWidgetFactory.h"
#include "datamodel/dmelement.h"
#include "tier1/KeyValues.h"
#include "vgui_controls/Button.h"
#include "vgui_controls/ComboBox.h"
#include "dme_controls/dmepicker.h"
#include "movieobjects/dmeeditortypedictionary.h"
#include "dme_controls/inotifyui.h"
#include "dme_controls/dmecontrols.h"
#include "dme_controls/dmecontrols_utils.h"

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


using namespace vgui;

// ----------------------------------------------------------------------------
CAttributeElementPickerPanel::CAttributeElementPickerPanel( vgui::Panel *parent, const AttributeWidgetInfo_t &info ) :
BaseClass( parent, info )
{
	m_hEdit = new vgui::Button( this, "Open", "...", this, "open" );

	m_pData = new CAttributeTextEntry( this, "AttributeValue" );
	m_pData->SetEnabled( !HasFlag( READONLY ) );
	m_pData->AddActionSignalTarget(this);
	m_pType->SetText( "element" );

	m_bShowMemoryUsage = info.m_bShowMemoryUsage;
}

void CAttributeElementPickerPanel::PostConstructor()
{
	Refresh();
}

// ----------------------------------------------------------------------------
vgui::Panel *CAttributeElementPickerPanel::GetDataPanel()
{
	return static_cast< vgui::Panel * >( m_pData );
}

void CAttributeElementPickerPanel::Apply()
{
	// FIXME: Implement when needed
	Assert( 0 );
}

void CAttributeElementPickerPanel::Refresh()
{
	char elemText[ 512 ];
	elemText[0] = 0;

	CDmElement *element = NULL;
	if ( !GetEditorInfo() || !GetEditorInfo()->GetValue<bool>( "hideText" ) )
	{
		if ( HasAttribute( ) )
		{
			element = GetAttributeValueElement( );
		}
		else
		{
			element = GetPanelElement();
		}
	}

	if ( element )
	{
		char idstr[ 37 ];
		UniqueIdToString( element->GetId(), idstr, sizeof( idstr ) );
		if ( m_bShowMemoryUsage )
		{
			Q_snprintf( elemText, sizeof( elemText ), "%s %s %.3fMB", element->GetTypeString(), idstr, element->EstimateMemoryUsage() / float( 1 << 20 ) );
		}
		else
		{
			Q_snprintf( elemText, sizeof( elemText ), "%s %s", element->GetTypeString(), idstr );
		}
	}

	m_pData->SetText( elemText );
	m_pData->SetEditable( false );
}


//-----------------------------------------------------------------------------
// Called when it's time to show the Dme picker
//-----------------------------------------------------------------------------
void CAttributeElementPickerPanel::ShowPickerDialog()
{
	CDmeEditorChoicesInfo *pInfo = CastElement<CDmeEditorChoicesInfo>( GetEditorInfo() );
	if ( !pInfo )
		return;

	// FIXME: Sucky. Should we make GetElementChoiceList return a DmeHandleVec_t? 
	ElementChoiceList_t choices;
	CUtlVector< DmePickerInfo_t > vec;
	if ( ElementPropertiesChoices()->GetElementChoiceList( pInfo->GetChoiceType(), GetPanelElement(), GetAttributeName(), IsArrayEntry(), choices ) )
	{
		int c = choices.Count();
		vec.EnsureCapacity( c );
		for ( int i = 0; i < c; ++i )
		{
			int j = vec.AddToTail( );
			vec[j].m_hElement = choices[i].m_pValue->GetHandle();
			vec[j].m_pChoiceString = choices[i].m_pChoiceString;
		}
	}

	CDmePickerFrame *pDmePickerDialog = new CDmePickerFrame( this, "Select DME Element" );
	pDmePickerDialog->AddActionSignalTarget( this );
	pDmePickerDialog->DoModal( vec );
}


//-----------------------------------------------------------------------------
// Called by the dme picker dialog if a dme was selected
//-----------------------------------------------------------------------------
void CAttributeElementPickerPanel::OnDmeSelected( KeyValues *pKeyValues )
{
	// We're either going to get an activity or sequence name
	CDmElement *pElement = GetElementKeyValue< CDmElement >( pKeyValues, "dme" );
	SetAttributeValueElement( pElement );
	Refresh( );
}


//-----------------------------------------------------------------------------
// Handle commands
//-----------------------------------------------------------------------------
void CAttributeElementPickerPanel::OnCommand( char const *cmd )
{
	if ( !Q_stricmp( cmd, "open" ) )
	{
		ShowPickerDialog();
	}
	else
	{
		BaseClass::OnCommand( cmd );
	}
}


//-----------------------------------------------------------------------------
// Lay out the panel
//-----------------------------------------------------------------------------
void CAttributeElementPickerPanel::PerformLayout()
{
	BaseClass::PerformLayout();

	int x, y, w, h;
	m_pType->GetBounds( x, y, w, h );

	int inset = 25;
	m_pType->SetWide( w - inset );

	x += w;
	x -= inset;

	h -= 2;

	m_hEdit->SetBounds( x, y, inset, h );
}