Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- float sliderMinVariable = 2, sliderMaxVariable = 4, sliderMinLimit = 1, sliderMaxLimit = 5;
- bool DragTop, DragBottom, DragMiddle;
- void OnGUI () {
- VertMinMax( new Rect( 3, 3, 20, 150 ), ref sliderMinVariable, ref sliderMaxVariable, sliderMinLimit, sliderMaxLimit );
- GUI.Label( new Rect( 20, 3, 500, 400 ), "Min: " + sliderMinVariable + "\nMax: " + sliderMaxVariable );
- }
- void VertMinMax ( Rect position, ref float minVariable, ref float maxVariable, float minLimit, float maxLimit ) {
- float totalRange = maxLimit - minLimit;
- float topY = position.y + ( ( minVariable - minLimit ) / totalRange ) * position.height;
- float bottomY = position.y + ( ( maxVariable - minLimit ) / totalRange ) * position.height;
- //the values here of 6 and 12 assume the thumb image is 12 pixels height
- Rect topButton = new Rect( position.x, topY - 6, position.width, 12 );
- Rect bottomButton = new Rect( position.x, bottomY - 6, position.width, 12 );
- Rect middleArea = new Rect( position.x, topY + 6, position.width, bottomY - topY - 12 );
- if ( Event.current.type == EventType.MouseDown ) {
- if ( topButton.Contains( Event.current.mousePosition ) ) {
- DragTop = true;
- Event.current.Use();
- }
- if ( bottomButton.Contains( Event.current.mousePosition ) ) {
- DragBottom = true;
- Event.current.Use();
- }
- if ( middleArea.Contains( Event.current.mousePosition ) ) {
- DragMiddle = true;
- Event.current.Use();
- }
- }
- if ( Event.current.type == EventType.MouseDrag ) {
- if ( DragTop ) {
- minVariable = ( ( Event.current.mousePosition.y - position.y ) / position.height ) * totalRange + minLimit;
- minVariable = Mathf.Clamp( minVariable, minLimit, maxVariable );
- Event.current.Use();
- }
- if ( DragBottom ) {
- maxVariable = ( ( Event.current.mousePosition.y - position.y ) / position.height ) * totalRange + minLimit;
- maxVariable = Mathf.Clamp( maxVariable, minVariable, maxLimit );
- Event.current.Use();
- }
- if ( DragMiddle ) {
- if ( ( minVariable == minLimit && Event.current.delta.y < 0 ) || ( maxVariable == maxLimit && Event.current.delta.y > 0 ) ) return;
- float delta = ( ( Event.current.delta.y / position.height ) * totalRange );
- minVariable = Mathf.Clamp( minVariable + delta, minLimit, maxLimit );
- maxVariable = Mathf.Clamp( maxVariable + delta, minLimit, maxLimit );
- Event.current.Use();
- }
- }
- if ( Event.current.type == EventType.MouseUp ) {
- DragTop = DragBottom = DragMiddle = false;
- }
- //Draw labels using styles from the current skin, change these to custom styles if you want them
- GUI.Label( position, "", "VerticalSlider" );
- GUI.Label( topButton, "", "VerticalSliderThumb" );
- GUI.Label( bottomButton, "", "VerticalSliderThumb" );
- //GUI.Label( middleArea, "", "Box" );
- //Doesnt really draw nicely, when the range between top and bottom gets small, might be better with a custom style
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement