-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use factory pattern for creating scene icons #280
Comments
@samreid asked:
Factory implementation (untested): // Copyright 2018, University of Colorado Boulder
/**
* Creates icons for each of the scenes.
*
* @author Sam Reid (PhET Interactive Simulations)
*/
define( require => {
'use strict';
// modules
const BooleanProperty = require( 'AXON/BooleanProperty' );
const FaucetNode = require( 'SCENERY_PHET/FaucetNode' );
const Image = require( 'SCENERY/nodes/Image' );
const LaserPointerNode = require( 'SCENERY_PHET/LaserPointerNode' );
const LightWaveGeneratorNode = require( 'WAVE_INTERFERENCE/common/view/LightWaveGeneratorNode' );
const NumberProperty = require( 'AXON/NumberProperty' );
const speakerImage = require( 'image!WAVE_INTERFERENCE/speaker/speaker_MID.png' );
const waveInterference = require( 'WAVE_INTERFERENCE/waveInterference' );
const SceneIconFactory = {
/**
* Creates the icon for the 'water' scene, a faucet.
* @param {number} width
* @returns {Node}
*/
createWaterSceneIcon( width ) {
const icon = new FaucetNode( 1, new NumberProperty( 0 ), new BooleanProperty( true ), {
interactiveProperty: new BooleanProperty( false )
} ).rasterized();
icon.scale( width / icon .width * 0.7 );
return icon;
},
/**
* Creates the icon for the 'light' scene, a laser pointer.
* @param {number} width
* @returns {Node}
*/
createLightSceneIcon( width ) {
const icon = new LaserPointerNode( new BooleanProperty( false ), LightWaveGeneratorNode.DEFAULT_OPTIONS );
icon.scale( width / icon.width );
return icon;
},
/**
* Creates the icon for the 'sound' scene, a speaker.
* @param {number} width
* @returns {Node}
*/
createSoundSceneIcon( width ) {
const icon = new Image( speakerImage );
icon.scale( width / icon.height );
return icon;
}
};
return waveInterference.register( 'SceneIconFactory', SceneIconFactory );
} ); Usage in const SceneIconFactory = require( 'WAVE_INTERFERENCE/common/view/SceneIconFactory' );
...
const SCENE_ICON_WIDTH = 29;
...
const sceneRadioButtonGroup = new RadioButtonGroup( model.sceneProperty, [
{ value: model.waterScene, node: SceneIconFactory.createWaterSceneIcon( SCENE_ICON_WIDTH ) },
{ value: model.soundScene, node: SceneIconFactory.createSoundSceneIcon( SCENE_ICON_WIDTH ) },
{ value: model.lightScene, node: SceneIconFactory.createLightSceneIcon( SCENE_ICON_WIDTH ) }
], {
... |
While preparing the above recommendation, I noticed a couple of anomalies in (1) (2) |
One more nit about WaveInterferenceSceneIcons, in case you choose to keep the "instantiated class" approach instead of factory... The icon names ( |
Signed-off-by: Chris Malley <[email protected]>
I converted the icons to constants and improved the naming, please review. |
Code looks good. Node constants can sometimes result in an unintended memory leak, so just confirming... Are these icons children of any Node that is disposed? If so, you'll need to explicitly Btw... This potential memory leak is why I prefer factory methods to constants for icons, unless there's a clear performance/memory advantage to the constants. |
These nodes are not involved in disposal, the scene selection buttons persist for the life of the sim. Thanks for explaining that memory leak, I have seen it before but not been able to explain it so clearly. OK to close? |
👍 Closing. |
Proposed fix is pushed, @pixelzoom can you please review? |
Yeah, that's why I recommended factory methods and not constants. Now we're sort of back to where we started, with an instantiated class. But I guess that's OK. |
I prefer the instantiated class to #280 (comment), can this issue be closed? |
Yep. |
Related to #259 code review.
This was originally a REVIEW comment, promoted to an issue because it got complicated.
WaveInterferenceSceneIcons
is currently a class that is instantiated, and it's fields are the individual icons. Current implementation:And the usage in
WaveInterferenceControlPanel
is:REVIEW comment thread in
WaveInterferenceSceneIcons
:The text was updated successfully, but these errors were encountered: