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.

370 lines
14 KiB

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