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.

263 lines
7.4 KiB

  1. //=========== Copyright Valve Corporation, All rights reserved. ===============//
  2. //
  3. // Purpose:
  4. //=============================================================================//
  5. #ifndef BACKGROUNDIMAGE_H
  6. #define BACKGROUNDIMAGE_H
  7. #ifdef _WIN32
  8. #pragma once
  9. #endif
  10. #include "csshelpers.h"
  11. #include "../data/panoramavideoplayer.h"
  12. #include "utlstring.h"
  13. #include "uilength.h"
  14. namespace panorama
  15. {
  16. class IImageSource;
  17. class CMovie;
  18. class CPanel2D;
  19. //-----------------------------------------------------------------------------
  20. // Purpose: Background position for background image
  21. //-----------------------------------------------------------------------------
  22. class CBackgroundPosition
  23. {
  24. public:
  25. CBackgroundPosition();
  26. ~CBackgroundPosition() {}
  27. EHorizontalAlignment GetHorizontalAlignment() const { return m_eHorizontalAlignment; }
  28. CUILength GetHorizontalLength() const { return m_horizontal; }
  29. EVerticalAlignment GetVeriticalAlignment() const { return m_eVerticalAlignment; }
  30. CUILength GetVerticalLength() const { return m_vertical; }
  31. bool IsHorizontalSet() const
  32. {
  33. return ( m_eHorizontalAlignment != k_EHorizontalAlignmentUnset && m_horizontal.IsSet() );
  34. }
  35. bool IsVerticalSet() const
  36. {
  37. return ( m_eVerticalAlignment != k_EVerticalAlignmentUnset && m_vertical.IsSet() );
  38. }
  39. bool IsSet() const
  40. {
  41. return ( IsHorizontalSet() && IsVerticalSet() );
  42. }
  43. void Set( EHorizontalAlignment eHorizontal, const CUILength &horizontal, EVerticalAlignment eVertical, const CUILength &vertical );
  44. void ResolveDefaultValues();
  45. void ToString( CFmtStr1024 *pfmtBuffer );
  46. void ScaleLengthValues( float flScaleFactor )
  47. {
  48. m_horizontal.ScaleLengthValue( flScaleFactor );
  49. m_vertical.ScaleLengthValue( flScaleFactor );
  50. }
  51. bool operator==( const CBackgroundPosition &rhs ) const
  52. {
  53. return ( m_eHorizontalAlignment == rhs.m_eHorizontalAlignment && m_horizontal == rhs.m_horizontal && m_eVerticalAlignment == rhs.m_eVerticalAlignment && m_vertical == rhs.m_vertical );
  54. }
  55. bool operator!=( const CBackgroundPosition &rhs ) const
  56. {
  57. return !( *this == rhs );
  58. }
  59. private:
  60. EHorizontalAlignment m_eHorizontalAlignment;
  61. CUILength m_horizontal;
  62. EVerticalAlignment m_eVerticalAlignment;
  63. CUILength m_vertical;
  64. };
  65. //-----------------------------------------------------------------------------
  66. // Purpose: Background images have a repeat in the x & y directions
  67. //-----------------------------------------------------------------------------
  68. class CBackgroundRepeat
  69. {
  70. public:
  71. CBackgroundRepeat() { Set( k_EBackgroundRepeatUnset, k_EBackgroundRepeatUnset ); }
  72. ~CBackgroundRepeat() {}
  73. bool IsSet() const { return (m_eHorizontal != k_EBackgroundRepeatUnset && m_eVertical != k_EBackgroundRepeatUnset); }
  74. void Set( EBackgroundRepeat eHorizontal, EBackgroundRepeat eVertical )
  75. {
  76. m_eHorizontal = eHorizontal;
  77. m_eVertical = eVertical;
  78. }
  79. void ResolveDefaultValues()
  80. {
  81. if ( m_eHorizontal == k_EBackgroundRepeatUnset )
  82. m_eHorizontal = k_EBackgroundRepeatRepeat;
  83. if ( m_eVertical == k_EBackgroundRepeatUnset )
  84. m_eVertical = k_EBackgroundRepeatRepeat;
  85. }
  86. EBackgroundRepeat GetHorizontal() const { return m_eHorizontal; }
  87. EBackgroundRepeat GetVertical() const { return m_eVertical; }
  88. bool operator==( const CBackgroundRepeat &rhs ) const
  89. {
  90. return ( m_eHorizontal == rhs.m_eHorizontal && m_eVertical == rhs.m_eVertical );
  91. }
  92. bool operator!=( const CBackgroundRepeat &rhs ) const
  93. {
  94. return !( *this == rhs );
  95. }
  96. private:
  97. EBackgroundRepeat m_eHorizontal;
  98. EBackgroundRepeat m_eVertical;
  99. };
  100. //-----------------------------------------------------------------------------
  101. // Purpose: Can have multiple background image layers, each contains the following
  102. //-----------------------------------------------------------------------------
  103. class CBackgroundImageLayer
  104. {
  105. private:
  106. enum EImagePath
  107. {
  108. k_EImagePathUnset,
  109. k_EImagePathNone,
  110. k_EImagePathSet
  111. };
  112. public:
  113. CBackgroundImageLayer();
  114. ~CBackgroundImageLayer();
  115. void SetPathUnset()
  116. {
  117. m_eImagePath = k_EImagePathUnset;
  118. m_sURLPath.Clear();
  119. }
  120. void SetPathToNone()
  121. {
  122. m_eImagePath = m_eImagePath;
  123. m_sURLPath.Clear();
  124. }
  125. void SetPath( const char *pchPath )
  126. {
  127. m_eImagePath = k_EImagePathSet;
  128. m_sURLPath = pchPath;
  129. }
  130. bool IsPathSet() { return (m_eImagePath != k_EImagePathUnset); }
  131. bool IsPathNone() { return (m_eImagePath == k_EImagePathNone); }
  132. const char *GetPath() { return m_sURLPath.String(); }
  133. // from background-position
  134. const CBackgroundPosition &GetPosition() { return m_position; }
  135. void SetPosition( const CBackgroundPosition &position ) { m_position = position; }
  136. const CUILength &GetWidth() { return m_width; }
  137. const CUILength &GetHeight() { return m_height; }
  138. void SetBackgroundSize( const CUILength &width, const CUILength &height )
  139. {
  140. m_width = width;
  141. m_height = height;
  142. m_eBackgroundSizeConstant = k_EBackgroundSizeConstantNone;
  143. }
  144. EBackgroundSizeConstant GetBackgroundSizeConstant() { return m_eBackgroundSizeConstant; }
  145. void SetBackgroundSize( EBackgroundSizeConstant eConstant )
  146. {
  147. Assert( eConstant != k_EBackgroundSizeConstantNone );
  148. m_eBackgroundSizeConstant = eConstant;
  149. m_width.SetLength( k_flFloatAuto );
  150. m_height.SetLength( k_flFloatAuto );
  151. }
  152. CBackgroundRepeat GetRepeat() { return m_repeat; }
  153. void SetRepeat( const CBackgroundRepeat &repeat ) { m_repeat = repeat; }
  154. IImageSource *GetImage() { return m_pImage; }
  155. CVideoPlayerPtr GetMovie() { return m_pVideoPlayer; }
  156. bool IsCompletelyUnset()
  157. {
  158. return (m_eImagePath == k_EImagePathUnset && !m_position.IsSet() && !m_width.IsSet() && !m_height.IsSet() && !m_repeat.IsSet() );
  159. }
  160. bool IsSet()
  161. {
  162. return (m_eImagePath != k_EImagePathUnset && m_position.IsSet() && m_width.IsSet() && m_height.IsSet() && m_repeat.IsSet() );
  163. }
  164. void ResolveDefaultValues();
  165. void ApplyUIScaleFactor( float flScaleFactor );
  166. void OnAppliedToPanel( IUIPanel *pPanel );
  167. void MergeTo( CBackgroundImageLayer *pTarget );
  168. void ToString( CFmtStr1024 *pfmtBuffer );
  169. // comparison operators
  170. bool operator==( const CBackgroundImageLayer &rhs ) const
  171. {
  172. return ( m_eImagePath == rhs.m_eImagePath && m_sURLPath == rhs.m_sURLPath && m_position == rhs.m_position &&
  173. m_width == rhs.m_width&& m_height == rhs.m_height && m_repeat == rhs.m_repeat && m_eBackgroundSizeConstant == rhs.m_eBackgroundSizeConstant );
  174. }
  175. bool operator!=( const CBackgroundImageLayer &rhs ) const
  176. {
  177. return !( *this == rhs );
  178. }
  179. // helpers for drawing
  180. void CalculateFinalDimensions( float *pflWidth, float *pflHeight, float flPanelWidth, float flPanelHeight, float flScaleFactor );
  181. void CalculateFinalPosition( float *px, float *py, float flWidthPanel, float flHeightPanel, float flWidthImage, float flHeightImage );
  182. void CalculateFinalSpacing( float *px, float *py, float flWidthPanel, float flHeightPanel, float flWidthImage, float flHeightImage );
  183. void Set( const CBackgroundImageLayer &rhs );
  184. #ifdef DBGFLAG_VALIDATE
  185. virtual void Validate( CValidator &validator, const tchar *pchName );
  186. #endif
  187. private:
  188. CBackgroundImageLayer( const CBackgroundImageLayer &rhs );
  189. CBackgroundImageLayer& operator=( const CBackgroundImageLayer &rhs ) const;
  190. // from background-image
  191. CUtlString m_sURLPath;
  192. EImagePath m_eImagePath;
  193. // from background-position
  194. CBackgroundPosition m_position;
  195. // from background-size
  196. EBackgroundSizeConstant m_eBackgroundSizeConstant;
  197. CUILength m_width;
  198. CUILength m_height;
  199. // from background-repeat
  200. CBackgroundRepeat m_repeat;
  201. // loaded when applied to a panel
  202. IImageSource *m_pImage;
  203. CVideoPlayerPtr m_pVideoPlayer;
  204. };
  205. } // namespace panorama
  206. #endif //BACKGROUNDIMAGE_H