Advertisement
Guest User

VertMinMaxSlider

a guest
Jan 5th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.91 KB | None | 0 0
  1.     float sliderMinVariable = 2, sliderMaxVariable = 4, sliderMinLimit = 1, sliderMaxLimit = 5;
  2.     bool DragTop, DragBottom, DragMiddle;
  3.  
  4.  
  5.     void OnGUI () {
  6.         VertMinMax( new Rect( 3, 3, 20, 150 ), ref sliderMinVariable, ref sliderMaxVariable, sliderMinLimit, sliderMaxLimit );
  7.         GUI.Label( new Rect( 20, 3, 500, 400 ), "Min: " + sliderMinVariable + "\nMax: " + sliderMaxVariable );
  8.     }
  9.  
  10.  
  11.     void VertMinMax ( Rect position, ref float minVariable, ref float maxVariable, float minLimit, float maxLimit ) {
  12.         float totalRange = maxLimit - minLimit;
  13.         float topY = position.y + ( ( minVariable - minLimit ) / totalRange ) * position.height;
  14.         float bottomY = position.y + ( ( maxVariable - minLimit ) / totalRange ) * position.height;
  15.  
  16.         //the values here of 6 and 12 assume the thumb image is 12 pixels height
  17.         Rect topButton = new Rect( position.x, topY - 6, position.width, 12 );
  18.         Rect bottomButton = new Rect( position.x, bottomY - 6, position.width, 12 );
  19.         Rect middleArea = new Rect( position.x, topY + 6, position.width, bottomY - topY - 12 );
  20.  
  21.         if ( Event.current.type == EventType.MouseDown ) {
  22.             if ( topButton.Contains( Event.current.mousePosition ) ) {
  23.                 DragTop = true;
  24.                 Event.current.Use();
  25.             }
  26.  
  27.             if ( bottomButton.Contains( Event.current.mousePosition ) ) {
  28.                 DragBottom = true;
  29.                 Event.current.Use();
  30.             }
  31.  
  32.             if ( middleArea.Contains( Event.current.mousePosition ) ) {
  33.                 DragMiddle = true;
  34.                 Event.current.Use();
  35.             }
  36.         }
  37.  
  38.         if ( Event.current.type == EventType.MouseDrag ) {
  39.             if ( DragTop ) {
  40.                 minVariable = ( ( Event.current.mousePosition.y - position.y ) / position.height ) * totalRange + minLimit;
  41.                 minVariable = Mathf.Clamp( minVariable, minLimit, maxVariable );
  42.                 Event.current.Use();
  43.             }
  44.  
  45.             if ( DragBottom ) {
  46.                 maxVariable = ( ( Event.current.mousePosition.y - position.y ) / position.height ) * totalRange + minLimit;
  47.                 maxVariable = Mathf.Clamp( maxVariable, minVariable, maxLimit );
  48.                 Event.current.Use();
  49.             }
  50.  
  51.             if ( DragMiddle ) {
  52.                 if ( ( minVariable == minLimit && Event.current.delta.y < 0 ) || ( maxVariable == maxLimit && Event.current.delta.y > 0 ) ) return;
  53.                 float delta = ( ( Event.current.delta.y / position.height ) * totalRange );
  54.                 minVariable = Mathf.Clamp( minVariable + delta, minLimit, maxLimit );
  55.                 maxVariable = Mathf.Clamp( maxVariable + delta, minLimit, maxLimit );
  56.                 Event.current.Use();
  57.             }
  58.         }
  59.         if ( Event.current.type == EventType.MouseUp ) {
  60.             DragTop = DragBottom = DragMiddle = false;
  61.         }
  62.  
  63.  
  64.         //Draw labels using styles from the current skin, change these to custom styles if you want them
  65.         GUI.Label( position, "", "VerticalSlider" );
  66.         GUI.Label( topButton, "", "VerticalSliderThumb" );
  67.         GUI.Label( bottomButton, "", "VerticalSliderThumb" );
  68.         //GUI.Label( middleArea, "", "Box" );
  69.         //Doesnt really draw nicely, when the range between top and bottom gets small, might be better with a custom style
  70.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement