-
Notifications
You must be signed in to change notification settings - Fork 9
/
ShakerNode.js
143 lines (120 loc) · 4.42 KB
/
ShakerNode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// Copyright 2013-2019, University of Colorado Boulder
/**
* Shaker that contains a solute in solid form.
*
* @author Chris Malley (PixelZoom, Inc.)
*/
define( require => {
'use strict';
// modules
const ArrowNode = require( 'SCENERY_PHET/ArrowNode' );
const beersLawLab = require( 'BEERS_LAW_LAB/beersLawLab' );
const Circle = require( 'SCENERY/nodes/Circle' );
const Image = require( 'SCENERY/nodes/Image' );
const inherit = require( 'PHET_CORE/inherit' );
const MovableDragHandler = require( 'SCENERY_PHET/input/MovableDragHandler' );
const Node = require( 'SCENERY/nodes/Node' );
const PhetFont = require( 'SCENERY_PHET/PhetFont' );
const RichText = require( 'SCENERY/nodes/RichText' );
// images
const shakerImage = require( 'image!BEERS_LAW_LAB/shaker.png' );
// constants
const DEBUG_ORIGIN = false;
const ARROW_LENGTH = 40;
const ARROW_OPTIONS = {
tailWidth: 23,
headWidth: 40,
headHeight: 30,
fill: 'yellow',
stroke: 'rgb(160,160,160)'
};
/**
* @param {Shaker} shaker
* @param {ModelViewTransform2} modelViewTransform
* @param {Tandem} tandem
* @constructor
*/
function ShakerNode( shaker, modelViewTransform, tandem ) {
const self = this;
Node.call( this, {
tandem: tandem,
// Performance optimization so Scenery won't fit blocks around this.
// See https://github.com/phetsims/beers-law-lab/issues/113
preventFit: true
} );
// shaker image
const imageNode = new Image( shakerImage );
imageNode.setScaleMagnitude( 0.75 );
// label
const labelNode = new RichText( shaker.soluteProperty.get().formula, {
font: new PhetFont( { size: 22, weight: 'bold' } ),
fill: 'black',
maxWidth: 0.5 * imageNode.width, // constrain width for i18n
tandem: tandem.createTandem( 'labelNode' )
} );
// arrows
const arrowsOffset = imageNode.height / 2 + 4;
const downArrowNode = new ArrowNode( 0, 0, 0, ARROW_LENGTH, _.extend( {
top: arrowsOffset,
tandem: tandem.createTandem( 'downArrowNode' )
}, ARROW_OPTIONS ) );
const upArrowNode = new ArrowNode( 0, 0, 0, -ARROW_LENGTH, _.extend( {
bottom: -arrowsOffset,
tandem: tandem.createTandem( 'upArrowNode' )
}, ARROW_OPTIONS ) );
const arrowsParent = new Node( {
children: [ upArrowNode, downArrowNode ],
center: imageNode.center,
pickable: false,
visible: false
});
// common parent, to simplify rotation and label alignment.
const parentNode = new Node( { children: [ imageNode, labelNode, arrowsParent ] } );
this.addChild( parentNode );
parentNode.rotate( shaker.orientation - Math.PI ); // assumes that shaker points to the left in the image file
// Manually adjust these values until the origin is in the middle hole of the shaker.
parentNode.translate( -12, -imageNode.height / 2 );
// origin
if ( DEBUG_ORIGIN ) {
this.addChild( new Circle( { radius: 3, fill: 'red' } ) );
}
// sync location with model
let shakerWasMoved = false;
shaker.locationProperty.link( function( location ) {
self.translation = modelViewTransform.modelToViewPosition( location );
shakerWasMoved = true;
arrowsParent.visible = false;
} );
shakerWasMoved = false; // reset to false, because function is fired when link is performed
// sync visibility with model
shaker.visibleProperty.link( function( visible ) {
self.setVisible( visible );
} );
// sync solute with model
shaker.soluteProperty.link( function( solute ) {
// label the shaker with the solute formula
labelNode.setText( solute.formula );
// center the label on the shaker
const capWidth = 0.3 * imageNode.width; // multiplier is dependent on image file
labelNode.centerX = capWidth + ( imageNode.width - capWidth ) / 2;
labelNode.centerY = imageNode.centerY;
} );
// interactivity
this.cursor = 'pointer';
this.addInputListener( new MovableDragHandler( shaker.locationProperty, {
tandem: tandem.createTandem( 'inputListener' ),
dragBounds: shaker.dragBounds,
modelViewTransform: modelViewTransform
} ) );
this.addInputListener( {
enter: function() {
arrowsParent.visible = !shakerWasMoved;
},
exit: function() {
arrowsParent.visible = false;
}
} );
}
beersLawLab.register( 'ShakerNode', ShakerNode );
return inherit( Node, ShakerNode );
} );