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.

380 lines
15 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. #include "dme_controls/AttributeWidgetFactory.h"
  9. #include "tier1/utldict.h"
  10. #include "tier1/KeyValues.h"
  11. #include "movieobjects/dmeeditortypedictionary.h"
  12. #include "dme_controls/AttributeTextEntry.h"
  13. #include "dme_controls/AttributeBooleanPanel.h"
  14. #include "dme_controls/AttributeFilePickerPanel.h"
  15. #include "dme_controls/AttributeBoolChoicePanel.h"
  16. #include "dme_controls/AttributeIntChoicePanel.h"
  17. #include "dme_controls/AttributeStringChoicePanel.h"
  18. #include "dme_controls/AttributeElementPanel.h"
  19. #include "dme_controls/AttributeElementPickerPanel.h"
  20. #include "dme_controls/AttributeMDLPickerPanel.h"
  21. #include "dme_controls/AttributeSequencePickerPanel.h"
  22. #include "dme_controls/AttributeSoundPickerPanel.h"
  23. #include "dme_controls/AttributeAssetPickerPanel.h"
  24. #include "dme_controls/AttributeShaderPickerPanel.h"
  25. #include "dme_controls/AttributeSurfacePropertyPickerPanel.h"
  26. #include "dme_controls/AttributeDetailTypePickerPanel.h"
  27. #include "dme_controls/AttributeColorPickerPanel.h"
  28. #include "dme_controls/AttributeInterpolatorChoicePanel.h"
  29. #include "dme_controls/attributesheetsequencepickerpanel.h"
  30. // memdbgon must be the last include file in a .cpp file!!!
  31. #include "tier0/memdbgon.h"
  32. //-----------------------------------------------------------------------------
  33. // Forward declaration
  34. //-----------------------------------------------------------------------------
  35. class CAttributeWidgetFactoryList;
  36. using namespace vgui;
  37. //-----------------------------------------------------------------------------
  38. // CAttributeWidgetFactoryList class definition
  39. //-----------------------------------------------------------------------------
  40. class CAttributeWidgetFactoryList : public IAttributeWidgetFactoryList
  41. {
  42. public:
  43. // Inherited from IAttributeWidgetFactoryList
  44. virtual IAttributeWidgetFactory *GetWidgetFactory( const char *pWidgetName );
  45. virtual IAttributeWidgetFactory *GetWidgetFactory( CDmElement *object, CDmAttribute *pAttribute, CDmeEditorTypeDictionary *pTypeDictionary );
  46. virtual IAttributeWidgetFactory *GetArrayWidgetFactory( CDmElement *object, CDmAttribute *pAttribute, CDmeEditorTypeDictionary *pTypeDictionary );
  47. virtual void ApplyChanges( vgui::Panel *pWidget, vgui::Panel *pSender = NULL );
  48. virtual void Refresh( vgui::Panel *pWidget, vgui::Panel *pSender = NULL );
  49. // Adds a widget to the factory
  50. void AddWidgetFactory( IAttributeWidgetFactory *pFactory, const char *pWidgetName );
  51. // Finds a widget factory by name
  52. IAttributeWidgetFactory *FindWidgetFactory( const char *pWidgetName );
  53. // Creates a widget using editor attribute info
  54. // vgui::Panel *CreateWidget( vgui::Panel *parent, CDmElement *obj, INotifyUI *pNotify, CDmeEditorAttributeInfo *pWidgetInfo, bool bAutoApply );
  55. private:
  56. CUtlDict< IAttributeWidgetFactory*, unsigned short > m_Factories;
  57. };
  58. //-----------------------------------------------------------------------------
  59. // Singleton instance
  60. //-----------------------------------------------------------------------------
  61. static CAttributeWidgetFactoryList *g_pWidgetFactoryFactoryList;
  62. IAttributeWidgetFactoryList *attributewidgetfactorylist;
  63. CAttributeWidgetFactoryList *GetWidgetFactoryManager()
  64. {
  65. if ( !g_pWidgetFactoryFactoryList )
  66. {
  67. g_pWidgetFactoryFactoryList = new CAttributeWidgetFactoryList;
  68. attributewidgetfactorylist = g_pWidgetFactoryFactoryList;
  69. }
  70. return g_pWidgetFactoryFactoryList;
  71. }
  72. //-----------------------------------------------------------------------------
  73. // Standard implementation of a widget factory
  74. //-----------------------------------------------------------------------------
  75. template < class T >
  76. class CAttributeWidgetFactory : public IAttributeWidgetFactory
  77. {
  78. public:
  79. CAttributeWidgetFactory( const char *pWidgetName )
  80. {
  81. GetWidgetFactoryManager()->AddWidgetFactory( this, pWidgetName );
  82. }
  83. // Backward compat
  84. virtual vgui::Panel *Create( vgui::Panel *pParent, const AttributeWidgetInfo_t &info )
  85. {
  86. CBaseAttributePanel *newPanel = new T( pParent, info );
  87. if ( newPanel )
  88. {
  89. newPanel->PostConstructor();
  90. }
  91. return newPanel;
  92. }
  93. };
  94. //-----------------------------------------------------------------------------
  95. // create all the AttributeWidgetFactorys
  96. //-----------------------------------------------------------------------------
  97. // An Attribute Widget Factory for: text entry
  98. static CAttributeWidgetFactory<CAttributeTextPanel> g_AttributeTextWidgetFactory( "text" );
  99. // An Attribute Widget Factory for: boolean entry
  100. static CAttributeWidgetFactory<CAttributeBooleanPanel> g_AttributeBooleanWidgetFactory( "boolean" );
  101. // An Attribute Widget Factory for: picking files
  102. static CAttributeWidgetFactory<CAttributeDmeFilePickerPanel> g_AttributeFilePickerWidgetFactory( "filepicker" );
  103. // An Attribute Widget Factory for: choosing integers
  104. static CAttributeWidgetFactory<CAttributeBoolChoicePanel> g_AttributeBoolChoiceWidgetFactory( "boolchoice" );
  105. // An Attribute Widget Factory for: choosing integers
  106. static CAttributeWidgetFactory<CAttributeIntChoicePanel> g_AttributeIntChoiceWidgetFactory( "intchoice" );
  107. // An Attribute Widget Factory for: choosing strings
  108. static CAttributeWidgetFactory<CAttributeStringChoicePanel> g_AttributeStringChoiceWidgetFactory( "stringchoice" );
  109. // An Attribute Widget Factory for: elements
  110. static CAttributeWidgetFactory<CAttributeElementPanel> g_AttributeElementWidgetFactory( "element" );
  111. // An Attribute Widget Factory for: picking elements
  112. static CAttributeWidgetFactory<CAttributeElementPickerPanel> g_AttributeElementPickerWidgetFactory( "elementchoice" );
  113. // An Attribute Widget Factory for: picking MDLs
  114. static CAttributeWidgetFactory<CAttributeMDLPickerPanel> g_AttributeMDLPickerWidgetFactory( "mdlpicker" );
  115. // An Attribute Widget Factory for: picking sequences
  116. static CAttributeWidgetFactory<CAttributeSequencePickerPanel> g_AttributeSequencePickerWidgetFactory( "sequencepicker" );
  117. // An Attribute Widget Factory for: picking sounds
  118. static CAttributeWidgetFactory<CAttributeSoundPickerPanel> g_AttributeSoundPickerWidgetFactory( "soundpicker" );
  119. // An Attribute Widget Factory for: picking bsps
  120. static CAttributeWidgetFactory<CAttributeBspPickerPanel> g_AttributeBspPickerWidgetFactory( "bsppicker" );
  121. // An Attribute Widget Factory for: picking vmts
  122. static CAttributeWidgetFactory<CAttributeVmtPickerPanel> g_AttributeVmtPickerWidgetFactory( "vmtpicker" );
  123. // An Attribute Widget Factory for: picking vtfs
  124. static CAttributeWidgetFactory<CAttributeVtfPickerPanel> g_AttributeVtfPickerWidgetFactory( "vtfpicker" );
  125. // An Attribute Widget Factory for: picking tgas
  126. static CAttributeWidgetFactory<CAttributeTgaPickerPanel> g_AttributeTgaPickerWidgetFactory( "tgapicker" );
  127. // An Attribute Widget Factory for: picking shaders
  128. static CAttributeWidgetFactory<CAttributeShaderPickerPanel> g_AttributeShaderPickerWidgetFactory( "shaderpicker" );
  129. // An Attribute Widget Factory for: picking surface properties
  130. static CAttributeWidgetFactory<CAttributeSurfacePropertyPickerPanel> g_AttributeSurfacePropertyPickerWidgetFactory( "surfacepropertypicker" );
  131. // An Attribute Widget Factory for: picking surface properties
  132. static CAttributeWidgetFactory<CAttributeColorPickerPanel> g_AttributeColorPickerWidgetFactory( "colorpicker" );
  133. // An Attribute Widget Factory for: picking avis
  134. static CAttributeWidgetFactory<CAttributeAviFilePickerPanel> g_AttributeAviPickerWidgetFactory( "avipicker" );
  135. // An Attribute Widget Factory for: picking sht
  136. static CAttributeWidgetFactory<CAttributeShtFilePickerPanel> g_AttributeShtPickerWidgetFactory( "shtpicker" );
  137. // An Attribute Widget Factory for: picking detail types
  138. static CAttributeWidgetFactory<CAttributeDetailTypePickerPanel> g_AttributeDetailTypePickerWidgetFactory( "detailtypepicker" );
  139. // An Attribute Widget Factory for: picking color correction lookup files
  140. static CAttributeWidgetFactory<CAttributeRawFilePickerPanel> g_AttributeRawPickerWidgetFactory( "rawpicker" );
  141. // An Attribute Widget Factory for: choosing interpolator types (left and right)
  142. static CAttributeWidgetFactory<CAttributeInterpolatorChoicePanel> g_AttributeInterpolatorChoiceWidgetFactory( "interpolatorchoice" );
  143. // An Attribute Widget Factory for: selecting sheet sequences
  144. static CAttributeWidgetFactory<CAttributeSheetSequencePickerPanel> g_AttributeSheetSequencePickerWidgetFactory( "sheetsequencepicker" );
  145. // Special-case for the second sequence in a double-sequence material
  146. static CAttributeWidgetFactory<CAttributeSheetSequencePickerPanel> g_AttributeSheetSequencePickerWidgetFactorySecond( "sheetsequencepicker_second" );
  147. //-----------------------------------------------------------------------------
  148. // Name-based widget factories
  149. //-----------------------------------------------------------------------------
  150. // ----------------------------------------------------------------------------
  151. // g_AttributeWidgetFactories
  152. // Purpose: a mapping of all attribute types to AttributeWidgetFactories
  153. struct DefaultAttributeFactoryEntry_t
  154. {
  155. int attributeType;
  156. IAttributeWidgetFactory *factory;
  157. };
  158. static DefaultAttributeFactoryEntry_t g_AttributeWidgetFactories[] =
  159. {
  160. { AT_UNKNOWN, NULL },
  161. { AT_ELEMENT, &g_AttributeElementWidgetFactory },
  162. { AT_INT, &g_AttributeTextWidgetFactory },
  163. { AT_FLOAT, &g_AttributeTextWidgetFactory },
  164. { AT_BOOL, &g_AttributeBooleanWidgetFactory },
  165. { AT_STRING, &g_AttributeTextWidgetFactory },
  166. { AT_VOID, &g_AttributeTextWidgetFactory },
  167. { AT_TIME, &g_AttributeTextWidgetFactory },
  168. { AT_COLOR, &g_AttributeColorPickerWidgetFactory },
  169. { AT_VECTOR2, &g_AttributeTextWidgetFactory },
  170. { AT_VECTOR3, &g_AttributeTextWidgetFactory },
  171. { AT_VECTOR4, &g_AttributeTextWidgetFactory },
  172. { AT_QANGLE, &g_AttributeTextWidgetFactory },
  173. { AT_QUATERNION, &g_AttributeTextWidgetFactory },
  174. { AT_VMATRIX, &g_AttributeTextWidgetFactory },
  175. { AT_ELEMENT_ARRAY, &g_AttributeTextWidgetFactory },
  176. { AT_INT_ARRAY, &g_AttributeTextWidgetFactory },
  177. { AT_FLOAT_ARRAY, &g_AttributeTextWidgetFactory },
  178. { AT_BOOL_ARRAY, &g_AttributeTextWidgetFactory },
  179. { AT_STRING_ARRAY, &g_AttributeTextWidgetFactory },
  180. { AT_VOID_ARRAY, &g_AttributeTextWidgetFactory },
  181. { AT_TIME_ARRAY, &g_AttributeTextWidgetFactory },
  182. { AT_COLOR_ARRAY, &g_AttributeTextWidgetFactory },
  183. { AT_VECTOR2_ARRAY, &g_AttributeTextWidgetFactory },
  184. { AT_VECTOR3_ARRAY, &g_AttributeTextWidgetFactory },
  185. { AT_VECTOR4_ARRAY, &g_AttributeTextWidgetFactory },
  186. { AT_QANGLE_ARRAY, &g_AttributeTextWidgetFactory },
  187. { AT_QUATERNION_ARRAY, &g_AttributeTextWidgetFactory },
  188. { AT_VMATRIX_ARRAY, &g_AttributeTextWidgetFactory },
  189. };
  190. //-----------------------------------------------------------------------------
  191. //
  192. // CAttributeWidgetFactoryList
  193. //
  194. //-----------------------------------------------------------------------------
  195. //-----------------------------------------------------------------------------
  196. // Adds a widget to the factory
  197. //-----------------------------------------------------------------------------
  198. void CAttributeWidgetFactoryList::AddWidgetFactory( IAttributeWidgetFactory *pFactory, const char *pWidgetName )
  199. {
  200. m_Factories.Insert( pWidgetName, pFactory );
  201. }
  202. //-----------------------------------------------------------------------------
  203. // Finds a widget factory by name
  204. //-----------------------------------------------------------------------------
  205. IAttributeWidgetFactory *CAttributeWidgetFactoryList::FindWidgetFactory( const char *pWidgetName )
  206. {
  207. unsigned short i = m_Factories.Find( pWidgetName );
  208. if ( i != m_Factories.InvalidIndex() )
  209. return m_Factories[i];
  210. return NULL;
  211. }
  212. //-----------------------------------------------------------------------------
  213. // Returns a factory requested by name
  214. //-----------------------------------------------------------------------------
  215. IAttributeWidgetFactory *CAttributeWidgetFactoryList::GetWidgetFactory( const char *pWidgetName )
  216. {
  217. return FindWidgetFactory( pWidgetName );
  218. }
  219. //-----------------------------------------------------------------------------
  220. // Returns a factory used to create widget for the attribute passed in
  221. //-----------------------------------------------------------------------------
  222. IAttributeWidgetFactory *CAttributeWidgetFactoryList::GetWidgetFactory( CDmElement *object,
  223. CDmAttribute *pAttribute, CDmeEditorTypeDictionary *pTypeDictionary )
  224. {
  225. if ( !object )
  226. return NULL;
  227. DmAttributeType_t attributeType = pAttribute->GetType();
  228. IAttributeWidgetFactory *pFactory = g_AttributeWidgetFactories[ attributeType ].factory;
  229. // Override behavior with editor info, if it exists
  230. if ( pTypeDictionary )
  231. {
  232. const char *pAttributeName = pAttribute->GetName();
  233. CDmeEditorAttributeInfo *pEditorInfo = pTypeDictionary->GetAttributeInfo( object, pAttributeName );
  234. if ( pEditorInfo )
  235. {
  236. if ( !pEditorInfo->m_bIsVisible )
  237. return NULL;
  238. if ( pEditorInfo->GetWidgetName() )
  239. {
  240. IAttributeWidgetFactory *pOverriddenFactory = g_pWidgetFactoryFactoryList->FindWidgetFactory( pEditorInfo->GetWidgetName() );
  241. if ( pOverriddenFactory )
  242. {
  243. pFactory = pOverriddenFactory;
  244. }
  245. }
  246. }
  247. }
  248. return pFactory;
  249. }
  250. //-----------------------------------------------------------------------------
  251. // Returns a factory used to create widgets for entries in an attribute array
  252. //-----------------------------------------------------------------------------
  253. IAttributeWidgetFactory *CAttributeWidgetFactoryList::GetArrayWidgetFactory( CDmElement *object,
  254. CDmAttribute *pAttribute, CDmeEditorTypeDictionary *pTypeDictionary )
  255. {
  256. if ( !object )
  257. return NULL;
  258. DmAttributeType_t attributeType = ArrayTypeToValueType( pAttribute->GetType() );
  259. IAttributeWidgetFactory *pFactory = g_AttributeWidgetFactories[ attributeType ].factory;
  260. // Override behavior with editor info, if it exists
  261. if ( pTypeDictionary )
  262. {
  263. CDmeEditorAttributeInfo *pEditorInfo = pTypeDictionary->GetAttributeArrayInfo( object, pAttribute->GetName() );
  264. if ( pEditorInfo )
  265. {
  266. if ( !pEditorInfo->m_bIsVisible )
  267. return NULL;
  268. if ( pEditorInfo->GetWidgetName() )
  269. {
  270. IAttributeWidgetFactory *pOverriddenFactory = g_pWidgetFactoryFactoryList->FindWidgetFactory( pEditorInfo->GetWidgetName() );
  271. if ( pOverriddenFactory )
  272. {
  273. pFactory = pOverriddenFactory;
  274. }
  275. }
  276. }
  277. }
  278. return pFactory;
  279. }
  280. //-----------------------------------------------------------------------------
  281. // Applies changes to a widget
  282. //-----------------------------------------------------------------------------
  283. void CAttributeWidgetFactoryList::ApplyChanges( vgui::Panel *pWidget, vgui::Panel *pSender )
  284. {
  285. CBaseAttributePanel *pPanel = dynamic_cast< CBaseAttributePanel *>( pWidget );
  286. if ( pPanel && pPanel->GetDirty() )
  287. {
  288. Assert( !pPanel->IsAutoApply() );
  289. vgui::ipanel()->SendMessage( pWidget->GetVPanel(), new KeyValues( "ApplyChanges" ), pSender ? pSender->GetVPanel() : NULL );
  290. }
  291. }
  292. //-----------------------------------------------------------------------------
  293. // Refreshes a widget when attributes change
  294. //-----------------------------------------------------------------------------
  295. void CAttributeWidgetFactoryList::Refresh( vgui::Panel *pWidget, vgui::Panel *pSender )
  296. {
  297. if ( pWidget )
  298. {
  299. vgui::ipanel()->SendMessage( pWidget->GetVPanel(), new KeyValues( "Refresh" ), pSender ? pWidget->GetVPanel() : NULL );
  300. }
  301. }