# all files / src/plugin/ FreeControl.js

1.22% Statements 1/82
0% Branches 0/59
0% Functions 0/11
1.22% Lines 1/82

    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

    1×

     import Base from '../core/Base';
    import Vector3 from '../math/Vector3';

    /**
     * @constructor clay.plugin.FreeControl
     * @example
     *     var control = new clay.plugin.FreeControl({
     *         target: camera,
     *         domElement: renderer.canvas
     *     });
     *     ...
     *     animation.on('frame', function(frameTime) {
     *         control.update(frameTime);
     *         renderer.render(scene, camera);
     *     });
     */
    var FreeControl = Base.extend(function() {
        return /** @lends clay.plugin.FreeControl# */ {
            /**
             * Scene node to control, mostly it is a camera
             * @type {clay.Node}
             */
            target: null,

            /**
             * Target dom to bind with mouse events
             * @type {HTMLElement}
             */
            domElement: null,

            /**
             * Mouse move sensitivity
             * @type {number}
             */
            sensitivity: 1,

            /**
             * Target move speed
             * @type {number}
             */
            speed: 0.4,

            /**
             * Up axis
             * @type {clay.Vector3}
             */
            up: new Vector3(0, 1, 0),

            /**
             * If lock vertical movement
             * @type {boolean}
             */
            verticalMoveLock: false,

            /**
             * @type {clay.Timeline}
             */
            animation: null,

            _moveForward: false,
            _moveBackward: false,
            _moveLeft: false,
            _moveRight: false,

            _offsetPitch: 0,
            _offsetRoll: 0
        };
    }, function() {
        this._lockChange = this._lockChange.bind(this);
        this._keyDown = this._keyDown.bind(this);
        this._keyUp = this._keyUp.bind(this);
        this._mouseMove = this._mouseMove.bind(this);

        if (this.domElement) {
            this.enable();
        }
    },
    /** @lends clay.plugin.FreeControl.prototype */
    {
        /**
         * Enable control
         */
        enable: function() {
            // Use pointer lock
            // http://www.html5rocks.com/en/tutorials/pointerlock/intro/
            var el = this.domElement;

            //Must request pointer lock after click event, can't not do it directly
            //Why ? ?
            el.addEventListener('click', this._requestPointerLock);

            document.addEventListener('pointerlockchange', this._lockChange);
            document.addEventListener('mozpointerlockchange', this._lockChange);
            document.addEventListener('webkitpointerlockchange', this._lockChange);

            document.addEventListener('keydown', this._keyDown);
            document.addEventListener('keyup', this._keyUp);

            if (this.animation) {
                this.animation.on('frame', this._detectMovementChange, this);
            }
        },

        /**
         * Disable control
         */
        disable: function() {

            this.target.off('beforeupdate', this._beforeUpdateCamera);

            var el = this.domElement;

            el.exitPointerLock = el.exitPointerLock
                || el.mozExitPointerLock
                || el.webkitExitPointerLock;

            if (el.exitPointerLock) {
                el.exitPointerLock();
            }

            this.domElement.removeEventListener('click', this._requestPointerLock);

            document.removeEventListener('pointerlockchange', this._lockChange);
            document.removeEventListener('mozpointerlockchange', this._lockChange);
            document.removeEventListener('webkitpointerlockchange', this._lockChange);

            document.removeEventListener('keydown', this._keyDown);
            document.removeEventListener('keyup', this._keyUp);

            if (this.animation) {
                this.animation.off('frame', this._detectMovementChange);
            }
        },

        _requestPointerLock: function() {
            var el = this;
            el.requestPointerLock = el.requestPointerLock
                || el.mozRequestPointerLock
                || el.webkitRequestPointerLock;

            el.requestPointerLock();
        },

        /**
         * Control update. Should be invoked every frame
         * @param {number} frameTime Frame time
         */
        update: function(frameTime) {
            var target = this.target;

            var position = this.target.position;
            var xAxis = target.localTransform.x.normalize();
            var zAxis = target.localTransform.z.normalize();

            if (this.verticalMoveLock) {
                zAxis.y = 0;
                zAxis.normalize();
            }

            var speed = this.speed * frameTime / 20;

            if (this._moveForward) {
                // Opposite direction of z
                position.scaleAndAdd(zAxis, -speed);
            }
            if (this._moveBackward) {
                position.scaleAndAdd(zAxis, speed);
            }
            if (this._moveLeft) {
                position.scaleAndAdd(xAxis, -speed / 2);
            }
            if (this._moveRight) {
                position.scaleAndAdd(xAxis, speed / 2);
            }

            target.rotateAround(target.position, this.up, -this._offsetPitch * frameTime * Math.PI / 360);
            var xAxis = target.localTransform.x;
            target.rotateAround(target.position, xAxis, -this._offsetRoll * frameTime * Math.PI / 360);

            this._offsetRoll = this._offsetPitch = 0;
        },

        _lockChange: function() {
            if (
                document.pointerLockElement === this.domElement
                || document.mozPointerLockElement === this.domElement
                || document.webkitPointerLockElement === this.domElement
            ) {
                document.addEventListener('mousemove', this._mouseMove, false);
            } else {
                document.removeEventListener('mousemove', this._mouseMove);
            }
        },

        _mouseMove: function(e) {
            var dx = e.movementX || e.mozMovementX || e.webkitMovementX || 0;
            var dy = e.movementY || e.mozMovementY || e.webkitMovementY || 0;

            this._offsetPitch += dx * this.sensitivity / 200;
            this._offsetRoll += dy * this.sensitivity / 200;

            // Trigger change event to remind renderer do render
            this.trigger('change');
        },

        _detectMovementChange: function () {
            if (this._moveForward || this._moveBackward || this._moveLeft || this._moveRight) {
                this.trigger('change');
            }
        },

        _keyDown: function(e) {
            switch(e.keyCode) {
                case 87: //w
                case 37: //up arrow
                    this._moveForward = true;
                    break;
                case 83: //s
                case 40: //down arrow
                    this._moveBackward = true;
                    break;
                case 65: //a
                case 37: //left arrow
                    this._moveLeft = true;
                    break;
                case 68: //d
                case 39: //right arrow
                    this._moveRight = true;
                    break;
            }
            // Trigger change event to remind renderer do render
            this.trigger('change');
        },

        _keyUp: function(e) {
            switch(e.keyCode) {
                case 87: //w
                case 37: //up arrow
                    this._moveForward = false;
                    break;
                case 83: //s
                case 40: //down arrow
                    this._moveBackward = false;
                    break;
                case 65: //a
                case 37: //left arrow
                    this._moveLeft = false;
                    break;
                case 68: //d
                case 39: //right arrow
                    this._moveRight = false;
                    break;
            }
        }
    });

    export default FreeControl;