Skip to content

Commit

Permalink
Use opacity for EquationNode visibility so we can use resize: true in…
Browse files Browse the repository at this point in the history
… its containers, see #90
  • Loading branch information
samreid committed Dec 27, 2024
1 parent 7714dd5 commit 5a9f8ef
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
16 changes: 8 additions & 8 deletions js/least-squares-regression/view/BestFitLineAccordionBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ export default class BestFitLineAccordionBox extends AccordionBox {

// Create the 'Best Fit Line' equation
// initial values set the spacing, the correct values for the slope and the intercept will be updated below
const equationText = new EquationNode( { mode: 'bestFitLine' } );
equationText.visible = false;
const equationNode = new EquationNode( { mode: 'bestFitLine' } );
equationNode.setEquationVisible( false );

// Create a Panel to contain the Equation Node
const equationPanel = new Panel( equationText, {
const equationPanel = new Panel( equationNode, {
fill: 'white',
stroke: LeastSquaresRegressionConstants.SMALL_PANEL_STROKE,
cornerRadius: LeastSquaresRegressionConstants.SMALL_PANEL_CORNER_RADIUS,
resize: false
resize: true // here
} );

// Text options for checkboxes
Expand Down Expand Up @@ -110,7 +110,7 @@ export default class BestFitLineAccordionBox extends AccordionBox {

// Set Equation to invisible if there is less than one point on the graph
if ( graph.isLinearFitDefined() ) {
equationText.visible = enabled;
equationNode.visible = enabled;
}
equationPanel.opacity = enabled ? 1 : SceneryConstants.DISABLED_OPACITY;
residualsCheckbox.enabled = enabled;
Expand All @@ -132,7 +132,7 @@ export default class BestFitLineAccordionBox extends AccordionBox {

super( content, options );

this.equationText = equationText;
this.equationText = equationNode;
this.sumOfSquaredResidualsChart = sumOfSquaredResidualsChart;

// Update the Best Fit Line Equation initially
Expand All @@ -158,11 +158,11 @@ export default class BestFitLineAccordionBox extends AccordionBox {

// Ensure the equation is visible if the Best Fit Line is enabled
if ( this.graph.bestFitLineVisibleProperty.value ) {
this.equationText.visible = true;
this.equationText.setEquationVisible( true );
}
}
else {
this.equationText.visible = false;
this.equationText.setEquationVisible( false );
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions js/least-squares-regression/view/EquationNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ export default class EquationNode extends Node {
this.mutate( options );
}

/**
* Change the visibility by changing the opacity, so that a blank equation will still take up layout space.
* @param equationVisible
*/
public setEquationVisible( equationVisible: boolean ): void {
this.opacity = equationVisible ? 1 : 0;
}

/**
* Sets the text of the slope and its accompanying sign.
* @param slope - The slope value to display.
Expand Down
4 changes: 2 additions & 2 deletions js/least-squares-regression/view/MyLineControlPanel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export default class MyLineControlPanel extends Panel {
fill: 'white',
cornerRadius: LeastSquaresRegressionConstants.SMALL_PANEL_CORNER_RADIUS,
stroke: LeastSquaresRegressionConstants.SMALL_PANEL_STROKE,
resize: true
resize: true // here
} );

// Create two sliders: The aSlider controls the angle of the line and by proxy the slope, the bSlider controls the intercept
Expand Down Expand Up @@ -208,7 +208,7 @@ export default class MyLineControlPanel extends Panel {

// Trigger the opacity/non-opacity when checking the myLine checkbox
graph.myLineVisibleProperty.link( enabled => {
equationText.visible = enabled;
equationText.setEquationVisible( enabled );
aSlider.pickable = enabled; // enable/disable slider
bSlider.pickable = enabled;// enable/disable slider
residualsCheckbox.enabled = enabled;
Expand Down

0 comments on commit 5a9f8ef

Please sign in to comment.