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.

186 lines
4.2 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #ifndef MOUSEOVERPANELBUTTON_H
  8. #define MOUSEOVERPANELBUTTON_H
  9. #ifdef _WIN32
  10. #pragma once
  11. #endif
  12. #include <vgui/IScheme.h>
  13. #include <vgui_controls/Button.h>
  14. #include <vgui/KeyCode.h>
  15. #include <filesystem.h>
  16. extern vgui::Panel *g_lastPanel;
  17. extern vgui::Button *g_lastButton;
  18. //-----------------------------------------------------------------------------
  19. // Purpose: Triggers a new panel when the mouse goes over the button
  20. //
  21. // the new panel has the same dimensions as the passed templatePanel and is of
  22. // the same class.
  23. //
  24. // must at least inherit from vgui::EditablePanel to support LoadControlSettings
  25. //-----------------------------------------------------------------------------
  26. template <class T>
  27. class MouseOverButton : public vgui::Button
  28. {
  29. private:
  30. DECLARE_CLASS_SIMPLE( MouseOverButton, vgui::Button );
  31. public:
  32. MouseOverButton(vgui::Panel *parent, const char *panelName, T *templatePanel ) :
  33. Button( parent, panelName, "MouseOverButton")
  34. {
  35. m_pPanel = new T( parent, NULL );
  36. m_pPanel ->SetVisible( false );
  37. // copy size&pos from template panel
  38. int x,y,wide,tall;
  39. templatePanel->GetBounds( x, y, wide, tall );
  40. m_pPanel->SetBounds( x, y, wide, tall );
  41. int px, py;
  42. templatePanel->GetPinOffset( px, py );
  43. int rx, ry;
  44. templatePanel->GetResizeOffset( rx, ry );
  45. // Apply pin settings from template, too
  46. m_pPanel->SetAutoResize( templatePanel->GetPinCorner(), templatePanel->GetAutoResize(), px, py, rx, ry );
  47. m_bPreserveArmedButtons = false;
  48. m_bUpdateDefaultButtons = false;
  49. }
  50. virtual void SetPreserveArmedButtons( bool bPreserve ){ m_bPreserveArmedButtons = bPreserve; }
  51. virtual void SetUpdateDefaultButtons( bool bUpdate ){ m_bUpdateDefaultButtons = bUpdate; }
  52. virtual void ShowPage()
  53. {
  54. if( m_pPanel )
  55. {
  56. m_pPanel->SetVisible( true );
  57. m_pPanel->MoveToFront();
  58. g_lastPanel = m_pPanel;
  59. }
  60. }
  61. virtual void HidePage()
  62. {
  63. if ( m_pPanel )
  64. {
  65. m_pPanel->SetVisible( false );
  66. }
  67. }
  68. const char *GetClassPage( const char *className )
  69. {
  70. static char classPanel[ _MAX_PATH ];
  71. Q_snprintf( classPanel, sizeof( classPanel ), "classes/%s.res", className);
  72. if ( g_pFullFileSystem->FileExists( classPanel, IsX360() ? "MOD" : "GAME" ) )
  73. {
  74. }
  75. else if (g_pFullFileSystem->FileExists( "classes/default.res", IsX360() ? "MOD" : "GAME" ) )
  76. {
  77. Q_snprintf ( classPanel, sizeof( classPanel ), "classes/default.res" );
  78. }
  79. else
  80. {
  81. return NULL;
  82. }
  83. return classPanel;
  84. }
  85. #ifdef REFRESH_CLASSMENU_TOOL
  86. void RefreshClassPage( void )
  87. {
  88. m_pPanel->LoadControlSettings( GetClassPage( GetName() ) );
  89. }
  90. #endif
  91. virtual void ApplySettings( KeyValues *resourceData )
  92. {
  93. BaseClass::ApplySettings( resourceData );
  94. // name, position etc of button is set, now load matching
  95. // resource file for associated info panel:
  96. m_pPanel->LoadControlSettings( GetClassPage( GetName() ) );
  97. }
  98. T *GetClassPanel( void ) { return m_pPanel; }
  99. virtual void OnCursorExited()
  100. {
  101. if ( !m_bPreserveArmedButtons )
  102. {
  103. BaseClass::OnCursorExited();
  104. }
  105. }
  106. virtual void OnCursorEntered()
  107. {
  108. BaseClass::OnCursorEntered();
  109. if ( !IsEnabled() )
  110. return;
  111. // are we updating the default buttons?
  112. if ( m_bUpdateDefaultButtons )
  113. {
  114. SetAsDefaultButton( 1 );
  115. }
  116. // are we preserving the armed state (and need to turn off the old button)?
  117. if ( m_bPreserveArmedButtons )
  118. {
  119. if ( g_lastButton && g_lastButton != this )
  120. {
  121. g_lastButton->SetArmed( false );
  122. }
  123. g_lastButton = this;
  124. }
  125. // turn on our panel (if it isn't already)
  126. if ( m_pPanel && ( !m_pPanel->IsVisible() ) )
  127. {
  128. // turn off the previous panel
  129. if ( g_lastPanel && g_lastPanel->IsVisible() )
  130. {
  131. g_lastPanel->SetVisible( false );
  132. }
  133. ShowPage();
  134. }
  135. }
  136. virtual void OnKeyCodeReleased( vgui::KeyCode code )
  137. {
  138. BaseClass::OnKeyCodeReleased( code );
  139. if ( m_bPreserveArmedButtons )
  140. {
  141. if ( g_lastButton )
  142. {
  143. g_lastButton->SetArmed( true );
  144. }
  145. }
  146. }
  147. private:
  148. T *m_pPanel;
  149. bool m_bPreserveArmedButtons;
  150. bool m_bUpdateDefaultButtons;
  151. };
  152. #define MouseOverPanelButton MouseOverButton<vgui::EditablePanel>
  153. #endif // MOUSEOVERPANELBUTTON_H