Counter Strike : Global Offensive Source Code
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.

363 lines
9.8 KiB

  1. //====== Copyright � 1996-2005, 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. // Gets/Sets the attribute value from a string
  151. //-----------------------------------------------------------------------------
  152. void CBaseAttributePanel::SetAttributeValueFromString( const char *pString )
  153. {
  154. if ( m_nArrayIndex < 0 )
  155. {
  156. GetPanelElement()->SetValueFromString( m_szAttributeName, pString );
  157. }
  158. else
  159. {
  160. CDmrGenericArray array( GetPanelElement(), m_szAttributeName );
  161. array.SetFromString( m_nArrayIndex, pString );
  162. }
  163. }
  164. bool CBaseAttributePanel::GetAttributeValueAsString( char *pBuf, int nLength )
  165. {
  166. CDmElement *pElement = m_hObject.Get();
  167. if ( !pElement )
  168. {
  169. *pBuf = '\0';
  170. return false;
  171. }
  172. if ( m_nArrayIndex < 0 )
  173. {
  174. pElement->GetValueAsString( m_szAttributeName, pBuf, nLength );
  175. }
  176. else
  177. {
  178. CDmrGenericArray array( pElement, m_szAttributeName );
  179. array.GetAsString( m_nArrayIndex, pBuf, nLength );
  180. }
  181. return true;
  182. }
  183. CDmAttribute *CBaseAttributePanel::GetAttribute()
  184. {
  185. CDmElement *pElement = m_hObject.Get();
  186. if ( !pElement )
  187. return NULL;
  188. return pElement->GetAttribute( m_szAttributeName );
  189. }
  190. //-----------------------------------------------------------------------------
  191. // Helper to get/set the attribute value for elements
  192. //-----------------------------------------------------------------------------
  193. CDmElement *CBaseAttributePanel::GetAttributeValueElement()
  194. {
  195. return GetElement< CDmElement >( GetAttributeValue<DmElementHandle_t>( ) );
  196. }
  197. void CBaseAttributePanel::SetAttributeValueElement( CDmElement *pElement )
  198. {
  199. return SetAttributeValue( pElement->GetHandle() );
  200. }
  201. void CBaseAttributePanel::SetDirty( bool dirty )
  202. {
  203. SetFlag( DIRTY, dirty );
  204. InvalidateLayout( false, true );
  205. }
  206. void CBaseAttributePanel::SetColumnSize( Panel *panel, int width )
  207. {
  208. colinfo_t search;
  209. search.panel = panel;
  210. int idx = m_ColumnSize.Find( search );
  211. if ( idx == m_ColumnSize.InvalidIndex() )
  212. {
  213. idx = m_ColumnSize.Insert( search );
  214. }
  215. m_ColumnSize[ idx ].width = width;
  216. }
  217. int CBaseAttributePanel::GetSizeForColumn( Panel *panel )
  218. {
  219. colinfo_t search;
  220. search.panel = panel;
  221. int idx = m_ColumnSize.Find( search );
  222. if ( idx == m_ColumnSize.InvalidIndex() )
  223. {
  224. return 100;
  225. }
  226. return m_ColumnSize[ idx ].width;
  227. }
  228. void CBaseAttributePanel::GetPickerBounds( int *x, int *y, int *w, int *h )
  229. {
  230. int viewX, viewY, viewWidth, viewHeight;
  231. GetBounds( viewX, viewY, viewWidth, viewHeight );
  232. *x = ColumnBorderWidth;
  233. *w = PickerWidth;
  234. *y = MAX(0, ceil((viewHeight - PickerHeight) * 0.5)) + 1;
  235. *h = PickerHeight;
  236. }
  237. //-----------------------------------------------------------------------------
  238. // Creates a widget using editor attribute info
  239. //-----------------------------------------------------------------------------
  240. void CBaseAttributePanel::PerformLayout()
  241. {
  242. BaseClass::PerformLayout();
  243. vgui::Panel *valuePanel = GetDataPanel();
  244. if ( valuePanel && HasFlag( HIDEVALUE ) )
  245. {
  246. valuePanel->SetVisible( false );
  247. }
  248. vgui::Panel *typePanel = m_pType;
  249. if ( typePanel && HasFlag( HIDETYPE ) )
  250. {
  251. typePanel->SetVisible( false );
  252. }
  253. int viewWidth, viewHeight;
  254. GetSize( viewWidth, viewHeight );
  255. if( typePanel->IsVisible() && valuePanel->IsVisible() )
  256. {
  257. valuePanel->SetBounds(
  258. FirstColumnWidth,
  259. 1,
  260. viewWidth - FirstColumnWidth - ColumnBorderWidth - TypeColumnWidth - ColumnBorderWidth,
  261. viewHeight );
  262. typePanel->SetBounds(
  263. viewWidth - TypeColumnWidth,
  264. 1,
  265. TypeColumnWidth,
  266. viewHeight );
  267. }
  268. else if( typePanel->IsVisible() )
  269. {
  270. typePanel->SetBounds( FirstColumnWidth, 1, viewWidth - FirstColumnWidth - ColumnBorderWidth - ColumnBorderWidth, viewHeight);
  271. }
  272. else if( valuePanel->IsVisible() )
  273. {
  274. valuePanel->SetBounds( FirstColumnWidth, 1, viewWidth - FirstColumnWidth - ColumnBorderWidth - ColumnBorderWidth, viewHeight);
  275. }
  276. }
  277. void CBaseAttributePanel::OnApplyChanges()
  278. {
  279. Assert( !IsAutoApply() );
  280. Apply();
  281. SetDirty(false);
  282. }
  283. void CBaseAttributePanel::OnRefresh()
  284. {
  285. Refresh();
  286. }
  287. void CBaseAttributePanel::OnCreateDragData( KeyValues *msg )
  288. {
  289. if ( GetPanelElement() )
  290. {
  291. msg->SetInt( "root", GetPanelElement() ? GetPanelElement()->GetHandle() : DMELEMENT_HANDLE_INVALID );
  292. msg->SetString( "type", g_pDataModel->GetAttributeNameForType( m_AttributeType ) );
  293. msg->SetString( "attributename", m_szAttributeName );
  294. if ( m_nArrayIndex >= 0 )
  295. {
  296. msg->SetInt( "arrayIndex", m_nArrayIndex );
  297. }
  298. if ( m_AttributeType != AT_ELEMENT && m_AttributeType != AT_ELEMENT_ARRAY )
  299. {
  300. char pTemp[512];
  301. GetAttributeValueAsString( pTemp, sizeof( pTemp ) );
  302. msg->SetString( "text", pTemp );
  303. }
  304. }
  305. }