@@ -119,6 +119,8 @@ export type AppState = {
119119 sceneDimensions : SceneDimensions ,
120120 drawingEnabled : boolean ,
121121 runningState : RunningState ,
122+ // programSpeed is a number in the range 1..(speedLookUp.length)
123+ programSpeed : number ,
122124 disallowedActions : ActionToggleRegister ,
123125 keyBindingsEnabled : boolean ,
124126 keyboardInputSchemeName : KeyboardInputSchemeName ,
@@ -154,7 +156,6 @@ export class App extends React.Component<AppProps, AppState> {
154156 customBackgroundSerializer : CustomBackgroundSerializer ;
155157 speedLookUp : Array < number > ;
156158 pushStateTimeoutID : ?TimeoutID ;
157- speedControlRef : { current : null | HTMLElement } ;
158159 programBlockEditorRef : { current : any } ;
159160 sequenceInProgress : Array < KeyboardEvent > ;
160161 announcementBuilder : AnnouncementBuilder ;
@@ -216,6 +217,8 @@ export class App extends React.Component<AppProps, AppState> {
216217 sceneDimensions : this . sceneDimensions ,
217218 drawingEnabled : true ,
218219 runningState : 'stopped' ,
220+ // Default to the middle speed
221+ programSpeed : Math . ceil ( this . speedLookUp . length / 2 ) ,
219222 disallowedActions : { } ,
220223 keyBindingsEnabled : false ,
221224 showKeyboardModal : false ,
@@ -262,12 +265,16 @@ export class App extends React.Component<AppProps, AppState> {
262265 this . programChangeController = new ProgramChangeController ( this ,
263266 this . props . intl , this . audioManager ) ;
264267
265- this . speedControlRef = React . createRef ( ) ;
266268 this . programBlockEditorRef = React . createRef ( ) ;
267269
268270 const actionsHandler = new ActionsHandler ( this , this . audioManager ,
269271 this . sceneDimensions , this . props . intl ) ;
270- this . interpreter = new Interpreter ( 1000 , this , actionsHandler ) ;
272+
273+ this . interpreter = new Interpreter (
274+ this . speedLookUp [ this . state . programSpeed - 1 ] ,
275+ this ,
276+ actionsHandler
277+ ) ;
271278 }
272279
273280 setStateSettings ( settings : $Shape < AppSettings > ) {
@@ -660,10 +667,24 @@ export class App extends React.Component<AppProps, AppState> {
660667 }
661668 break ;
662669 case ( "decreaseProgramSpeed" ) :
663- this . changeProgramSpeedIndex ( this . speedLookUp . indexOf ( this . interpreter . stepTimeMs ) - 1 ) ;
670+ this . setState ( ( state ) => {
671+ return {
672+ programSpeed : Math . max (
673+ 1 ,
674+ state . programSpeed - 1
675+ )
676+ } ;
677+ } ) ;
664678 break ;
665679 case ( "increaseProgramSpeed" ) :
666- this . changeProgramSpeedIndex ( this . speedLookUp . indexOf ( this . interpreter . stepTimeMs ) + 1 ) ;
680+ this . setState ( ( state ) => {
681+ return {
682+ programSpeed : Math . min (
683+ this . speedLookUp . length ,
684+ state . programSpeed + 1
685+ )
686+ } ;
687+ } ) ;
667688 break ;
668689 case ( "selectForward1" ) :
669690 this . setState ( { "selectedAction" : "forward1" } ) ;
@@ -964,20 +985,14 @@ export class App extends React.Component<AppProps, AppState> {
964985 } ) ;
965986 }
966987
967- changeProgramSpeedIndex = ( newSpeedIndex : number ) = > {
968- if ( newSpeedIndex >= 0 && newSpeedIndex <= ( this . speedLookUp . length - 1 ) ) {
969- this . interpreter . setStepTime ( this . speedLookUp [ newSpeedIndex ] ) ;
970- if ( this . speedControlRef . current ) {
971- // $FlowFixMe: Flow doesn't believe that we have sufficiently ensured that current !== null.
972- this . speedControlRef . current . value = ( newSpeedIndex + 1 ) . toString ( ) ;
973- }
988+ handleChangeProgramSpeed = ( value : number ) = > {
989+ if ( value >= 1 && value <= this . speedLookUp . length ) {
990+ this . setState ( {
991+ programSpeed : value
992+ } ) ;
974993 }
975994 }
976995
977- handleChangeProgramSpeed = ( stepTimeMs : number ) = > {
978- this . interpreter . setStepTime ( stepTimeMs ) ;
979- }
980-
981996 renderCommandBlocks = ( commands : Array < DisplayedCommandName > ) => {
982997 const commandBlocks = [ ] ;
983998
@@ -1570,8 +1585,8 @@ export class App extends React.Component<AppProps, AppState> {
15701585 onClick = { this . handleStop }
15711586 />
15721587 < ProgramSpeedController
1573- rangeControlRef = { this . speedControlRef }
1574- values = { this . speedLookUp }
1588+ numValues = { this . speedLookUp . length }
1589+ value = { this . state . programSpeed }
15751590 onChange = { this . handleChangeProgramSpeed }
15761591 />
15771592 </ div >
@@ -1993,6 +2008,16 @@ export class App extends React.Component<AppProps, AppState> {
19932008 document.body.className = ` $ { this . state . settings . theme } - theme `;
19942009 }
19952010
2011+ if ( this . state . programSpeed !== prevState . programSpeed ) {
2012+ if ( this . speedLookUp . length > 0
2013+ && this . state . programSpeed >= 1
2014+ && this . state . programSpeed <= this . speedLookUp . length ) {
2015+ this . interpreter . setStepTime (
2016+ this . speedLookUp [ this . state . programSpeed - 1 ]
2017+ ) ;
2018+ }
2019+ }
2020+
19962021 /* Dash connection removed for version 0.5
19972022 if (this.state.dashConnectionStatus !== prevState.dashConnectionStatus) {
19982023 console.log(this.state.dashConnectionStatus);
0 commit comments