Changeset 84 in tmcsimulator-scriptbuilder for trunk/src/scriptbuilder/gui


Ignore:
Timestamp:
08/26/2017 03:46:13 PM (9 years ago)
Author:
jdalbey
Message:

RangeSlider?: Fix defect that was allowing slider thumb to be dragged too far right.

Location:
trunk/src/scriptbuilder/gui/drawers
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/scriptbuilder/gui/drawers/RangeSlider.java

    r27 r84  
    102102        // Compute new value and extent to maintain upper value. 
    103103        int oldExtent = getExtent(); 
    104         int newValue = Math.min(Math.max(getMinimum(), value), oldValue + oldExtent); 
     104        // Ensure value is greater than minimum allowed 
     105        int lower = Math.max(getMinimum(), value); 
     106        // Ensure value is less than maximum allowed 
     107        // Fix defect that was letting lower thumb go to far right. 
     108        // Note this still allows the mouse to drag it beyond limit, 
     109        // but when released it returns to proper place. 
     110//        int newValue = Math.min(lower, oldValue + oldExtent); 
     111        int newValue = Math.min(lower, getMaximum() - oldExtent); 
    105112        // Hacked to not change the extent (move the entire slider) 
    106113        //int newExtent = oldExtent + oldValue - newValue; 
  • trunk/src/scriptbuilder/gui/drawers/RangeSliderDemo.java

    r26 r84  
    2020 
    2121Copyright (c) 2010 Ernest Yu. All rights reserved. 
     22https://github.com/ernieyu/Swing-range-slider 
    2223 
    2324Permission is hereby granted, free of charge, to any person obtaining a copy 
     
    6465        rangeSlider.setPreferredSize(new Dimension(240, rangeSlider.getPreferredSize().height)); 
    6566        rangeSlider.setMinimum(0); 
    66         rangeSlider.setMaximum(10); 
     67        rangeSlider.setMaximum(20); 
    6768 
    6869        // Add listener to update display. 
  • trunk/src/scriptbuilder/gui/drawers/RangeSliderUI.java

    r27 r84  
    176176        // Calculate upper thumb location.  The thumb is centered over its 
    177177        // value on the track. 
    178         if (slider.getOrientation() == JSlider.HORIZONTAL) 
    179         { 
    180178            int upperPosition = xPositionForValue(slider.getValue() + slider.getExtent()); 
    181179            upperThumbRect.x = upperPosition - (upperThumbRect.width / 2); 
    182180            upperThumbRect.y = trackRect.y; 
    183  
    184         } 
    185         else 
    186         { 
    187             int upperPosition = yPositionForValue(slider.getValue() + slider.getExtent()); 
    188             upperThumbRect.x = trackRect.x; 
    189             upperThumbRect.y = upperPosition - (upperThumbRect.height / 2); 
    190         } 
    191181    } 
    192182 
     
    248238        Rectangle trackBounds = trackRect; 
    249239 
    250         if (slider.getOrientation() == JSlider.HORIZONTAL) 
    251         { 
    252240            // Determine position of selected range by moving from the middle 
    253241            // of one thumb to the other. 
     
    272260            g.translate(-trackBounds.x, -(trackBounds.y + cy)); 
    273261            g.setColor(oldColor); 
    274  
    275         } 
    276         else 
    277         { 
    278             // Determine position of selected range by moving from the middle 
    279             // of one thumb to the other. 
    280             int lowerY = thumbRect.x + (thumbRect.width / 2); 
    281             int upperY = upperThumbRect.x + (upperThumbRect.width / 2); 
    282  
    283             // Determine track position. 
    284             int cx = (trackBounds.width / 2) - 2; 
    285  
    286             // Save color and shift position. 
    287             Color oldColor = g.getColor(); 
    288             g.translate(trackBounds.x + cx, trackBounds.y); 
    289  
    290             // Draw selected range. 
    291             g.setColor(rangeColor); 
    292             for (int x = 0; x <= 3; x++) 
    293             { 
    294                 g.drawLine(x, lowerY - trackBounds.y, x, upperY - trackBounds.y); 
    295             } 
    296  
    297             // Restore position and color. 
    298             g.translate(-(trackBounds.x + cx), -trackBounds.y); 
    299             g.setColor(oldColor); 
    300         } 
    301262    } 
    302263 
     
    346307    private void paintUpperThumb(Graphics g) 
    347308    { 
    348 //        Remove drawing of upper thumb - jdalbey 
    349 //        Rectangle knobBounds = upperThumbRect; 
    350 //        int w = knobBounds.width; 
    351 //        int h = knobBounds.height; 
    352 // 
    353 //        // Create graphics copy. 
    354 //        Graphics2D g2d = (Graphics2D) g.create(); 
    355 // 
    356 //        // Create default thumb shape. 
    357 //        Shape thumbShape = createThumbShape(w - 1, h - 1); 
    358 // 
    359 //        // Draw thumb. 
    360 //        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
    361 //                RenderingHints.VALUE_ANTIALIAS_ON); 
    362 //        g2d.translate(knobBounds.x, knobBounds.y); 
    363 // 
    364 //        g2d.setColor(Color.PINK); 
    365 //        g2d.fill(thumbShape); 
    366 // 
    367 //        g2d.setColor(Color.RED); 
    368 //        g2d.draw(thumbShape); 
    369 // 
    370 //        // Dispose graphics. 
    371 //        g2d.dispose(); 
    372309    } 
    373310 
     
    414351            int delta = blockIncrement * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); 
    415352 
    416             if (upperThumbSelected) 
    417             { 
    418                 int oldValue = ((RangeSlider) slider).getUpperValue(); 
    419                 ((RangeSlider) slider).setUpperValue(oldValue + delta); 
    420             } 
    421             else 
    422             { 
     353 
     354            int oldValue = slider.getValue(); 
     355            slider.setValue(oldValue + delta); 
     356             
     357        } 
     358    } 
     359 
     360    /** 
     361     * Moves the selected thumb in the specified direction by a unit increment. 
     362     * This method is called when the user presses one of the arrow keys. 
     363     */ 
     364    public void scrollByUnit(int direction) 
     365    { 
     366        synchronized (slider) 
     367        { 
     368            int delta = 1 * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); 
     369 
     370 
    423371                int oldValue = slider.getValue(); 
    424372                slider.setValue(oldValue + delta); 
    425             } 
    426         } 
    427     } 
    428  
    429     /** 
    430      * Moves the selected thumb in the specified direction by a unit increment. 
    431      * This method is called when the user presses one of the arrow keys. 
    432      */ 
    433     public void scrollByUnit(int direction) 
    434     { 
    435         synchronized (slider) 
    436         { 
    437             int delta = 1 * ((direction > 0) ? POSITIVE_SCROLL : NEGATIVE_SCROLL); 
    438  
    439             if (upperThumbSelected) 
    440             { 
    441                 int oldValue = ((RangeSlider) slider).getUpperValue(); 
    442                 ((RangeSlider) slider).setUpperValue(oldValue + delta); 
    443             } 
    444             else 
    445             { 
    446                 int oldValue = slider.getValue(); 
    447                 slider.setValue(oldValue + delta); 
    448             } 
     373             
    449374        } 
    450375    } 
     
    534459            boolean lowerPressed = false; 
    535460            boolean upperPressed = false; 
    536             if (upperThumbSelected || slider.getMinimum() == slider.getValue()) 
    537             { 
    538                 if (upperThumbRect.contains(currentMouseX, currentMouseY)) 
    539                 { 
    540                     //upperPressed = true; 
    541                 } 
    542                 else if (thumbRect.contains(currentMouseX, currentMouseY)) 
    543                 { 
    544                     lowerPressed = true; 
    545                 } 
    546             } 
    547             else 
     461            if (slider.getMinimum() == slider.getValue()) 
    548462            { 
    549463                if (thumbRect.contains(currentMouseX, currentMouseY)) 
     
    551465                    lowerPressed = true; 
    552466                } 
    553                 else if (upperThumbRect.contains(currentMouseX, currentMouseY)) 
     467            } 
     468            else 
     469            { 
     470                if (thumbRect.contains(currentMouseX, currentMouseY)) 
    554471                { 
    555                     //upperPressed = true; 
     472                    lowerPressed = true; 
    556473                } 
    557474            } 
     
    560477            if (lowerPressed) 
    561478            { 
    562                 switch (slider.getOrientation()) 
    563                 { 
    564                     case JSlider.VERTICAL: 
    565                         offset = currentMouseY - thumbRect.y; 
    566                         break; 
    567                     case JSlider.HORIZONTAL: 
    568                         offset = currentMouseX - thumbRect.x; 
    569                         break; 
    570                 } 
     479                offset = currentMouseX - thumbRect.x; 
    571480                upperThumbSelected = false; 
    572481                lowerDragging = true; 
    573                 // ALERT: Return from middle of method 
    574                 return; 
    575             } 
    576             lowerDragging = false; 
    577  
    578             // Handle upper thumb pressed. 
    579             if (upperPressed) 
    580             { 
    581                 switch (slider.getOrientation()) 
    582                 { 
    583                     case JSlider.VERTICAL: 
    584                         offset = currentMouseY - upperThumbRect.y; 
    585                         break; 
    586                     case JSlider.HORIZONTAL: 
    587                         offset = currentMouseX - upperThumbRect.x; 
    588                         break; 
    589                 } 
    590                 upperThumbSelected = true; 
    591                 upperDragging = true; 
    592                 // ALERT: Return from middle of method 
    593                 return; 
    594             } 
    595             upperDragging = false; 
     482            } 
     483            else 
     484            { 
     485                lowerDragging = false; 
     486                upperDragging = false; 
     487            } 
    596488        } 
    597489 
     
    621513                moveLowerThumb(); 
    622514                syncUpperThumb(); 
    623             } 
    624             else if (upperDragging) 
    625             { 
    626                 slider.setValueIsAdjusting(true); 
    627                 moveUpperThumb(); 
    628515            } 
    629516        } 
     
    642529        { 
    643530            int thumbMiddle = 0; 
    644  
    645             switch (slider.getOrientation()) 
    646             { 
    647                 case JSlider.VERTICAL: 
    648                     int halfThumbHeight = thumbRect.height / 2; 
    649                     int thumbTop = currentMouseY - offset; 
    650                     int trackTop = trackRect.y; 
    651                     int trackBottom = trackRect.y + (trackRect.height - 1); 
    652                     int vMax = yPositionForValue(slider.getValue() + slider.getExtent()); 
    653  
    654                     // Apply bounds to thumb position. 
    655                     if (drawInverted()) 
    656                     { 
    657                         trackBottom = vMax; 
    658                     } 
    659                     else 
    660                     { 
    661                         trackTop = vMax; 
    662                     } 
    663                     thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight); 
    664                     thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight); 
    665  
    666                     setThumbLocation(thumbRect.x, thumbTop); // repaint 
    667  
    668                     // Update slider value. 
    669                     thumbMiddle = thumbTop + halfThumbHeight; 
    670                     slider.setValue(valueForYPosition(thumbMiddle)); 
    671                     break; 
    672  
    673                 case JSlider.HORIZONTAL: 
    674                     int halfThumbWidth = thumbRect.width / 2; 
    675                     int thumbLeft = currentMouseX - offset; 
    676                     int trackLeft = trackRect.x; 
    677                     int trackRight = trackRect.x + (trackRect.width - 1); 
    678                     int hMax = xPositionForValue(slider.getValue() + slider.getExtent()); 
    679  
    680                     // Apply bounds to thumb position. 
    681                     if (drawInverted()) 
    682                     { 
    683                         trackLeft = hMax; 
    684                     } 
    685                     else 
    686                     { 
    687                         trackRight = hMax; 
    688                     } 
    689                     thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); 
    690                     thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); 
    691  
    692                     setThumbLocation(thumbLeft, thumbRect.y); //repaint 
    693  
    694                     // Update slider value. 
    695                     thumbMiddle = thumbLeft + halfThumbWidth; 
    696                     slider.setValue(valueForXPosition(thumbMiddle)); 
    697                     break; 
    698  
    699                 default: 
    700                     return; 
    701             } 
     531            int halfThumbWidth = thumbRect.width / 2; 
     532            int thumbLeft = currentMouseX - offset; 
     533            int trackLeft = trackRect.x; 
     534            int trackRight = trackRect.x + (trackRect.width - 1); 
     535            int hMax = xPositionForValue(slider.getValue() + slider.getExtent()); 
     536            System.out.println("Value="+slider.getValue()+"   hMax="+hMax); 
     537            // Apply bounds to thumb position. 
     538            if (drawInverted()) 
     539            { 
     540                trackLeft = hMax; 
     541            } 
     542            else 
     543            { 
     544                trackRight = hMax; 
     545            } 
     546            thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); 
     547            thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); 
     548 
     549            setThumbLocation(thumbLeft, thumbRect.y); //repaint 
     550 
     551            // Update slider value. 
     552            thumbMiddle = thumbLeft + halfThumbWidth; 
     553            slider.setValue(valueForXPosition(thumbMiddle)); 
    702554        } 
    703555 
     
    712564            int thumbMiddle = 0; 
    713565 
    714             switch (slider.getOrientation()) 
    715             { 
    716                 case JSlider.HORIZONTAL: 
    717                     int halfThumbWidth = thumbRect.width / 2; 
    718                     int sliderLength = xPositionForValue(slider.getExtent()); 
    719                     int thumbLeft = currentMouseX - offset + sliderLength; 
    720                     int trackLeft = trackRect.x; 
    721                     int trackRight = trackRect.x + (trackRect.width - 1); 
    722                     int hMin = xPositionForValue(slider.getValue()); 
    723  
    724                     // Handle backwards (inverted) slider 
    725                     if (drawInverted()) 
    726                     { 
    727                         trackRight = hMin; 
    728                     } 
    729                     else 
    730                     { 
    731                         trackLeft = hMin; 
    732                     } 
    733                     // Apply bounds to thumb position. 
    734                     thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); 
    735                     thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); 
    736  
    737                     setUpperThumbLocation(thumbLeft, thumbRect.y);  // repaint slider 
    738  
    739                     // Don't Update slider extent. 
    740                     break; 
    741  
    742                 default: 
    743                     return; 
    744             } 
    745         } 
    746  
    747         /** 
    748          * Moves the location of the upper thumb, and sets its corresponding 
    749          * value in the slider. 
    750          */ 
    751         private void moveUpperThumb() 
    752         { 
    753             int thumbMiddle = 0; 
    754  
    755             switch (slider.getOrientation()) 
    756             { 
    757                 case JSlider.VERTICAL: 
    758                     int halfThumbHeight = thumbRect.height / 2; 
    759                     int thumbTop = currentMouseY - offset; 
    760                     int trackTop = trackRect.y; 
    761                     int trackBottom = trackRect.y + (trackRect.height - 1); 
    762                     int vMin = yPositionForValue(slider.getValue()); 
    763  
    764                     // Apply bounds to thumb position. 
    765                     if (drawInverted()) 
    766                     { 
    767                         trackTop = vMin; 
    768                     } 
    769                     else 
    770                     { 
    771                         trackBottom = vMin; 
    772                     } 
    773                     thumbTop = Math.max(thumbTop, trackTop - halfThumbHeight); 
    774                     thumbTop = Math.min(thumbTop, trackBottom - halfThumbHeight); 
    775  
    776                     setUpperThumbLocation(thumbRect.x, thumbTop); 
    777  
    778                     // Update slider extent. 
    779                     thumbMiddle = thumbTop + halfThumbHeight; 
    780                     slider.setExtent(valueForYPosition(thumbMiddle) - slider.getValue()); 
    781                     break; 
    782  
    783                 case JSlider.HORIZONTAL: 
    784                     int halfThumbWidth = thumbRect.width / 2; 
    785                     int thumbLeft = currentMouseX - offset; 
    786                     int trackLeft = trackRect.x; 
    787                     int trackRight = trackRect.x + (trackRect.width - 1); 
    788                     int hMin = xPositionForValue(slider.getValue()); 
    789  
    790                     // Apply bounds to thumb position. 
    791                     if (drawInverted()) 
    792                     { 
    793                         trackRight = hMin; 
    794                     } 
    795                     else 
    796                     { 
    797                         trackLeft = hMin; 
    798                     } 
    799                     thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); 
    800                     thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); 
    801  
    802                     setUpperThumbLocation(thumbLeft, thumbRect.y); 
    803  
    804                     // Update slider extent. 
    805                     thumbMiddle = thumbLeft + halfThumbWidth; 
    806                     slider.setExtent(valueForXPosition(thumbMiddle) - slider.getValue()); 
    807                     break; 
    808  
    809                 default: 
    810                     return; 
    811             } 
     566 
     567            int halfThumbWidth = thumbRect.width / 2; 
     568            int sliderLength = xPositionForValue(slider.getExtent()); 
     569            int thumbLeft = currentMouseX - offset + sliderLength; 
     570            int trackLeft = trackRect.x; 
     571            int trackRight = trackRect.x + (trackRect.width - 1); 
     572            int hMin = xPositionForValue(slider.getValue()); 
     573 
     574            // Handle backwards (inverted) slider 
     575            if (drawInverted()) 
     576            { 
     577                trackRight = hMin; 
     578            } 
     579            else 
     580            { 
     581                trackLeft = hMin; 
     582            } 
     583            // Apply bounds to thumb position. 
     584            thumbLeft = Math.max(thumbLeft, trackLeft - halfThumbWidth); 
     585            thumbLeft = Math.min(thumbLeft, trackRight - halfThumbWidth); 
     586 
     587            setUpperThumbLocation(thumbLeft, thumbRect.y);  // repaint slider 
     588 
     589             
    812590        } 
    813591    } 
Note: See TracChangeset for help on using the changeset viewer.