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.

275 lines
7.0 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. #ifndef BASEATTRIBUTEPANEL_H
  11. #define BASEATTRIBUTEPANEL_H
  12. #ifdef _WIN32
  13. #pragma once
  14. #endif
  15. #include "datamodel/dmattribute.h"
  16. #include "vgui_controls/Panel.h"
  17. #include "datamodel/dmehandle.h"
  18. //-----------------------------------------------------------------------------
  19. // Forward declarations
  20. //-----------------------------------------------------------------------------
  21. class CDmElement;
  22. class IDmNotify;
  23. class IElementPropertiesChoices;
  24. struct AttributeWidgetInfo_t;
  25. class CDmeEditorAttributeInfo;
  26. class CDmeEditorTypeDictionary;
  27. namespace vgui
  28. {
  29. class Label;
  30. }
  31. using namespace vgui;
  32. //-----------------------------------------------------------------------------
  33. // CBaseAttributePanel
  34. //-----------------------------------------------------------------------------
  35. class CBaseAttributePanel : public vgui::Panel
  36. {
  37. DECLARE_CLASS_SIMPLE( CBaseAttributePanel, vgui::Panel );
  38. public:
  39. CBaseAttributePanel( vgui::Panel *pParent, const AttributeWidgetInfo_t &info );
  40. virtual void PostConstructor();
  41. virtual void PerformLayout();
  42. virtual void SetFont( HFont font );
  43. virtual void ApplySchemeSettings( IScheme *pScheme );
  44. virtual void OnCreateDragData( KeyValues *msg );
  45. void SetDirty( bool dirty );
  46. bool GetDirty() const;
  47. bool IsAutoApply() const;
  48. // Sets/gets the attribute value
  49. template< class T >
  50. void SetAttributeValue( const T& value );
  51. void SetAttributeValue( const char *pValue );
  52. template< class T >
  53. const T& GetAttributeValue( );
  54. // Helper to get/set the attribute value for elements
  55. CDmElement *GetAttributeValueElement();
  56. void SetAttributeValueElement( CDmElement *pElement );
  57. void SetAttributeValueFromString( const char *pString );
  58. const char *GetAttributeValueAsString( char *pBuf, int nLength );
  59. // Returns the attribute type to edit
  60. DmAttributeType_t GetAttributeType() const;
  61. // Returns the editor info
  62. CDmeEditorTypeDictionary *GetEditorTypeDictionary();
  63. CDmeEditorAttributeInfo *GetEditorInfo();
  64. // Call this when the data changed
  65. IDmNotify *GetNotify();
  66. protected:
  67. enum
  68. {
  69. HIDETYPE = 0x01,
  70. HIDEVALUE = 0x02,
  71. READONLY = 0x04,
  72. DIRTY = 0x08,
  73. AUTOAPPLY = 0x10,
  74. };
  75. // Inherited classes must implement this
  76. virtual Panel *GetDataPanel() = 0;
  77. virtual void Apply() = 0;
  78. virtual void Refresh() = 0;
  79. // Methods to get/set column size
  80. int GetSizeForColumn( Panel *panel );
  81. void SetColumnSize( Panel *panel, int width );
  82. // Returns the element being edited by the panel
  83. CDmElement *GetPanelElement();
  84. const CDmElement *GetPanelElement() const;
  85. // Returns the attribute name
  86. const char* GetAttributeName() const;
  87. // Does the element have the attribute we're attempting to reference?
  88. bool HasAttribute() const;
  89. // Returns the attribute array count
  90. int GetAttributeArrayCount() const;
  91. // Are we editing an entry in an attribute array?
  92. bool IsArrayEntry() const;
  93. // Is a particular flag set?
  94. bool HasFlag( int flagMask ) const;
  95. private:
  96. struct colinfo_t
  97. {
  98. Panel *panel;
  99. int width;
  100. };
  101. // Set a flag
  102. void SetFlag( int flagMask, bool bOn );
  103. // Used to sort the column list
  104. static bool ColInfoLessFunc( const colinfo_t& lhs, const colinfo_t& rhs );
  105. // Initializes flags from the attribute editor info
  106. void InitializeFlags( const AttributeWidgetInfo_t &info );
  107. // Called when the OK / Apply button is pressed. Changed data should be written into document.
  108. MESSAGE_FUNC( OnApplyChanges, "ApplyChanges" );
  109. MESSAGE_FUNC( OnRefresh, "Refresh" );
  110. protected:
  111. Label *m_pType;
  112. private:
  113. CDmeHandle< CDmElement > m_hObject;
  114. CDmeHandle< CDmeEditorAttributeInfo > m_hEditorInfo;
  115. CDmeHandle< CDmeEditorTypeDictionary > m_hEditorTypeDict;
  116. char m_szAttributeName[ 256 ];
  117. int m_nArrayIndex;
  118. DmAttributeType_t m_AttributeType;
  119. IDmNotify *m_pNotify;
  120. int m_nFlags;
  121. CUtlRBTree< colinfo_t, int > m_ColumnSize;
  122. HFont m_hFont;
  123. };
  124. //-----------------------------------------------------------------------------
  125. // Inline methods
  126. //-----------------------------------------------------------------------------
  127. inline bool CBaseAttributePanel::HasFlag( int flagMask ) const
  128. {
  129. return ( m_nFlags & flagMask ) ? true : false;
  130. }
  131. inline void CBaseAttributePanel::SetFlag( int flagMask, bool bOn )
  132. {
  133. if ( bOn )
  134. {
  135. m_nFlags |= flagMask;
  136. }
  137. else
  138. {
  139. m_nFlags &= ~flagMask;
  140. }
  141. }
  142. inline bool CBaseAttributePanel::GetDirty() const
  143. {
  144. return HasFlag( DIRTY );
  145. }
  146. inline bool CBaseAttributePanel::IsAutoApply() const
  147. {
  148. return HasFlag( AUTOAPPLY );
  149. }
  150. inline DmAttributeType_t CBaseAttributePanel::GetAttributeType() const
  151. {
  152. return m_AttributeType;
  153. }
  154. inline IDmNotify *CBaseAttributePanel::GetNotify()
  155. {
  156. return m_pNotify;
  157. }
  158. inline const char* CBaseAttributePanel::GetAttributeName() const
  159. {
  160. return m_szAttributeName;
  161. }
  162. inline bool CBaseAttributePanel::IsArrayEntry() const
  163. {
  164. return ( m_nArrayIndex >= 0 );
  165. }
  166. template< class T > inline const T& GetArrayAttributeValue( CDmElement *pElement, const char *pAttribute, int nArrayIndex )
  167. {
  168. const CDmrArray<T> array( pElement, pAttribute );
  169. return array[ nArrayIndex ];
  170. }
  171. template<> inline const DmElementHandle_t& GetArrayAttributeValue<DmElementHandle_t>( CDmElement *pElement, const char *pAttribute, int nArrayIndex )
  172. {
  173. const CDmrElementArray<> array( pElement, pAttribute );
  174. return array.GetHandle( nArrayIndex );
  175. }
  176. template< class T > inline void SetArrayAttributeValue( CDmElement *pElement, const char *pAttribute, int nArrayIndex, const T& value )
  177. {
  178. CDmrArray<T> array( pElement, pAttribute );
  179. array.Set( nArrayIndex, value );
  180. }
  181. template<> inline void SetArrayAttributeValue<DmElementHandle_t>( CDmElement *pElement, const char *pAttribute, int nArrayIndex, const DmElementHandle_t& value )
  182. {
  183. CDmrElementArray<> array( pElement, pAttribute );
  184. array.SetHandle( nArrayIndex, value );
  185. }
  186. //-----------------------------------------------------------------------------
  187. // Sets/gets the attribute value
  188. //-----------------------------------------------------------------------------
  189. template< class T >
  190. void CBaseAttributePanel::SetAttributeValue( const T& value )
  191. {
  192. if ( !IsArrayEntry() )
  193. {
  194. GetPanelElement()->SetValue( m_szAttributeName, value );
  195. }
  196. else
  197. {
  198. SetArrayAttributeValue<T>( GetPanelElement(), m_szAttributeName, m_nArrayIndex, value );
  199. }
  200. }
  201. inline void CBaseAttributePanel::SetAttributeValue( const char *pValue )
  202. {
  203. if ( !IsArrayEntry() )
  204. {
  205. GetPanelElement()->SetValue( m_szAttributeName, pValue );
  206. }
  207. else
  208. {
  209. SetArrayAttributeValue<CUtlString>( GetPanelElement(), m_szAttributeName, m_nArrayIndex, pValue );
  210. }
  211. }
  212. template< class T >
  213. const T& CBaseAttributePanel::GetAttributeValue( )
  214. {
  215. if ( !IsArrayEntry() )
  216. return GetPanelElement()->GetValue<T>( m_szAttributeName );
  217. return GetArrayAttributeValue<T>( GetPanelElement(), m_szAttributeName, m_nArrayIndex );
  218. }
  219. #endif // BASEATTRIBUTEPANEL_H