'use strict';

Object.defineProperty(exports, "__esModule", {
  value: true
});

var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();

var _orbitViewport = require('../viewports/orbit-viewport');

var _orbitViewport2 = _interopRequireDefault(_orbitViewport);

var _orbitState = require('../controllers/orbit-state');

var _orbitState2 = _interopRequireDefault(_orbitState);

var _viewportControls = require('../controllers/viewport-controls');

var _viewportControls2 = _interopRequireDefault(_viewportControls);

var _mjolnir = require('mjolnir.js');

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }

var PREFIX = '-webkit-';

var CURSOR = {
  GRABBING: PREFIX + 'grabbing',
  GRAB: PREFIX + 'grab',
  POINTER: 'pointer'
};

var getDefaultCursor = function getDefaultCursor(_ref) {
  var isDragging = _ref.isDragging;
  return isDragging ? CURSOR.GRABBING : CURSOR.GRAB;
};

function getPropTypes(PropTypes) {
  return {
    /* Viewport properties */
    lookAt: PropTypes.arrayOf(PropTypes.number), // target position
    distance: PropTypes.number, // distance from camera to the target
    rotationX: PropTypes.number, // rotation around X axis
    rotationY: PropTypes.number, // rotation around Y axis
    translationX: PropTypes.number, // translation x in screen space
    translationY: PropTypes.number, // translation y in screen space
    zoom: PropTypes.number, // scale in screen space
    minZoom: PropTypes.number,
    maxZoom: PropTypes.number,
    fov: PropTypes.number, // field of view
    near: PropTypes.number,
    far: PropTypes.number,
    width: PropTypes.number.isRequired, // viewport width in pixels
    height: PropTypes.number.isRequired, // viewport height in pixels

    /* Model properties */
    bounds: PropTypes.object, // bounds in the shape of {minX, minY, minZ, maxX, maxY, maxZ}

    /* Callbacks */
    onViewportChange: PropTypes.func.isRequired,

    /** Accessor that returns a cursor style to show interactive state */
    getCursor: PropTypes.func,

    /* Controls */
    orbitControls: PropTypes.object
  };
}

var defaultProps = {
  lookAt: [0, 0, 0],
  rotationX: 0,
  rotationY: 0,
  translationX: 0,
  translationY: 0,
  distance: 10,
  zoom: 1,
  minZoom: 0,
  maxZoom: Infinity,
  fov: 50,
  near: 1,
  far: 1000,
  getCursor: getDefaultCursor
};

/*
 * Maps mouse interaction to a deck.gl Viewport
 */

var OrbitControllerJS = function () {
  _createClass(OrbitControllerJS, null, [{
    key: 'getViewport',

    // Returns a deck.gl Viewport instance, to be used with the DeckGL component
    value: function getViewport(viewport) {
      return new _orbitViewport2.default(viewport);
    }
  }]);

  function OrbitControllerJS(props) {
    _classCallCheck(this, OrbitControllerJS);

    props = Object.assign({}, defaultProps, props);

    this.props = props;

    this.state = {
      // Whether the cursor is down
      isDragging: false
    };

    this.canvas = props.canvas;

    var eventManager = new _mjolnir.EventManager(this.canvas);

    this._eventManager = eventManager;

    this._controls = props.orbitControls || new _viewportControls2.default(_orbitState2.default);
    this._controls.setOptions(Object.assign({}, this.props, {
      onStateChange: this._onInteractiveStateChange.bind(this),
      eventManager: eventManager
    }));
  }

  _createClass(OrbitControllerJS, [{
    key: 'setProps',
    value: function setProps(props) {
      props = Object.assign({}, this.props, props);
      this.props = props;

      this._controls.setOptions(props);
    }
  }, {
    key: 'finalize',
    value: function finalize() {
      this._eventManager.destroy();
    }
  }, {
    key: '_onInteractiveStateChange',
    value: function _onInteractiveStateChange(_ref2) {
      var _ref2$isDragging = _ref2.isDragging,
          isDragging = _ref2$isDragging === undefined ? false : _ref2$isDragging;

      if (isDragging !== this.state.isDragging) {
        this.state.isDragging = isDragging;
        var getCursor = this.props.getCursor;

        this.canvas.style.cursor = getCursor(this.state);
      }
    }
  }]);

  return OrbitControllerJS;
}();

exports.default = OrbitControllerJS;


OrbitControllerJS.displayName = 'OrbitController';
OrbitControllerJS.defaultProps = defaultProps;
OrbitControllerJS.getPropTypes = getPropTypes;
//# sourceMappingURL=orbit-controller-js.js.map