<commit_before>import merge from 'lodash/object/merge';
import OrbitControls from './OrbitControls';
import TWEEN from 'tween.js';

export default class ModelControls {

  constructor(container, camera, group, config = {}) {

    this.container = container;
    this.camera = camera;
    this.group = group;

    // Default configuration params
    this.controlsConfigDefault = {

      startupAnimation: true,

      targetRotationX: 0,
      targetRotationOnMouseDownX: 0,

      targetRotationY: 0,
      targetRotationOnMouseDownY: 0,

      mouseX: 0,
      mouseXOnMouseDown: 0,

      mouseY: 0,
      mouseYOnMouseDown: 0,

      clientHalfX: (this.container.clientWidth / 2),
      clientHalfY: (this.container.clientHeight / 2),

      finalRotationY: null
    };

    this.controlsConfig = merge({}, this.controlsConfigDefault, config);

    this._init();

    if (this.controlsConfig.startupAnimation === true) {
      this._createStartUpAnimation();
    }
  }

  update(time) {

    const group = this.group;

    if (this.controls) {
      this.controls.update();
    }

    if (this.tween) {
      TWEEN.update(time);
    }

    if (group) {

      const cfg = this.controlsConfig;

      group.rotation.y += ( cfg.targetRotationX - group.rotation.y ) * 0.1;

      // vertical rotation
      cfg.finalRotationY = (cfg.targetRotationY - group.rotation.x);
      group.rotation.x += cfg.finalRotationY * 0.05;
    }
  }

  destroy() {
    this._removeEventListener();

    // Remove orbit control
    if (this.controls) {
      this.controls.dispose();
    }

    if (this.tween) {
      TWEEN.remove(this.tween);
      this.tween = null;
    }
  }

  _createStartUpAnimation() {

    const {targetRotationY, targetRotationX} = this.controlsConfig;
    const self = this;

    const coords = {
      x: targetRotationX / Math.PI * 180,
      y: targetRotationY / Math.PI * 180
    };

    if (this.tween) {
      TWEEN.remove(this.tween);
      this.tween = null;
    }

    this.tween = new TWEEN.Tween(coords)
      .to({ x: 360, y: 360 }, 5000)
      .onUpdate(function() {
        self.controlsConfig.targetRotationY = this.y * Math.PI / 180;
        self.controlsConfig.targetRotationX = this.x * Math.PI / 180;
      })
      .start();
  }

  _init() {

    // Clean registered event listener
    this._removeEventListener();
    this.controlsConfig = merge({}, this.controlsConfigDefault);
    this._setupListener();

    // Delecate to orbit controls
    const controls = new OrbitControls(this.camera, this.container);

    controls.enableKeys = false;
    controls.enableRotate = false;
    controls.enablePan = false;
    controls.enableDamping = false;
    controls.enableZoom = true;

    let bb = new THREE.Box3();
    bb.setFromObject(this.group);
    bb.center(controls.target);

    this.controls = controls;
  }

  _setupListener() {

    const container = this.container;

    // Add resize listener
    this._resizeListener = (evt)=>{ this._onWindowResize(evt); };
    window.addEventListener('resize', this._resizeListener, false);

    // Controls
    this._mouseDownListener = (e) => { this._onMouseDown(e); };
    this._mouseMoveListener = (e) => { this._onMouseMove(e); };
    this._mouseUpListener = (e) => { this._onMouseUp(e); };
    this._mouseOutListener = (e) => { this._onMouseOut(e); };
    this._touchStartListener = (e) => { this._onTouchStart(e); };
    this._touchEndListener = (e) => { this._onTouchEnd(e); };
    this._touchMoveListener = (e) => { this._onTouchMove(e); };

    // Mouse / Touch events
    container.addEventListener( 'mousedown', this._mouseDownListener, false );
    container.addEventListener( 'touchstart', this._touchStartListener, false );
    container.addEventListener( 'touchmove', this._touchMoveListener, false );
  }

  _removeEventListener() {

    const container = this.container;

    // Remove resize listener
    window.removeEventListener('resize', this._resizeListener, false);
    this._resizeListener = null;

    // Remove model spinning controls
    container.removeEventListener( 'mouseup',    this._mouseUpListener, false );
    container.removeEventListener( 'mousemove',  this._mouseMoveListener, false );
    container.removeEventListener( 'mousedown',  this._mouseDownListener, false );
    container.removeEventListener( 'touchstart', this._touchStartListener, false );
    container.removeEventListener( 'touchmove',  this._touchMoveListener, false );
  }

