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.

310 lines
6.8 KiB

  1. // ModelBrowser.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "ModelBrowser.h"
  5. #include "matsys_controls/mdlpicker.h"
  6. #include "matsys_controls/baseassetpicker.h"
  7. #include "matsys_controls/matsyscontrols.h"
  8. #include "vgui_controls/TextEntry.h"
  9. #include "vgui_controls/Splitter.h"
  10. #include "vgui_controls/Button.h"
  11. #include "KeyValues.h"
  12. #include "vgui/KeyCode.h"
  13. #include "texturesystem.h"
  14. #include "HammerVGui.h"
  15. static LPCTSTR pszIniSection = "Model Browser";
  16. // CModelBrowser dialog
  17. class CModelBrowserPanel : public vgui::EditablePanel
  18. {
  19. public:
  20. CModelBrowserPanel( CModelBrowser *pBrowser, const char *panelName, vgui::HScheme hScheme ) :
  21. vgui::EditablePanel( NULL, panelName, hScheme )
  22. {
  23. m_pBrowser = pBrowser;
  24. }
  25. virtual void OnSizeChanged(int newWide, int newTall)
  26. {
  27. // call Panel and not EditablePanel OnSizeChanged.
  28. Panel::OnSizeChanged(newWide, newTall);
  29. }
  30. virtual void OnCommand( const char *pCommand )
  31. {
  32. if ( Q_strcmp( pCommand, "OK" ) == 0 )
  33. {
  34. m_pBrowser->EndDialog( IDOK );
  35. }
  36. else if ( Q_strcmp( pCommand, "Cancel" ) == 0 )
  37. {
  38. m_pBrowser->EndDialog( IDCANCEL );
  39. }
  40. }
  41. virtual void OnKeyCodeTyped(vgui::KeyCode code)
  42. {
  43. vgui::EditablePanel::OnKeyCodeTyped( code );
  44. if ( code == KEY_ENTER )
  45. {
  46. m_pBrowser->EndDialog( IDOK );
  47. }
  48. else if ( code == KEY_ESCAPE )
  49. {
  50. m_pBrowser->EndDialog( IDCANCEL );
  51. }
  52. }
  53. virtual void OnMessage(const KeyValues *params, vgui::VPANEL ifromPanel)
  54. {
  55. vgui::EditablePanel::OnMessage( params, ifromPanel );
  56. if ( Q_strcmp( params->GetName(), "MDLPreviewChanged" ) == 0 )
  57. {
  58. m_pBrowser->UpdateStatusLine();
  59. }
  60. else if ( Q_stricmp( params->GetName(), "AssetPickerFind" ) == 0 )
  61. {
  62. m_pBrowser->EndDialog( ID_FIND_ASSET );
  63. }
  64. }
  65. CModelBrowser *m_pBrowser;
  66. };
  67. IMPLEMENT_DYNAMIC(CModelBrowser, CDialog)
  68. CModelBrowser::CModelBrowser(CWnd* pParent /*=NULL*/)
  69. : CDialog(CModelBrowser::IDD, pParent)
  70. {
  71. m_pPicker = new CMDLPicker( NULL );
  72. m_pStatusLine = new vgui::TextEntry( NULL, "StatusLine" );
  73. m_pButtonOK = new vgui::Button( NULL, "OpenButton", "OK" );
  74. m_pButtonCancel = new vgui::Button( NULL, "CancelButton", "Cancel" );
  75. }
  76. CModelBrowser::~CModelBrowser()
  77. {
  78. delete m_pPicker;
  79. delete m_pStatusLine;
  80. delete m_pButtonOK;
  81. delete m_pButtonCancel;
  82. }
  83. void CModelBrowser::SetUsedModelList( CUtlVector<AssetUsageInfo_t> &usedModels )
  84. {
  85. m_pPicker->SetUsedAssetList( usedModels );
  86. }
  87. void CModelBrowser::SetModelName( const char *pModelName )
  88. {
  89. char pszTempModelName[255];
  90. strcpy( pszTempModelName, pModelName );
  91. char * pszSelectedModel = strchr( pszTempModelName, '/' );
  92. if( pszSelectedModel)
  93. {
  94. pszSelectedModel += 1;
  95. Q_FixSlashes( pszSelectedModel, '\\' );
  96. }
  97. m_pPicker->SelectMDL( pModelName );
  98. m_pPicker->SetInitialSelection( pszSelectedModel );
  99. m_pStatusLine->SetText( pModelName );
  100. }
  101. void CModelBrowser::GetModelName( char *pModelName, int length )
  102. {
  103. m_pPicker->GetSelectedMDLName( pModelName, length );
  104. Q_FixSlashes( pModelName, '/' );
  105. }
  106. void CModelBrowser::GetSkin( int &nSkin )
  107. {
  108. nSkin = m_pPicker->GetSelectedSkin();
  109. }
  110. void CModelBrowser::SetSkin( int nSkin )
  111. {
  112. m_pPicker->SelectSkin( nSkin );
  113. }
  114. void CModelBrowser::UpdateStatusLine()
  115. {
  116. char szModel[1024];
  117. m_pPicker->GetSelectedMDLName( szModel, sizeof(szModel) );
  118. m_pStatusLine->SetText( szModel );
  119. /* MDLHandle_t hMDL = vgui::MDLCache()->FindMDL( szModel );
  120. studiohdr_t *hdr = vgui::MDLCache()->GetStudioHdr( hMDL );
  121. vgui::MDLCache()->Release( hMDL ); */
  122. }
  123. void CModelBrowser::SaveLoadSettings( bool bSave )
  124. {
  125. CString str;
  126. CRect rect;
  127. CWinApp *pApp = AfxGetApp();
  128. if ( bSave )
  129. {
  130. GetWindowRect(rect);
  131. str.Format("%d %d %d %d", rect.left, rect.top, rect.right, rect.bottom);
  132. pApp->WriteProfileString(pszIniSection, "Position", str);
  133. pApp->WriteProfileString(pszIniSection, "Filter", m_pPicker->GetFilter() );
  134. }
  135. else
  136. {
  137. str = pApp->GetProfileString(pszIniSection, "Position");
  138. if (!str.IsEmpty())
  139. {
  140. sscanf(str, "%d %d %d %d", &rect.left, &rect.top, &rect.right, &rect.bottom);
  141. MoveWindow(rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top, FALSE);
  142. Resize();
  143. }
  144. str = pApp->GetProfileString(pszIniSection, "Filter");
  145. if (!str.IsEmpty())
  146. {
  147. m_pPicker->SetFilter( str );
  148. }
  149. }
  150. }
  151. void CModelBrowser::DoDataExchange(CDataExchange* pDX)
  152. {
  153. CDialog::DoDataExchange(pDX);
  154. }
  155. void CModelBrowser::Resize()
  156. {
  157. // reposition controls
  158. CRect rect;
  159. GetClientRect(&rect);
  160. m_VGuiWindow.MoveWindow( rect );
  161. m_pPicker->SetBounds( 0,0, rect.Width(), rect.Height() - 32 );
  162. m_pButtonCancel->SetPos( 8, rect.Height() - 30 );
  163. m_pButtonOK->SetPos( 84, rect.Height() - 30 );
  164. m_pStatusLine->SetBounds( 160, rect.Height() - 30, max( 100, rect.Width() - 166 ), 24 );
  165. }
  166. void CModelBrowser::OnSize(UINT nType, int cx, int cy)
  167. {
  168. if (nType == SIZE_MINIMIZED || !IsWindow(m_VGuiWindow.m_hWnd) )
  169. {
  170. CDialog::OnSize(nType, cx, cy);
  171. return;
  172. }
  173. Resize();
  174. CDialog::OnSize(nType, cx, cy);
  175. }
  176. BEGIN_MESSAGE_MAP(CModelBrowser, CDialog)
  177. ON_WM_SIZE()
  178. ON_WM_DESTROY()
  179. ON_WM_ERASEBKGND()
  180. END_MESSAGE_MAP()
  181. BOOL CModelBrowser::PreTranslateMessage( MSG* pMsg )
  182. {
  183. // don't filter dialog message
  184. return CWnd::PreTranslateMessage( pMsg );
  185. }
  186. BOOL CModelBrowser::OnInitDialog()
  187. {
  188. CDialog::OnInitDialog();
  189. m_VGuiWindow.Create( NULL, _T("ModelViewer"), WS_VISIBLE|WS_CHILD, CRect(0,0,100,100), this, 1001);
  190. vgui::EditablePanel *pMainPanel = new CModelBrowserPanel( this, "ModelBrowerPanel", HammerVGui()->GetHammerScheme() );
  191. m_VGuiWindow.SetParentWindow( &m_VGuiWindow );
  192. m_VGuiWindow.SetMainPanel( pMainPanel );
  193. pMainPanel->MakePopup( false, false );
  194. m_VGuiWindow.SetRepaintInterval( 75 );
  195. m_pPicker->SetParent( pMainPanel );
  196. m_pPicker->AddActionSignalTarget( pMainPanel );
  197. m_pButtonOK->SetParent( pMainPanel );
  198. m_pButtonOK->AddActionSignalTarget( pMainPanel );
  199. m_pButtonOK->SetCommand( "OK" );
  200. m_pButtonCancel->SetParent( pMainPanel );
  201. m_pButtonCancel->AddActionSignalTarget( pMainPanel );
  202. m_pButtonCancel->SetCommand( "Cancel" );
  203. m_pStatusLine->SetParent( pMainPanel );
  204. m_pStatusLine->SetEditable( false );
  205. SaveLoadSettings( false ); // load
  206. m_pPicker->Activate();
  207. return TRUE;
  208. }
  209. void CModelBrowser::OnDestroy()
  210. {
  211. SaveLoadSettings( true ); // save
  212. // model browser destoys our default cube map, reload it
  213. g_Textures.RebindDefaultCubeMap();
  214. CDialog::OnDestroy();
  215. }
  216. void CModelBrowser::Show()
  217. {
  218. if (m_pPicker)
  219. {
  220. m_pPicker->SetVisible( true );
  221. }
  222. if (m_pStatusLine)
  223. m_pStatusLine->SetVisible( true );
  224. if (m_pButtonOK)
  225. m_pButtonOK->SetVisible( true );
  226. if (m_pButtonCancel)
  227. m_pButtonCancel->SetVisible( true );
  228. }
  229. void CModelBrowser::Hide()
  230. {
  231. if (m_pPicker)
  232. m_pPicker->SetVisible( false );
  233. if (m_pStatusLine)
  234. m_pStatusLine->SetVisible( false );
  235. if (m_pButtonOK)
  236. m_pButtonOK->SetVisible( false );
  237. if (m_pButtonCancel)
  238. m_pButtonCancel->SetVisible( false );
  239. }
  240. BOOL CModelBrowser::OnEraseBkgnd(CDC* pDC)
  241. {
  242. return TRUE;
  243. }