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.

199 lines
5.2 KiB

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