  _onWindowResize() {

    ['controlsConfig', 'controlsConfigDefault'].forEach((key)=>{

      if (this.hasOwnProperty(key)) {
        this[key].clientHalfX = (this.container.clientWidth / 2);
        this[key].clientHalfY = (this.container.clientHeight / 2);
      }
    });
  }

  _onMouseDown(evt) {

    evt.preventDefault();

    const container = this.container;
    const cfg = this.controlsConfig;

    if (container) {

      const { clientX, clientY } = evt;

      if (this.tween) {
        TWEEN.remove(this.tween);
        this.tween = null;
      }

      container.addEventListener('mousemove', this._mouseMoveListener, false);
      container.addEventListener('mouseup', this._mouseUpListener, false);
      container.addEventListener('mouseout', this._mouseOutListener, false);

      cfg.mouseXOnMouseDown = clientX - cfg.clientHalfX;
      cfg.targetRotationOnMouseDownX = cfg.targetRotationX;

      cfg.mouseYOnMouseDown = clientY - cfg.clientHalfY;
      cfg.targetRotationOnMouseDownY = cfg.targetRotationY;
    }

  }

  _onMouseMove(evt) {

    if (evt) {

      const cfg = this.controlsConfig;

      cfg.mouseX = evt.clientX - cfg.clientHalfX;
      cfg.mouseY = evt.clientY - cfg.clientHalfY;

      cfg.targetRotationY = cfg.targetRotationOnMouseDownY + (cfg.mouseY - cfg.mouseYOnMouseDown) * 0.02;
      cfg.targetRotationX = cfg.targetRotationOnMouseDownX + (cfg.mouseX - cfg.mouseXOnMouseDown) * 0.02;
    }
  }


  _onMouseUp() {

    const {container} = this;

    if (container) {
      container.removeEventListener( 'mousemove', this._mouseMoveListener, false );
      container.removeEventListener( 'mouseup', this._mouseUpListener, false );
      container.removeEventListener( 'mouseout', this._mouseOutListener, false );
    }
  }

  _onMouseOut() {

    const {container} = this;

    if (container) {
      container.removeEventListener('mousemove', this._mouseMoveListener, false);
      container.removeEventListener('mouseup', this._mouseUpListener, false);
      container.removeEventListener('mouseout', this._mouseOutListener, false);
    }
  }

  _onTouchStart(evt = {touches: [] }) {

    const touches = evt.touches;
    const cfg = this.controlsConfig;

    if (touches.length === 1) {

      evt.preventDefault();
      const {pageX, pageY} = touches[0];

      cfg.mouseXOnMouseDown = pageX - cfg.clientHalfX;
      cfg.targetRotationOnMouseDownX = cfg.targetRotationX;

      cfg.mouseYOnMouseDown = pageY - cfg.clientHalfY;
      cfg.targetRotationOnMouseDownY = cfg.targetRotationY;
    }
  }

  _onTouchEnd(evt = {touches: [] }) {

    const touches = evt.touches;
    const cfg = this.controlsConfig;

    if (touches.length === 1) {

      evt.preventDefault();

      const {pageX, pageY} = touches[0];

      cfg.mouseX = pageX - cfg.clientHalfX;
      cfg.targetRotationX = cfg.targetRotationOnMouseDownX + ( cfg.mouseX - cfg.mouseXOnMouseDown ) * 0.05;

      cfg.mouseY = pageY - cfg.clientHalfY;
      cfg.targetRotationY = cfg.targetRotationOnMouseDownY + (cfg.mouseY - cfg.mouseYOnMouseDown) * 0.05;

    }
  }

  _onTouchMove(evt = {touches: [] }) {

    const touches = evt.touches;
    const cfg = this.controlsConfig;

    if (touches.length === 1 ) {

      evt.preventDefault();
      const {pageX, pageY} = touches[0];

      cfg.mouseX = pageX - cfg.clientHalfX;
      cfg.targetRotationX = cfg.targetRotationOnMouseDownX + ( cfg.mouseX - cfg.mouseXOnMouseDown ) * 0.05;

      cfg.mouseY = pageY - cfg.clientHalfY;
      cfg.targetRotationY = cfg.targetRotationOnMouseDownY + (cfg.mouseY - cfg.mouseYOnMouseDown) * 0.05;
    }
  }
}
<commit_msg>change easing<commit_after>import merge from 'lodash/object/merge';
import OrbitControls from './OrbitControls';
import TWEEN from 'tween.js';

export default class ModelControls {

