Skip to content

Commit

Permalink
support creating GrabDragInteraction with state from previous model, #…
Browse files Browse the repository at this point in the history
…354

Signed-off-by: Michael Kauzmann <[email protected]>
  • Loading branch information
zepumph committed Aug 23, 2024
1 parent a5c3250 commit 9424e93
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 11 additions & 0 deletions js/common/model/Mass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,14 @@ export default abstract class Mass extends PhetioObject {

public readonly resetEmitter = new Emitter();

// MassViews and their GrabDragInteractions are recreated, the mass stores relevant information from one instantiation to
// another. These quantities do not need to be PhET-iO stateful, because they are related to usability and
// accessibility, and when studio launches a Standard PhET-iO Wrapper, these values should be zeroed out, not preserved
// in the state. These values are internal, and should not be read. Instead, get the most up-to-date info from the
// view's GrabDragInteraction itself.
public numberOfKeyboardGrabs = 0;
public numberOfGrabs = 0;

protected constructor( engine: PhysicsEngine, providedOptions: MassOptions ) {

const options = optionize<MassOptions, SelfOptions, PhetioObjectOptions>()( {
Expand Down Expand Up @@ -582,6 +590,9 @@ export default abstract class Mass extends PhetioObject {

this.resetPosition();

this.numberOfGrabs = 0;
this.numberOfKeyboardGrabs = 0;

this.resetEmitter.emit();
}

Expand Down
10 changes: 8 additions & 2 deletions js/common/view/MassView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,11 @@ export default abstract class MassView extends Disposable {
onRelease() {
endKeyboardInteraction();
},
tandem: Tandem.OPT_OUT
tandem: Tandem.OPT_OUT,
numberOfKeyboardGrabs: mass.numberOfKeyboardGrabs,
numberOfGrabs: mass.numberOfGrabs
} );


const myListener = () => {

if ( this.focusablePath && this.grabDragInteraction && !this.focusablePath.isDisposed ) {
Expand Down Expand Up @@ -205,6 +206,11 @@ export default abstract class MassView extends Disposable {
mass.transformedEmitter.addListener( myListener );

this.disposeEmitter.addListener( () => {

// Keep in sync for the next time this mass will create a new view
mass.numberOfKeyboardGrabs = this.grabDragInteraction!.numberOfKeyboardGrabs;
mass.numberOfGrabs = this.grabDragInteraction!.numberOfGrabs;

this.grabDragInteraction!.dispose();
keyboardDragListener.dispose();
this.focusablePath!.dispose();
Expand Down

0 comments on commit 9424e93

Please sign in to comment.