Home Reference Source Repository

src/app/components/intersection-component/drawers/intersection-drawer.js

/*
 * iGeom by LInE
 * Provides intersetion point to Geometric Objects (GO)
 * www.matematica.br/igeom
 * ./app/components/intersection-component/drawers/intersection-drawer.js
 * @calledby 
 * @version 2020/11/02: Implemented Line instersection.
 */

import { PointDrawer } from "../../point-component/drawers/point-drawer";
import { ELEMENTS_CLASS } from "../../../core/enums/elements-class-enum";
import { Drawer } from "../../../core/drawers/drawer";
import { app as App } from "../../../app";
import { objects } from "../../../core/application/objects";
import { DrawerAggregator } from "../../../core/drawers/drawer-aggregator";
import { intersectionService } from "../services/intersection-service";

export class IntersectionDrawer extends Drawer {

  static FIRST_OBJECT_STATE() {
    return "FIRST_OBJECT";
  }

  static SECOND_OBJECT_STATE() {
    return "SECOND_OBJECTf";
  }

  constructor() {
    super();
    this.aggregatorA;
    this.aggregatorB;
    this.label;
    this.states = [IntersectionDrawer.FIRST_OBJECT_STATE, IntersectionDrawer.SECOND_OBJECT_STATE];
    this.intersections = [];
    this.pointDrawer = new PointDrawer();
    super.setElementClass(ELEMENTS_CLASS.INTERSECTION_POINT);
  }

  draw(e) {
    if (e != undefined && e.attrs != undefined) {
      this.aggregatorA = this.getObjectAggregatorByGenericObject(e.attrs.genericObject.r);
      this.aggregatorB = this.getObjectAggregatorByGenericObject(e.attrs.genericObject.s);
      this.drawByIntersectionPoints([e.attrs.genericObject]);
      return;
    }
    if (e == undefined || !this.isValidObject(e.target)) return;

    const selectedTool = App.getSelectedTool();
    if (selectedTool != undefined &&
      selectedTool.drawer != undefined &&
      selectedTool.drawer.elementClass == ELEMENTS_CLASS.INTERSECTION_POINT
    ) {
      if (this.state == undefined || this.state == IntersectionDrawer.FIRST_OBJECT_STATE) {
        this.setFirstObject(e.target);
        this.setState(IntersectionDrawer.SECOND_OBJECT_STATE);
      }
      else if (this.state == IntersectionDrawer.SECOND_OBJECT_STATE) {
        this.setSecondObject(e.target);
        this.drawPoint();
        this.reset();
        // this.clear();
      }
    }
  }

  setFirstObject(konvaObject) {
    const aggregator = this.getObjectAggregator(konvaObject);
    this.aggregatorA = aggregator;
  }

  setSecondObject(konvaObject) {
    const aggregator = this.getObjectAggregator(konvaObject);
    this.aggregatorB = aggregator;
  }

  getObjectAggregator(konvaObject) {
    return objects.getByKonvaObject(konvaObject)[0];
  }

  getObjectAggregatorByGenericObject(genericObject) {
    return objects.getByGenericObject(genericObject)[0];
  }

  drawPoint() {
    const intersectionPoints = intersectionService.addIntersections(
      this.aggregatorA.genericObject.getIntersection(this.aggregatorB.genericObject));
    this.drawByIntersectionPoints(intersectionPoints);
  }

  drawByIntersectionPoints(intersectionPoints) {
    for (let index = 0; index < intersectionPoints.length; index++) {
      const intersectionPoint = intersectionPoints[index];
      intersectionPoint.update();
      // if (!intersectionPoint.visible) return;
      const point = PointDrawer.drawPoint(intersectionPoint, true, false, true);
      const aggregator = new DrawerAggregator(this, intersectionPoint, point.konvaObject, ELEMENTS_CLASS.INTERSECTION_POINT);
      super.addAggregator(aggregator);
      this.aggregatorB.addAggregator(aggregator);
      this.aggregatorA.addAggregator(aggregator);
    }
  }

  isValidObject(konvaObject) {
    switch (konvaObject.attrs.class) {
      case ELEMENTS_CLASS.LINE:
        return true;
      case ELEMENTS_CLASS.LINE_SEGMENT:
        return true;
      case ELEMENTS_CLASS.CIRCUMFERENCE:
        return true;
      default:
        return false;
    }
  }

  update(aggregator, e) {
    aggregator.genericObject.update(aggregator, e);

    if (!aggregator.genericObject.visible || aggregator.genericObject.posX == Number.MAX_SAFE_INTEGER) {
      if (aggregator.visible) {

        aggregator.konvaObject.hide();
        aggregator.aggregators.forEach(a => {
          a.visible = false;
          a.konvaObject.hide();
        });
        this.batchDraw();
      }
      aggregator.visible = false;
      return;
    }
    //todo: konva objects
    aggregator.konvaObject.children[0].x(aggregator.genericObject.posX + 10);
    aggregator.konvaObject.children[0].y(aggregator.genericObject.posY - 10);

    aggregator.konvaObject.children[1].x(aggregator.genericObject.posX);
    aggregator.konvaObject.children[1].y(aggregator.genericObject.posY);

    if (!aggregator.visible || aggregator.genericObject.posX == Number.MAX_SAFE_INTEGER) {
      aggregator.genericObject.visible = true;
      aggregator.konvaObject.show();
      aggregator.aggregators.forEach(a => {
        a.visible = true;
        a.konvaObject.show();
      });
      aggregator.visible = true;
    }
    this.batchDraw();
  }

  reset() {
    this.aggregatorA = undefined;
    this.aggregatorB = undefined;
    this.setState(undefined);
  }

}