  constructor(container, camera, group, config = {}) {

    this.container = container;
    this.camera = camera;
    this.group = group;

    // Default configuration params
    this.controlsConfigDefault = {

      startupAnimation: true,

      targetRotationX: 0,
      targetRotationOnMouseDownX: 0,

      targetRotationY: 0,
      targetRotationOnMouseDownY: 0,

      mouseX: 0,
      mouseXOnMouseDown: 0,

      mouseY: 0,
      mouseYOnMouseDown: 0,

      clientHalfX: (this.container.clientWidth / 2),
      clientHalfY: (this.container.clientHeight / 2),

      finalRotationY: null
    };

    this.controlsConfig = merge({}, this.controlsConfigDefault, config);

    this._init();

    if (this.controlsConfig.startupAnimation === true) {
      this._createStartUpAnimation();
    }
  }

  update(time) {

    const group = this.group;

    if (this.controls) {
      this.controls.update();
    }

    if (this.tween) {
      TWEEN.update(time);
    }

    if (group) {

      const cfg = this.controlsConfig;

      group.rotation.y += ( cfg.targetRotationX - group.rotation.y ) * 0.1;

      // vertical rotation
      cfg.finalRotationY = (cfg.targetRotationY - group.rotation.x);
      group.rotation.x += cfg.finalRotationY * 0.05;
    }
  }

  destroy() {
    this._removeEventListener();

    // Remove orbit control
    if (this.controls) {
      this.controls.dispose();
    }

    if (this.tween) {
      TWEEN.remove(this.tween);
      this.tween = null;
    }
  }

  _createStartUpAnimation() {

    const {targetRotationY, targetRotationX} = this.controlsConfig;
    const self = this;

    const coords = {
      x: targetRotationX / Math.PI * 180,
      y: targetRotationY / Math.PI * 180
    };

    if (this.tween) {
      TWEEN.remove(this.tween);
      this.tween = null;
    }

    this.tween = new TWEEN.Tween(coords)
      .easing(TWEEN.Easing.Quadratic.In)
      .to({ x: 360, y: 360 }, 3000)
      .onUpdate(function() {
        self.controlsConfig.targetRotationX = this.x * Math.PI / 180;
        self.controlsConfig.targetRotationY = this.y * Math.PI / 180;
      })
      .start();
  }

  _init() {

    // Clean registered event listener
    this._removeEventListener();
    this.controlsConfig = merge({}, this.controlsConfigDefault);
    this._setupListener();

    // Delecate to orbit controls
    const controls = new OrbitControls(this.camera, this.container);

    controls.enableKeys = false;
    controls.enableRotate = false;
    controls.enablePan = false;
    controls.enableDamping = false;
    controls.enableZoom = true;

    let bb = new THREE.Box3();
    bb.setFromObject(this.group);
    bb.center(controls.target);

    this.controls = controls;
  }

  _setupListener() {

    const container = this.container;

    // Add resize listener
    this._resizeListener = (evt)=>{ this._onWindowResize(evt); };
    window.addEventListener('resize', this._resizeListener, false);

    // Controls
    this._mouseDownListener = (e) => { this._onMouseDown(e); };
    this._mouseMoveListener = (e) => { this._onMouseMove(e); };
    this._mouseUpListener = (e) => { this._onMouseUp(e); };
    this._mouseOutListener = (e) => { this._onMouseOut(e); };
    this._touchStartListener = (e) => { this._onTouchStart(e); };
    this._touchEndListener = (e) => { this._onTouchEnd(e); };
    this._touchMoveListener = (e) => { this._onTouchMove(e); };

    // Mouse / Touch events
    container.addEventListener( 'mousedown', this._mouseDownListener, false );
    container.addEventListener( 'touchstart', this._touchStartListener, false );
    container.addEventListener( 'touchmove', this._touchMoveListener, false );
  }

  _removeEventListener() {

    const container = this.container;

    // Remove resize listener
    window.removeEventListener('resize', this._resizeListener, false);
    this._resizeListener = null;

    // Remove model spinning controls
    container.removeEventListener( 'mouseup',    this._mouseUpListener, false );
    container.removeEventListener( 'mousemove',  this._mouseMoveListener, false );
    container.removeEventListener( 'mousedown',  this._mouseDownListener, false );
    container.removeEventListener( 'touchstart', this._touchStartListener, false );
    container.removeEventListener( 'touchmove',  this._touchMoveListener, false );
  }

