status.js 2.3 KB
Newer Older
1 2 3 4 5
import * as THREE from 'three';
import STORE from 'store';
import {
  drawSegmentsFromPoints, drawCircle, drawArrow, disposeMeshGroup,
} from 'utils/draw';
K
kiboliu 已提交
6

7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
function drawPullOverBox({
  lengthFront, lengthBack, widthLeft, widthRight,
}) {
  const pullOverStatus = new THREE.Group();
  const color = 0x006AFF;
  const polygon = drawSegmentsFromPoints(
    [
      new THREE.Vector3(lengthFront, -widthLeft, 0),
      new THREE.Vector3(lengthFront, widthRight, 0),
      new THREE.Vector3(-lengthBack, widthRight, 0),
      new THREE.Vector3(-lengthBack, -widthLeft, 0),
      new THREE.Vector3(lengthFront, -widthLeft, 0),
    ],
    color,
    2,
    5,
  );
  pullOverStatus.add(polygon);
K
kiboliu 已提交
25

26 27 28 29 30 31 32
  const material = new THREE.MeshBasicMaterial({
    color,
    transparent: false,
    opacity: 0.5,
  });
  const circle = drawCircle(0.2, material);
  pullOverStatus.add(circle);
K
kiboliu 已提交
33

34 35 36
  const heading = drawArrow(1.5, 2, 0.5, 0.5, color);
  heading.rotation.set(0, 0, -Math.PI / 2);
  pullOverStatus.add(heading);
K
kiboliu 已提交
37

38
  return pullOverStatus;
K
kiboliu 已提交
39 40 41
}

export default class PlanningStatus {
42 43 44 45
  constructor() {
    this.pullOverBox = null;
    this.dimension = {};
  }
K
kiboliu 已提交
46

47 48 49 50 51 52 53 54 55
  update(planningData, coordinates, scene) {
    const shouldDrawStatus = STORE.options.customizedToggles.get('pullOver');
    const pullOver = _.get(planningData, 'pullOver');
    if (!pullOver || !shouldDrawStatus) {
      if (this.pullOverBox) {
        this.pullOverBox.visible = false;
      }
      return;
    }
56

57 58 59 60 61 62 63 64 65 66
    // Dispose old status if dimension is different
    const isNewDimension = pullOver.lengthFront !== this.dimension.lengthFront
            || pullOver.lengthBack !== this.dimension.lengthBack
            || pullOver.widthLeft !== this.dimension.widthLeft
            || pullOver.widthRight !== this.dimension.widthRight;
    if (this.pullOverBox && isNewDimension) {
      disposeMeshGroup(this.pullOverBox);
      scene.remove(this.pullOverBox);
      this.pullOverBox = null;
    }
67

68 69 70 71
    // Draw pull over status
    if (!this.pullOverBox) {
      this.pullOverBox = drawPullOverBox(pullOver);
      scene.add(this.pullOverBox);
K
kiboliu 已提交
72
    }
73 74 75 76 77 78 79 80 81 82 83

    // Set position and theta
    const position = coordinates.applyOffset({
      x: pullOver.position.x,
      y: pullOver.position.y,
      z: 0.3,
    });
    this.pullOverBox.position.set(position.x, position.y, position.z);
    this.pullOverBox.rotation.set(0, 0, pullOver.theta);
  }
}