File size: 8,086 Bytes
e1d589b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/**
 * Sets up and supports the controls in the top left of the screen. This currently includes:
 *    - A debug toggle button.
 *    - A blob refresh button.
 *    - A gravity control.
 *
 * The gravity control button is the most intricate as it work with mouse up, down and drag events.
 * It also responds to clicks for enabling and disabling.
 *
 * @param {Object} options An object literal with the following properties:
 *     - {Experiment} experiment The experiment that these controls are bound to.
 *     - {debugEl} Element The dom element for the debug toggle button.
 *     - {resetEl} Element The dom element for the reset button.
 *     - {gravityEl} Element The dom element for the gravity control button.
 */
var Controls = function(options){
    this.experiment_ = options.experiment;

    this.debugEl_ = options.debugEl;
    this.debugEl_.addEventListener('click', this.toggleRenderMode_.bind(this));

    this.resetEl_ = options.resetEl;
    this.resetEl_.addEventListener('click', this.resetButtonClicked_.bind(this));

    this.gravityEl_ = options.gravityEl;
    this.gravityArrow_ = this.gravityEl_.querySelector('i');
    this.updateGravitButtonDims_();

    this.prevGravityScale_ = 1;
    this.prevGravityDirection_ = vec2.createFrom(0, 1);
    this.currentGravity = vec2.create();

    // Pre-bind the mouse move callback to ease adding and removing event listeners.
    this.gravityButtonMouseMove_ = this.gravityButtonMouseMove_.bind(this);
    this.gravityEl_.addEventListener('mousedown', this.gravityButtonMouseDown_.bind(this));
    document.body.addEventListener('mouseup', this.gravityButtonMouseUp_.bind(this));
};

Controls.prototype = {
    /**
     * Whether or not the blob is rendering in debug mode.
     *
     * @type {Boolean}
     * @private
     */
    debugModeEnabled: null,

    /**
     * The current gravity the blob should be feeling.
     *
     * @type {vec2}
     */
    currentGravity: null,

    /**
     * A reference to the experiment that these controls belong to.
     *
     * @type {Experiment}
     * @private
     */
    experiment_: null,

    /**
     * The dom element for the debug toggle button.
     *
     * @type {Element}
     * @private
     */
    debugEl_: null,

    /**
     * The dom element for the reset button.
     *
     * @type {Element}
     * @private
     */
    resetEl_: null,

    /**
     * The dom element for the gravity control button.
     *
     * @type {Element}
     * @private
     */
    gravityEl_: null,

    /**
     * The dom element for the arrow button inside of the gravity control.
     *
     * @type {Element}
     * @private
     */
    gravityArrow_: null,

    /**
     * A timer id that is set when the user clicks the reset button.
     *
     * @type {number|null}
     */
    resetAnimationTimeout_: null,

    /**
     * The radius of the gravity button.
     *
     * @type {number}
     * @private
     */
    gravityButtonRadius_: null,

    /**
     * The center coordinates of the gravity button.
     *
     * @type {Vec2}
     * @private
     */
    buttonCenter_: null,

    /**
     * The position where the user started their mouse down action. This will be reset to null on
     * the next mouse up event.
     *
     * @type {Vec2|null}
     * @private
     */
    mouseStartPosition_: null,

    /**
     * Called when the reset button is clicked.
     *
     * @param {MouseEvent} evt
     * @private
     */
    resetButtonClicked_: function(evt){
        evt.preventDefault();

        if (this.resetAnimationTimeout_) return;

        this.resetEl_.classList.add('animate');

        this.resetAnimationTimeout_ = setTimeout(function(){
            this.resetEl_.classList.remove('animate');

            this.resetAnimationTimeout_ = null;
        }.bind(this), 500);

        this.experiment_.reset();
    },

    /**
     * Toggle whether or not the blob should be rendering in debug mode.
     *
     * @private
     */
    toggleRenderMode_: function(){
        this.debugModeEnabled = !this.debugModeEnabled;

        if (this.debugModeEnabled){
            document.body.classList.add('debug-mode');
        } else {
            document.body.classList.remove('debug-mode');
        }
    },

    /**
     * Updates the cached dimensions and location of the gravity control button.
     *
     * @private
     */
    updateGravitButtonDims_: function(){
        this.gravityButtonRadius_ = this.gravityEl_.clientWidth / 2;
        this.buttonCenter_ = vec2.createFrom(this.gravityEl_.offsetLeft + this.gravityButtonRadius_,
            this.gravityEl_.offsetTop + this.gravityButtonRadius_);
    },

    /**
     * Called when the user presses their mouse down on the gravity control.
     *
     * @param {MouseEvent} evt
     * @private
     */
    gravityButtonMouseDown_: function(evt){
        evt.preventDefault();

        this.updateGravitButtonDims_();

        // Store the start position so we know if the user has clicked later.
        this.mouseStartPosition_ = Utilities.eventToVec2(evt);

        document.body.addEventListener('mousemove', this.gravityButtonMouseMove_);
    },

    /**
     * Called when ever the user has already pressed down on the gravity button and is now moving
     * their mouse, this method will update the direction and strength of the gravity
     *
     * @param {MouseEvent} evt
     * @private
     */
    gravityButtonMouseMove_: function(evt){
        evt.preventDefault();

        var direction = vec2.subtract(this.buttonCenter_, Utilities.eventToVec2(evt));
        vec2.scale(direction, -1);

        var length = vec2.length(direction);
        var magnitude = (length < this.gravityButtonRadius_) ?
            length / this.gravityButtonRadius_ : 1;

        this.updateGravity_(direction, Math.max(magnitude, 0.3));
    },

    /**
     * Called when ever the user releases their mouse down. This method will check if the user
     * clicked or if they performed a drag.
     *
     * @param {MouseEvent} evt
     * @private
     */
    gravityButtonMouseUp_: function(evt){
        if (!this.mouseStartPosition_) return;

        evt.preventDefault();

        // If the user dragged less that 5px, just assume this was a click.
        if (vec2.dist(Utilities.eventToVec2(evt), this.mouseStartPosition_) < 5){
            if (vec2.length(this.currentGravity)){
                // If the world currently has gravity, disable it.
                this.updateGravity_(vec2.createFrom(0, 0), 0);
            } else {
                // If the world previously did not have gravity, restore the previous values.
                this.updateGravity_(this.prevGravityDirection_, this.prevGravityScale_);
            }
        }

        this.mouseStartPosition_ = null;

        document.body.removeEventListener('mousemove', this.gravityButtonMouseMove_);
    },

    /**
     * Updates the current gravity given a direction and a magnitude.
     * @param {vec2} direction The direction gravity should be pointing.
     * @param {number} magnitude The magnitude of the gravity [0-1];
     */
    updateGravity_: function(direction, magnitude){
        if (magnitude !== 0){
            // Save this state and update the button.
            this.prevGravityScale_ = magnitude;
            this.prevGravityDirection_ = direction;

            // Update the angle of the arrow.
            var angle = Math.atan2(direction[1], direction[0]);

            [
                'webkitTransform',
                'mozTransform',
                'msTransform',
                'oTransform',
                'transform'
            ].forEach(function(property){
                this.gravityArrow_.style[property] = 'rotate(' + angle.toFixed(2) +
                    'rad) ' + 'scale(' + magnitude + ')';
            }, this);
            document.body.classList.add('gravity-enabled');
        } else {
            document.body.classList.remove('gravity-enabled');
        }

        this.currentGravity = vec2.scale(vec2.normalize(direction),
            Blob.MAX_GRAVITY * magnitude);
    }
};