  _onWindowResize() {

    ['controlsConfig', 'controlsConfigDefault'].forEach((key)=>{

      if (this.hasOwnProperty(key)) {
        this[key].clientHalfX = (this.container.clientWidth / 2);
        this[key].clientHalfY = (this.container.clientHeight / 2);
      }
    });
  }

  _onMouseDown(evt) {

    evt.preventDefault();

    const container = this.container;
    const cfg = this.controlsConfig;

    if (container) {

      const { clientX, clientY } = evt;

      if (this.tween) {
        TWEEN.remove(this.tween);
        this.tween = null;
      }

      container.addEventListener('mousemove', this._mouseMoveListener, false);
      container.addEventListener('mouseup', this._mouseUpListener, false);
      container.addEventListener('mouseout', this._mouseOutListener, false);

      cfg.mouseXOnMouseDown = clientX - cfg.clientHalfX;
      cfg.targetRotationOnMouseDownX = cfg.targetRotationX;

      cfg.mouseYOnMouseDown = clientY - cfg.clientHalfY;
      cfg.targetRotationOnMouseDownY = cfg.targetRotationY;
    }

  }

  _onMouseMove(evt) {

    if (evt) {

      const cfg = this.controlsConfig;

      cfg.mouseX = evt.clientX - cfg.clientHalfX;
      cfg.mouseY = evt.clientY - cfg.clientHalfY;

      cfg.targetRotationY = cfg.targetRotationOnMouseDownY + (cfg.mouseY - cfg.mouseYOnMouseDown) * 0.02;
      cfg.targetRotationX = cfg.targetRotationOnMouseDownX + (cfg.mouseX - cfg.mouseXOnMouseDown) * 0.02;
    }
  }


  _onMouseUp() {

    const {container} = this;

    if (container) {
      container.removeEventListener( 'mousemove', this._mouseMoveListener, false );
      container.removeEventListener( 'mouseup', this._mouseUpListener, false );
      container.removeEventListener( 'mouseout', this._mouseOutListener, false );
    }
  }

  _onMouseOut() {

    const {container} = this;

    if (container) {
      container.removeEventListener('mousemove', this._mouseMoveListener, false);
      container.removeEventListener('mouseup', this._mouseUpListener, false);
      container.removeEventListener('mouseout', this._mouseOutListener, false);
    }
  }

  _onTouchStart(evt = {touches: [] }) {

    const touches = evt.touches;
    const cfg = this.controlsConfig;

    if (touches.length === 1) {

      evt.preventDefault();
      const {pageX, pageY} = touches[0];

      cfg.mouseXOnMouseDown = pageX - cfg.clientHalfX;
      cfg.targetRotationOnMouseDownX = cfg.targetRotationX;

      cfg.mouseYOnMouseDown = pageY - cfg.clientHalfY;
      cfg.targetRotationOnMouseDownY = cfg.targetRotationY;
    }
  }

  _onTouchEnd(evt = {touches: [] }) {

    const touches = evt.touches;
    const cfg = this.controlsConfig;

    if (touches.length === 1) {

      evt.preventDefault();

      const {pageX, pageY} = touches[0];

      cfg.mouseX = pageX - cfg.clientHalfX;
      cfg.targetRotationX = cfg.targetRotationOnMouseDownX + ( cfg.mouseX - cfg.mouseXOnMouseDown ) * 0.05;

      cfg.mouseY = pageY - cfg.clientHalfY;
      cfg.targetRotationY = cfg.targetRotationOnMouseDownY + (cfg.mouseY - cfg.mouseYOnMouseDown) * 0.05;

    }
  }

  _onTouchMove(evt = {touches: [] }) {

    const touches = evt.touches;
    const cfg = this.controlsConfig;

    if (touches.length === 1 ) {

      evt.preventDefault();
      const {pageX, pageY} = touches[0];

      cfg.mouseX = pageX - cfg.clientHalfX;
      cfg.targetRotationX = cfg.targetRotationOnMouseDownX + ( cfg.mouseX - cfg.mouseXOnMouseDown ) * 0.05;

      cfg.mouseY = pageY - cfg.clientHalfY;
      cfg.targetRotationY = cfg.targetRotationOnMouseDownY + (cfg.mouseY - cfg.mouseYOnMouseDown) * 0.05;
    }
  }
}

{'fraction_non_alphanumeric': 0.12551681579759333, 'fraction_numerical': 0.007898796667695156, 'mean_word_length': 2.980840088430361, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2631904', 'n_tokens_mistral': 5240, 'n_tokens_neox': 4957, 'n_words': 930}