Skip to content

Commit

Permalink
convert to TypeScript, #80
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Mar 1, 2023
1 parent 3427ef9 commit 326de5a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
6 changes: 3 additions & 3 deletions js/sandwiches/model/SandwichRecipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ export default class SandwichRecipe extends Reaction {

// sandwich ingredients (symbols are internal for sandwiches, no i18n required)
const ingredients = [];
const bread = new Substance( breadCount, 'bread', SandwichNode.createBreadNode() );
const meat = new Substance( meatCount, 'meat', SandwichNode.createMeatNode() );
const cheese = new Substance( cheeseCount, 'cheese', SandwichNode.createCheeseNode() );
const bread = new Substance( breadCount, 'bread', SandwichNode.createBreadIcon() );
const meat = new Substance( meatCount, 'meat', SandwichNode.createMeatIcon() );
const cheese = new Substance( cheeseCount, 'cheese', SandwichNode.createCheeseIcon() );
if ( breadCount > 0 || options.coefficientsMutable ) { ingredients.push( bread ); }
if ( meatCount > 0 || options.coefficientsMutable ) { ingredients.push( meat ); }
if ( cheeseCount > 0 || options.coefficientsMutable ) { ingredients.push( cheese ); }
Expand Down
56 changes: 28 additions & 28 deletions js/sandwiches/view/SandwichNode.ts
Original file line number Diff line number Diff line change
@@ -1,49 +1,43 @@
// Copyright 2014-2023, University of Colorado Boulder

// @ts-nocheck
/**
* Creates a sandwich by attempting to mimic how a person would make a sandwich.
*
* @author Chris Malley (PixelZoom, Inc.)
*/

import merge from '../../../../phet-core/js/merge.js';
import { HStrut, Image, Node } from '../../../../scenery/js/imports.js';
import { HStrut, Image, Node, NodeOptions } from '../../../../scenery/js/imports.js';
import bread_png from '../../../images/bread_png.js';
import cheese_png from '../../../images/cheese_png.js';
import meat_png from '../../../images/meat_png.js';
import reactantsProductsAndLeftovers from '../../reactantsProductsAndLeftovers.js';

// constants
const MAX_WIDTH = _.maxBy( [ bread_png, cheese_png, meat_png ], image => image.width ).width;
const MAX_WIDTH = _.maxBy( [ bread_png, cheese_png, meat_png ], image => image.width )!.width;
const Y_SPACING = 4; // vertical space between centers of ingredients
const SANDWICH_SCALE = 0.65; // default scale of Nodes for sandwiches and their ingredients

export default class SandwichNode extends Node {
/**
* @param {number} breadCount
* @param {number} meatCount
* @param {number} cheeseCount
* @param {Object} [options]
*/
constructor( breadCount, meatCount, cheeseCount, options ) {

public constructor( breadCount: number, meatCount: number, cheeseCount: number ) {

assert && assert( Number.isInteger( breadCount ) && Number.isInteger( meatCount ) && Number.isInteger( cheeseCount ) );
assert && assert( breadCount >= 0 && meatCount >= 0 && cheeseCount >= 0 );

options = merge( {
const options: NodeOptions = {
scale: SANDWICH_SCALE
}, options );
};

super();
const children: Node[] = [];

let centerY = 0;

// ensure that all sandwiches are the same width
this.addChild( new HStrut( MAX_WIDTH, { centerX: 0 } ) );
children.push( new HStrut( MAX_WIDTH, { centerX: 0 } ) );

// Put a slice of bread on the bottom.
if ( breadCount > 0 ) {
this.addChild( new Image( bread_png, { centerX: 0, centerY: centerY } ) );
children.push( new Image( bread_png, { centerX: 0, centerY: centerY } ) );
centerY -= Y_SPACING;
breadCount--;
}
Expand Down Expand Up @@ -73,18 +67,19 @@ export default class SandwichNode extends Node {
imageAdded = false;

// Add ingredients that go between the bread.
ingredients.forEach( ingredient => {
for ( let i = 0; i < ingredients.length; i++ ) {
const ingredient = ingredients[ i ];
if ( ingredient.count > 0 ) {
this.addChild( new Image( ingredient.image, { centerX: 0, centerY: centerY } ) );
children.push( new Image( ingredient.image, { centerX: 0, centerY: centerY } ) );
centerY -= Y_SPACING;
imageAdded = true;
ingredient.count--;
}
} );
}

// Add a slice of bread, but save one slice of bread for the top.
if ( breadCount > 1 ) {
this.addChild( new Image( bread_png, { centerX: 0, centerY: centerY } ) );
children.push( new Image( bread_png, { centerX: 0, centerY: centerY } ) );
centerY -= Y_SPACING;
imageAdded = true;
breadCount--;
Expand All @@ -93,20 +88,25 @@ export default class SandwichNode extends Node {

// Put a slice of bread on the top.
if ( breadCount > 0 ) {
this.addChild( new Image( bread_png, { centerX: 0, centerY: centerY } ) );
children.push( new Image( bread_png, { centerX: 0, centerY: centerY } ) );
}

this.mutate( options );
options.children = children;

super( options );
}

// @public
static createBreadNode() { return new Image( bread_png, { scale: SANDWICH_SCALE } ); }
public static createBreadIcon(): Node {
return new Image( bread_png, { scale: SANDWICH_SCALE } );
}

// @public
static createMeatNode() { return new Image( meat_png, { scale: SANDWICH_SCALE } ); }
public static createMeatIcon(): Node {
return new Image( meat_png, { scale: SANDWICH_SCALE } );
}

// @public
static createCheeseNode() { return new Image( cheese_png, { scale: SANDWICH_SCALE } ); }
public static createCheeseIcon(): Node {
return new Image( cheese_png, { scale: SANDWICH_SCALE } );
}
}

reactantsProductsAndLeftovers.register( 'SandwichNode', SandwichNode );

0 comments on commit 326de5a

Please sign in to comment.