Skip to content

Commit

Permalink
convert DynamicStringTest to a proper class, phetsims/chipper#1319
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelzoom committed Jan 25, 2023
1 parent 5594568 commit 1a5deeb
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 55 deletions.
130 changes: 77 additions & 53 deletions js/DynamicStringTest.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2022-2023, University of Colorado Boulder

/**
* For testing dynamic layout in sims that may not yet have submitted translations, enabled via ?stringTest=dynamic.
* Please see initialize-globals for the hotkeys.
* DynamicStringTest is a handler for KeyboardEvents. It's used for testing dynamic layout in sims that may not yet
* have submitted translations, and is enabled via ?stringTest=dynamic. Please see initialize-globals for the hotkeys.
*
* @author Sam Reid (PhET Interactive Simulations)
* @author Marla Schulz (PhET Interactive Simulations)
Expand All @@ -20,7 +20,7 @@ const RIGHT_ARROW = 39;
const DOWN_ARROW = 40;
const SPACE_BAR = 32;

// Words of different lengths that can be cycled through
// Source of 'random' words
const WORD_SOURCE = 'Sometimes when Hippopotomonstrosesquippedaliophobia want lyrics you turn to Shakespeare like ' +
'the following text copied from some work ' +
'To be or not to be that is the question ' +
Expand All @@ -29,72 +29,96 @@ const WORD_SOURCE = 'Sometimes when Hippopotomonstrosesquippedaliophobia want ly
'Or to take Incomprehensibility against a sea of Floccinaucinihilipilification';

export default class DynamicStringTest {
public static init(): void {

const words = WORD_SOURCE.split( ' ' );
// How much to increase or decrease the length of the string
private stringFactor = 1;

// How much to increase or decrease the length of the string
let stringFactor = 1;
// An integer used to create an index into WORDS.
private stride = 0;

// An integer that assists in indexing into words.
let stride = 0;
// Words of different lengths that can be cycled through by changing stride
private static readonly WORDS = WORD_SOURCE.split( ' ' );

function doubleStrings(): void {
stringFactor = stringFactor * 2;
applyToAllStrings( stringFactor );
/**
* Handles a KeyboardEvent.
*/
public handleEvent( event: KeyboardEvent ): void {
if ( event.keyCode === LEFT_ARROW ) {
this.halveStrings();
}

function halveStrings(): void {
stringFactor = Math.max( stringFactor * 0.5, 0.01 );
applyToAllStrings( stringFactor );
else if ( event.keyCode === RIGHT_ARROW ) {
this.doubleStrings();
}
else if ( event.keyCode === UP_ARROW ) {
this.setStride( this.stride + 1 );
}
else if ( event.keyCode === DOWN_ARROW ) {
this.setStride( this.stride - 1 );
}
else if ( event.keyCode === SPACE_BAR ) {
this.reset();
}
}

function setStride( newStride: number ): void {

// Handle wraparound.
if ( newStride > words.length - 1 ) {
newStride = 0;
}
else if ( newStride < 0 ) {
newStride = words.length - 1;
}
/**
* Doubles the length of all strings.
*/
private doubleStrings(): void {
this.setStringFactor( this.stringFactor * 2 );
}

stride = newStride;
console.log( `stride = ${stride}` );
/**
* Halves the length of all strings.
*/
private halveStrings(): void {
this.setStringFactor( Math.max( this.stringFactor * 0.5, 0.01 ) );
}

localizedStrings.forEach( ( localizedString, index ) => {
localizedString.property.value = words[ ( index + stride ) % words.length ];
} );
}
/**
* Sets a new stringFactor, and applies that stringFactor to all strings.
*/
private setStringFactor( stringFactor: number ): void {
assert && assert( stringFactor > 0, `stringFactor must be > 0: ${stringFactor}` );
assert && assert( stringFactor <= 1 || Number.isInteger( stringFactor ),
`stringFactor values greater than 1 must be integers: ${stringFactor}` );

this.stringFactor = stringFactor;
console.log( `stringFactor = ${this.stringFactor}` );
applyToAllStrings( this.stringFactor );
}

function reset(): void {
/**
* Sets a new stride value, and causes strings to be set to values from the WORDS array.
*/
private setStride( newStride: number ): void {
assert && assert( Number.isInteger( newStride ), `newString must be an integer: ${newStride}` );

stringFactor = 1;
console.log( `stringFactor = ${stringFactor}` );
applyToAllStrings( stringFactor );
const words = DynamicStringTest.WORDS;

stride = 0;
console.log( `stride = ${stride}` );
// Handle wraparound.
if ( newStride > words.length - 1 ) {
newStride = 0;
}
else if ( newStride < 0 ) {
newStride = words.length - 1;
}

window.addEventListener( 'keydown', event => {
if ( event.keyCode === LEFT_ARROW ) {
halveStrings();
}
else if ( event.keyCode === RIGHT_ARROW ) {
doubleStrings();
}
else if ( event.keyCode === UP_ARROW ) {
setStride( stride + 1 );
}
else if ( event.keyCode === DOWN_ARROW ) {
setStride( stride - 1 );
}
else if ( event.keyCode === SPACE_BAR ) {
reset();
}
this.stride = newStride;
console.log( `stride = ${this.stride}` );

// Set each string to a word from WORDS.
localizedStrings.forEach( ( localizedString, index ) => {
localizedString.property.value = words[ ( index + this.stride ) % words.length ];
} );
}

/**
* Resets stride and stringFactor.
*/
private reset(): void {
this.setStride( 0 );
this.setStringFactor( 1 ); // reset stringFactor last, so that strings are reset to initial values
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions js/SimDisplay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ export default class SimDisplay extends Display {
}

if ( phet.chipper.queryParameters.stringTest === 'dynamic' ) {

DynamicStringTest.init();
const dynamicStringTest = new DynamicStringTest();
window.addEventListener( 'keydown', event => dynamicStringTest.handleEvent( event ) );
}

this.initializeEvents( {
Expand Down

0 comments on commit 1a5deeb

Please sign in to comment.