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.

120 lines
3.0 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose: This is a panel which is rendered image on top of an entity
  4. //
  5. // $Revision: $
  6. // $NoKeywords: $
  7. //=============================================================================//
  8. #include "cbase.h"
  9. #pragma warning (disable: 4514)
  10. #include "vgui_bitmapimage.h"
  11. #include "vgui_bitmapbutton.h"
  12. #include <KeyValues.h>
  13. // memdbgon must be the last include file in a .cpp file!!!
  14. #include "tier0/memdbgon.h"
  15. //-----------------------------------------------------------------------------
  16. // Constructor
  17. //-----------------------------------------------------------------------------
  18. CBitmapButton::CBitmapButton( vgui::Panel *pParent, const char *pName, const char *pText ) :
  19. BaseClass( pParent, pName, pText )
  20. {
  21. SetPaintBackgroundEnabled( false );
  22. for ( int i = 0; i < BUTTON_STATE_COUNT; ++i )
  23. {
  24. m_bImageLoaded[i] = false;
  25. }
  26. }
  27. CBitmapButton::~CBitmapButton()
  28. {
  29. }
  30. //-----------------------------------------------------------------------------
  31. // initialization
  32. //-----------------------------------------------------------------------------
  33. bool CBitmapButton::Init( KeyValues* pInitData )
  34. {
  35. // Unimplemented
  36. Assert( 0 );
  37. return true;
  38. }
  39. //-----------------------------------------------------------------------------
  40. // initialization from build-mode dialog style .res files
  41. //-----------------------------------------------------------------------------
  42. void CBitmapButton::ApplySettings(KeyValues *pInitData)
  43. {
  44. BaseClass::ApplySettings(pInitData);
  45. COMPILE_TIME_ASSERT( BUTTON_STATE_COUNT == 4 );
  46. const char *pSectionName[BUTTON_STATE_COUNT] =
  47. {
  48. "enabledImage",
  49. "mouseOverImage",
  50. "pressedImage",
  51. "disabledImage"
  52. };
  53. for ( int i = 0; i < BUTTON_STATE_COUNT; ++i )
  54. {
  55. m_bImageLoaded[i] = InitializeImage(pInitData, pSectionName[i], this, &m_pImage[i] );
  56. }
  57. }
  58. //-----------------------------------------------------------------------------
  59. // Sets the image
  60. //-----------------------------------------------------------------------------
  61. void CBitmapButton::SetImage( ButtonImageType_t type, const char *pMaterialName, color32 color )
  62. {
  63. m_bImageLoaded[type] = m_pImage[type].Init( GetVPanel(), pMaterialName );
  64. if (m_bImageLoaded[type])
  65. {
  66. Color vcol( color.r, color.g, color.b, color.a );
  67. m_pImage[type].SetColor( vcol );
  68. }
  69. }
  70. bool CBitmapButton::IsImageLoaded( ButtonImageType_t type ) const
  71. {
  72. return m_bImageLoaded[type];
  73. }
  74. //-----------------------------------------------------------------------------
  75. // Draws the puppy
  76. //-----------------------------------------------------------------------------
  77. void CBitmapButton::Paint( void )
  78. {
  79. // Determine the image to use based on the state
  80. int nCurrentImage = BUTTON_ENABLED;
  81. if (IsArmed())
  82. {
  83. if (IsDepressed())
  84. {
  85. nCurrentImage = BUTTON_PRESSED;
  86. }
  87. else
  88. {
  89. nCurrentImage = BUTTON_ENABLED_MOUSE_OVER;
  90. }
  91. }
  92. else if (!IsEnabled())
  93. {
  94. nCurrentImage = BUTTON_DISABLED;
  95. }
  96. if (m_bImageLoaded[nCurrentImage])
  97. {
  98. m_pImage[nCurrentImage].DoPaint( GetVPanel() );
  99. }
  100. BaseClass::Paint();
  101. }