123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- import Point from '../Geometry/Point.js'
- import Line from '../Geometry/Line.js'
- import ElementEvents from '../enum/ElementEvents.js'
- import { listenLayer } from '../ListenLayer'
- import Constant from '../Constant'
- import { floorplanService } from './FloorplanService'
- import { mathUtil } from '../MathUtil.js'
- import { wallService } from './WallService.js'
- export class ElementService {
- constructor() {
- this.startAddWall = null
- this.newWall = null
- this.checkLines = {
- X: null,
- Y: null,
- }
- this.vCheckLines = {
- X: null,
- Y: null,
- }
- this.init()
- }
- init() {
- this.startAddWall = new Point(0, 0)
- this.startAddWall.name = ElementEvents.StartAddWall
- this.newWall = new Line({ x: 0, y: 0 }, { x: 1, y: 1 })
- this.newWall.name = ElementEvents.NewWall
- this.checkLines.X = new Line({ x: 0, y: 0 }, { x: 1, y: 1 })
- this.checkLines.X.name = ElementEvents.CheckLinesX
- this.checkLines.Y = new Line({ x: 0, y: 0 }, { x: 1, y: 1 })
- this.checkLines.Y.name = ElementEvents.CheckLinesY
- this.vCheckLines.X = new Line({ x: 0, y: 0 }, { x: 1, y: 1 })
- this.vCheckLines.X.name = ElementEvents.VCheckLinesX
- this.vCheckLines.Y = new Line({ x: 0, y: 0 }, { x: 1, y: 1 })
- this.vCheckLines.Y.name = ElementEvents.VCheckLinesY
- }
- showStartAddWall() {
- this.startAddWall.display = true
- }
- hideStartAddWall() {
- this.startAddWall.display = false
- }
- setStartAddWall(position) {
- this.startAddWall.setPosition(position)
- }
- showNewWall() {
- this.newWall.display = true
- }
- hideNewWall() {
- this.newWall.display = false
- }
- setNewWall(point1, point2) {
- this.newWall.setPositions(point1, point2)
- }
- setNewWallStartPosition(startPosition) {
- this.newWall.start.x = startPosition.x
- this.newWall.start.y = startPosition.y
- }
- setNewWallEndPosition(endPosition) {
- this.newWall.end.x = endPosition.x
- this.newWall.end.y = endPosition.y
- }
- setNewWallState(state) {
- this.newWall.state = state
- }
- showCheckLinesX() {
- this.checkLines.X.display = true
- }
- hideCheckLinesX() {
- this.checkLines.X.display = false
- }
- setCheckLinesX(point1, point2) {
- this.checkLines.X.setPositions(point1, point2)
- }
- showCheckLinesY() {
- this.checkLines.Y.display = true
- }
- hideCheckLinesY() {
- this.checkLines.Y.display = false
- }
- setCheckLinesY(point1, point2) {
- this.checkLines.Y.setPositions(point1, point2)
- }
- showVCheckLinesX() {
- this.vCheckLines.X.display = true
- }
- hideVCheckLinesX() {
- this.vCheckLines.X.display = false
- }
- setVCheckLinesX(point1, point2) {
- this.vCheckLines.X.setPositions(point1, point2)
- }
- showVCheckLinesY() {
- this.vCheckLines.Y.display = true
- }
- hideVCheckLinesY() {
- this.vCheckLines.Y.display = false
- }
- setVCheckLinesY(point1, point2) {
- this.vCheckLines.Y.setPositions(point1, point2)
- }
- hideAll() {
- this.hideCheckLinesX()
- this.hideCheckLinesY()
- this.hideStartAddWall()
- this.hideNewWall()
- this.hideVCheckLinesX()
- this.hideVCheckLinesY()
- }
- execute(startPosition, position) {
- this.hideVCheckLinesX()
- this.hideVCheckLinesY()
- this.hideCheckLinesX()
- this.hideCheckLinesY()
- if (listenLayer.modifyPoint) {
- if (listenLayer.modifyPoint.linkedPointIdX) {
- const linkedPointX = floorplanService.getPoint(listenLayer.modifyPoint.linkedPointIdX)
- this.setCheckLinesX(linkedPointX, position)
- this.showCheckLinesX()
- }
- if (listenLayer.modifyPoint.linkedPointIdY) {
- const linkedPointY = floorplanService.getPoint(listenLayer.modifyPoint.linkedPointIdY)
- this.setCheckLinesY(linkedPointY, position)
- this.showCheckLinesY()
- }
- }
- //垂直校验
- if (startPosition) {
- if (Math.abs(position.x - startPosition.x) < Constant.minAdsorb) {
- position.x = startPosition.x
- this.setVCheckLinesX(startPosition, position)
- this.showVCheckLinesX()
- }
- if (Math.abs(position.y - startPosition.y) < Constant.minAdsorb) {
- position.y = startPosition.y
- this.setVCheckLinesY(startPosition, position)
- this.showVCheckLinesY()
- }
- if (mathUtil.equalPoint(position, startPosition)) {
- this.hideVCheckLinesX()
- this.hideVCheckLinesY()
- }
- }
- }
- //pointId是角度的顶点
- //exceptPointId表示position对应的pointId(如果有的话)
- checkAngle(position, pointId, exceptPointId) {
- //type:1表示90°,2表示180°
- function ajust(position, point1, point2, type) {
- let line = mathUtil.createLine1(point1, point2)
- let join = null
- if (type == 1) {
- let vLine = mathUtil.getVerticalLine(line, point1)
- join = mathUtil.getJoinLinePoint(position, vLine)
- } else if (type == 2) {
- join = mathUtil.getJoinLinePoint(position, line)
- }
- return join
- }
- let points = wallService.getNeighPoints(pointId, exceptPointId)
- let point = floorplanService.getPoint(pointId)
- let newPosition = null
- for (let i = 0; i < points.length; ++i) {
- let angle = mathUtil.Angle(point, position, points[i])
- if (Math.abs((angle / Math.PI) * 180 - 90) < Constant.minAngle/2) {
- newPosition = ajust(position, point, points[i], 1)
- } else if (Math.abs((angle / Math.PI) * 180) < Constant.minAngle/2 || Math.abs((angle / Math.PI) * 180 - 180) < Constant.minAngle/2) {
- newPosition = ajust(position, point, points[i], 2)
- }
- if (newPosition != null) {
- return newPosition
- }
- }
- return newPosition
- }
- clear(){
- this.startAddWall = null
- this.newWall = null
- this.checkLines = {
- X: null,
- Y: null,
- }
- this.vCheckLines = {
- X: null,
- Y: null,
- }
- this.init()
- }
- }
- const elementService = new ElementService()
- export { elementService }
|