Skip to content

Commit

Permalink
move ButtonNode enabled logic to enabledAppearanceStrategy, phetsims/…
Browse files Browse the repository at this point in the history
  • Loading branch information
zepumph committed Nov 6, 2020
1 parent 4557a27 commit 7d5cc3e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
33 changes: 16 additions & 17 deletions js/buttons/ButtonNode.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ class ButtonNode extends Node {
// See RectangularRadioButton.ContentAppearanceStrategy for an example.
contentAppearanceStrategy: null,

/**
* Alter the appearance when changing the enabled of the button.
* @param {boolean} enabled
* @param {Node} background
* @param {Node|null} content - if there is content, style can be applied to a containing Node around it.
*/
enabledAppearanceStrategy: ( enabled, background, content ) => {
background.filters = enabled ? [] : [ CONTRAST_FILTER, BRIGHTNESS_FILTER ];

if ( content ) {
content.filters = enabled ? [] : [ Grayscale.FULL ];
content.opacity = enabled ? 1 : SunConstants.DISABLED_OPACITY;
}
},

// pdom
tagName: 'button',

Expand Down Expand Up @@ -155,24 +170,8 @@ class ButtonNode extends Node {

this.mutate( options );

const defaultEnabledListener = SunConstants.getComponentEnabledListener( this );

// No need to dispose because enabledProperty is disposed in Node
this.enabledProperty.link( enabled => {
defaultEnabledListener( enabled );

// additional behavior specific for buttons.
this.opacity = 1.0;
buttonBackground.filters = enabled ? [] : [
CONTRAST_FILTER,
BRIGHTNESS_FILTER
];

if ( alignBox ) {
alignBox.filters = enabled ? [] : [ Grayscale.FULL ];
alignBox.opacity = enabled ? 1 : SunConstants.DISABLED_OPACITY;
}
} );
this.enabledProperty.link( enabled => options.enabledAppearanceStrategy( enabled, buttonBackground, alignBox ) );

// @private - define a dispose function
this.disposeButtonNode = () => {
Expand Down
24 changes: 17 additions & 7 deletions js/demo/ButtonsScreenView.js
Original file line number Diff line number Diff line change
Expand Up @@ -340,14 +340,23 @@ class ButtonsScreenView extends ScreenView {
} );

// transparent button with something behind it
const rectangleNode = new Rectangle( 0, 0, 25, 50, { fill: 'red' } );
const transparentButton = new RectangularPushButton( {
content: new Text( 'Transparent Button', { font: BUTTON_FONT } ),
listener: () => console.log( 'transparentButton fired' ),
const rectangleNode = new Rectangle( 0, 0, 25, 100, { fill: 'red' } );
const transparentAlphaButton = new RectangularPushButton( {
content: new Text( 'Transparent Button via alpha', { font: BUTTON_FONT } ),
listener: () => console.log( 'transparentAlphaButton fired' ),
baseColor: new Color( 255, 255, 0, 0.7 ),
center: rectangleNode.center
centerX: rectangleNode.centerX,
top: rectangleNode.top + 10
} );
const transparentParent = new Node( { children: [ rectangleNode, transparentButton ] } );
const transparentOpacityButton = new RectangularPushButton( {
content: new Text( 'Transparent Button via opacity', { font: BUTTON_FONT } ),
listener: () => console.log( 'transparentOpacityButton fired' ),
baseColor: new Color( 255, 255, 0 ),
opacity: .6,
centerX: rectangleNode.centerX,
bottom: rectangleNode.bottom - 10
} );
const transparentParent = new Node( { children: [ rectangleNode, transparentAlphaButton, transparentOpacityButton ] } );

const arrowButton = new ArrowButton( 'left', () => console.log( 'arrowButton fired' ), {
enabledProperty: buttonsEnabledProperty
Expand Down Expand Up @@ -523,7 +532,8 @@ class ButtonsScreenView extends ScreenView {
goButton.enabled = enabled;
helpButton.enabled = enabled;
fireOnDownButton.enabled = enabled;
transparentButton.enabled = enabled;
transparentAlphaButton.enabled = enabled;
transparentOpacityButton.enabled = enabled;
roundStickyToggleButton.enabled = enabled;
booleanRectangularStickyToggleButton.enabled = enabled;
fireQuicklyWhenHeldButton.enabled = enabled;
Expand Down

0 comments on commit 7d5cc3e

Please sign in to comment.