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.

300 lines
6.4 KiB

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