@@ -518,6 +518,20 @@ private Rect shownAreaInsideMarginsInternal
518518 }
519519 }
520520
521+ float GetWidthInsideMargins ( float widthWithMargins , bool substractSliderWidth = false )
522+ {
523+ float width = ( widthWithMargins < kMinWidth ) ? kMinWidth : widthWithMargins ;
524+ float widthInsideMargins = width - leftmargin - rightmargin - ( substractSliderWidth ? ( m_VSlider ? styles . visualSliderWidth : 0 ) : 0 ) ;
525+ return Mathf . Max ( widthInsideMargins , kMinWidth ) ;
526+ }
527+
528+ float GetHeightInsideMargins ( float heightWithMargins , bool substractSliderHeight = false )
529+ {
530+ float height = ( heightWithMargins < kMinHeight ) ? kMinHeight : heightWithMargins ;
531+ float heightInsideMargins = height - topmargin - bottommargin - ( substractSliderHeight ? ( m_HSlider ? styles . visualSliderWidth : 0 ) : 0 ) ;
532+ return Mathf . Max ( heightInsideMargins , kMinHeight ) ;
533+ }
534+
521535 public virtual Bounds drawingBounds
522536 {
523537 get
@@ -920,45 +934,38 @@ public void EnforceScaleAndRange()
920934 return ;
921935
922936 float epsilon = 0.00001f ;
937+ float minChange = 0.01f ;
923938
924- if ( newArea . width < oldArea . width - epsilon )
925- {
926- float xLerp = Mathf . InverseLerp ( oldArea . width , newArea . width , rect . width / m_HScaleMax ) ;
927- newArea = new Rect (
928- Mathf . Lerp ( oldArea . x , newArea . x , xLerp ) ,
929- newArea . y ,
930- Mathf . Lerp ( oldArea . width , newArea . width , xLerp ) ,
931- newArea . height
932- ) ;
933- }
934- if ( newArea . height < oldArea . height - epsilon )
935- {
936- float yLerp = Mathf . InverseLerp ( oldArea . height , newArea . height , rect . height / m_VScaleMax ) ;
937- newArea = new Rect (
938- newArea . x ,
939- Mathf . Lerp ( oldArea . y , newArea . y , yLerp ) ,
940- newArea . width ,
941- Mathf . Lerp ( oldArea . height , newArea . height , yLerp )
942- ) ;
943- }
944- if ( newArea . width > oldArea . width + epsilon )
939+ if ( ! Mathf . Approximately ( newArea . width , oldArea . width ) )
945940 {
946- float xLerp = Mathf . InverseLerp ( oldArea . width , newArea . width , constrainedHScaleMin ) ;
941+ float constrainedValue = newArea . width < oldArea . width - epsilon ? rect . width / m_HScaleMax : constrainedHScaleMin ;
942+ constrainedValue = GetWidthInsideMargins ( constrainedValue , true ) ;
943+ float xLerp = Mathf . InverseLerp ( oldArea . width , newArea . width , constrainedValue ) ;
944+ float newWidth = Mathf . Lerp ( oldArea . width , newArea . width , xLerp ) ;
945+ float widthChange = Mathf . Abs ( newWidth - newArea . width ) ;
947946 newArea = new Rect (
948- Mathf . Lerp ( oldArea . x , newArea . x , xLerp ) ,
947+ // only affect the position if there was any significant change in width (the unit is in pixels so technically anything under 0.05f would already be impossible to see)
948+ // this fixes an issue where if width was only different due to rounding issues, position changes are ignored as xLerp comes back 0 (or very nearly 0)
949+ widthChange > minChange ? Mathf . Lerp ( oldArea . x , newArea . x , xLerp ) : newArea . x ,
949950 newArea . y ,
950- Mathf . Lerp ( oldArea . width , newArea . width , xLerp ) ,
951+ newWidth ,
951952 newArea . height
952953 ) ;
953954 }
954- if ( newArea . height > oldArea . height + epsilon )
955+ if ( ! Mathf . Approximately ( newArea . height , oldArea . height ) )
955956 {
956- float yLerp = Mathf . InverseLerp ( oldArea . height , newArea . height , constrainedVScaleMin ) ;
957+ float constrainedValue = newArea . height < oldArea . height - epsilon ? rect . height / m_VScaleMax : constrainedVScaleMin ;
958+ constrainedValue = GetHeightInsideMargins ( constrainedValue , true ) ;
959+ float yLerp = Mathf . InverseLerp ( oldArea . height , newArea . height , constrainedValue ) ;
960+ float newHeight = Mathf . Lerp ( oldArea . height , newArea . height , yLerp ) ;
961+ float heightChange = Mathf . Abs ( newHeight - newArea . height ) ;
957962 newArea = new Rect (
958963 newArea . x ,
959- Mathf . Lerp ( oldArea . y , newArea . y , yLerp ) ,
964+ // only affect the position if there was any significant change in height (the unit is in pixels so technically anything under 0.05f would already be impossible to see)
965+ // this fixes an issue where if height was only different due to rounding issues, position changes are ignored as yLerp comes back 0 (or very nearly 0)
966+ heightChange > minChange ? Mathf . Lerp ( oldArea . y , newArea . y , yLerp ) : newArea . y ,
960967 newArea . width ,
961- Mathf . Lerp ( oldArea . height , newArea . height , yLerp )
968+ newHeight
962969 ) ;
963970 }
964971
0 commit comments