Counter Strike : Global Offensive Source Code
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.

154 lines
4.2 KiB

  1. //========= Copyright � 1996-2005, 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. _doublePressedActionMessage = NULL;
  27. }
  28. CBitmapButton::~CBitmapButton()
  29. {
  30. if (_doublePressedActionMessage)
  31. {
  32. _doublePressedActionMessage->deleteThis();
  33. }
  34. }
  35. //-----------------------------------------------------------------------------
  36. // initialization
  37. //-----------------------------------------------------------------------------
  38. bool CBitmapButton::Init( KeyValues* pInitData )
  39. {
  40. // Unimplemented
  41. Assert( 0 );
  42. return true;
  43. }
  44. //-----------------------------------------------------------------------------
  45. // initialization from build-mode dialog style .res files
  46. //-----------------------------------------------------------------------------
  47. void CBitmapButton::ApplySettings(KeyValues *pInitData)
  48. {
  49. BaseClass::ApplySettings(pInitData);
  50. COMPILE_TIME_ASSERT( BUTTON_STATE_COUNT == 4 );
  51. const char *pSectionName[BUTTON_STATE_COUNT] =
  52. {
  53. "enabledImage",
  54. "mouseOverImage",
  55. "pressedImage",
  56. "disabledImage"
  57. };
  58. for ( int i = 0; i < BUTTON_STATE_COUNT; ++i )
  59. {
  60. m_bImageLoaded[i] = InitializeImage(pInitData, pSectionName[i], this, &m_pImage[i] );
  61. }
  62. }
  63. //-----------------------------------------------------------------------------
  64. // Sets the image
  65. //-----------------------------------------------------------------------------
  66. void CBitmapButton::SetImage( ButtonImageType_t type, const char *pMaterialName, color32 color )
  67. {
  68. m_bImageLoaded[type] = m_pImage[type].Init( GetVPanel(), pMaterialName );
  69. if (m_bImageLoaded[type])
  70. {
  71. Color vcol( color.r, color.g, color.b, color.a );
  72. m_pImage[type].SetColor( vcol );
  73. }
  74. }
  75. bool CBitmapButton::IsImageLoaded( ButtonImageType_t type ) const
  76. {
  77. return m_bImageLoaded[type];
  78. }
  79. //-----------------------------------------------------------------------------
  80. // Draws the puppy
  81. //-----------------------------------------------------------------------------
  82. void CBitmapButton::Paint( void )
  83. {
  84. // Determine the image to use based on the state
  85. int nCurrentImage = BUTTON_ENABLED;
  86. if (IsArmed())
  87. {
  88. if (IsDepressed())
  89. {
  90. nCurrentImage = BUTTON_PRESSED;
  91. }
  92. else
  93. {
  94. nCurrentImage = BUTTON_ENABLED_MOUSE_OVER;
  95. }
  96. }
  97. else if (!IsEnabled())
  98. {
  99. nCurrentImage = BUTTON_DISABLED;
  100. }
  101. if (m_bImageLoaded[nCurrentImage])
  102. {
  103. m_pImage[nCurrentImage].DoPaint( GetVPanel() );
  104. }
  105. BaseClass::Paint();
  106. }
  107. void CBitmapButton::OnMouseDoublePressed( vgui::MouseCode code )
  108. {
  109. BaseClass::OnMouseDoublePressed( code );
  110. if ( _doublePressedActionMessage )
  111. {
  112. PostActionSignal( _doublePressedActionMessage->MakeCopy() );
  113. }
  114. }
  115. //-----------------------------------------------------------------------------
  116. // Purpose: sets the message to send when the button is double clicked
  117. //-----------------------------------------------------------------------------
  118. void CBitmapButton::SetDoublePressedCommand( const char *command )
  119. {
  120. SetDoublePressedCommand( new KeyValues( "Command", "command", command ) );
  121. }
  122. //-----------------------------------------------------------------------------
  123. // Purpose: sets the message to send when the button is double clicked
  124. //-----------------------------------------------------------------------------
  125. void CBitmapButton::SetDoublePressedCommand( KeyValues *message )
  126. {
  127. // delete the old message
  128. if (_doublePressedActionMessage)
  129. {
  130. _doublePressedActionMessage->deleteThis();
  131. }
  132. _doublePressedActionMessage = message;
  133. }