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.

888 lines
26 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //===========================================================================//
  7. #include "dme_controls/particlesystempanel.h"
  8. #include "dme_controls/dmepanel.h"
  9. #include "movieobjects/dmeparticlesystemdefinition.h"
  10. #include "materialsystem/imesh.h"
  11. #include "materialsystem/imaterial.h"
  12. #include "vguimatsurface/imatsystemsurface.h"
  13. #include "matsys_controls/matsyscontrols.h"
  14. #include "vgui/ivgui.h"
  15. #include "vgui_controls/propertypage.h"
  16. #include "vgui_controls/propertysheet.h"
  17. #include "vgui_controls/textentry.h"
  18. #include "vgui_controls/splitter.h"
  19. #include "vgui_controls/checkbutton.h"
  20. #include "matsys_controls/colorpickerpanel.h"
  21. #include "particles/particles.h"
  22. #include "tier1/keyvalues.h"
  23. #include "tier1/utlbuffer.h"
  24. #include "tier2/renderutils.h"
  25. using namespace vgui;
  26. //-----------------------------------------------------------------------------
  27. // Enums
  28. //-----------------------------------------------------------------------------
  29. enum
  30. {
  31. SCROLLBAR_SIZE=18, // the width of a scrollbar
  32. WINDOW_BORDER_WIDTH=2 // the width of the window's border
  33. };
  34. #define SPHERE_RADIUS 10.0f
  35. //-----------------------------------------------------------------------------
  36. // Constructor, destructor
  37. //-----------------------------------------------------------------------------
  38. CParticleSystemPanel::CParticleSystemPanel( vgui::Panel *pParent, const char *pName ) : BaseClass( pParent, pName )
  39. {
  40. m_pParticleSystem = NULL;
  41. m_flLastTime = FLT_MAX;
  42. m_bRenderBounds = false;
  43. m_bRenderCullBounds = false;
  44. m_bRenderHelpers = false;
  45. m_bRenderControlPoints = false;
  46. m_bPerformNameBasedLookup = true;
  47. m_bTickMyself = true;
  48. m_bAutoView = false;
  49. m_bSuppressAutoView = false;
  50. m_ParticleSystemName = NULL;
  51. InvalidateUniqueId( &m_ParticleSystemId );
  52. InvalidateUniqueId( &m_RenderHelperId );
  53. LookAt( SPHERE_RADIUS );
  54. m_pLightmapTexture.Init( "//platform/materials/debug/defaultlightmap", "editor" );
  55. m_DefaultEnvCubemap.Init( "editor/cubemap", "editor", true );
  56. for ( int i = 0; i < MAX_PARTICLE_CONTROL_POINTS; ++i )
  57. {
  58. SetControlPointValue( i, vec3_invalid );
  59. }
  60. }
  61. CParticleSystemPanel::~CParticleSystemPanel()
  62. {
  63. m_pLightmapTexture.Shutdown();
  64. m_DefaultEnvCubemap.Shutdown();
  65. delete m_pParticleSystem;
  66. m_pParticleSystem = NULL;
  67. }
  68. void CParticleSystemPanel::ResetView()
  69. {
  70. BaseClass::ResetView();
  71. LookAt(SPHERE_RADIUS);
  72. m_BestViewBoundsMin = vec3_origin;
  73. m_BestViewBoundsMax = vec3_origin;
  74. m_bSuppressAutoView = false;
  75. }
  76. //-----------------------------------------------------------------------------
  77. // Scheme
  78. //-----------------------------------------------------------------------------
  79. void CParticleSystemPanel::ApplySchemeSettings( vgui::IScheme *pScheme )
  80. {
  81. BaseClass::ApplySchemeSettings( pScheme );
  82. SetBorder( pScheme->GetBorder( "MenuBorder") );
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Stop System
  86. //-----------------------------------------------------------------------------
  87. void CParticleSystemPanel::StopEffect()
  88. {
  89. if ( !m_pParticleSystem )
  90. return;
  91. m_pParticleSystem->StopEmission(false, false, false, true );
  92. }
  93. //-----------------------------------------------------------------------------
  94. // Indicates that the grid should be drawn
  95. //-----------------------------------------------------------------------------
  96. void CParticleSystemPanel::RenderGrid( bool bEnable )
  97. {
  98. m_bRenderGrid = bEnable;
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Indicates that bounds should be drawn
  102. //-----------------------------------------------------------------------------
  103. void CParticleSystemPanel::RenderBounds( bool bEnable )
  104. {
  105. m_bRenderBounds = bEnable;
  106. }
  107. //-----------------------------------------------------------------------------
  108. // Indicates that control points should be drawn
  109. //-----------------------------------------------------------------------------
  110. void CParticleSystemPanel::RenderControlPoints( bool bEnable )
  111. {
  112. m_bRenderControlPoints = bEnable;
  113. }
  114. //-----------------------------------------------------------------------------
  115. // Indicates that cull sphere should be drawn
  116. //-----------------------------------------------------------------------------
  117. void CParticleSystemPanel::RenderCullBounds( bool bEnable )
  118. {
  119. m_bRenderCullBounds = bEnable;
  120. }
  121. //-----------------------------------------------------------------------------
  122. // Indicates that bounds should be drawn
  123. //-----------------------------------------------------------------------------
  124. void CParticleSystemPanel::RenderHelpers( bool bEnable )
  125. {
  126. m_bRenderHelpers = bEnable;
  127. }
  128. //-----------------------------------------------------------------------------
  129. // Indicates which helper to draw
  130. //-----------------------------------------------------------------------------
  131. void CParticleSystemPanel::SetRenderedHelper( CDmeParticleFunction *pOp )
  132. {
  133. if ( !pOp )
  134. {
  135. InvalidateUniqueId( &m_RenderHelperId );
  136. }
  137. else
  138. {
  139. CopyUniqueId( pOp->GetId(), &m_RenderHelperId );
  140. }
  141. }
  142. //-----------------------------------------------------------------------------
  143. // Simulate the particle system
  144. //-----------------------------------------------------------------------------
  145. void CParticleSystemPanel::OnTick()
  146. {
  147. BaseClass::OnTick();
  148. if( m_bTickMyself )
  149. {
  150. Simulate();
  151. }
  152. }
  153. void CParticleSystemPanel::Simulate()
  154. {
  155. if ( !m_pParticleSystem )
  156. return;
  157. float flTime = Plat_FloatTime();
  158. if ( m_flLastTime == FLT_MAX )
  159. {
  160. m_flLastTime = flTime;
  161. }
  162. float flDt = flTime - m_flLastTime;
  163. m_flLastTime = flTime;
  164. for ( int i = 0; i < MAX_PARTICLE_CONTROL_POINTS; ++i )
  165. {
  166. if ( !m_pParticleSystem->ReadsControlPoint( i ) || m_pControlPointValue[i] == vec3_invalid )
  167. continue;
  168. m_pParticleSystem->SetControlPoint( i, m_pControlPointValue[i] );
  169. m_pParticleSystem->SetControlPointOrientation( i, Vector( 1, 0, 0 ), Vector( 0, -1, 0 ), Vector( 0, 0, 1 ) );
  170. m_pParticleSystem->SetControlPointParent( i, i );
  171. }
  172. // Restart the particle system if it's finished
  173. bool bIsInvalid = !m_pParticleSystem->IsFullyValid();
  174. if ( m_pParticleSystem->IsFinished() || bIsInvalid )
  175. {
  176. delete m_pParticleSystem;
  177. m_pParticleSystem = NULL;
  178. if ( m_bPerformNameBasedLookup )
  179. {
  180. if ( m_ParticleSystemName.Length() )
  181. {
  182. CParticleCollection *pNewParticleSystem = g_pParticleSystemMgr->CreateParticleCollection( m_ParticleSystemName );
  183. m_pParticleSystem = pNewParticleSystem;
  184. }
  185. }
  186. else
  187. {
  188. if ( IsUniqueIdValid( m_ParticleSystemId ) )
  189. {
  190. CParticleCollection *pNewParticleSystem = g_pParticleSystemMgr->CreateParticleCollection( m_ParticleSystemId );
  191. m_pParticleSystem = pNewParticleSystem;
  192. }
  193. }
  194. if ( bIsInvalid )
  195. {
  196. PostActionSignal( new KeyValues( "ParticleSystemReconstructed" ) );
  197. }
  198. m_flLastTime = FLT_MAX;
  199. }
  200. else
  201. {
  202. m_pParticleSystem->Simulate( flDt );
  203. }
  204. if( m_bAutoView && !m_bSuppressAutoView )
  205. {
  206. UseAutoView();
  207. }
  208. }
  209. //-----------------------------------------------------------------------------
  210. // Startup, shutdown particle collection
  211. //-----------------------------------------------------------------------------
  212. void CParticleSystemPanel::StartupParticleCollection()
  213. {
  214. if ( m_pParticleSystem && m_bTickMyself )
  215. {
  216. vgui::ivgui()->AddTickSignal( GetVPanel(), 0 );
  217. }
  218. m_flLastTime = FLT_MAX;
  219. }
  220. void CParticleSystemPanel::ShutdownParticleCollection()
  221. {
  222. if ( m_pParticleSystem )
  223. {
  224. if( m_bTickMyself )
  225. {
  226. vgui::ivgui()->RemoveTickSignal( GetVPanel() );
  227. }
  228. delete m_pParticleSystem;
  229. m_pParticleSystem = NULL;
  230. }
  231. }
  232. void CParticleSystemPanel::SetSelfSimulation(bool bSelfSimulate )
  233. {
  234. if( m_pParticleSystem && ( m_bTickMyself != bSelfSimulate ) )
  235. {
  236. if ( !bSelfSimulate )
  237. {
  238. vgui::ivgui()->RemoveTickSignal( GetVPanel() );
  239. }
  240. else
  241. {
  242. vgui::ivgui()->AddTickSignal( GetVPanel(), 0 );
  243. }
  244. }
  245. m_bTickMyself = bSelfSimulate;
  246. }
  247. //-----------------------------------------------------------------------------
  248. // Set the particle system to draw
  249. //-----------------------------------------------------------------------------
  250. void CParticleSystemPanel::SetParticleSystem( CDmeParticleSystemDefinition *pDef )
  251. {
  252. ShutdownParticleCollection();
  253. if ( pDef )
  254. {
  255. m_bPerformNameBasedLookup = pDef->UseNameBasedLookup();
  256. if ( m_bPerformNameBasedLookup )
  257. {
  258. m_ParticleSystemName = pDef->GetName();
  259. Assert( g_pParticleSystemMgr->IsParticleSystemDefined( m_ParticleSystemName ) );
  260. m_pParticleSystem = g_pParticleSystemMgr->CreateParticleCollection( m_ParticleSystemName );
  261. }
  262. else
  263. {
  264. CopyUniqueId( pDef->GetId(), &m_ParticleSystemId );
  265. Assert( g_pParticleSystemMgr->IsParticleSystemDefined( m_ParticleSystemId ) );
  266. m_pParticleSystem = g_pParticleSystemMgr->CreateParticleCollection( m_ParticleSystemId );
  267. }
  268. PostActionSignal( new KeyValues( "ParticleSystemReconstructed" ) );
  269. }
  270. StartupParticleCollection();
  271. }
  272. void CParticleSystemPanel::SetParticleSystem( const char* szParticleSystemName )
  273. {
  274. ShutdownParticleCollection();
  275. if ( szParticleSystemName )
  276. {
  277. m_ParticleSystemName = szParticleSystemName;
  278. if ( g_pParticleSystemMgr->IsParticleSystemDefined( m_ParticleSystemName ) )
  279. {
  280. m_pParticleSystem = g_pParticleSystemMgr->CreateParticleCollection( m_ParticleSystemName );
  281. }
  282. else
  283. {
  284. m_pParticleSystem = NULL;
  285. }
  286. PostActionSignal( new KeyValues( "ParticleSystemReconstructed" ) );
  287. }
  288. StartupParticleCollection();
  289. }
  290. void CParticleSystemPanel::SetDmeElement( CDmeParticleSystemDefinition *pDef )
  291. {
  292. SetParticleSystem( pDef );
  293. }
  294. CParticleCollection *CParticleSystemPanel::GetParticleSystem()
  295. {
  296. return m_pParticleSystem;
  297. }
  298. //-----------------------------------------------------------------------------
  299. // Auto-viewing
  300. //-----------------------------------------------------------------------------
  301. void CParticleSystemPanel::EnterManipulationMode( ManipulationMode_t manipMode, bool bMouseCapture, vgui::MouseCode mouseCode )
  302. {
  303. BaseClass::EnterManipulationMode(manipMode,bMouseCapture,mouseCode);
  304. if( manipMode != CAMERA_ROTATE )
  305. {
  306. m_bSuppressAutoView = true;
  307. }
  308. }
  309. void CParticleSystemPanel::EnableAutoViewing( bool bEnable )
  310. {
  311. m_bAutoView = bEnable;
  312. m_BestViewBoundsMin = vec3_origin;
  313. m_BestViewBoundsMax = vec3_origin;
  314. if ( m_bAutoView )
  315. {
  316. m_bSuppressAutoView = false;
  317. }
  318. }
  319. void CParticleSystemPanel::UseAutoView()
  320. {
  321. Vector vecMins, vecMaxs;
  322. m_pParticleSystem->GetBounds( &vecMins, &vecMaxs );
  323. m_BestViewBoundsMin = m_BestViewBoundsMin.Min(vecMins);
  324. m_BestViewBoundsMax = m_BestViewBoundsMax.Max(vecMaxs);
  325. float flBestViewRad = MAX( 5.0f, 0.25f*(m_BestViewBoundsMax-m_BestViewBoundsMin).Length() );
  326. Vector viewCenter = ( m_BestViewBoundsMin + m_BestViewBoundsMax ) * 0.5f;
  327. LookAt( viewCenter, flBestViewRad );
  328. }
  329. //-----------------------------------------------------------------------------
  330. // Draw bounds
  331. //-----------------------------------------------------------------------------
  332. void CParticleSystemPanel::DrawBounds()
  333. {
  334. Vector vecMins, vecMaxs;
  335. m_pParticleSystem->GetBounds( &vecMins, &vecMaxs );
  336. RenderWireframeBox( vec3_origin, vec3_angle, vecMins, vecMaxs, Color( 0, 255, 255, 255 ), true );
  337. }
  338. //-----------------------------------------------------------------------------
  339. // Draw cull bounds
  340. //-----------------------------------------------------------------------------
  341. void CParticleSystemPanel::DrawCullBounds()
  342. {
  343. Vector vecCenter;
  344. m_pParticleSystem->GetControlPointAtTime( m_pParticleSystem->m_pDef->GetCullControlPoint(), m_pParticleSystem->m_flCurTime, &vecCenter );
  345. RenderWireframeSphere( vecCenter, m_pParticleSystem->m_pDef->GetCullRadius(), 32, 16, Color( 0, 255, 255, 255 ), true );
  346. }
  347. //-----------------------------------------------------------------------------
  348. // paint it!
  349. //-----------------------------------------------------------------------------
  350. #define AXIS_SIZE 5.0f
  351. void CParticleSystemPanel::OnPaint3D()
  352. {
  353. if ( !m_pParticleSystem )
  354. return;
  355. CMatRenderContextPtr pRenderContext( MaterialSystem() );
  356. pRenderContext->BindLightmapTexture( m_pLightmapTexture );
  357. pRenderContext->BindLocalCubemap( m_DefaultEnvCubemap );
  358. // Draw axes
  359. pRenderContext->MatrixMode( MATERIAL_MODEL );
  360. pRenderContext->PushMatrix();
  361. pRenderContext->LoadIdentity( );
  362. if ( m_bRenderGrid )
  363. {
  364. DrawGrid();
  365. }
  366. if ( m_bRenderBounds )
  367. {
  368. DrawBounds();
  369. }
  370. if ( m_bRenderCullBounds )
  371. {
  372. DrawCullBounds();
  373. }
  374. if ( m_bRenderHelpers && IsUniqueIdValid( m_RenderHelperId ) )
  375. {
  376. m_pParticleSystem->VisualizeOperator( &m_RenderHelperId );
  377. }
  378. Vector4D vecDiffuseModulation( 1.0f, 1.0f, 1.0f, 1.0f );
  379. m_pParticleSystem->Render( 0, pRenderContext, vecDiffuseModulation );
  380. m_pParticleSystem->VisualizeOperator( );
  381. RenderAxes( vec3_origin, AXIS_SIZE, true );
  382. if ( m_bRenderControlPoints )
  383. {
  384. for ( int i = 0; i < MAX_PARTICLE_CONTROL_POINTS; ++i )
  385. {
  386. if ( m_pParticleSystem->ReadsControlPoint( i ) )
  387. {
  388. Vector vP;
  389. m_pParticleSystem->GetControlPointAtTime( i, m_pParticleSystem->m_flCurTime, &vP );
  390. RenderAxes( vP, 3.0f, true );
  391. }
  392. }
  393. }
  394. pRenderContext->MatrixMode( MATERIAL_MODEL );
  395. pRenderContext->PopMatrix();
  396. }
  397. //-----------------------------------------------------------------------------
  398. //
  399. // Control point page
  400. //
  401. //-----------------------------------------------------------------------------
  402. class CControlPointPage : public vgui::PropertyPage
  403. {
  404. DECLARE_CLASS_SIMPLE( CControlPointPage, vgui::PropertyPage );
  405. public:
  406. // constructor, destructor
  407. CControlPointPage( vgui::Panel *pParent, const char *pName, CParticleSystemPanel *pParticleSystemPanel );
  408. virtual void PerformLayout();
  409. void CreateControlPointControls( );
  410. private:
  411. MESSAGE_FUNC_PARAMS( OnTextChanged, "TextChanged", params );
  412. MESSAGE_FUNC_PARAMS( OnNewLine, "TextNewLine", params );
  413. void LayoutControlPointControls();
  414. void CleanUpControlPointControls();
  415. vgui::Label *m_pControlPointName[MAX_PARTICLE_CONTROL_POINTS];
  416. vgui::TextEntry *m_pControlPointValue[MAX_PARTICLE_CONTROL_POINTS];
  417. CParticleSystemPanel *m_pParticleSystemPanel;
  418. };
  419. //-----------------------------------------------------------------------------
  420. // Contstructor
  421. //-----------------------------------------------------------------------------
  422. CControlPointPage::CControlPointPage( vgui::Panel *pParent, const char *pName, CParticleSystemPanel *pParticleSystemPanel ) :
  423. BaseClass( pParent, pName )
  424. {
  425. for ( int i = 0; i < MAX_PARTICLE_CONTROL_POINTS; ++i )
  426. {
  427. m_pControlPointName[i] = NULL;
  428. m_pControlPointValue[i] = NULL;
  429. }
  430. m_pParticleSystemPanel = pParticleSystemPanel;
  431. }
  432. //-----------------------------------------------------------------------------
  433. // Called when the text entry for a control point is changed
  434. //-----------------------------------------------------------------------------
  435. void CControlPointPage::OnTextChanged( KeyValues *pParams )
  436. {
  437. vgui::Panel *pPanel = (vgui::Panel *)pParams->GetPtr( "panel" );
  438. for ( int i = 0; i < MAX_PARTICLE_CONTROL_POINTS; ++i )
  439. {
  440. if ( pPanel != m_pControlPointValue[i] )
  441. continue;
  442. char pBuf[512];
  443. m_pControlPointValue[i]->GetText( pBuf, sizeof(pBuf) );
  444. Vector vecValue( 0, 0, 0 );
  445. sscanf( pBuf, "%f %f %f", &vecValue.x, &vecValue.y, &vecValue.z );
  446. m_pParticleSystemPanel->SetControlPointValue( i, vecValue );
  447. break;
  448. }
  449. }
  450. //-----------------------------------------------------------------------------
  451. // Called when the text entry for a control point is changed
  452. //-----------------------------------------------------------------------------
  453. void CControlPointPage::OnNewLine( KeyValues *pParams )
  454. {
  455. vgui::Panel *pPanel = (vgui::Panel *)pParams->GetPtr( "panel" );
  456. for ( int i = 0; i < MAX_PARTICLE_CONTROL_POINTS; ++i )
  457. {
  458. if ( pPanel != m_pControlPointValue[i] )
  459. continue;
  460. char pBuf[512];
  461. m_pControlPointValue[i]->GetText( pBuf, sizeof(pBuf) );
  462. Vector vecValue( 0, 0, 0 );
  463. sscanf( pBuf, "%f %f %f", &vecValue.x, &vecValue.y, &vecValue.z );
  464. m_pParticleSystemPanel->SetControlPointValue( i, vecValue );
  465. vecValue = m_pParticleSystemPanel->GetControlPointValue( i );
  466. Q_snprintf( pBuf, sizeof(pBuf), "%.3f %.3f %.3f", vecValue.x, vecValue.y, vecValue.z );
  467. m_pControlPointValue[i]->SetText( pBuf );
  468. break;
  469. }
  470. }
  471. //-----------------------------------------------------------------------------
  472. // Called when the particle system changes
  473. //-----------------------------------------------------------------------------
  474. void CControlPointPage::PerformLayout()
  475. {
  476. BaseClass::PerformLayout();
  477. LayoutControlPointControls();
  478. }
  479. //-----------------------------------------------------------------------------
  480. // Creates controls used to modify control point values
  481. //-----------------------------------------------------------------------------
  482. void CControlPointPage::CreateControlPointControls()
  483. {
  484. CleanUpControlPointControls();
  485. CParticleCollection* pParticleSystem = m_pParticleSystemPanel->GetParticleSystem();
  486. if ( !pParticleSystem )
  487. return;
  488. for ( int i = 0; i < MAX_PARTICLE_CONTROL_POINTS; ++i )
  489. {
  490. if ( !pParticleSystem->ReadsControlPoint( i ) )
  491. continue;
  492. char pName[512];
  493. Q_snprintf( pName, sizeof(pName), "Pt #%d:", i );
  494. m_pControlPointName[i] = new Label( this, pName, pName );
  495. Q_snprintf( pName, sizeof(pName), "Entry #%d:", i );
  496. m_pControlPointValue[i] = new TextEntry( this, pName );
  497. m_pControlPointValue[i]->AddActionSignalTarget( this );
  498. m_pControlPointValue[i]->SendNewLine( true );
  499. m_pControlPointValue[i]->SetMultiline( false );
  500. Vector vecValue = m_pParticleSystemPanel->GetControlPointValue( i );
  501. if ( vecValue == vec3_invalid )
  502. vecValue = vec3_origin;
  503. Q_snprintf( pName, sizeof(pName), "%.3f %.3f %.3f", vecValue.x, vecValue.y, vecValue.z );
  504. m_pControlPointValue[i]->SetText( pName );
  505. }
  506. LayoutControlPointControls();
  507. }
  508. //-----------------------------------------------------------------------------
  509. // Lays out the controls
  510. //-----------------------------------------------------------------------------
  511. void CControlPointPage::LayoutControlPointControls()
  512. {
  513. int nFoundControlCount = 0;
  514. for ( int i = 0; i < MAX_PARTICLE_CONTROL_POINTS; ++i )
  515. {
  516. if ( !m_pControlPointName[i] )
  517. continue;
  518. int yVal = 8 + nFoundControlCount * 28;
  519. m_pControlPointName[i]->SetBounds( 8, yVal, 48, 24 );
  520. m_pControlPointValue[i]->SetBounds( 64, yVal, 160, 24 );
  521. ++nFoundControlCount;
  522. }
  523. }
  524. //-----------------------------------------------------------------------------
  525. // Cleans up controls used to modify control point values
  526. //-----------------------------------------------------------------------------
  527. void CControlPointPage::CleanUpControlPointControls( )
  528. {
  529. for ( int i = 0; i < MAX_PARTICLE_CONTROL_POINTS; ++i )
  530. {
  531. if ( m_pControlPointName[i] )
  532. {
  533. delete m_pControlPointName[i];
  534. m_pControlPointName[i] = NULL;
  535. }
  536. if ( m_pControlPointValue[i] )
  537. {
  538. delete m_pControlPointValue[i];
  539. m_pControlPointValue[i] = NULL;
  540. }
  541. }
  542. }
  543. //-----------------------------------------------------------------------------
  544. //
  545. // CParticleSystemPreviewPanel
  546. //
  547. //-----------------------------------------------------------------------------
  548. //-----------------------------------------------------------------------------
  549. // Dme panel connection
  550. //-----------------------------------------------------------------------------
  551. IMPLEMENT_DMEPANEL_FACTORY( CParticleSystemPreviewPanel, DmeParticleSystemDefinition, "DmeParticleSystemDefinitionViewer", "Particle System Viewer", false );
  552. //-----------------------------------------------------------------------------
  553. // constructor, destructor
  554. //-----------------------------------------------------------------------------
  555. CParticleSystemPreviewPanel::CParticleSystemPreviewPanel( vgui::Panel *pParent, const char *pName ) :
  556. BaseClass( pParent, pName )
  557. {
  558. m_pUnlockSystem = NULL;
  559. m_Splitter = new vgui::Splitter( this, "Splitter", SPLITTER_MODE_VERTICAL, 1 );
  560. vgui::Panel *pSplitterLeftSide = m_Splitter->GetChild( 0 );
  561. vgui::Panel *pSplitterRightSide = m_Splitter->GetChild( 1 );
  562. m_pParticleSystemPanel = new CParticleSystemPanel( pSplitterRightSide, "ParticlePreview" );
  563. m_pParticleSystemPanel->AddActionSignalTarget( this );
  564. m_pParticleSystemPanel->SetBackgroundColor( 0, 0, 0 );
  565. m_pParticleCount = new vgui::Label( pSplitterRightSide, "ParticleCountLabel", "" );
  566. m_pParticleCount->SetZPos( 1 );
  567. m_pControlSheet = new vgui::PropertySheet( pSplitterLeftSide, "ControlSheet" );
  568. m_pRenderPage = new vgui::PropertyPage( m_pControlSheet, "RenderPage" );
  569. m_pRenderBounds = new vgui::CheckButton( m_pRenderPage, "RenderBounds", "Render Bounding Box" );
  570. m_pRenderBounds->AddActionSignalTarget( this );
  571. m_pRenderControlPoints = new vgui::CheckButton( m_pRenderPage, "RenderControlPoints", "Render Control Points" );
  572. m_pRenderControlPoints->AddActionSignalTarget( this );
  573. m_pRenderCullBounds = new vgui::CheckButton( m_pRenderPage, "RenderCullBounds", "Render Culling Bounds" );
  574. m_pRenderCullBounds->AddActionSignalTarget( this );
  575. m_pRenderHelpers = new vgui::CheckButton( m_pRenderPage, "RenderHelpers", "Render Helpers" );
  576. m_pRenderHelpers->AddActionSignalTarget( this );
  577. m_pRenderGrid = new vgui::CheckButton( m_pRenderPage, "RenderGrid", "Render Grid" );
  578. m_pRenderGrid->AddActionSignalTarget( this );
  579. m_pBackgroundColor = new CColorPickerButton( m_pRenderPage, "BackgroundColor", this );
  580. m_pBackgroundColor->SetColor( m_pParticleSystemPanel->GetBackgroundColor() );
  581. m_pLockPreview = new vgui::CheckButton( m_pRenderPage, "LockPreview", "Lock Preview System" );
  582. m_pLockPreview->AddActionSignalTarget( this );
  583. m_pStopEffect = new vgui::Button( m_pRenderPage, "StopEffect", "Stop Effect", this, "StopEffect" );
  584. m_pRenderPage->LoadControlSettingsAndUserConfig( "resource/particlesystempreviewpanel_renderpage.res" );
  585. m_pControlPointPage = new CControlPointPage( m_pControlSheet, "ControlPointPage", m_pParticleSystemPanel );
  586. // Load layout settings; has to happen before pinning occurs in code
  587. LoadControlSettingsAndUserConfig( "resource/particlesystempreviewpanel.res" );
  588. // NOTE: Page adding happens *after* LoadControlSettingsAndUserConfig
  589. // because the layout of the sheet is correct at this point.
  590. m_pControlSheet->AddPage( m_pRenderPage, "Render" );
  591. m_pControlSheet->AddPage( m_pControlPointPage, "Ctrl Pts" );
  592. }
  593. CParticleSystemPreviewPanel::~CParticleSystemPreviewPanel()
  594. {
  595. }
  596. //-----------------------------------------------------------------------------
  597. // Set the particle system to draw
  598. //-----------------------------------------------------------------------------
  599. void CParticleSystemPreviewPanel::OnThink()
  600. {
  601. BaseClass::OnThink();
  602. CParticleCollection* pParticleSystem = m_pParticleSystemPanel->GetParticleSystem();
  603. if ( !pParticleSystem )
  604. {
  605. m_pParticleCount->SetText( "" );
  606. }
  607. else
  608. {
  609. char buf[256];
  610. Q_snprintf( buf, sizeof(buf), "Particle Count: %5d/%5d",
  611. pParticleSystem->m_nActiveParticles, pParticleSystem->m_nAllocatedParticles );
  612. m_pParticleCount->SetText( buf );
  613. }
  614. }
  615. //-----------------------------------------------------------------------------
  616. // Called when the particle system changes
  617. //-----------------------------------------------------------------------------
  618. void CParticleSystemPreviewPanel::OnParticleSystemReconstructed()
  619. {
  620. m_pControlPointPage->CreateControlPointControls();
  621. }
  622. //-----------------------------------------------------------------------------
  623. // Set the particle system to draw
  624. //-----------------------------------------------------------------------------
  625. void CParticleSystemPreviewPanel::SetParticleSystem( CDmeParticleSystemDefinition *pDef, bool bOverrideLock )
  626. {
  627. bool bLocked = m_pLockPreview->IsSelected();
  628. if ( bOverrideLock || !bLocked )
  629. {
  630. m_pParticleSystemPanel->SetParticleSystem( pDef );
  631. if ( bOverrideLock && bLocked )
  632. {
  633. m_pLockPreview->SetSelected(false);
  634. m_pUnlockSystem = NULL;
  635. }
  636. else
  637. {
  638. m_pUnlockSystem = pDef;
  639. }
  640. }
  641. else
  642. {
  643. m_pUnlockSystem = pDef;
  644. }
  645. }
  646. void CParticleSystemPreviewPanel::ClearParticleSystemLock()
  647. {
  648. m_pLockPreview->SetSelected(false);
  649. m_pUnlockSystem = NULL;
  650. }
  651. void CParticleSystemPreviewPanel::SetDmeElement( CDmeParticleSystemDefinition *pDef )
  652. {
  653. SetParticleSystem( pDef, false );
  654. }
  655. //-----------------------------------------------------------------------------
  656. // Indicates which helper to draw
  657. //-----------------------------------------------------------------------------
  658. void CParticleSystemPreviewPanel::SetParticleFunction( CDmeParticleFunction *pFunction )
  659. {
  660. m_pParticleSystemPanel->SetRenderedHelper( pFunction );
  661. }
  662. //-----------------------------------------------------------------------------
  663. // Called when the check button is checked
  664. //-----------------------------------------------------------------------------
  665. void CParticleSystemPreviewPanel::OnCheckButtonChecked( KeyValues *pParams )
  666. {
  667. bool bState = pParams->GetBool( "state", false );
  668. vgui::Panel *pPanel = (vgui::Panel*)pParams->GetPtr( "panel" );
  669. if ( pPanel == m_pRenderGrid )
  670. {
  671. m_pParticleSystemPanel->RenderGrid( bState );
  672. return;
  673. }
  674. if ( pPanel == m_pLockPreview )
  675. {
  676. if ( !m_pLockPreview->IsSelected() )
  677. {
  678. // unchecked the lock - switch to the latest requested selection
  679. m_pParticleSystemPanel->SetParticleSystem( m_pUnlockSystem );
  680. }
  681. return;
  682. }
  683. if ( pPanel == m_pRenderBounds )
  684. {
  685. m_pParticleSystemPanel->RenderBounds( bState );
  686. return;
  687. }
  688. if ( pPanel == m_pRenderCullBounds )
  689. {
  690. m_pParticleSystemPanel->RenderCullBounds( bState );
  691. return;
  692. }
  693. if ( pPanel == m_pRenderHelpers )
  694. {
  695. m_pParticleSystemPanel->RenderHelpers( bState );
  696. return;
  697. }
  698. if ( pPanel == m_pRenderControlPoints )
  699. {
  700. m_pParticleSystemPanel->RenderControlPoints( bState );
  701. return;
  702. }
  703. }
  704. //-----------------------------------------------------------------------------
  705. // Called when a new background color is picked
  706. //-----------------------------------------------------------------------------
  707. void CParticleSystemPreviewPanel::OnBackgroundColorChanged( KeyValues *pParams )
  708. {
  709. m_pParticleSystemPanel->SetBackgroundColor( pParams->GetColor( "color" ) );
  710. }
  711. void CParticleSystemPreviewPanel::OnBackgroundColorPreview( KeyValues *pParams )
  712. {
  713. m_pParticleSystemPanel->SetBackgroundColor( pParams->GetColor( "color" ) );
  714. }
  715. void CParticleSystemPreviewPanel::OnBackgroundColorCancel( KeyValues *pParams )
  716. {
  717. m_pParticleSystemPanel->SetBackgroundColor( pParams->GetColor( "startingColor" ) );
  718. }
  719. //-----------------------------------------------------------------------------
  720. // Called when buttons are clicked
  721. //-----------------------------------------------------------------------------
  722. void CParticleSystemPreviewPanel::OnCommand( const char *pCommand )
  723. {
  724. if ( !Q_stricmp( pCommand, "StopEffect" ) )
  725. {
  726. m_pParticleSystemPanel->StopEffect();
  727. return;
  728. }
  729. BaseClass::OnCommand( pCommand );
  730. }