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.

204 lines
5.3 KiB

  1. //===== Copyright � 1996-2005, Valve Corporation, All rights reserved. ======//
  2. //
  3. // Purpose:
  4. //
  5. //===========================================================================//
  6. #include "matsys_controls/curveeditorpanel.h"
  7. #include "matsys_controls/matsyscontrols.h"
  8. #include "tier1/keyvalues.h"
  9. #include "vgui/ISurface.h"
  10. #include "vgui/IInput.h"
  11. #include "vgui/MouseCode.h"
  12. // NOTE: This has to be the last file included!
  13. #include "tier0/memdbgon.h"
  14. using namespace vgui;
  15. //-----------------------------------------------------------------------------
  16. // constructor, destructor
  17. //-----------------------------------------------------------------------------
  18. CCurveEditorPanel::CCurveEditorPanel( vgui::Panel *pParent, const char *pName ) : BaseClass( pParent, pName )
  19. {
  20. m_nSelectedPoint = -1;
  21. SetMouseInputEnabled( true );
  22. SetKeyBoardInputEnabled( true );
  23. m_nHighlightedPoint = -1;
  24. }
  25. CCurveEditorPanel::~CCurveEditorPanel()
  26. {
  27. }
  28. //-----------------------------------------------------------------------------
  29. // Converts screen location to normalized values
  30. //-----------------------------------------------------------------------------
  31. void CCurveEditorPanel::ScreenToValue( int x, int y, float *pIn, float *pOut )
  32. {
  33. int w, h;
  34. GetSize( w, h );
  35. *pIn = (float)x / (w-1);
  36. *pOut = 1.0f - ((float)y / (h-1));
  37. }
  38. void CCurveEditorPanel::ValueToScreen( float flIn, float flOut, int *x, int *y )
  39. {
  40. int w, h;
  41. GetSize( w, h );
  42. *x = (int)(flIn * (w-1) + 0.5f);
  43. *y = (h-1) - (int)(flOut * (h-1) + 0.5f);
  44. }
  45. //-----------------------------------------------------------------------------
  46. // Handle input
  47. //-----------------------------------------------------------------------------
  48. void CCurveEditorPanel::OnMousePressed( vgui::MouseCode code )
  49. {
  50. BaseClass::OnMousePressed( code );
  51. int x, y;
  52. input()->GetCursorPos( x, y );
  53. ScreenToLocal( x, y );
  54. if ( code == MOUSE_LEFT )
  55. {
  56. int w, h;
  57. GetSize( w, h );
  58. float flIn, flOut;
  59. ScreenToValue( x, y, &flIn, &flOut );
  60. float flTolerance = 5.0f / (w-1); // +/- 3 pixels to select the point
  61. m_nSelectedPoint = FindOrAddControlPoint( flIn, flTolerance, flOut );
  62. }
  63. }
  64. void CCurveEditorPanel::OnMouseReleased( vgui::MouseCode code )
  65. {
  66. BaseClass::OnMouseReleased( code );
  67. if ( code == MOUSE_LEFT )
  68. {
  69. m_nSelectedPoint = -1;
  70. }
  71. }
  72. void CCurveEditorPanel::OnCursorMoved( int x, int y )
  73. {
  74. BaseClass::OnCursorMoved( x, y );
  75. float flIn, flOut;
  76. ScreenToValue( x, y, &flIn, &flOut );
  77. int w, h;
  78. GetSize( w, h );
  79. float flTolerance = 5.0f / (w-1); // +/- 3 pixels to select the point
  80. m_nHighlightedPoint = FindControlPoint( flIn, flTolerance );
  81. if ( m_nSelectedPoint < 0 )
  82. return;
  83. m_nSelectedPoint = ModifyControlPoint( m_nSelectedPoint, flIn, flOut );
  84. }
  85. //-----------------------------------------------------------------------------
  86. // Handles keypresses
  87. //-----------------------------------------------------------------------------
  88. void CCurveEditorPanel::OnKeyCodePressed( vgui::KeyCode code )
  89. {
  90. BaseClass::OnKeyCodePressed( code );
  91. if ( m_nSelectedPoint >= 0 )
  92. {
  93. RemoveControlPoint( m_nSelectedPoint );
  94. m_nSelectedPoint = -1;
  95. }
  96. }
  97. //-----------------------------------------------------------------------------
  98. // This paints the grid behind the curves
  99. //-----------------------------------------------------------------------------
  100. void CCurveEditorPanel::PaintBackground( void )
  101. {
  102. int w, h;
  103. GetSize( w, h );
  104. vgui::surface()->DrawSetColor( 255, 255, 255, 255 );
  105. vgui::surface()->DrawFilledRect( 0, 0, w, h );
  106. vgui::surface()->DrawSetColor( 128, 128, 128, 255 );
  107. vgui::surface()->DrawLine( 0, h/4, w, h/4 );
  108. vgui::surface()->DrawLine( 0, h/2, w, h/2 );
  109. vgui::surface()->DrawLine( 0, 3*h/4, w, 3*h/4 );
  110. vgui::surface()->DrawLine( w/4, 0, w/4, h );
  111. vgui::surface()->DrawLine( w/2, 0, w/2, h );
  112. vgui::surface()->DrawLine( 3*w/4, 0, 3*w/4, h );
  113. vgui::surface()->DrawSetColor( 0, 0, 0, 255 );
  114. vgui::surface()->DrawLine( 0, 0, w, 0 );
  115. vgui::surface()->DrawLine( w, 0, w, h );
  116. vgui::surface()->DrawLine( w, h, 0, h );
  117. vgui::surface()->DrawLine( 0, h, 0, 0 );
  118. }
  119. //-----------------------------------------------------------------------------
  120. // Sets the color curves operation to edit
  121. //-----------------------------------------------------------------------------
  122. void CCurveEditorPanel::Paint( void )
  123. {
  124. int w, h;
  125. GetSize( w, h );
  126. int x0 = 0, y0 = 0;
  127. // FIXME: Add method to draw multiple lines DrawPolyLine connects the 1st and last points... bleah
  128. vgui::surface()->DrawSetColor( 0, 0, 0, 255 );
  129. for ( int i = 0; i < w; ++i )
  130. {
  131. float flIn = (float)i / (w-1);
  132. float flOut = GetValue( flIn );
  133. int x = i;
  134. int y = (h-1) - (int)(flOut * (h-1) + 0.5f);
  135. y = clamp( y, 0, h-1 );
  136. if ( i != 0 )
  137. {
  138. vgui::surface()->DrawLine( x0, y0, x, y );
  139. }
  140. x0 = x; y0 = y;
  141. }
  142. // This will paint the control points
  143. // The currently selected one will be painted red and filled.
  144. for ( int i = ControlPointCount(); --i >= 0; )
  145. {
  146. float flIn, flOut;
  147. GetControlPoint( i, &flIn, &flOut );
  148. int cx, cy;
  149. ValueToScreen( flIn, flOut, &cx, &cy );
  150. if ( (i == m_nSelectedPoint) || ((m_nSelectedPoint == -1) && (i == m_nHighlightedPoint)) )
  151. {
  152. vgui::surface()->DrawSetColor( 255, 0, 0, 255 );
  153. vgui::surface()->DrawFilledRect( cx-3, cy-3, cx+3, cy+3 );
  154. }
  155. else
  156. {
  157. vgui::surface()->DrawSetColor( 0, 0, 0, 255 );
  158. vgui::surface()->DrawOutlinedRect( cx-3, cy-3, cx+3, cy+3 );
  159. }
  160. }
  161. }