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.

300 lines
9.5 KiB

  1. //====== Copyright � 1996-2005, Valve Corporation, All rights reserved. =======
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================
  6. #include "filesystem.h"
  7. #include "matsys_controls/vmtpicker.h"
  8. #include "matsys_controls/vmtpreviewpanel.h"
  9. #include "vgui_controls/Splitter.h"
  10. #include "vgui_controls/CheckButton.h"
  11. #include "vgui_controls/Slider.h"
  12. #include "vgui_controls/MenuButton.h"
  13. #include "vgui_controls/Panel.h"
  14. #include "matsys_controls/sheetsequencepanel.h"
  15. #include "bitmap/psheet.h"
  16. #include "keyvalues.h"
  17. // NOTE: This has to be the last file included!
  18. #include "tier0/memdbgon.h"
  19. //-----------------------------------------------------------------------------
  20. // VMT Preview Toolbar - controls for tweaking the VMT preview
  21. //-----------------------------------------------------------------------------
  22. CVMTPreviewToolbar::CVMTPreviewToolbar( vgui::Panel *parent, const char *panelName, CVMTPicker *parentpicker ) :
  23. BaseClass( parent, panelName ), m_pParentPicker( parentpicker )
  24. {
  25. vgui::CBoxSizer* pSizer = new vgui::CBoxSizer( vgui::ESLD_HORIZONTAL );
  26. SetSizer( pSizer );
  27. // buttons + controls
  28. vgui::SizerAddArgs_t buttonAddArgs = vgui::SizerAddArgs_t().FixedSize(-1,18).Padding(2);
  29. pSizer->AddSpacer( vgui::SizerAddArgs_t().Padding( 15 ) );
  30. m_pPrevSeqButton = new vgui::Button( this, "PrevSeqButton", "<", this );
  31. m_pPrevSeqButton->SetCommand( new KeyValues( "OnPrevSequence" ) );
  32. pSizer->AddPanel( m_pPrevSeqButton, buttonAddArgs );
  33. m_pSequenceSelection = new vgui::MenuButton( this, "SequenceSelection", "-" );
  34. pSizer->AddPanel( m_pSequenceSelection, vgui::SizerAddArgs_t().FixedSize(120,18).Padding(2) );
  35. m_pNextSeqButton = new vgui::Button( this, "NextSeqButton", ">", this );
  36. m_pNextSeqButton->SetCommand( new KeyValues( "OnNextSequence" ) );
  37. pSizer->AddPanel( m_pNextSeqButton, buttonAddArgs );
  38. m_pSequenceSelection_Second = new vgui::MenuButton( this, "SequenceSelection", "(Color)" );
  39. pSizer->AddPanel( m_pSequenceSelection_Second, buttonAddArgs );
  40. m_pSheetPreviewSpeed = new vgui::Slider( this, "SheetPreviewSpeed" );
  41. m_pSheetPreviewSpeed->SetRange( 5, 3000 );
  42. m_pSheetPreviewSpeed->SetValue( 750 );
  43. m_pSheetPreviewSpeed->AddActionSignalTarget( this );
  44. pSizer->AddPanel( m_pSheetPreviewSpeed, vgui::SizerAddArgs_t().FixedSize(150,18).Padding(2) );
  45. // preview panels
  46. m_pSheetPanel = new CSheetSequencePanel(this, "sheetpanel");
  47. m_pSheetPanel->AddActionSignalTarget( this );
  48. m_pSequenceSelection->SetMenu( m_pSheetPanel );
  49. m_pSheetPanel_Second = new CSheetSequencePanel(this, "sheetpanel_second");
  50. m_pSheetPanel_Second->SetSecondSequenceView( true );
  51. m_pSheetPanel_Second->AddActionSignalTarget( this );
  52. m_pSequenceSelection_Second->SetMenu( m_pSheetPanel_Second );
  53. UpdateToolbarGUI();
  54. }
  55. void CVMTPreviewToolbar::OnNextSequence( )
  56. {
  57. int nSeq = m_pParentPicker->GetCurrentSequence();
  58. nSeq = (nSeq + 1) % m_pParentPicker->GetSheetSequenceCount();
  59. m_pParentPicker->SetSelectedSequence( nSeq );
  60. UpdateToolbarGUI();
  61. }
  62. void CVMTPreviewToolbar::OnPrevSequence( )
  63. {
  64. int nSeq = m_pParentPicker->GetCurrentSequence();
  65. if ( nSeq == 0 )
  66. {
  67. nSeq = m_pParentPicker->GetSheetSequenceCount()-1;
  68. }
  69. else
  70. {
  71. nSeq = (nSeq - 1) % m_pParentPicker->GetSheetSequenceCount();
  72. }
  73. m_pParentPicker->SetSelectedSequence( nSeq );
  74. UpdateToolbarGUI();
  75. }
  76. void CVMTPreviewToolbar::OnSliderMoved( KeyValues *pData )
  77. {
  78. vgui::Panel *pPanel = reinterpret_cast<vgui::Panel *>( const_cast<KeyValues*>(pData)->GetPtr("panel") );
  79. if ( pPanel == m_pSheetPreviewSpeed )
  80. {
  81. m_pParentPicker->SetSheetPreviewSpeed( m_pSheetPreviewSpeed->GetValue() );
  82. }
  83. }
  84. void CVMTPreviewToolbar::PopulateSequenceMenu( vgui::Menu *menu )
  85. {
  86. menu->DeleteAllItems();
  87. int nSequences = m_pParentPicker->GetSheetSequenceCount();
  88. for ( int i = 0; i < nSequences; ++i )
  89. {
  90. char sz[64];
  91. Q_snprintf( sz, sizeof( sz ), "Sequence %d", i );
  92. menu->AddMenuItem( "seqitem", sz, new KeyValues( "OnSelectSequence", "nSequenceNumber", i ), this );
  93. }
  94. }
  95. int CVMTPreviewToolbar::GetSequenceMenuItemCount( )
  96. {
  97. return m_pParentPicker->GetSheetSequenceCount();
  98. }
  99. void CVMTPreviewToolbar::OnSelectSequence( int nSequenceNumber )
  100. {
  101. m_pParentPicker->SetSelectedSequence(nSequenceNumber);
  102. UpdateToolbarGUI();
  103. }
  104. void CVMTPreviewToolbar::OnSheetSequenceSelected( KeyValues *pData )
  105. {
  106. if ( pData->GetBool("bIsSecondSequence") )
  107. {
  108. m_pParentPicker->SetSelectedSecondarySequence( pData->GetInt("nSequenceNumber") );
  109. }
  110. else
  111. {
  112. m_pParentPicker->SetSelectedSequence( pData->GetInt("nSequenceNumber") );
  113. }
  114. UpdateToolbarGUI();
  115. }
  116. void CVMTPreviewToolbar::ApplySchemeSettings(vgui::IScheme *pScheme)
  117. {
  118. BaseClass::ApplySchemeSettings(pScheme);
  119. m_pSequenceSelection->SetFont( pScheme->GetFont( "DefaultVerySmall" ) );
  120. }
  121. void CVMTPreviewToolbar::UpdateToolbarGUI()
  122. {
  123. int nSequences = m_pParentPicker->GetSheetSequenceCount();
  124. int nCurrentSequence = m_pParentPicker->GetCurrentSequence();
  125. int nRealSeqNumber = m_pParentPicker->GetRealSequenceNumber();
  126. bool bSecondPicker = CSheetExtended::IsMaterialSeparateAlphaColorMaterial( m_pParentPicker->GetMaterial() );
  127. if ( nSequences == 0 )
  128. {
  129. m_pSequenceSelection->SetText( "No Sequences" );
  130. m_pSequenceSelection->SetEnabled(false);
  131. m_pNextSeqButton->SetEnabled(false);
  132. m_pPrevSeqButton->SetEnabled(false);
  133. m_pSequenceSelection_Second->SetVisible(false);
  134. m_pSheetPanel->SetFromMaterial( NULL );
  135. m_pSheetPanel_Second->SetFromMaterial( NULL );
  136. }
  137. else
  138. {
  139. char sz[64];
  140. Q_snprintf( sz, sizeof( sz ), "%d/%d Sequences (#%d)", nCurrentSequence+1, nSequences, nRealSeqNumber );
  141. m_pSequenceSelection->SetText( sz );
  142. m_pSequenceSelection->SetEnabled(true);
  143. m_pSequenceSelection_Second->SetVisible(bSecondPicker);
  144. m_pNextSeqButton->SetEnabled(true);
  145. m_pPrevSeqButton->SetEnabled(true);
  146. m_pSheetPanel->SetFromMaterial( m_pParentPicker->GetMaterial() );
  147. m_pSheetPanel_Second->SetFromMaterial( m_pParentPicker->GetMaterial() );
  148. }
  149. InvalidateLayout();
  150. }
  151. //-----------------------------------------------------------------------------
  152. // Purpose: Constructor
  153. //-----------------------------------------------------------------------------
  154. CVMTPicker::CVMTPicker( vgui::Panel *pParent, bool bAllowMultiselect ) :
  155. BaseClass( pParent, "VMT Files", "vmt", "materials", "vmtName" )
  156. {
  157. // Horizontal splitter for preview
  158. m_pPreviewSplitter = new vgui::Splitter( this, "PreviewSplitter", vgui::SPLITTER_MODE_VERTICAL, 1 );
  159. vgui::Panel *pSplitterLeftSide = m_pPreviewSplitter->GetChild( 0 );
  160. vgui::Panel *pSplitterRightSide = m_pPreviewSplitter->GetChild( 1 );
  161. m_p2D3DSplitter = new vgui::Splitter( pSplitterRightSide, "2D3DSplitter", vgui::SPLITTER_MODE_HORIZONTAL, 1 );
  162. vgui::Panel *pSplitterTopSide = m_p2D3DSplitter->GetChild( 0 );
  163. vgui::Panel *pSplitterBottomSide = m_p2D3DSplitter->GetChild( 1 );
  164. // VMT preview
  165. m_pVMTPreview2D = new CVMTPreviewPanel( pSplitterTopSide, "VMTPreview2D" );
  166. m_pVMTPreview3D = new CVMTPreviewPanel( pSplitterBottomSide, "VMTPreview3D" );
  167. m_pVMTPreview3D->DrawIn3DMode( true );
  168. m_pVMTPreviewToolbar = new CVMTPreviewToolbar( pSplitterBottomSide, "PreviewToolbar", this );
  169. // Standard browser controls
  170. CreateStandardControls( pSplitterLeftSide, bAllowMultiselect );
  171. LoadControlSettingsAndUserConfig( "resource/vmtpicker.res" );
  172. }
  173. CVMTPicker::~CVMTPicker()
  174. {
  175. }
  176. void CVMTPicker::SetSheetPreviewSpeed( float flPreviewSpeed )
  177. {
  178. m_pVMTPreview2D->SetSheetPreviewSpeed( flPreviewSpeed );
  179. m_pVMTPreview3D->SetSheetPreviewSpeed( flPreviewSpeed );
  180. }
  181. void CVMTPicker::SetSelectedSequence( int nSequence )
  182. {
  183. m_pVMTPreview2D->SetSheetSequence( nSequence );
  184. m_pVMTPreview3D->SetSheetSequence( nSequence );
  185. }
  186. void CVMTPicker::SetSelectedSecondarySequence( int nSequence )
  187. {
  188. m_pVMTPreview2D->SetSecondarySheetSequence( nSequence );
  189. m_pVMTPreview3D->SetSecondarySheetSequence( nSequence );
  190. }
  191. int CVMTPicker::GetSheetSequenceCount()
  192. {
  193. return m_pVMTPreview3D->GetSheetSequenceCount();
  194. }
  195. int CVMTPicker::GetCurrentSequence()
  196. {
  197. return m_pVMTPreview3D->GetCurrentSequence();
  198. }
  199. int CVMTPicker::GetCurrentSecondarySequence()
  200. {
  201. return m_pVMTPreview3D->GetCurrentSecondarySequence();
  202. }
  203. int CVMTPicker::GetRealSequenceNumber()
  204. {
  205. return m_pVMTPreview3D->GetRealSequenceNumber();
  206. }
  207. void CVMTPicker::CustomizeSelectionMessage( KeyValues *pKeyValues )
  208. {
  209. BaseClass::CustomizeSelectionMessage(pKeyValues);
  210. pKeyValues->SetInt( "sheet_sequence_count", GetSheetSequenceCount() );
  211. pKeyValues->SetInt( "sheet_sequence_number", GetCurrentSequence() );
  212. pKeyValues->SetInt( "sheet_sequence_secondary_number", GetCurrentSecondarySequence() );
  213. }
  214. CSheetExtended* CVMTPicker::GetSheet()
  215. {
  216. return m_pVMTPreview3D->GetSheet();
  217. }
  218. IMaterial* CVMTPicker::GetMaterial()
  219. {
  220. return m_pVMTPreview3D->GetMaterial();
  221. }
  222. //-----------------------------------------------------------------------------
  223. // Derived classes have this called when the previewed asset changes
  224. //-----------------------------------------------------------------------------
  225. void CVMTPicker::OnSelectedAssetPicked( const char *pAssetName )
  226. {
  227. m_pVMTPreview2D->SetVMT( pAssetName );
  228. m_pVMTPreview3D->SetVMT( pAssetName );
  229. m_pVMTPreviewToolbar->UpdateToolbarGUI();
  230. }
  231. //-----------------------------------------------------------------------------
  232. //
  233. // Purpose: Modal picker frame
  234. //
  235. //-----------------------------------------------------------------------------
  236. CVMTPickerFrame::CVMTPickerFrame( vgui::Panel *pParent, const char *pTitle, bool bAllowMultiselect ) :
  237. BaseClass( pParent )
  238. {
  239. SetAssetPicker( new CVMTPicker( this, bAllowMultiselect ) );
  240. LoadControlSettingsAndUserConfig( "resource/vmtpickerframe.res" );
  241. SetTitle( pTitle, false );
  242. }