//========= Copyright © 1996-2006, Valve Corporation, All rights reserved. ============// // // Purpose: // // $NoKeywords: $ //=============================================================================// #ifndef IMAGE_MOUSE_OVER_BUTTON_H #define IMAGE_MOUSE_OVER_BUTTON_H #include "vgui/isurface.h" #include "mouseoverpanelbutton.h" //=============================================== // CImageMouseOverButton - used for class images //=============================================== template class CImageMouseOverButton : public MouseOverButton { private: //DECLARE_CLASS_SIMPLE( CImageMouseOverButton, MouseOverButton ); public: CImageMouseOverButton( vgui::Panel *parent, const char *panelName, T *templatePanel ); virtual void ApplySettings( KeyValues *inResourceData ); virtual void ApplySchemeSettings( vgui::IScheme *pScheme ); virtual void OnSizeChanged( int newWide, int newTall ); void RecalculateImageSizes( void ); void SetActiveImage( const char *imagename ); void SetInactiveImage( const char *imagename ); void SetActiveImage( vgui::IImage *image ); void SetInactiveImage( vgui::IImage *image ); public: virtual void Paint(); virtual void ShowPage( void ); virtual void HidePage( void ); private: vgui::IImage *m_pActiveImage; char *m_pszActiveImageName; vgui::IImage *m_pInactiveImage; char *m_pszInactiveImageName; bool m_bScaleImage; }; template CImageMouseOverButton::CImageMouseOverButton( vgui::Panel *parent, const char *panelName, T *templatePanel ) : MouseOverButton( parent, panelName, templatePanel ) { m_pszActiveImageName = NULL; m_pszInactiveImageName = NULL; m_pActiveImage = NULL; m_pInactiveImage = NULL; } template void CImageMouseOverButton::ApplySettings( KeyValues *inResourceData ) { m_bScaleImage = inResourceData->GetInt( "scaleImage", 0 ); // Active Image delete [] m_pszActiveImageName; m_pszActiveImageName = NULL; const char *activeImageName = inResourceData->GetString( "activeimage", "" ); if ( *activeImageName ) { SetActiveImage( activeImageName ); } // Inactive Image delete [] m_pszInactiveImageName; m_pszInactiveImageName = NULL; const char *inactiveImageName = inResourceData->GetString( "inactiveimage", "" ); if ( *inactiveImageName ) { SetInactiveImage( inactiveImageName ); } MouseOverButton::ApplySettings( inResourceData ); InvalidateLayout( false, true ); // force applyschemesettings to run } template void CImageMouseOverButton::ApplySchemeSettings( vgui::IScheme *pScheme ) { MouseOverButton::ApplySchemeSettings( pScheme ); if ( m_pszActiveImageName && strlen( m_pszActiveImageName ) > 0 ) { SetActiveImage(scheme()->GetImage( m_pszActiveImageName, m_bScaleImage ) ); } if ( m_pszInactiveImageName && strlen( m_pszInactiveImageName ) > 0 ) { SetInactiveImage(scheme()->GetImage( m_pszInactiveImageName, m_bScaleImage ) ); } IBorder *pBorder = pScheme->GetBorder( "NoBorder" ); SetDefaultBorder( pBorder); SetDepressedBorder( pBorder ); SetKeyFocusBorder( pBorder ); Color defaultFgColor = GetSchemeColor( "Button.TextColor", Color(255, 255, 255, 255), pScheme ); Color armedFgColor = GetSchemeColor( "Button.ArmedTextColor", Color(255, 255, 255, 255), pScheme ); Color depressedFgColor = GetSchemeColor( "Button.DepressedTextColor", Color(255, 255, 255, 255), pScheme ); Color blank(0,0,0,0); SetDefaultColor( defaultFgColor, blank ); SetArmedColor( armedFgColor, blank ); SetDepressedColor( depressedFgColor, blank ); } template void CImageMouseOverButton::RecalculateImageSizes( void ) { // Reset our images, which will force them to recalculate their size. // Necessary for images shared with other scaling buttons. SetActiveImage( m_pActiveImage ); SetInactiveImage( m_pInactiveImage ); } template void CImageMouseOverButton::SetActiveImage( const char *imagename ) { int len = Q_strlen( imagename ) + 1; m_pszActiveImageName = new char[ len ]; Q_strncpy( m_pszActiveImageName, imagename, len ); } template void CImageMouseOverButton::SetInactiveImage( const char *imagename ) { int len = Q_strlen( imagename ) + 1; m_pszInactiveImageName = new char[ len ]; Q_strncpy( m_pszInactiveImageName, imagename, len ); } template void CImageMouseOverButton::SetActiveImage( vgui::IImage *image ) { m_pActiveImage = image; if ( m_pActiveImage ) { int wide, tall; if ( m_bScaleImage ) { // scaling, force the image size to be our size GetSize( wide, tall ); m_pActiveImage->SetSize( wide, tall ); } else { // not scaling, so set our size to the image size m_pActiveImage->GetSize( wide, tall ); SetSize( wide, tall ); } } Repaint(); } template void CImageMouseOverButton::SetInactiveImage( vgui::IImage *image ) { m_pInactiveImage = image; if ( m_pInactiveImage ) { int wide, tall; if ( m_bScaleImage) { // scaling, force the image size to be our size GetSize( wide, tall ); m_pInactiveImage->SetSize( wide, tall ); } else { // not scaling, so set our size to the image size m_pInactiveImage->GetSize( wide, tall ); SetSize( wide, tall ); } } Repaint(); } template void CImageMouseOverButton::OnSizeChanged( int newWide, int newTall ) { if ( m_bScaleImage ) { // scaling, force the image size to be our size if ( m_pActiveImage ) m_pActiveImage->SetSize( newWide, newTall ); if ( m_pInactiveImage ) m_pInactiveImage->SetSize( newWide, newTall ); } MouseOverButton::OnSizeChanged( newWide, newTall ); } template void CImageMouseOverButton::Paint() { SetActiveImage( m_pActiveImage ); SetInactiveImage( m_pInactiveImage ); if ( IsArmed() ) { // draw the active image if ( m_pActiveImage ) { vgui::surface()->DrawSetColor( 255, 255, 255, 255 ); m_pActiveImage->SetPos( 0, 0 ); m_pActiveImage->Paint(); } } else { // draw the inactive image if ( m_pInactiveImage ) { vgui::surface()->DrawSetColor( 255, 255, 255, 255 ); m_pInactiveImage->SetPos( 0, 0 ); m_pInactiveImage->Paint(); } } MouseOverButton::Paint(); } template void CImageMouseOverButton::ShowPage( void ) { MouseOverButton::ShowPage(); // send message to parent that we triggered something PostActionSignal( new KeyValues( "ShowPage", "page", GetName() ) ); } template void CImageMouseOverButton::HidePage( void ) { MouseOverButton::HidePage(); // send message to parent that we triggered something PostActionSignal( new KeyValues( "ShowPage", "page", GetName() ) ); } #endif //IMAGE_MOUSE_OVER_BUTTON_H