Button.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. GWEN
  3. Copyright (c) 2010 Facepunch Studios
  4. See license in Gwen.h
  5. */
  6. #include "Gwen/Gwen.h"
  7. #include "Gwen/Skin.h"
  8. #include "Gwen/Controls/Button.h"
  9. #include "Gwen/Controls/ImagePanel.h"
  10. using namespace Gwen;
  11. using namespace Gwen::Controls;
  12. GWEN_CONTROL_CONSTRUCTOR( Button )
  13. {
  14. m_Image = NULL;
  15. m_bDepressed = false;
  16. m_bCenterImage = false;
  17. SetSize( 100, 20 );
  18. SetMouseInputEnabled( true );
  19. SetIsToggle( false );
  20. SetAlignment( Gwen::Pos::Center );
  21. SetTextPadding( Padding( 3, 0, 3, 0 ) );
  22. m_bToggleStatus = false;
  23. SetKeyboardInputEnabled( false );
  24. SetTabable( false );
  25. }
  26. void Button::Render( Skin::Base* skin )
  27. {
  28. if ( ShouldDrawBackground() )
  29. {
  30. bool bDrawDepressed = IsDepressed() && IsHovered();
  31. if ( IsToggle() ) bDrawDepressed = bDrawDepressed || GetToggleState();
  32. bool bDrawHovered = IsHovered() && ShouldDrawHover();
  33. skin->DrawButton( this, bDrawDepressed, bDrawHovered );
  34. }
  35. }
  36. void Button::OnMouseClickLeft( int /*x*/, int /*y*/, bool bDown )
  37. {
  38. if ( bDown )
  39. {
  40. m_bDepressed = true;
  41. Gwen::MouseFocus = this;
  42. onDown.Call( this );
  43. }
  44. else
  45. {
  46. if ( IsHovered() && m_bDepressed )
  47. {
  48. OnPress();
  49. }
  50. m_bDepressed = false;
  51. Gwen::MouseFocus = NULL;
  52. onUp.Call( this );
  53. }
  54. Redraw();
  55. }
  56. void Button::OnPress()
  57. {
  58. if ( IsToggle() )
  59. {
  60. SetToggleState( !GetToggleState() );
  61. }
  62. onPress.Call( this );
  63. }
  64. void Button::SetImage( const TextObject& strName, bool bCenter )
  65. {
  66. if ( strName.GetUnicode() == L"" )
  67. {
  68. if ( m_Image )
  69. {
  70. delete m_Image;
  71. m_Image= NULL;
  72. }
  73. return;
  74. }
  75. if ( !m_Image )
  76. {
  77. m_Image = new ImagePanel( this );
  78. }
  79. m_Image->SetImage( strName );
  80. m_Image->SizeToContents();
  81. m_Image->SetPos( m_Padding.left, 2 );
  82. m_bCenterImage = bCenter;
  83. int IdealTextPadding = m_Image->Right() + m_Padding.left + 4;
  84. if ( m_rTextPadding.left < IdealTextPadding )
  85. {
  86. m_rTextPadding.left = IdealTextPadding;
  87. }
  88. }
  89. void Button::SetToggleState( bool b )
  90. {
  91. if ( m_bToggleStatus == b ) return;
  92. m_bToggleStatus = b;
  93. onToggle.Call( this );
  94. if ( m_bToggleStatus )
  95. {
  96. onToggleOn.Call( this );
  97. }
  98. else
  99. {
  100. onToggleOff.Call( this );
  101. }
  102. }
  103. void Button::SizeToContents()
  104. {
  105. BaseClass::SizeToContents();
  106. if ( m_Image )
  107. {
  108. int height = m_Image->Height() + 4;
  109. if ( Height() < height )
  110. {
  111. SetHeight( height );
  112. }
  113. }
  114. }
  115. bool Button::OnKeySpace( bool bDown )
  116. {
  117. OnMouseClickLeft( 0, 0, bDown );
  118. return true;
  119. }
  120. void Button::AcceleratePressed()
  121. {
  122. OnPress();
  123. }
  124. void Button::Layout( Skin::Base* pSkin )
  125. {
  126. BaseClass::Layout( pSkin );
  127. if ( m_Image )
  128. {
  129. Gwen::Align::CenterVertically( m_Image );
  130. if ( m_bCenterImage )
  131. Gwen::Align::CenterHorizontally( m_Image );
  132. }
  133. }
  134. void Button::OnMouseDoubleClickLeft( int x, int y )
  135. {
  136. OnMouseClickLeft( x, y, true );
  137. onDoubleClick.Call( this );
  138. };