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.

194 lines
6.3 KiB

  1. //========= Copyright � 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //=============================================================================//
  7. #include <assert.h>
  8. #include <math.h>
  9. #include <stdio.h>
  10. #include <vgui_controls/RotatingProgressBar.h>
  11. #include <vgui/IVGui.h>
  12. #include <vgui/ILocalize.h>
  13. #include <vgui/IScheme.h>
  14. #include <vgui/ISurface.h>
  15. #include <keyvalues.h>
  16. #include "mathlib/mathlib.h"
  17. // memdbgon must be the last include file in a .cpp file!!!
  18. #include <tier0/memdbgon.h>
  19. using namespace vgui;
  20. DECLARE_BUILD_FACTORY( RotatingProgressBar );
  21. //-----------------------------------------------------------------------------
  22. // Purpose: Constructor
  23. //-----------------------------------------------------------------------------
  24. RotatingProgressBar::RotatingProgressBar(Panel *parent, const char *panelName) : ProgressBar(parent, panelName)
  25. {
  26. m_flStartRadians = 0;
  27. m_flEndRadians = 0;
  28. m_flLastAngle = 0;
  29. m_nTextureId = -1;
  30. m_pszImageName = NULL;
  31. m_flTickDelay = 30;
  32. ivgui()->AddTickSignal(GetVPanel(), m_flTickDelay );
  33. SetPaintBorderEnabled(false);
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Purpose: Destructor
  37. //-----------------------------------------------------------------------------
  38. RotatingProgressBar::~RotatingProgressBar()
  39. {
  40. delete [] m_pszImageName;
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose:
  44. //-----------------------------------------------------------------------------
  45. void RotatingProgressBar::ApplySettings(KeyValues *inResourceData)
  46. {
  47. const char *imageName = inResourceData->GetString("image", "");
  48. if (*imageName)
  49. {
  50. SetImage( imageName );
  51. }
  52. // Find min and max rotations in radians
  53. m_flStartRadians = DEG2RAD(inResourceData->GetFloat( "start_degrees", 0 ) );
  54. m_flEndRadians = DEG2RAD( inResourceData->GetFloat( "end_degrees", 0 ) );
  55. // Start at 0 progress
  56. m_flLastAngle = m_flStartRadians;
  57. // approach speed is specified in degrees per second.
  58. // convert to radians per 1/30th of a second
  59. float flDegressPerSecond = DEG2RAD( inResourceData->GetFloat( "approach_speed", 360.0 ) ); // default is super fast
  60. m_flApproachSpeed = flDegressPerSecond * ( m_flTickDelay / 1000.0f ); // divide by number of frames in a second
  61. m_flRotOriginX = inResourceData->GetFloat( "rot_origin_x_percent", 0.5f );
  62. m_flRotOriginY = inResourceData->GetFloat( "rot_origin_y_percent", 0.5f );
  63. m_flRotatingX = inResourceData->GetFloat( "rotating_x", 0 );
  64. m_flRotatingY = inResourceData->GetFloat( "rotating_y", 0 );
  65. m_flRotatingWide = inResourceData->GetFloat( "rotating_wide", 0 );
  66. m_flRotatingTall = inResourceData->GetFloat( "rotating_tall", 0 );
  67. BaseClass::ApplySettings( inResourceData );
  68. }
  69. //-----------------------------------------------------------------------------
  70. // Purpose:
  71. //-----------------------------------------------------------------------------
  72. void RotatingProgressBar::ApplySchemeSettings(IScheme *pScheme)
  73. {
  74. BaseClass::ApplySchemeSettings(pScheme);
  75. if ( m_pszImageName && strlen( m_pszImageName ) > 0 )
  76. {
  77. if ( m_nTextureId == -1 )
  78. {
  79. m_nTextureId = surface()->CreateNewTextureID();
  80. }
  81. surface()->DrawSetTextureFile( m_nTextureId, m_pszImageName, true, false);
  82. }
  83. }
  84. //-----------------------------------------------------------------------------
  85. // Purpose: sets an image by file name
  86. //-----------------------------------------------------------------------------
  87. void RotatingProgressBar::SetImage(const char *imageName)
  88. {
  89. if ( m_pszImageName )
  90. {
  91. delete [] m_pszImageName;
  92. m_pszImageName = NULL;
  93. }
  94. const char *pszDir = "vgui/";
  95. int len = Q_strlen(imageName) + 1;
  96. len += strlen(pszDir);
  97. m_pszImageName = new char[ len ];
  98. Q_snprintf( m_pszImageName, len, "%s%s", pszDir, imageName );
  99. InvalidateLayout(false, true); // force applyschemesettings to run
  100. }
  101. //-----------------------------------------------------------------------------
  102. // Purpose:
  103. //-----------------------------------------------------------------------------
  104. void RotatingProgressBar::PaintBackground()
  105. {
  106. // No background
  107. }
  108. //-----------------------------------------------------------------------------
  109. // Purpose: Update event when we aren't drawing so we don't get huge sweeps
  110. // when we start drawing it
  111. //-----------------------------------------------------------------------------
  112. void RotatingProgressBar::OnTick( void )
  113. {
  114. float flDesiredAngle = RemapVal( GetProgress(), 0.0, 1.0, m_flStartRadians, m_flEndRadians );
  115. m_flLastAngle = Approach( flDesiredAngle, m_flLastAngle, m_flApproachSpeed );
  116. BaseClass::OnTick();
  117. }
  118. //-----------------------------------------------------------------------------
  119. // Purpose:
  120. //-----------------------------------------------------------------------------
  121. void RotatingProgressBar::Paint()
  122. {
  123. // we have an image that we rotate based on the progress,
  124. // where '0' is not rotated,'90' is rotated 90 degrees to the right.
  125. // Image is rotated around its center.
  126. // desired rotation is GetProgress() ( 0.0 -> 1.0 ) mapped into
  127. // ( m_flStartDegrees -> m_flEndDegrees )
  128. vgui::surface()->DrawSetTexture( m_nTextureId );
  129. vgui::surface()->DrawSetColor( Color(255,255,255,255) );
  130. int wide, tall;
  131. GetSize( wide, tall );
  132. float mid_x = m_flRotatingX + m_flRotOriginX * m_flRotatingWide;
  133. float mid_y = m_flRotatingY + m_flRotOriginY * m_flRotatingTall;
  134. Vertex_t vert[4];
  135. vert[0].Init( Vector2D( m_flRotatingX, m_flRotatingY ), Vector2D(0,0) );
  136. vert[1].Init( Vector2D( m_flRotatingX+m_flRotatingWide, m_flRotatingY ), Vector2D(1,0) );
  137. vert[2].Init( Vector2D( m_flRotatingX+m_flRotatingWide, m_flRotatingY+m_flRotatingTall ), Vector2D(1,1) );
  138. vert[3].Init( Vector2D( m_flRotatingX, m_flRotatingY+m_flRotatingTall ), Vector2D(0,1) );
  139. float flCosA = cos(m_flLastAngle);
  140. float flSinA = sin(m_flLastAngle);
  141. // rotate each point around (mid_x, mid_y) by flAngle radians
  142. for ( int i=0;i<4;i++ )
  143. {
  144. Vector2D result;
  145. // subtract the (x,y) we're rotating around, we'll add it on at the end.
  146. vert[i].m_Position.x -= mid_x;
  147. vert[i].m_Position.y -= mid_y;
  148. result.x = ( vert[i].m_Position.x * flCosA - vert[i].m_Position.y * flSinA ) + mid_x;
  149. result.y = ( vert[i].m_Position.x * flSinA + vert[i].m_Position.y * flCosA ) + mid_y;
  150. vert[i].m_Position = result;
  151. }
  152. vgui::surface()->DrawTexturedPolygon( 4, vert );
  153. }