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.

992 lines
31 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose: Classes/types to represent fill brushes of varius types
  4. //
  5. //=============================================================================//
  6. #ifndef FILLBRUSH_H
  7. #define FILLBRUSH_H
  8. #ifdef _WIN32
  9. #pragma once
  10. #endif
  11. #include <float.h>
  12. #include "color.h"
  13. #include "utlvector.h"
  14. #include "uilength.h"
  15. namespace panorama
  16. {
  17. //-----------------------------------------------------------------------------
  18. // Purpose: Represents a gradient color stop value
  19. //-----------------------------------------------------------------------------
  20. class CGradientColorStop
  21. {
  22. public:
  23. CGradientColorStop()
  24. {
  25. m_flPercent = 0.0f;
  26. m_color = Color( 0, 0, 0, 0 );
  27. }
  28. CGradientColorStop( float flPercent, Color color )
  29. {
  30. m_flPercent = clamp( flPercent, 0.0f, 1.0f );
  31. m_color = color;
  32. }
  33. void SetPosition( float flPercent )
  34. {
  35. m_flPercent = clamp( flPercent, 0.0f, 1.0f );
  36. }
  37. void SetColor( Color color )
  38. {
  39. m_color = color;
  40. }
  41. void SetColor( int r, int g, int b, int a )
  42. {
  43. m_color.SetColor( r, g, b, a );
  44. }
  45. float GetPosition() const { return m_flPercent; }
  46. Color GetColor() const { return m_color; }
  47. bool operator==( const CGradientColorStop &rhs ) const
  48. {
  49. return ( m_flPercent == rhs.m_flPercent && m_color == rhs.m_color );
  50. }
  51. bool operator!=( const CGradientColorStop &rhs ) const
  52. {
  53. return !(*this == rhs);
  54. }
  55. private:
  56. // 0.0->1.0f
  57. float m_flPercent;
  58. // rgba color
  59. Color m_color;
  60. };
  61. //-----------------------------------------------------------------------------
  62. // Purpose: Represents a particle system
  63. //-----------------------------------------------------------------------------
  64. class CParticleSystem
  65. {
  66. public:
  67. // Default constructor, sets empty system
  68. CParticleSystem()
  69. {
  70. SetParticleSystem( Vector( 0.0f, 0.0f, 0.0f ), Vector( 0.0f, 0.0f, 0.0f ), 0.0f, 0.0f, 0.0f, 0.0f,
  71. 0.0f, 0.0f, Vector( 0.0f, 0.0f, 0.0f ), Vector( 0.0f, 0.0f, 0.0f ),
  72. Vector( -9999999.0f, -9999999.0f, -9999999.0f ), Vector( 9999999.0f, 9999999.0f, 9999999.0f ),
  73. Vector( 0.0f, 0.0f, 0.0f ), Vector( 0.0f, 0.0f, 0.0f ),
  74. Color( 0, 0, 0, 0 ), Color( 0, 0, 0, 0 ), Color( 0, 0, 0, 0 ), Color (0, 0, 0, 0 ), 1.0, 0.0f, 0.0f, 0.0f );
  75. }
  76. CParticleSystem( Vector vecBasePositon, Vector vecBasePositionVariance, float flSize, float flSizeVariance, float flParticlesPerSecond, float flParticlesPerSecondVariance,
  77. float flLifeSpanSeconds, float flLifeSpanSecondsVariance, Vector vecInitialVelocity, Vector vecInitialVelocityVariance, Vector vecVelocityMin, Vector vecVelocityMax,
  78. Vector vecGravityAcceleration, Vector vecGravityAccelerationParticleVariance,
  79. Color colorStart, Color colorStartVariance, Color colorEnd, Color colorEndVariance, float flSharpness, float flSharpnessVariance, float flFlicker, float flFlickerVariance )
  80. {
  81. SetParticleSystem( vecBasePositon, vecBasePositionVariance, flSize, flSizeVariance, flParticlesPerSecond, flParticlesPerSecondVariance,
  82. flLifeSpanSeconds, flLifeSpanSecondsVariance, vecInitialVelocity, vecInitialVelocityVariance, vecVelocityMin, vecVelocityMax,
  83. vecGravityAcceleration, vecGravityAccelerationParticleVariance,
  84. colorStart, colorStartVariance, colorEnd, colorEndVariance, flSharpness, flSharpnessVariance, flFlicker, flFlickerVariance );
  85. }
  86. void SetParticleSystem( Vector vecBasePositon, Vector vecBasePositionVariance, float flSize, float flSizeVariance, float flParticlesPerSecond, float flParticlesPerSecondVariance,
  87. float flLifeSpanSeconds, float flLifeSpanSecondsVariance, Vector vecInitialVelocity, Vector vecInitialVelocityVariance, Vector vecVelocityMin, Vector vecVelocityMax,
  88. Vector vecGravityAcceleration, Vector vecGravityAccelerationParticleVariance, Color colorStart, Color colorStartVariance, Color colorEnd, Color colorEndVariance,
  89. float flSharpness, float flSharpnessVariance, float flFlicker, float flFlickerVariance )
  90. {
  91. m_vecBasePosition = vecBasePositon;
  92. m_vecBasePositionVariance = vecBasePositionVariance;
  93. m_flParticleSize = flSize;
  94. m_flParticleSizeVariance = flSizeVariance;
  95. m_flParticlesPerSecond = flParticlesPerSecond;
  96. m_flParticlesPerSecondVariance = flParticlesPerSecondVariance;
  97. m_flParticleLifeSpanSeconds = flLifeSpanSeconds;
  98. m_flParticleLifeSpanSecondsVariance = flLifeSpanSecondsVariance;
  99. m_vecParticleInitialVelocity = vecInitialVelocity;
  100. m_vecParticleInitialVelocityVariance = vecInitialVelocityVariance;
  101. m_vecVelocityMin = vecVelocityMin;
  102. m_vecVelocityMax = vecVelocityMax;
  103. m_vecGravityAcceleration = vecGravityAcceleration;
  104. m_vecGravityAccelerationParticleVariance = vecGravityAccelerationParticleVariance;
  105. m_colorStartRGBA = colorStart;
  106. m_colorStartRGBAVariance = colorStartVariance;
  107. m_colorEndRGBA = colorEnd;
  108. m_colorEndRGBAVariance = colorEndVariance;
  109. m_flSharpness = flSharpness;
  110. m_flSharpnessVariance = flSharpnessVariance;
  111. m_flFlicker = flFlicker;
  112. m_flFlickerVariance = flFlickerVariance;
  113. }
  114. bool operator==( const CParticleSystem &rhs ) const
  115. {
  116. if ( m_vecBasePosition != rhs.m_vecBasePosition )
  117. return false;
  118. if ( m_vecBasePositionVariance != rhs.m_vecBasePositionVariance )
  119. return false;
  120. if ( m_flParticleSize != rhs.m_flParticleSize )
  121. return false;
  122. if ( m_flParticleSizeVariance != rhs.m_flParticleSizeVariance )
  123. return false;
  124. if ( m_flParticlesPerSecond != rhs.m_flParticlesPerSecond )
  125. return false;
  126. if ( m_flParticlesPerSecondVariance != rhs.m_flParticlesPerSecondVariance )
  127. return false;
  128. if ( m_flParticleLifeSpanSeconds != rhs.m_flParticleLifeSpanSeconds )
  129. return false;
  130. if ( m_flParticleLifeSpanSecondsVariance != rhs.m_flParticleLifeSpanSecondsVariance )
  131. return false;
  132. if ( m_vecParticleInitialVelocity != rhs.m_vecParticleInitialVelocity )
  133. return false;
  134. if ( m_vecParticleInitialVelocityVariance != rhs.m_vecParticleInitialVelocityVariance )
  135. return false;
  136. if ( m_vecGravityAcceleration != rhs.m_vecGravityAcceleration )
  137. return false;
  138. if ( m_vecGravityAccelerationParticleVariance != rhs.m_vecGravityAccelerationParticleVariance )
  139. return false;
  140. if ( m_colorStartRGBA != rhs.m_colorStartRGBA )
  141. return false;
  142. if ( m_colorStartRGBAVariance != rhs.m_colorStartRGBAVariance )
  143. return false;
  144. if ( m_colorEndRGBA != rhs.m_colorEndRGBA )
  145. return false;
  146. if ( m_colorEndRGBAVariance != rhs.m_colorEndRGBAVariance )
  147. return false;
  148. if ( m_flSharpness != rhs.m_flSharpness )
  149. return false;
  150. if ( m_flSharpnessVariance != rhs.m_flSharpnessVariance )
  151. return false;
  152. if ( m_flFlicker != rhs.m_flFlicker )
  153. return false;
  154. if ( m_flFlickerVariance != rhs.m_flFlickerVariance )
  155. return false;
  156. if ( m_vecVelocityMin != rhs.m_vecVelocityMin )
  157. return false;
  158. if ( m_vecVelocityMax != rhs.m_vecVelocityMax )
  159. return false;
  160. return true;
  161. }
  162. void Interpolate( float flProgress, CParticleSystem &target )
  163. {
  164. m_vecBasePosition.x = Lerp( flProgress, m_vecBasePosition.x, target.m_vecBasePosition.x );
  165. m_vecBasePosition.y = Lerp( flProgress, m_vecBasePosition.y, target.m_vecBasePosition.y );
  166. m_vecBasePosition.z = Lerp( flProgress, m_vecBasePosition.z, target.m_vecBasePosition.z );
  167. m_vecBasePositionVariance.x = Lerp( flProgress, m_vecBasePositionVariance.x, target.m_vecBasePositionVariance.x );
  168. m_vecBasePositionVariance.y = Lerp( flProgress, m_vecBasePositionVariance.y, target.m_vecBasePositionVariance.y );
  169. m_vecBasePositionVariance.z = Lerp( flProgress, m_vecBasePositionVariance.z, target.m_vecBasePositionVariance.z );
  170. m_flParticleSize = Lerp( flProgress, m_flParticleSize, target.m_flParticleSize );
  171. m_flParticleSizeVariance = Lerp( flProgress, m_flParticleSizeVariance, target.m_flParticleSizeVariance );
  172. m_flParticlesPerSecond = Lerp( flProgress, m_flParticlesPerSecond, target.m_flParticlesPerSecond );
  173. m_flParticlesPerSecondVariance = Lerp( flProgress, m_flParticlesPerSecondVariance, target.m_flParticlesPerSecondVariance );
  174. m_vecParticleInitialVelocity.x = Lerp( flProgress, m_vecParticleInitialVelocity.x, target.m_vecParticleInitialVelocity.x );
  175. m_vecParticleInitialVelocity.y = Lerp( flProgress, m_vecParticleInitialVelocity.y, target.m_vecParticleInitialVelocity.y );
  176. m_vecParticleInitialVelocity.z = Lerp( flProgress, m_vecParticleInitialVelocity.z, target.m_vecParticleInitialVelocity.z );
  177. m_vecParticleInitialVelocityVariance.x = Lerp( flProgress, m_vecParticleInitialVelocityVariance.x, target.m_vecParticleInitialVelocityVariance.x );
  178. m_vecParticleInitialVelocityVariance.y = Lerp( flProgress, m_vecParticleInitialVelocityVariance.y, target.m_vecParticleInitialVelocityVariance.y );
  179. m_vecParticleInitialVelocityVariance.z = Lerp( flProgress, m_vecParticleInitialVelocityVariance.z, target.m_vecParticleInitialVelocityVariance.z );
  180. m_vecVelocityMin.x = Lerp( flProgress, m_vecVelocityMin.x, target.m_vecVelocityMin.x );
  181. m_vecVelocityMin.y = Lerp( flProgress, m_vecVelocityMin.y, target.m_vecVelocityMin.y );
  182. m_vecVelocityMax.x = Lerp( flProgress, m_vecVelocityMax.x, target.m_vecVelocityMax.x );
  183. m_vecVelocityMax.y = Lerp( flProgress, m_vecVelocityMax.y, target.m_vecVelocityMax.y );
  184. m_vecGravityAcceleration.x = Lerp( flProgress, m_vecGravityAcceleration.x, target.m_vecGravityAcceleration.x );
  185. m_vecGravityAcceleration.y = Lerp( flProgress, m_vecGravityAcceleration.y, target.m_vecGravityAcceleration.y );
  186. m_vecGravityAcceleration.z = Lerp( flProgress, m_vecGravityAcceleration.z, target.m_vecGravityAcceleration.z );
  187. m_vecGravityAccelerationParticleVariance.x = Lerp( flProgress, m_vecGravityAccelerationParticleVariance.x, target.m_vecGravityAccelerationParticleVariance.x );
  188. m_vecGravityAccelerationParticleVariance.y = Lerp( flProgress, m_vecGravityAccelerationParticleVariance.y, target.m_vecGravityAccelerationParticleVariance.y );
  189. m_vecGravityAccelerationParticleVariance.z = Lerp( flProgress, m_vecGravityAccelerationParticleVariance.z, target.m_vecGravityAccelerationParticleVariance.z );
  190. m_colorStartRGBA.SetColor(
  191. Lerp( flProgress, m_colorStartRGBA.r(), target.m_colorStartRGBA.r() ),
  192. Lerp( flProgress, m_colorStartRGBA.g(), target.m_colorStartRGBA.g() ),
  193. Lerp( flProgress, m_colorStartRGBA.b(), target.m_colorStartRGBA.b() ),
  194. Lerp( flProgress, m_colorStartRGBA.a(), target.m_colorStartRGBA.a() )
  195. );
  196. m_colorStartRGBAVariance.SetColor(
  197. Lerp( flProgress, m_colorStartRGBAVariance.r(), target.m_colorStartRGBAVariance.r() ),
  198. Lerp( flProgress, m_colorStartRGBAVariance.g(), target.m_colorStartRGBAVariance.g() ),
  199. Lerp( flProgress, m_colorStartRGBAVariance.b(), target.m_colorStartRGBAVariance.b() ),
  200. Lerp( flProgress, m_colorStartRGBAVariance.a(), target.m_colorStartRGBAVariance.a() )
  201. );
  202. m_colorEndRGBA.SetColor(
  203. Lerp( flProgress, m_colorEndRGBA.r(), target.m_colorEndRGBA.r() ),
  204. Lerp( flProgress, m_colorEndRGBA.g(), target.m_colorEndRGBA.g() ),
  205. Lerp( flProgress, m_colorEndRGBA.b(), target.m_colorEndRGBA.b() ),
  206. Lerp( flProgress, m_colorEndRGBA.a(), target.m_colorEndRGBA.a() )
  207. );
  208. m_colorEndRGBAVariance.SetColor(
  209. Lerp( flProgress, m_colorEndRGBAVariance.r(), target.m_colorEndRGBAVariance.r() ),
  210. Lerp( flProgress, m_colorEndRGBAVariance.g(), target.m_colorEndRGBAVariance.g() ),
  211. Lerp( flProgress, m_colorEndRGBAVariance.b(), target.m_colorEndRGBAVariance.b() ),
  212. Lerp( flProgress, m_colorEndRGBAVariance.a(), target.m_colorEndRGBAVariance.a() )
  213. );
  214. m_flSharpness = Lerp( flProgress, m_flSharpness, target.m_flSharpness );
  215. m_flSharpnessVariance = Lerp( flProgress, m_flSharpnessVariance, target.m_flSharpnessVariance );
  216. m_flFlicker = Lerp( flProgress, m_flFlicker, target.m_flFlicker );
  217. m_flFlickerVariance = Lerp( flProgress, m_flFlickerVariance, target.m_flFlickerVariance );
  218. }
  219. void ScaleLengthValues( float flScaleFactor )
  220. {
  221. // Scale all X/Y size/position values
  222. m_vecBasePosition.x *= flScaleFactor;
  223. m_vecBasePosition.y *= flScaleFactor;
  224. m_vecBasePositionVariance.x *= flScaleFactor;
  225. m_vecBasePositionVariance.y *= flScaleFactor;
  226. m_flParticleSize *= flScaleFactor;
  227. m_flParticleSizeVariance *= flScaleFactor;
  228. m_vecParticleInitialVelocity.x *= flScaleFactor;
  229. m_vecParticleInitialVelocity.y *= flScaleFactor;
  230. m_vecParticleInitialVelocityVariance.x *= flScaleFactor;
  231. m_vecParticleInitialVelocityVariance.y *= flScaleFactor;
  232. m_vecGravityAcceleration.x *= flScaleFactor;
  233. m_vecGravityAcceleration.y *= flScaleFactor;
  234. m_vecGravityAccelerationParticleVariance.x *= flScaleFactor;
  235. m_vecGravityAccelerationParticleVariance.y *= flScaleFactor;
  236. m_vecVelocityMin.x *= flScaleFactor;
  237. m_vecVelocityMin.y *= flScaleFactor;
  238. m_vecVelocityMax.x *= flScaleFactor;
  239. m_vecVelocityMax.y *= flScaleFactor;
  240. }
  241. Vector GetBasePosition() const
  242. {
  243. return m_vecBasePosition;
  244. }
  245. Vector GetBasePositionVariance() const
  246. {
  247. return m_vecBasePositionVariance;
  248. }
  249. float GetParticleSize() const
  250. {
  251. return m_flParticleSize;
  252. }
  253. float GetParticleSizeVariance() const
  254. {
  255. return m_flParticleSizeVariance;
  256. }
  257. float GetParticlesPerSecond() const
  258. {
  259. return m_flParticlesPerSecond;
  260. }
  261. float GetParticlesPerSecondVariance() const
  262. {
  263. return m_flParticlesPerSecondVariance;
  264. }
  265. float GetParticleLifeSpanSeconds() const
  266. {
  267. return m_flParticleLifeSpanSeconds;
  268. }
  269. float GetParticleLifeSpanSecondsVariance() const
  270. {
  271. return m_flParticleLifeSpanSecondsVariance;
  272. }
  273. Vector GetParticleInitialVelocity() const
  274. {
  275. return m_vecParticleInitialVelocity;
  276. }
  277. Vector GetParticleInitialVelocityVariance() const
  278. {
  279. return m_vecParticleInitialVelocityVariance;
  280. }
  281. Vector GetParticleVelocityMin() const
  282. {
  283. return m_vecVelocityMin;
  284. }
  285. Vector GetParticleVelocityMax() const
  286. {
  287. return m_vecVelocityMax;
  288. }
  289. Vector GetGravityAcceleration() const
  290. {
  291. return m_vecGravityAcceleration;
  292. }
  293. Vector GetGravityAccelerationParticleVariance() const
  294. {
  295. return m_vecGravityAccelerationParticleVariance;
  296. }
  297. Color GetStartColor() const
  298. {
  299. return m_colorStartRGBA;
  300. }
  301. Color GetStartColorVariance() const
  302. {
  303. return m_colorStartRGBAVariance;
  304. }
  305. Color GetEndColor() const
  306. {
  307. return m_colorEndRGBA;
  308. }
  309. Color GetEndColorVariance() const
  310. {
  311. return m_colorEndRGBAVariance;
  312. }
  313. float GetSharpness() const
  314. {
  315. return m_flSharpness;
  316. }
  317. float GetSharpnessVariance() const
  318. {
  319. return m_flSharpnessVariance;
  320. }
  321. float GetFlicker() const
  322. {
  323. return m_flFlicker;
  324. }
  325. float GetFlickerVariance() const
  326. {
  327. return m_flFlickerVariance;
  328. }
  329. #ifdef DBGFLAG_VALIDATE
  330. virtual void Validate( CValidator &validator, const tchar *pchName )
  331. {
  332. VALIDATE_SCOPE();
  333. }
  334. #endif
  335. private:
  336. Vector m_vecBasePosition;
  337. Vector m_vecBasePositionVariance;
  338. float m_flParticleSize;
  339. float m_flParticleSizeVariance;
  340. float m_flParticlesPerSecond;
  341. float m_flParticlesPerSecondVariance;
  342. float m_flParticleLifeSpanSeconds;
  343. float m_flParticleLifeSpanSecondsVariance;
  344. Vector m_vecParticleInitialVelocity;
  345. Vector m_vecParticleInitialVelocityVariance;
  346. Vector m_vecVelocityMin;
  347. Vector m_vecVelocityMax;
  348. Vector m_vecGravityAcceleration;
  349. Vector m_vecGravityAccelerationParticleVariance;
  350. Color m_colorStartRGBA;
  351. Color m_colorStartRGBAVariance;
  352. Color m_colorEndRGBA;
  353. Color m_colorEndRGBAVariance;
  354. float m_flSharpness;
  355. float m_flSharpnessVariance;
  356. float m_flFlicker;
  357. float m_flFlickerVariance;
  358. };
  359. //-----------------------------------------------------------------------------
  360. // Purpose: Represents a linear gradient
  361. //-----------------------------------------------------------------------------
  362. class CLinearGradient
  363. {
  364. public:
  365. CLinearGradient()
  366. {
  367. m_StartPoint[0].SetLength( 0.0 );
  368. m_StartPoint[1].SetLength( 0.0 );
  369. m_EndPoint[0].SetLength( 0.0 );
  370. m_EndPoint[1].SetLength( 0.0 );
  371. }
  372. CLinearGradient( CUILength StartX, CUILength StartY, CUILength EndX, CUILength EndY, const CUtlVector<CGradientColorStop> &vecStopColors )
  373. {
  374. SetGradient( StartX, StartY, EndX, EndY, vecStopColors );
  375. }
  376. void SetGradient( CUILength StartX, CUILength StartY, CUILength EndX, CUILength EndY, const CUtlVector<CGradientColorStop> &vecStopColors )
  377. {
  378. m_StartPoint[0] = StartX;
  379. m_StartPoint[1] = StartY;
  380. m_EndPoint[0] = EndX;
  381. m_EndPoint[1] = EndY;
  382. m_vecStops.AddMultipleToTail( vecStopColors.Count(), vecStopColors.Base() );
  383. }
  384. void SetControlPoints( CUILength StartX, CUILength StartY, CUILength EndX, CUILength EndY )
  385. {
  386. m_StartPoint[0] = StartX;
  387. m_StartPoint[1] = StartY;
  388. m_EndPoint[0] = EndX;
  389. m_EndPoint[1] = EndY;
  390. }
  391. void GetStartPoint( CUILength &startX, CUILength &startY ) const
  392. {
  393. startX = m_StartPoint[0];
  394. startY = m_StartPoint[1];
  395. }
  396. void GetEndPoint( CUILength &endX, CUILength &endY ) const
  397. {
  398. endX = m_EndPoint[0];
  399. endY = m_EndPoint[1];
  400. }
  401. const CUtlVector<CGradientColorStop> &AccessStopColors() const
  402. {
  403. return m_vecStops;
  404. }
  405. CUtlVector<CGradientColorStop> &AccessMutableStopColors()
  406. {
  407. return m_vecStops;
  408. }
  409. bool operator==( const CLinearGradient &rhs ) const
  410. {
  411. if ( m_StartPoint[0] != rhs.m_StartPoint[0] || m_StartPoint[1] != rhs.m_StartPoint[1] || m_EndPoint[0] != rhs.m_EndPoint[0] || m_EndPoint[1] != rhs.m_EndPoint[1] )
  412. return false;
  413. if ( m_vecStops.Count() != rhs.m_vecStops.Count() )
  414. return false;
  415. FOR_EACH_VEC( m_vecStops, i )
  416. {
  417. if ( m_vecStops[i] != rhs.m_vecStops[i] )
  418. return false;
  419. }
  420. return true;
  421. }
  422. void ScaleLengthValues( float flScaleFactor )
  423. {
  424. m_StartPoint[0].ScaleLengthValue( flScaleFactor );
  425. m_StartPoint[1].ScaleLengthValue( flScaleFactor );
  426. m_EndPoint[0].ScaleLengthValue( flScaleFactor );
  427. m_EndPoint[1].ScaleLengthValue( flScaleFactor );
  428. }
  429. #ifdef DBGFLAG_VALIDATE
  430. virtual void Validate( CValidator &validator, const tchar *pchName )
  431. {
  432. VALIDATE_SCOPE();
  433. ValidateObj( m_vecStops );
  434. }
  435. #endif
  436. private:
  437. // Start/end point determine angle vector
  438. CUILength m_StartPoint[2];
  439. CUILength m_EndPoint[2];
  440. CCopyableUtlVector<CGradientColorStop> m_vecStops;
  441. };
  442. //-----------------------------------------------------------------------------
  443. // Purpose: Represents a radial gradient
  444. //-----------------------------------------------------------------------------
  445. class CRadialGradient
  446. {
  447. public:
  448. CRadialGradient()
  449. {
  450. m_Center[0].SetLength( 0.0 );
  451. m_Center[1].SetLength( 0.0 );
  452. m_Offset[0].SetLength( 0.0 );
  453. m_Offset[1].SetLength( 0.0 );
  454. m_Radius[0].SetLength( 0.0 );
  455. m_Radius[1].SetLength( 0.0 );
  456. }
  457. CRadialGradient( CUILength centerX, CUILength centerY, CUILength offsetX, CUILength offsetY, CUILength radiusX, CUILength radiusY, const CUtlVector<CGradientColorStop> &vecStopColors )
  458. {
  459. SetGradient( centerX, centerY, offsetX, offsetY, radiusX, radiusY, vecStopColors );
  460. }
  461. void SetGradient( CUILength centerX, CUILength centerY, CUILength offsetX, CUILength offsetY, CUILength radiusX, CUILength radiusY, const CUtlVector<CGradientColorStop> &vecStopColors )
  462. {
  463. m_Center[0] = centerX;
  464. m_Center[1] = centerY;
  465. m_Offset[0] = offsetX;
  466. m_Offset[1] = offsetY;
  467. m_Radius[0] = radiusX;
  468. m_Radius[1] = radiusY;
  469. m_vecStops.AddMultipleToTail( vecStopColors.Count(), vecStopColors.Base() );
  470. }
  471. void GetCenterPoint( CUILength &centerX, CUILength &centerY ) const
  472. {
  473. centerX = m_Center[0];
  474. centerY = m_Center[1];
  475. }
  476. void GetOffsetDistance( CUILength &offsetX, CUILength &offsetY ) const
  477. {
  478. offsetX = m_Offset[0];
  479. offsetY = m_Offset[1];
  480. }
  481. void GetRadii( CUILength &radiusX, CUILength &radiusY ) const
  482. {
  483. radiusX = m_Radius[0];
  484. radiusY = m_Radius[1];
  485. }
  486. void SetCenterPoint( const CUILength &x, const CUILength &y )
  487. {
  488. m_Center[0] = x;
  489. m_Center[1] = y;
  490. }
  491. void SetOffsetDistance( const CUILength &x, const CUILength &y )
  492. {
  493. m_Offset[0] = x;
  494. m_Offset[1] = y;
  495. }
  496. void SetRadii( const CUILength &x, const CUILength &y )
  497. {
  498. m_Radius[0] = x;
  499. m_Radius[1] = y;
  500. }
  501. const CUtlVector<CGradientColorStop> &AccessStopColors() const
  502. {
  503. return m_vecStops;
  504. }
  505. CUtlVector<CGradientColorStop> &AccessMutableStopColors()
  506. {
  507. return m_vecStops;
  508. }
  509. bool operator==( const CRadialGradient &rhs ) const
  510. {
  511. if ( m_Center[0] != rhs.m_Center[0] || m_Center[1] != rhs.m_Center[1] ||
  512. m_Offset[0] != rhs.m_Offset[0] || m_Offset[1] != rhs.m_Offset[1] ||
  513. m_Radius[0] != rhs.m_Radius[0] || m_Radius[1] != rhs.m_Radius[1] )
  514. {
  515. return false;
  516. }
  517. if ( m_vecStops.Count() != rhs.m_vecStops.Count() )
  518. return false;
  519. FOR_EACH_VEC( m_vecStops, i )
  520. {
  521. if ( m_vecStops[i] != rhs.m_vecStops[i] )
  522. return false;
  523. }
  524. return true;
  525. }
  526. void ScaleLengthValues( float flScaleFactor )
  527. {
  528. m_Center[0].ScaleLengthValue( flScaleFactor );
  529. m_Center[1].ScaleLengthValue( flScaleFactor );
  530. m_Offset[0].ScaleLengthValue( flScaleFactor );
  531. m_Offset[1].ScaleLengthValue( flScaleFactor );
  532. m_Radius[0].ScaleLengthValue( flScaleFactor );
  533. m_Radius[1].ScaleLengthValue( flScaleFactor );
  534. }
  535. #ifdef DBGFLAG_VALIDATE
  536. virtual void Validate( CValidator &validator, const tchar *pchName )
  537. {
  538. VALIDATE_SCOPE();
  539. ValidateObj( m_vecStops );
  540. }
  541. #endif
  542. private:
  543. // Start/end point determine angle vector
  544. CUILength m_Center[2];
  545. CUILength m_Offset[2];
  546. CUILength m_Radius[2];
  547. CCopyableUtlVector<CGradientColorStop> m_vecStops;
  548. };
  549. //-----------------------------------------------------------------------------
  550. // Purpose: Represents a fill color/stroke, may be a gradient
  551. //-----------------------------------------------------------------------------
  552. class CFillBrush
  553. {
  554. public:
  555. // Types of strokes we support
  556. enum EStrokeType
  557. {
  558. k_EStrokeTypeFillColor,
  559. k_EStrokeTypeLinearGradient,
  560. k_EStrokeTypeRadialGradient,
  561. k_EStrokeTypeParticleSystem,
  562. };
  563. // Default constructor, sets transparent fill color
  564. CFillBrush()
  565. {
  566. m_eType = k_EStrokeTypeFillColor;
  567. m_FillColor.SetColor( 0, 0, 0, 0 );
  568. }
  569. // Constructor for standard color fill
  570. CFillBrush( int r, int g, int b, int a )
  571. {
  572. m_eType = k_EStrokeTypeFillColor;
  573. m_FillColor.SetColor( r, g, b, a );
  574. }
  575. // Constructor for linear gradient fill
  576. CFillBrush( CUILength StartX, CUILength StartY, CUILength EndX, CUILength EndY, const CUtlVector<CGradientColorStop> &vecStopColors )
  577. {
  578. m_eType = k_EStrokeTypeLinearGradient;
  579. m_LinearGradient.SetGradient( StartX, StartY, EndX, EndY, vecStopColors );
  580. }
  581. // Constructor for radial gradient
  582. CFillBrush( CUILength centerX, CUILength centerY, CUILength offsetX, CUILength offsetY, CUILength radiusX, CUILength radiusY, const CUtlVector<CGradientColorStop> &vecStopColors )
  583. {
  584. m_eType = k_EStrokeTypeRadialGradient;
  585. m_RadialGradient.SetGradient( centerX, centerY, offsetX, offsetY, radiusX, radiusY, vecStopColors );
  586. }
  587. // Constructor for particle system
  588. CFillBrush( Vector vecBasePositon, Vector vecBasePositionVariance, float flSize, float flSizeVariance, float flParticlesPerSecond, float flParticlesPerSecondVariance,
  589. float flLifeSpanSeconds, float flLifeSpanSecondsVariance, Vector vecInitialVelocity, Vector vecInitialVelocityVariance, Vector vecVelocityMin, Vector vecVelocityMax,
  590. Vector vecGravityAcceleration, Vector vecGravityAccelerationParticleVariance,
  591. Color colorStart, Color colorStartVariance, Color colorEnd, Color colorEndVariance, float flSharpness, float flSharpnessVariance, float flFlicker, float flFlickerVariance )
  592. {
  593. m_eType = k_EStrokeTypeParticleSystem;
  594. m_ParticleSystem.SetParticleSystem( vecBasePositon, vecBasePositionVariance, flSize, flSizeVariance, flParticlesPerSecond, flParticlesPerSecondVariance,
  595. flLifeSpanSeconds, flLifeSpanSecondsVariance, vecInitialVelocity, vecInitialVelocityVariance, vecVelocityMin, vecVelocityMax,
  596. vecGravityAcceleration, vecGravityAccelerationParticleVariance,
  597. colorStart, colorStartVariance, colorEnd, colorEndVariance, flSharpness, flSharpnessVariance, flFlicker, flFlickerVariance );
  598. }
  599. // Get type
  600. EStrokeType GetType() const { return m_eType; }
  601. // Set to a fill color value
  602. void SetToFillColor( Color color )
  603. {
  604. m_eType = k_EStrokeTypeFillColor;
  605. m_FillColor = color;
  606. }
  607. // Get fill color for fill stroke types
  608. Color GetFillColor() const
  609. {
  610. Assert( m_eType == k_EStrokeTypeFillColor );
  611. return m_FillColor;
  612. }
  613. // Set brush to a linear gradient value
  614. void SetToLinearGradient( const CUILength &startX, const CUILength &startY, const CUILength &endX, const CUILength &endY, const CUtlVector<CGradientColorStop> &vecColorStops )
  615. {
  616. m_eType = k_EStrokeTypeLinearGradient;
  617. m_LinearGradient.SetGradient( startX, startY, endX, endY, vecColorStops );
  618. }
  619. // Set to a radial gradient value
  620. void SetToRadialGradient( CUILength centerX, CUILength centerY, CUILength offsetX, CUILength offsetY, CUILength radiusX, CUILength radiusY, const CUtlVector<CGradientColorStop> &vecStopColors )
  621. {
  622. m_eType = k_EStrokeTypeRadialGradient;
  623. m_RadialGradient.SetGradient( centerX, centerY, offsetX, offsetY, radiusX, radiusY, vecStopColors );
  624. }
  625. // Set to particle system value
  626. void SetToParticleSystem( Vector vecBasePositon, Vector vecBasePositionVariance, float flSize, float flSizeVariance, float flParticlesPerSecond, float flParticlesPerSecondVariance,
  627. float flLifeSpanSeconds, float flLifeSpanSecondsVariance, Vector vecInitialVelocity, Vector vecInitialVelocityVariance, Vector vecVelocityMin, Vector vecVelocityMax,
  628. Vector vecGravityAcceleration, Vector vecGravityAccelerationParticleVariance,
  629. Color colorStart, Color colorStartVariance, Color colorEnd, Color colorEndVariance, float flSharpness, float flSharpnessVariance, float flFlicker, float flFlickerVariance )
  630. {
  631. m_eType = k_EStrokeTypeParticleSystem;
  632. m_ParticleSystem.SetParticleSystem( vecBasePositon, vecBasePositionVariance, flSize, flSizeVariance, flParticlesPerSecond, flParticlesPerSecondVariance,
  633. flLifeSpanSeconds, flLifeSpanSecondsVariance, vecInitialVelocity, vecInitialVelocityVariance, vecVelocityMin, vecVelocityMax, vecGravityAcceleration, vecGravityAccelerationParticleVariance,
  634. colorStart, colorStartVariance, colorEnd, colorEndVariance, flSharpness, flSharpnessVariance, flFlicker, flFlickerVariance );
  635. }
  636. // Get start/end pos for linear gradient types
  637. void GetStartAndEndPoints( CUILength &StartX, CUILength &StartY, CUILength &EndX, CUILength &EndY ) const
  638. {
  639. Assert( m_eType == k_EStrokeTypeLinearGradient );
  640. m_LinearGradient.GetStartPoint( StartX, StartY );
  641. m_LinearGradient.GetEndPoint( EndX, EndY );
  642. }
  643. // Access start/end pos for linear or radial gradient types
  644. const CUtlVector<CGradientColorStop> &AccessStopColors() const
  645. {
  646. Assert( m_eType == k_EStrokeTypeLinearGradient || m_eType == k_EStrokeTypeRadialGradient );
  647. if ( m_eType == k_EStrokeTypeLinearGradient )
  648. return m_LinearGradient.AccessStopColors();
  649. if ( m_eType == k_EStrokeTypeRadialGradient )
  650. return m_RadialGradient.AccessStopColors();
  651. // Otherwise this is bad, let's just return the hopefully empty linear vector
  652. return m_LinearGradient.AccessStopColors();
  653. }
  654. // Get start/end pos for linear gradient types
  655. void GetRadialGradientValues( CUILength &centerX, CUILength &centerY, CUILength &offsetX, CUILength &offsetY, CUILength &radiusX, CUILength &radiusY ) const
  656. {
  657. Assert( m_eType == k_EStrokeTypeRadialGradient );
  658. m_RadialGradient.GetCenterPoint( centerX, centerY );
  659. m_RadialGradient.GetOffsetDistance( offsetX, offsetY );
  660. m_RadialGradient.GetRadii( radiusX, radiusY );
  661. }
  662. CParticleSystem * AccessParticleSystem()
  663. {
  664. Assert( m_eType == k_EStrokeTypeParticleSystem );
  665. return &m_ParticleSystem;
  666. }
  667. bool Interpolate( float flActualWidth, float flActualHeight, CFillBrush &target, float flProgress );
  668. bool operator==( const CFillBrush &rhs ) const
  669. {
  670. if ( m_eType != rhs.m_eType )
  671. return false;
  672. switch( m_eType )
  673. {
  674. case k_EStrokeTypeFillColor:
  675. return m_FillColor == rhs.m_FillColor;
  676. case k_EStrokeTypeLinearGradient:
  677. return m_LinearGradient == rhs.m_LinearGradient;
  678. case k_EStrokeTypeRadialGradient:
  679. return m_RadialGradient == rhs.m_RadialGradient;
  680. case k_EStrokeTypeParticleSystem:
  681. return m_ParticleSystem == rhs.m_ParticleSystem;
  682. default:
  683. AssertMsg( false, "Invalid type on fillbrush" );
  684. return false;
  685. }
  686. }
  687. bool operator!=( const CFillBrush &rhs ) const
  688. {
  689. return !( *this == rhs );
  690. }
  691. void ScaleLengthValues( float flScaleFactor )
  692. {
  693. if ( m_eType == k_EStrokeTypeLinearGradient )
  694. m_LinearGradient.ScaleLengthValues( flScaleFactor );
  695. else if ( m_eType == k_EStrokeTypeRadialGradient )
  696. m_RadialGradient.ScaleLengthValues( flScaleFactor );
  697. else if ( m_eType == k_EStrokeTypeParticleSystem )
  698. m_ParticleSystem.ScaleLengthValues( flScaleFactor );
  699. }
  700. #ifdef DBGFLAG_VALIDATE
  701. virtual void Validate( CValidator &validator, const tchar *pchName )
  702. {
  703. VALIDATE_SCOPE();
  704. ValidateObj( m_LinearGradient );
  705. ValidateObj( m_RadialGradient );
  706. ValidateObj( m_ParticleSystem );
  707. }
  708. #endif
  709. private:
  710. void ConvertToRadialGradient();
  711. void ConvertToLinearGradient();
  712. void NormalizeStopCount( CUtlVector<CGradientColorStop> &vec, int nStopsNeeded, Color defaultColor );
  713. EStrokeType m_eType;
  714. Color m_FillColor;
  715. CLinearGradient m_LinearGradient;
  716. CRadialGradient m_RadialGradient;
  717. CParticleSystem m_ParticleSystem;
  718. };
  719. //-----------------------------------------------------------------------------
  720. // Purpose: Represents a collection of fill brushes, when filling geometry with
  721. // such a collection they should be applied in order and blended appropriately.
  722. //-----------------------------------------------------------------------------
  723. #define MAX_FILL_BRUSHES_PER_COLLECTION 8
  724. class CFillBrushCollection
  725. {
  726. public:
  727. struct FillBrush_t
  728. {
  729. CFillBrush m_Brush;
  730. float m_Opacity;
  731. bool operator==( const FillBrush_t &rhs ) const { return (m_Brush == rhs.m_Brush && m_Opacity == rhs.m_Opacity); }
  732. bool operator!=( const FillBrush_t &rhs ) const { return !(*this == rhs ); }
  733. };
  734. CFillBrushCollection() { }
  735. // Get brush count
  736. uint32 GetBrushCount() const { return m_vecFillBrushes.Count(); }
  737. // Access brush vector directly
  738. CUtlVectorFixed< FillBrush_t, MAX_FILL_BRUSHES_PER_COLLECTION > &AccessBrushes() { return m_vecFillBrushes; }
  739. const CUtlVectorFixed< FillBrush_t, MAX_FILL_BRUSHES_PER_COLLECTION> &AccessBrushes() const { return m_vecFillBrushes; }
  740. // Add brush to collection
  741. void AddFillBrush( const CFillBrush &brush, float opacity = 1.0 )
  742. {
  743. int i = m_vecFillBrushes.AddToTail();
  744. m_vecFillBrushes[i].m_Brush = brush;
  745. m_vecFillBrushes[i].m_Opacity = opacity;
  746. }
  747. void ScaleLengthValues( float flScaleFactor )
  748. {
  749. FOR_EACH_VEC( m_vecFillBrushes, i )
  750. {
  751. m_vecFillBrushes[i].m_Brush.ScaleLengthValues( flScaleFactor );
  752. }
  753. }
  754. int GetNumParticleSystems()
  755. {
  756. int nParticleSystems = 0;
  757. FOR_EACH_VEC( m_vecFillBrushes, i )
  758. {
  759. if ( m_vecFillBrushes[i].m_Brush.GetType() == CFillBrush::k_EStrokeTypeParticleSystem )
  760. ++nParticleSystems;
  761. }
  762. return nParticleSystems;
  763. }
  764. void Clear()
  765. {
  766. m_vecFillBrushes.RemoveAll();
  767. }
  768. // Interpolate between two fill brushes
  769. void Interpolate( float flActualWidth, float flActualHeight, const CFillBrushCollection &target, float flProgress );
  770. bool operator==( const CFillBrushCollection &rhs ) const;
  771. #ifdef DBGFLAG_VALIDATE
  772. void Validate( CValidator &validator, const tchar *pchName )
  773. {
  774. VALIDATE_SCOPE();
  775. ValidateObj( m_vecFillBrushes );
  776. FOR_EACH_VEC( m_vecFillBrushes, i )
  777. {
  778. ValidateObj( m_vecFillBrushes[i].m_Brush );
  779. }
  780. }
  781. #endif
  782. private:
  783. CCopyableUtlVectorFixed< FillBrush_t, MAX_FILL_BRUSHES_PER_COLLECTION > m_vecFillBrushes;
  784. };
  785. } // namespace panorama
  786. #endif // FILLBRUSH_H