ElementService.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. import Point from '../Geometry/Point.js'
  2. import Line from '../Geometry/Line.js'
  3. import ElementEvents from '../enum/ElementEvents.js'
  4. import { listenLayer } from '../ListenLayer'
  5. import Constant from '../Constant'
  6. import { floorplanService } from './FloorplanService'
  7. import { mathUtil } from '../MathUtil.js'
  8. import { wallService } from './WallService.js'
  9. export class ElementService {
  10. constructor() {
  11. this.startAddWall = null
  12. this.newWall = null
  13. this.checkLines = {
  14. X: null,
  15. Y: null,
  16. }
  17. this.vCheckLines = {
  18. X: null,
  19. Y: null,
  20. }
  21. this.init()
  22. }
  23. init() {
  24. this.startAddWall = new Point(0, 0)
  25. this.startAddWall.name = ElementEvents.StartAddWall
  26. this.newWall = new Line({ x: 0, y: 0 }, { x: 1, y: 1 })
  27. this.newWall.name = ElementEvents.NewWall
  28. this.checkLines.X = new Line({ x: 0, y: 0 }, { x: 1, y: 1 })
  29. this.checkLines.X.name = ElementEvents.CheckLinesX
  30. this.checkLines.Y = new Line({ x: 0, y: 0 }, { x: 1, y: 1 })
  31. this.checkLines.Y.name = ElementEvents.CheckLinesY
  32. this.vCheckLines.X = new Line({ x: 0, y: 0 }, { x: 1, y: 1 })
  33. this.vCheckLines.X.name = ElementEvents.VCheckLinesX
  34. this.vCheckLines.Y = new Line({ x: 0, y: 0 }, { x: 1, y: 1 })
  35. this.vCheckLines.Y.name = ElementEvents.VCheckLinesY
  36. }
  37. showStartAddWall() {
  38. this.startAddWall.display = true
  39. }
  40. hideStartAddWall() {
  41. this.startAddWall.display = false
  42. }
  43. setStartAddWall(position) {
  44. this.startAddWall.setPosition(position)
  45. }
  46. showNewWall() {
  47. this.newWall.display = true
  48. }
  49. hideNewWall() {
  50. this.newWall.display = false
  51. }
  52. setNewWall(point1, point2) {
  53. this.newWall.setPositions(point1, point2)
  54. }
  55. setNewWallStartPosition(startPosition) {
  56. this.newWall.start.x = startPosition.x
  57. this.newWall.start.y = startPosition.y
  58. }
  59. setNewWallEndPosition(endPosition) {
  60. this.newWall.end.x = endPosition.x
  61. this.newWall.end.y = endPosition.y
  62. }
  63. setNewWallState(state) {
  64. this.newWall.state = state
  65. }
  66. showCheckLinesX() {
  67. this.checkLines.X.display = true
  68. }
  69. hideCheckLinesX() {
  70. this.checkLines.X.display = false
  71. }
  72. setCheckLinesX(point1, point2) {
  73. this.checkLines.X.setPositions(point1, point2)
  74. }
  75. showCheckLinesY() {
  76. this.checkLines.Y.display = true
  77. }
  78. hideCheckLinesY() {
  79. this.checkLines.Y.display = false
  80. }
  81. setCheckLinesY(point1, point2) {
  82. this.checkLines.Y.setPositions(point1, point2)
  83. }
  84. showVCheckLinesX() {
  85. this.vCheckLines.X.display = true
  86. }
  87. hideVCheckLinesX() {
  88. this.vCheckLines.X.display = false
  89. }
  90. setVCheckLinesX(point1, point2) {
  91. this.vCheckLines.X.setPositions(point1, point2)
  92. }
  93. showVCheckLinesY() {
  94. this.vCheckLines.Y.display = true
  95. }
  96. hideVCheckLinesY() {
  97. this.vCheckLines.Y.display = false
  98. }
  99. setVCheckLinesY(point1, point2) {
  100. this.vCheckLines.Y.setPositions(point1, point2)
  101. }
  102. hideAll() {
  103. this.hideCheckLinesX()
  104. this.hideCheckLinesY()
  105. this.hideStartAddWall()
  106. this.hideNewWall()
  107. this.hideVCheckLinesX()
  108. this.hideVCheckLinesY()
  109. }
  110. execute(startPosition, position) {
  111. this.hideVCheckLinesX()
  112. this.hideVCheckLinesY()
  113. this.hideCheckLinesX()
  114. this.hideCheckLinesY()
  115. if (listenLayer.modifyPoint) {
  116. if (listenLayer.modifyPoint.linkedPointIdX) {
  117. const linkedPointX = floorplanService.getPoint(listenLayer.modifyPoint.linkedPointIdX)
  118. this.setCheckLinesX(linkedPointX, position)
  119. this.showCheckLinesX()
  120. }
  121. if (listenLayer.modifyPoint.linkedPointIdY) {
  122. const linkedPointY = floorplanService.getPoint(listenLayer.modifyPoint.linkedPointIdY)
  123. this.setCheckLinesY(linkedPointY, position)
  124. this.showCheckLinesY()
  125. }
  126. }
  127. //垂直校验
  128. if (startPosition) {
  129. if (Math.abs(position.x - startPosition.x) < Constant.minAdsorb) {
  130. position.x = startPosition.x
  131. this.setVCheckLinesX(startPosition, position)
  132. this.showVCheckLinesX()
  133. }
  134. if (Math.abs(position.y - startPosition.y) < Constant.minAdsorb) {
  135. position.y = startPosition.y
  136. this.setVCheckLinesY(startPosition, position)
  137. this.showVCheckLinesY()
  138. }
  139. if (mathUtil.equalPoint(position, startPosition)) {
  140. this.hideVCheckLinesX()
  141. this.hideVCheckLinesY()
  142. }
  143. }
  144. }
  145. //pointId是角度的顶点
  146. //exceptPointId表示position对应的pointId(如果有的话)
  147. checkAngle(position, pointId, exceptPointId) {
  148. //type:1表示90°,2表示180°
  149. function ajust(position, point1, point2, type) {
  150. let line = mathUtil.createLine1(point1, point2)
  151. let join = null
  152. if (type == 1) {
  153. let vLine = mathUtil.getVerticalLine(line, point1)
  154. join = mathUtil.getJoinLinePoint(position, vLine)
  155. } else if (type == 2) {
  156. join = mathUtil.getJoinLinePoint(position, line)
  157. }
  158. return join
  159. }
  160. let points = wallService.getNeighPoints(pointId, exceptPointId)
  161. let point = floorplanService.getPoint(pointId)
  162. let newPosition = null
  163. for (let i = 0; i < points.length; ++i) {
  164. let angle = mathUtil.Angle(point, position, points[i])
  165. if (Math.abs((angle / Math.PI) * 180 - 90) < Constant.minAngle/2) {
  166. newPosition = ajust(position, point, points[i], 1)
  167. } else if (Math.abs((angle / Math.PI) * 180) < Constant.minAngle/2 || Math.abs((angle / Math.PI) * 180 - 180) < Constant.minAngle/2) {
  168. newPosition = ajust(position, point, points[i], 2)
  169. }
  170. if (newPosition != null) {
  171. return newPosition
  172. }
  173. }
  174. return newPosition
  175. }
  176. clear(){
  177. this.startAddWall = null
  178. this.newWall = null
  179. this.checkLines = {
  180. X: null,
  181. Y: null,
  182. }
  183. this.vCheckLines = {
  184. X: null,
  185. Y: null,
  186. }
  187. this.init()
  188. }
  189. }
  190. const elementService = new ElementService()
  191. export { elementService }