Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

365 lines
9.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: base class for all element attribute panels
  4. // An attribute panel is a one line widget that can be used by a list
  5. // or tree control.
  6. //
  7. // $NoKeywords: $
  8. //
  9. //=============================================================================//
  10. #include "dme_controls/BaseAttributePanel.h"
  11. #include "dme_controls/attributewidgetfactory.h"
  12. #include "tier1/KeyValues.h"
  13. #include "vgui_controls/Label.h"
  14. #include "movieobjects/dmeeditortypedictionary.h"
  15. #include "dme_controls/inotifyui.h"
  16. using namespace vgui;
  17. //-----------------------------------------------------------------------------
  18. // Lessfunc for columns.
  19. //-----------------------------------------------------------------------------
  20. bool CBaseAttributePanel::ColInfoLessFunc( const CBaseAttributePanel::colinfo_t& lhs, const CBaseAttributePanel::colinfo_t& rhs )
  21. {
  22. return lhs.panel < rhs.panel;
  23. }
  24. //-----------------------------------------------------------------------------
  25. // CBaseAttributePanel constructor
  26. //-----------------------------------------------------------------------------
  27. CBaseAttributePanel::CBaseAttributePanel( vgui::Panel *parent, const AttributeWidgetInfo_t &info ) :
  28. BaseClass( parent, info.m_pAttributeName ),
  29. m_pType( 0 ),
  30. m_hObject( info.m_pElement ),
  31. m_hEditorInfo( info.m_pEditorInfo ),
  32. m_hEditorTypeDict( info.m_pEditorTypeDictionary ),
  33. m_pNotify( info.m_pNotify ),
  34. m_nArrayIndex( info.m_nArrayIndex ),
  35. m_ColumnSize( 0, 0, ColInfoLessFunc )
  36. {
  37. Assert( info.m_pElement );
  38. InitializeFlags( info );
  39. Assert( info.m_pAttributeName );
  40. Q_strncpy( m_szAttributeName, info.m_pAttributeName, sizeof( m_szAttributeName ) );
  41. m_pType = new Label( this, "AttributeType", "" );
  42. SetColumnSize( m_pType, 100 );
  43. CDmAttribute *pAttribute = info.m_pElement->GetAttribute( info.m_pAttributeName );
  44. m_AttributeType = pAttribute ? pAttribute->GetType() : AT_UNKNOWN;
  45. if ( m_nArrayIndex >= 0 )
  46. {
  47. m_AttributeType = ArrayTypeToValueType( m_AttributeType );
  48. }
  49. m_pType->SetText( g_pDataModel->GetAttributeNameForType( m_AttributeType ) );
  50. m_hFont = NULL;
  51. // These are draggable
  52. SetDragEnabled( true );
  53. }
  54. //-----------------------------------------------------------------------------
  55. // This only exists so every class always can chain PostConstructors
  56. //-----------------------------------------------------------------------------
  57. void CBaseAttributePanel::PostConstructor()
  58. {
  59. }
  60. //-----------------------------------------------------------------------------
  61. // Initializes flags from the attribute editor info
  62. //-----------------------------------------------------------------------------
  63. void CBaseAttributePanel::InitializeFlags( const AttributeWidgetInfo_t &info )
  64. {
  65. m_nFlags = 0;
  66. if ( info.m_pEditorInfo )
  67. {
  68. if ( info.m_pEditorInfo->m_bHideType )
  69. {
  70. m_nFlags |= HIDETYPE;
  71. }
  72. if ( info.m_pEditorInfo->m_bHideValue )
  73. {
  74. m_nFlags |= HIDEVALUE;
  75. }
  76. if ( info.m_pEditorInfo->m_bIsReadOnly )
  77. {
  78. m_nFlags |= READONLY;
  79. }
  80. }
  81. CDmAttribute *pAttribute = info.m_pElement->GetAttribute( info.m_pAttributeName );
  82. if ( pAttribute && pAttribute->IsFlagSet( FATTRIB_READONLY ) )
  83. {
  84. m_nFlags |= READONLY;
  85. }
  86. if ( info.m_bAutoApply )
  87. {
  88. m_nFlags |= AUTOAPPLY;
  89. }
  90. }
  91. //-----------------------------------------------------------------------------
  92. // Returns the editor info
  93. //-----------------------------------------------------------------------------
  94. CDmeEditorTypeDictionary *CBaseAttributePanel::GetEditorTypeDictionary()
  95. {
  96. return m_hEditorTypeDict;
  97. }
  98. CDmeEditorAttributeInfo *CBaseAttributePanel::GetEditorInfo()
  99. {
  100. return m_hEditorInfo;
  101. }
  102. //-----------------------------------------------------------------------------
  103. // Does the element have the attribute we're attempting to reference?
  104. //-----------------------------------------------------------------------------
  105. bool CBaseAttributePanel::HasAttribute() const
  106. {
  107. return GetPanelElement()->HasAttribute( m_szAttributeName );
  108. }
  109. //-----------------------------------------------------------------------------
  110. // Returns the attribute array count
  111. //-----------------------------------------------------------------------------
  112. int CBaseAttributePanel::GetAttributeArrayCount() const
  113. {
  114. CDmrGenericArrayConst array( GetPanelElement(), m_szAttributeName );
  115. return array.IsValid() ? array.Count() : -1;
  116. }
  117. //-----------------------------------------------------------------------------
  118. // Sets the font
  119. //-----------------------------------------------------------------------------
  120. void CBaseAttributePanel::SetFont( HFont font )
  121. {
  122. m_hFont = font;
  123. m_pType->SetFont(font);
  124. }
  125. //-----------------------------------------------------------------------------
  126. // Applies scheme settings
  127. //-----------------------------------------------------------------------------
  128. void CBaseAttributePanel::ApplySchemeSettings( IScheme *pScheme )
  129. {
  130. BaseClass::ApplySchemeSettings( pScheme );
  131. // set the color of the "type" column
  132. m_pType->SetFgColor( Color ( 160, 160, 160, 255 ) );
  133. if ( GetDirty() )
  134. {
  135. SetBgColor( pScheme->GetColor( "AttributeWidget.DirtyBgColor", Color( 100, 100, 200, 63 ) ) );
  136. }
  137. else
  138. {
  139. SetBgColor( pScheme->GetColor( "Panel.BgColor", Color( 0, 0, 0, 0 ) ) );
  140. }
  141. HFont font = pScheme->GetFont( "DmePropertyVerySmall", IsProportional() );
  142. // m_pType->SetFont(font);
  143. if ( !m_hFont )
  144. {
  145. m_hFont = font;
  146. }
  147. SetFont( m_hFont );
  148. }
  149. //-----------------------------------------------------------------------------
  150. // Returns the panel element
  151. //-----------------------------------------------------------------------------
  152. CDmElement *CBaseAttributePanel::GetPanelElement()
  153. {
  154. return m_hObject;
  155. }
  156. const CDmElement *CBaseAttributePanel::GetPanelElement() const
  157. {
  158. return m_hObject;
  159. }
  160. //-----------------------------------------------------------------------------
  161. // Gets/Sets the attribute value from a string
  162. //-----------------------------------------------------------------------------
  163. void CBaseAttributePanel::SetAttributeValueFromString( const char *pString )
  164. {
  165. if ( m_nArrayIndex < 0 )
  166. {
  167. GetPanelElement()->SetValueFromString( m_szAttributeName, pString );
  168. }
  169. else
  170. {
  171. CDmrGenericArray array( GetPanelElement(), m_szAttributeName );
  172. array.SetFromString( m_nArrayIndex, pString );
  173. }
  174. }
  175. const char *CBaseAttributePanel::GetAttributeValueAsString( char *pBuf, int nLength )
  176. {
  177. if ( m_nArrayIndex < 0 )
  178. {
  179. GetPanelElement()->GetValueAsString( m_szAttributeName, pBuf, nLength );
  180. }
  181. else
  182. {
  183. CDmrGenericArray array( GetPanelElement(), m_szAttributeName );
  184. array.GetAsString( m_nArrayIndex, pBuf, nLength );
  185. }
  186. return pBuf;
  187. }
  188. //-----------------------------------------------------------------------------
  189. // Helper to get/set the attribute value for elements
  190. //-----------------------------------------------------------------------------
  191. CDmElement *CBaseAttributePanel::GetAttributeValueElement()
  192. {
  193. return GetElement< CDmElement >( GetAttributeValue<DmElementHandle_t>( ) );
  194. }
  195. void CBaseAttributePanel::SetAttributeValueElement( CDmElement *pElement )
  196. {
  197. return SetAttributeValue( pElement->GetHandle() );
  198. }
  199. void CBaseAttributePanel::SetDirty( bool dirty )
  200. {
  201. SetFlag( DIRTY, dirty );
  202. InvalidateLayout( false, true );
  203. }
  204. void CBaseAttributePanel::SetColumnSize( Panel *panel, int width )
  205. {
  206. colinfo_t search;
  207. search.panel = panel;
  208. int idx = m_ColumnSize.Find( search );
  209. if ( idx == m_ColumnSize.InvalidIndex() )
  210. {
  211. idx = m_ColumnSize.Insert( search );
  212. }
  213. m_ColumnSize[ idx ].width = width;
  214. }
  215. int CBaseAttributePanel::GetSizeForColumn( Panel *panel )
  216. {
  217. colinfo_t search;
  218. search.panel = panel;
  219. int idx = m_ColumnSize.Find( search );
  220. if ( idx == m_ColumnSize.InvalidIndex() )
  221. {
  222. return 100;
  223. }
  224. return m_ColumnSize[ idx ].width;
  225. }
  226. //-----------------------------------------------------------------------------
  227. // Creates a widget using editor attribute info
  228. //-----------------------------------------------------------------------------
  229. void CBaseAttributePanel::PerformLayout()
  230. {
  231. BaseClass::PerformLayout();
  232. CUtlVector< Panel * > vispanels;
  233. if ( HasFlag( HIDETYPE ) )
  234. {
  235. m_pType->SetVisible( false );
  236. }
  237. else
  238. {
  239. vispanels.AddToTail( m_pType );
  240. }
  241. vgui::Panel *dataPanel = GetDataPanel();
  242. if ( dataPanel )
  243. {
  244. if ( HasFlag( HIDEVALUE ) )
  245. {
  246. dataPanel->SetVisible( false );
  247. }
  248. else
  249. {
  250. vispanels.AddToTail( dataPanel );
  251. }
  252. }
  253. int c = vispanels.Count();
  254. Assert( c >= 0 );
  255. if ( c == 0 )
  256. {
  257. return;
  258. }
  259. int w, h;
  260. GetSize( w, h );
  261. int x = 1;
  262. int y = 0;
  263. w-= 2;
  264. for ( int i = 0; i < c; ++i )
  265. {
  266. Panel *panel = vispanels[ i ];
  267. int width = GetSizeForColumn( panel );
  268. if ( i == c - 1 )
  269. {
  270. width = w - x;
  271. }
  272. panel->SetBounds( x, y, width, h );
  273. x += width;
  274. }
  275. }
  276. void CBaseAttributePanel::OnApplyChanges()
  277. {
  278. Assert( !IsAutoApply() );
  279. Apply();
  280. SetDirty(false);
  281. }
  282. void CBaseAttributePanel::OnRefresh()
  283. {
  284. Refresh();
  285. }
  286. void CBaseAttributePanel::OnCreateDragData( KeyValues *msg )
  287. {
  288. if ( GetPanelElement() )
  289. {
  290. msg->SetInt( "root", GetPanelElement() ? GetPanelElement()->GetHandle() : DMELEMENT_HANDLE_INVALID );
  291. msg->SetString( "type", g_pDataModel->GetAttributeNameForType( m_AttributeType ) );
  292. msg->SetString( "attributename", m_szAttributeName );
  293. if ( m_nArrayIndex >= 0 )
  294. {
  295. msg->SetInt( "arrayIndex", m_nArrayIndex );
  296. }
  297. if ( m_AttributeType != AT_ELEMENT && m_AttributeType != AT_ELEMENT_ARRAY )
  298. {
  299. char pTemp[512];
  300. GetAttributeValueAsString( pTemp, sizeof( pTemp ) );
  301. msg->SetString( "text", pTemp );
  302. }
  303. }
  304. }