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.

255 lines
6.3 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef IMAGE_MOUSE_OVER_BUTTON_H
  8. #define IMAGE_MOUSE_OVER_BUTTON_H
  9. #include "vgui/ISurface.h"
  10. #include "vgui/IScheme.h"
  11. #include "mouseoverpanelbutton.h"
  12. //===============================================
  13. // CImageMouseOverButton - used for class images
  14. //===============================================
  15. template <class T>
  16. class CImageMouseOverButton : public MouseOverButton<T>
  17. {
  18. private:
  19. //DECLARE_CLASS_SIMPLE( CImageMouseOverButton, MouseOverButton );
  20. public:
  21. CImageMouseOverButton( vgui::Panel *parent, const char *panelName, T *templatePanel );
  22. virtual void ApplySettings( KeyValues *inResourceData );
  23. virtual void ApplySchemeSettings( vgui::IScheme *pScheme );
  24. virtual void OnSizeChanged( int newWide, int newTall );
  25. void RecalculateImageSizes( void );
  26. void SetActiveImage( const char *imagename );
  27. void SetInactiveImage( const char *imagename );
  28. void SetActiveImage( vgui::IImage *image );
  29. void SetInactiveImage( vgui::IImage *image );
  30. public:
  31. virtual void Paint();
  32. virtual void ShowPage( void );
  33. virtual void HidePage( void );
  34. private:
  35. vgui::IImage *m_pActiveImage;
  36. char *m_pszActiveImageName;
  37. vgui::IImage *m_pInactiveImage;
  38. char *m_pszInactiveImageName;
  39. bool m_bScaleImage;
  40. };
  41. template <class T>
  42. CImageMouseOverButton<T>::CImageMouseOverButton( vgui::Panel *parent, const char *panelName, T *templatePanel ) :
  43. MouseOverButton<T>( parent, panelName, templatePanel )
  44. {
  45. m_pszActiveImageName = NULL;
  46. m_pszInactiveImageName = NULL;
  47. m_pActiveImage = NULL;
  48. m_pInactiveImage = NULL;
  49. }
  50. template <class T>
  51. void CImageMouseOverButton<T>::ApplySettings( KeyValues *inResourceData )
  52. {
  53. m_bScaleImage = inResourceData->GetInt( "scaleImage", 0 );
  54. // Active Image
  55. delete [] m_pszActiveImageName;
  56. m_pszActiveImageName = NULL;
  57. const char *activeImageName = inResourceData->GetString( "activeimage", "" );
  58. if ( *activeImageName )
  59. {
  60. this->SetActiveImage( activeImageName );
  61. }
  62. // Inactive Image
  63. delete [] m_pszInactiveImageName;
  64. m_pszInactiveImageName = NULL;
  65. const char *inactiveImageName = inResourceData->GetString( "inactiveimage", "" );
  66. if ( *inactiveImageName )
  67. {
  68. this->SetInactiveImage( inactiveImageName );
  69. }
  70. MouseOverButton<T>::ApplySettings( inResourceData );
  71. this->InvalidateLayout( false, true ); // force applyschemesettings to run
  72. }
  73. template <class T>
  74. void CImageMouseOverButton<T>::ApplySchemeSettings( vgui::IScheme *pScheme )
  75. {
  76. MouseOverButton<T>::ApplySchemeSettings( pScheme );
  77. if ( m_pszActiveImageName && strlen( m_pszActiveImageName ) > 0 )
  78. {
  79. this->SetActiveImage( vgui::scheme()->GetImage( m_pszActiveImageName, m_bScaleImage ) );
  80. }
  81. if ( m_pszInactiveImageName && strlen( m_pszInactiveImageName ) > 0 )
  82. {
  83. this->SetInactiveImage( vgui::scheme()->GetImage( m_pszInactiveImageName, m_bScaleImage ) );
  84. }
  85. vgui::IBorder *pBorder = pScheme->GetBorder( "NoBorder" );
  86. this->SetDefaultBorder( pBorder);
  87. this->SetDepressedBorder( pBorder );
  88. this->SetKeyFocusBorder( pBorder );
  89. Color blank(0,0,0,0);
  90. this->SetDefaultColor( this->GetButtonFgColor(), blank );
  91. this->SetArmedColor( this->GetButtonArmedFgColor(), blank );
  92. this->SetDepressedColor( this->GetButtonDepressedFgColor(), blank );
  93. }
  94. template <class T>
  95. void CImageMouseOverButton<T>::RecalculateImageSizes( void )
  96. {
  97. // Reset our images, which will force them to recalculate their size.
  98. // Necessary for images shared with other scaling buttons.
  99. this->SetActiveImage( m_pActiveImage );
  100. this->SetInactiveImage( m_pInactiveImage );
  101. }
  102. template <class T>
  103. void CImageMouseOverButton<T>::SetActiveImage( const char *imagename )
  104. {
  105. int len = Q_strlen( imagename ) + 1;
  106. m_pszActiveImageName = new char[ len ];
  107. Q_strncpy( m_pszActiveImageName, imagename, len );
  108. }
  109. template <class T>
  110. void CImageMouseOverButton<T>::SetInactiveImage( const char *imagename )
  111. {
  112. int len = Q_strlen( imagename ) + 1;
  113. m_pszInactiveImageName = new char[ len ];
  114. Q_strncpy( m_pszInactiveImageName, imagename, len );
  115. }
  116. template <class T>
  117. void CImageMouseOverButton<T>::SetActiveImage( vgui::IImage *image )
  118. {
  119. m_pActiveImage = image;
  120. if ( m_pActiveImage )
  121. {
  122. int wide, tall;
  123. if ( m_bScaleImage )
  124. {
  125. // scaling, force the image size to be our size
  126. this->GetSize( wide, tall );
  127. m_pActiveImage->SetSize( wide, tall );
  128. }
  129. else
  130. {
  131. // not scaling, so set our size to the image size
  132. m_pActiveImage->GetSize( wide, tall );
  133. this->SetSize( wide, tall );
  134. }
  135. }
  136. this->Repaint();
  137. }
  138. template <class T>
  139. void CImageMouseOverButton<T>::SetInactiveImage( vgui::IImage *image )
  140. {
  141. m_pInactiveImage = image;
  142. if ( m_pInactiveImage )
  143. {
  144. int wide, tall;
  145. if ( m_bScaleImage)
  146. {
  147. // scaling, force the image size to be our size
  148. this->GetSize( wide, tall );
  149. m_pInactiveImage->SetSize( wide, tall );
  150. }
  151. else
  152. {
  153. // not scaling, so set our size to the image size
  154. m_pInactiveImage->GetSize( wide, tall );
  155. this->SetSize( wide, tall );
  156. }
  157. }
  158. this->Repaint();
  159. }
  160. template <class T>
  161. void CImageMouseOverButton<T>::OnSizeChanged( int newWide, int newTall )
  162. {
  163. if ( m_bScaleImage )
  164. {
  165. // scaling, force the image size to be our size
  166. if ( m_pActiveImage )
  167. m_pActiveImage->SetSize( newWide, newTall );
  168. if ( m_pInactiveImage )
  169. m_pInactiveImage->SetSize( newWide, newTall );
  170. }
  171. MouseOverButton<T>::OnSizeChanged( newWide, newTall );
  172. }
  173. template <class T>
  174. void CImageMouseOverButton<T>::Paint()
  175. {
  176. this->SetActiveImage( m_pActiveImage );
  177. this->SetInactiveImage( m_pInactiveImage );
  178. if ( this->IsArmed() )
  179. {
  180. // draw the active image
  181. if ( m_pActiveImage )
  182. {
  183. vgui::surface()->DrawSetColor( 255, 255, 255, 255 );
  184. m_pActiveImage->SetPos( 0, 0 );
  185. m_pActiveImage->Paint();
  186. }
  187. }
  188. else
  189. {
  190. // draw the inactive image
  191. if ( m_pInactiveImage )
  192. {
  193. vgui::surface()->DrawSetColor( 255, 255, 255, 255 );
  194. m_pInactiveImage->SetPos( 0, 0 );
  195. m_pInactiveImage->Paint();
  196. }
  197. }
  198. MouseOverButton<T>::Paint();
  199. }
  200. template <class T>
  201. void CImageMouseOverButton<T>::ShowPage( void )
  202. {
  203. MouseOverButton<T>::ShowPage();
  204. // send message to parent that we triggered something
  205. this->PostActionSignal( new KeyValues( "ShowPage", "page", this->GetName() ) );
  206. }
  207. template <class T>
  208. void CImageMouseOverButton<T>::HidePage( void )
  209. {
  210. MouseOverButton<T>::HidePage();
  211. }
  212. #endif //IMAGE_MOUSE_OVER_BUTTON_H