Layer.js 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. import Load from './Load'
  2. import { stateService } from './Service/StateService'
  3. import { elementService } from './Service/ElementService'
  4. import { floorplanService } from './Service/FloorplanService'
  5. import { tagService } from './Service/TagService'
  6. import { tableService } from './Service/TableService'
  7. import { historyService } from './Service/HistoryService'
  8. import UIControl from './Controls/UIControl'
  9. import { moveTag } from './Controls/MoveTag'
  10. import { moveTable } from './Controls/MoveTable'
  11. import { addWall } from './Controls/AddWall'
  12. import { moveWall } from './Controls/MoveWall'
  13. import { addRectangle } from './Controls/AddRectangle'
  14. import { moveRectangle } from './Controls/MoveRectangle'
  15. import { addCircle } from './Controls/AddCircle'
  16. import { moveCircle } from './Controls/MoveCircle'
  17. import { addIcon } from './Controls/AddIcon'
  18. import { moveIcon } from './Controls/MoveIcon'
  19. import { addArrow } from './Controls/AddArrow'
  20. import { moveArrow } from './Controls/MoveArrow'
  21. import { coordinate } from './Coordinate'
  22. import Render from './Renderer/Render'
  23. import { draw } from './Renderer/Draw'
  24. import { listenLayer } from './ListenLayer'
  25. import { floorplanData } from './FloorplanData'
  26. import LayerEvents from './enum/LayerEvents.js'
  27. import UIEvents from './enum/UIEvents.js'
  28. import SelectState from './enum/SelectState.js'
  29. import Constant from './Constant'
  30. import VectorType from './enum/VectorType'
  31. import MathUtil, { mathUtil } from './MathUtil'
  32. import { wallService } from './Service/WallService'
  33. import History from './History/History'
  34. import { signService } from './Service/SignService'
  35. export default class Layer {
  36. constructor() {
  37. //super()
  38. this.load = new Load(this)
  39. this.uiControl = new UIControl(this)
  40. this.renderer = new Render(this)
  41. this.history = new History(this)
  42. this.canvas = null;
  43. this.startX = null
  44. this.startY = null
  45. }
  46. //开始
  47. start(canvas,vectorData) {
  48. coordinate.init(canvas)
  49. this.canvas = canvas;
  50. this.load.load(vectorData);
  51. draw.initContext(this.canvas)
  52. this.bindEvents()
  53. }
  54. bindEvents() {
  55. this.canvas.addEventListener('contextmenu', function (e) {
  56. e.preventDefault()
  57. })
  58. this.canvas.addEventListener('mousedown', this.onMouseDown.bind(this))
  59. this.canvas.addEventListener('mousemove', this.onMouseMove.bind(this))
  60. this.canvas.addEventListener('mouseup', this.onMouseUp.bind(this))
  61. this.canvas.addEventListener('mousewheel', this.onWheel.bind(this))
  62. this.canvas.addEventListener('DOMMouseScroll', this.onWheel.bind(this))
  63. }
  64. onMouseDown(e) {
  65. this.startX = e.offsetX || e.layerX
  66. this.startY = e.offsetY || e.layerY
  67. this.lastX = e.offsetX || e.layerX
  68. this.lastY = e.offsetY || e.layerY
  69. // 右键
  70. if (e.button == 2) {
  71. this.stopAddVector()
  72. this.renderer.autoRedraw()
  73. return
  74. }
  75. const eventName = stateService.getEventName()
  76. //点击第一次
  77. if (eventName == LayerEvents.AddWall) {
  78. let flag = addWall.setNewWallPoint('start', {
  79. x: this.startX,
  80. y: this.startY,
  81. })
  82. if (!flag) {
  83. return
  84. }
  85. }
  86. //点击第二次
  87. else if (eventName == LayerEvents.AddingWall) {
  88. let flag = addWall.setNewWallPoint('end', {
  89. x: this.startX,
  90. y: this.startY,
  91. })
  92. if (!flag) {
  93. return
  94. }
  95. if (addWall.canAdd) {
  96. addWall.buildWall()
  97. this.history.save()
  98. } else {
  99. return
  100. }
  101. }
  102. else if (eventName == LayerEvents.AddRectangle) {
  103. addRectangle.execute({
  104. x: this.startX,
  105. y: this.startY,
  106. })
  107. }
  108. else if (eventName == LayerEvents.AddCircle) {
  109. addCircle.execute({
  110. x: this.startX,
  111. y: this.startY,
  112. })
  113. }
  114. else if (eventName == LayerEvents.AddIcon) {
  115. addIcon.execute({
  116. x: this.startX,
  117. y: this.startY,
  118. })
  119. }
  120. else if (eventName == LayerEvents.AddArrow) {
  121. addArrow.execute({
  122. x: this.startX,
  123. y: this.startY,
  124. })
  125. }
  126. else {
  127. const selectItem = stateService.getSelectItem()
  128. if (eventName == null && selectItem) {
  129. stateService.setDraggingItem(selectItem)
  130. this.uiControl.selectUI = selectItem.type
  131. } else if (eventName == null) {
  132. this.uiControl.clearUI();
  133. }
  134. }
  135. this.setEventName('mouseDown')
  136. // 清除上一个状态
  137. // 设置当前事件名称
  138. e.preventDefault()
  139. e.stopPropagation()
  140. }
  141. onMouseMove(e) {
  142. const X = e.offsetX || e.layerX
  143. const Y = e.offsetY || e.layerY
  144. if(this.lastX == null){
  145. this.lastX = X;
  146. }
  147. if(this.lastY == null){
  148. this.lastY = Y;
  149. }
  150. let dx = X - this.lastX
  151. let dy = Y - this.lastY
  152. let position = coordinate.getXYFromScreen({
  153. x: X,
  154. y: Y,
  155. })
  156. const eventName = stateService.getEventName()
  157. // 是否需要重绘
  158. let needAutoRedraw = false
  159. const draggingItem = stateService.getDraggingItem()
  160. switch (eventName) {
  161. case null:
  162. //监控当前选择的构件
  163. needAutoRedraw = listenLayer.start(position)
  164. break
  165. case LayerEvents.PanBackGround:
  166. stateService.clearItems()
  167. coordinate.center.x = coordinate.center.x - (dx * Constant.defaultZoom) / coordinate.zoom / coordinate.res
  168. coordinate.center.y = coordinate.center.y + (dy * Constant.defaultZoom) / coordinate.zoom / coordinate.res
  169. this.lastX = X
  170. this.lastY = Y
  171. needAutoRedraw = true
  172. break
  173. case LayerEvents.AddWall:
  174. stateService.clearDraggingItem()
  175. stateService.clearFocusItem()
  176. needAutoRedraw = true
  177. listenLayer.start(position)
  178. if (listenLayer.modifyPoint) {
  179. position = {
  180. x: listenLayer.modifyPoint.x,
  181. y: listenLayer.modifyPoint.y,
  182. }
  183. }
  184. elementService.execute(null, position)
  185. elementService.setStartAddWall(position)
  186. elementService.showStartAddWall()
  187. break
  188. case LayerEvents.AddingWall:
  189. stateService.clearDraggingItem()
  190. stateService.clearFocusItem()
  191. needAutoRedraw = true
  192. let startPosition = {
  193. x: addWall.startInfo.position.x,
  194. y: addWall.startInfo.position.y,
  195. }
  196. listenLayer.start(position,null,null,startPosition)
  197. if (listenLayer.modifyPoint) {
  198. position = {
  199. x: listenLayer.modifyPoint.x,
  200. y: listenLayer.modifyPoint.y,
  201. }
  202. }
  203. elementService.execute(startPosition, position)
  204. elementService.setStartAddWall(position)
  205. if (!elementService.newWall.display) {
  206. elementService.setNewWall(startPosition, position)
  207. elementService.showNewWall() //画墙
  208. } else {
  209. if (!listenLayer.modifyPoint && addWall.startInfo.linkedPointId) {
  210. //角度很正
  211. let newEndPosition = elementService.checkAngle(position, addWall.startInfo.linkedPointId, null)
  212. if (newEndPosition) {
  213. mathUtil.clonePoint(position, newEndPosition)
  214. listenLayer.modifyPoint = {
  215. x:newEndPosition.x,
  216. y:newEndPosition.y
  217. }
  218. }
  219. }
  220. elementService.setNewWallEndPosition(position) //改变end位置
  221. }
  222. addWall.canAdd = addWall.canAddWallForEnd(position)
  223. if (!addWall.canAdd) {
  224. elementService.setNewWallState('error')
  225. }
  226. else {
  227. elementService.setNewWallState('normal')
  228. }
  229. break
  230. case LayerEvents.MoveWall:
  231. dx = (dx * Constant.defaultZoom) / coordinate.zoom
  232. dy = (dy * Constant.defaultZoom) / coordinate.zoom
  233. // 1表示可以继续移动,2表示不能移动(启动距离还不够),3表示wallId被删除了,4表示重新开始移动(需要达到一定距离才能启动),5表示不能移动(不合适)
  234. let moveFlag = moveWall.moveWallPlane(draggingItem.vectorId, dx, dy)
  235. // 启动的时候需要点距离,所以真正移动了才更新lastX和lastY
  236. if (moveFlag == 1) {
  237. this.lastX = X
  238. this.lastY = Y
  239. needAutoRedraw = true
  240. }
  241. // 需要继续保持移动,一般是距离不够启动
  242. else if (moveFlag == 2) {
  243. }
  244. // wallId被删除了
  245. else if (moveFlag == 3) {
  246. this.history.save()
  247. stateService.clearSelectItem()
  248. stateService.clearDraggingItem()
  249. stateService.clearEventName()
  250. listenLayer.clear()
  251. needAutoRedraw = true
  252. }
  253. // wallId有一端被吸附了,这时候需要重新启动
  254. else if (moveFlag == 4) {
  255. this.lastX = X
  256. this.lastY = Y
  257. this.startX = X
  258. this.startY = Y
  259. needAutoRedraw = true
  260. } else if (moveFlag == 5) {
  261. this.lastX = X
  262. this.lastY = Y
  263. }
  264. break
  265. case LayerEvents.MoveWallPoint:
  266. let point = floorplanService.getPoint(draggingItem.vectorId)
  267. listenLayer.start(position, draggingItem.vectorId, point.parent)
  268. if (listenLayer.modifyPoint) {
  269. position = {
  270. x: listenLayer.modifyPoint.x,
  271. y: listenLayer.modifyPoint.y,
  272. }
  273. }
  274. elementService.execute(null, position)
  275. let flag = moveWall.movePoint(draggingItem.vectorId, position, listenLayer.modifyPoint)
  276. if (!flag) {
  277. elementService.hideAll()
  278. }
  279. needAutoRedraw = true
  280. break
  281. case LayerEvents.AddRectangle:
  282. stateService.clearDraggingItem()
  283. stateService.clearFocusItem()
  284. needAutoRedraw = true
  285. elementService.setStartAddWall(position)
  286. elementService.showStartAddWall()
  287. break
  288. case LayerEvents.AddingRectangle:
  289. stateService.clearDraggingItem()
  290. stateService.clearFocusItem()
  291. needAutoRedraw = true
  292. elementService.setStartAddWall(position)
  293. elementService.showStartAddWall()
  294. addRectangle.execute(position)
  295. break
  296. case LayerEvents.MoveRectangle:
  297. needAutoRedraw = true
  298. if (draggingItem != null) {
  299. moveRectangle.moveFullRectangle(dx,dy, draggingItem.vectorId)
  300. }
  301. this.lastX = X
  302. this.lastY = Y
  303. break
  304. case LayerEvents.MoveRectangleVertex:
  305. needAutoRedraw = true
  306. if (draggingItem != null) {
  307. elementService.setStartAddWall(position)
  308. elementService.showStartAddWall()
  309. moveRectangle.moveRectangleVertex(position,draggingItem.vectorId,parseInt(draggingItem.selectIndex.substring(7)))
  310. }
  311. break
  312. case LayerEvents.MoveRectangleSide:
  313. needAutoRedraw = true
  314. if (draggingItem != null) {
  315. moveRectangle.moveRectangleSide(position,draggingItem.vectorId,parseInt(draggingItem.selectIndex.substring(5)))
  316. }
  317. break
  318. case LayerEvents.AddCircle:
  319. stateService.clearDraggingItem()
  320. stateService.clearFocusItem()
  321. needAutoRedraw = true
  322. elementService.setStartAddWall(position)
  323. elementService.showStartAddWall()
  324. break
  325. case LayerEvents.AddingCircle:
  326. stateService.clearDraggingItem()
  327. stateService.clearFocusItem()
  328. needAutoRedraw = true
  329. elementService.setStartAddWall(position)
  330. elementService.showStartAddWall()
  331. addCircle.execute(position)
  332. break
  333. case LayerEvents.MoveCircle:
  334. needAutoRedraw = true
  335. if (draggingItem != null) {
  336. moveCircle.moveFullCircle(dx,dy, draggingItem.vectorId)
  337. }
  338. this.lastX = X
  339. this.lastY = Y
  340. break
  341. case LayerEvents.MoveCircleVertex:
  342. needAutoRedraw = true
  343. if (draggingItem != null) {
  344. moveCircle.moveCircleVertex(position,draggingItem.vectorId,parseInt(draggingItem.selectIndex.substring(7)))
  345. }
  346. break
  347. case LayerEvents.AddIcon:
  348. stateService.clearDraggingItem()
  349. stateService.clearFocusItem()
  350. needAutoRedraw = true
  351. elementService.setStartAddWall(position)
  352. elementService.showStartAddWall()
  353. break
  354. case LayerEvents.AddingIcon:
  355. stateService.clearDraggingItem()
  356. stateService.clearFocusItem()
  357. needAutoRedraw = true
  358. elementService.setStartAddWall(position)
  359. elementService.showStartAddWall()
  360. addIcon.execute(position)
  361. break
  362. case LayerEvents.MoveIcon:
  363. needAutoRedraw = true
  364. if (draggingItem != null) {
  365. moveIcon.moveFullIcon(dx,dy, draggingItem.vectorId)
  366. }
  367. this.lastX = X
  368. this.lastY = Y
  369. break
  370. case LayerEvents.MoveIconVertex:
  371. needAutoRedraw = true
  372. if (draggingItem != null) {
  373. moveIcon.moveIconVertex(position,draggingItem.vectorId,parseInt(draggingItem.selectIndex.substring(7)))
  374. }
  375. break
  376. case LayerEvents.AddArrow:
  377. stateService.clearDraggingItem()
  378. stateService.clearFocusItem()
  379. needAutoRedraw = true
  380. elementService.setStartAddWall(position)
  381. elementService.showStartAddWall()
  382. break
  383. case LayerEvents.AddingArrow:
  384. stateService.clearDraggingItem()
  385. stateService.clearFocusItem()
  386. needAutoRedraw = true
  387. elementService.setStartAddWall(position)
  388. elementService.showStartAddWall()
  389. addArrow.execute(position)
  390. break
  391. case LayerEvents.MoveArrow:
  392. needAutoRedraw = true
  393. if (draggingItem != null) {
  394. moveArrow.moveFullArrow(dx,dy, draggingItem.vectorId)
  395. }
  396. this.lastX = X
  397. this.lastY = Y
  398. break
  399. case LayerEvents.moveArrowVertex:
  400. needAutoRedraw = true
  401. if (draggingItem != null) {
  402. moveArrow.moveArrowVertex(position,draggingItem.vectorId,draggingItem.selectIndex)
  403. }
  404. break
  405. case LayerEvents.AddTag:
  406. needAutoRedraw = true
  407. if (draggingItem == null) {
  408. const tag = tagService.createTag(position)
  409. if (tag.vectorId) {
  410. stateService.setSelectItem(tag.vectorId, VectorType.Tag, SelectState.All)
  411. stateService.setDraggingItem(stateService.selectItem)
  412. }
  413. } else {
  414. moveTag.moveFullTag(dx,dy, draggingItem.vectorId)
  415. this.lastX = X
  416. this.lastY = Y
  417. }
  418. break
  419. case LayerEvents.MoveTag:
  420. needAutoRedraw = true
  421. if (draggingItem != null) {
  422. moveTag.moveFullTag(dx,dy, draggingItem.vectorId)
  423. this.lastX = X
  424. this.lastY = Y
  425. }
  426. break
  427. case LayerEvents.AddTable:
  428. needAutoRedraw = true
  429. if (draggingItem == null) {
  430. const table = tableService.createTable(position)
  431. if (table.vectorId) {
  432. stateService.setSelectItem(table.vectorId, VectorType.Table, SelectState.All)
  433. stateService.setDraggingItem(stateService.selectItem)
  434. }
  435. } else {
  436. moveTable.moveFullTable(dx,dy, draggingItem.vectorId)
  437. this.lastX = X
  438. this.lastY = Y
  439. }
  440. break
  441. case LayerEvents.MoveTable:
  442. needAutoRedraw = true
  443. if (draggingItem != null) {
  444. moveTable.moveFullTable(dx,dy, draggingItem.vectorId)
  445. this.lastX = X
  446. this.lastY = Y
  447. }
  448. break
  449. case LayerEvents.AddSign:
  450. needAutoRedraw = true
  451. if (draggingItem == null) {
  452. const signType = this.uiControl.getSignTypeForUI()
  453. const sign = signService.createSign(position, signType)
  454. if (sign.vectorId) {
  455. stateService.setSelectItem(sign.vectorId, signType, SelectState.All)
  456. stateService.setDraggingItem(stateService.selectItem)
  457. }
  458. } else {
  459. const sign = floorplanService.getSign(draggingItem.vectorId)
  460. mathUtil.clonePoint(sign.center, position)
  461. }
  462. break
  463. case LayerEvents.MoveSign:
  464. needAutoRedraw = true
  465. const sign = floorplanService.getSign(draggingItem.vectorId)
  466. mathUtil.clonePoint(sign.center, position)
  467. break
  468. }
  469. if (needAutoRedraw) {
  470. this.renderer.autoRedraw()
  471. }
  472. }
  473. onMouseUp(e) {
  474. const X = e.offsetX || e.layerX
  475. const Y = e.offsetY || e.layerY
  476. let eventName = stateService.getEventName()
  477. const draggingItem = stateService.getDraggingItem()
  478. let focusItem = null
  479. if (draggingItem && draggingItem.vectorId) {
  480. if(mathUtil.getDistance({
  481. x:this.startX,
  482. y:this.startY
  483. }, {
  484. x:X,
  485. y:Y
  486. })<Constant.minMovePix){
  487. focusItem = {
  488. vectorId: draggingItem.vectorId,
  489. type: draggingItem.type,
  490. selectIndex:draggingItem.selectIndex,
  491. cursor: { x: this.lastX, y: this.lastY },
  492. }
  493. this.uiControl.showAttributes(focusItem)
  494. }
  495. else{
  496. focusItem = null;
  497. }
  498. stateService.setFocusItem(focusItem)
  499. }
  500. let position = coordinate.getXYFromScreen({
  501. x: X,
  502. y: Y,
  503. })
  504. let needAutoRedraw = false
  505. switch (eventName) {
  506. case null:
  507. return
  508. case LayerEvents.PanBackGround:
  509. needAutoRedraw = true
  510. stateService.clearFocusItem()
  511. this.uiControl.clearUI();
  512. break
  513. case LayerEvents.MoveWallPoint:
  514. if(focusItem == null){
  515. needAutoRedraw = true
  516. elementService.hideAll()
  517. let point = floorplanService.getPoint(draggingItem.vectorId)
  518. if (point) {
  519. listenLayer.start(point, draggingItem.vectorId, point.parent)
  520. if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty('linkedPointId')) {
  521. wallService.moveTo(draggingItem.vectorId, listenLayer.modifyPoint.linkedPointId)
  522. } else if (listenLayer.modifyPoint && (listenLayer.modifyPoint.linkedPointIdX || listenLayer.modifyPoint.linkedPointIdY)) {
  523. mathUtil.clonePoint(point, listenLayer.modifyPoint)
  524. } else if (listenLayer.modifyPoint && listenLayer.modifyPoint.hasOwnProperty('linkedWallId')) {
  525. point = wallService.createPoint(listenLayer.modifyPoint.x, listenLayer.modifyPoint.y)
  526. wallService.splitWall(listenLayer.modifyPoint.linkedWallId, point.vectorId, 'start')
  527. wallService.moveTo(draggingItem.vectorId, point.vectorId)
  528. } else if (moveWall.splitWallId != null) {
  529. wallService.splitWall(moveWall.splitWallId, draggingItem.vectorId, 'start')
  530. }
  531. //draggingItem.vectorId所在的墙面与其他墙角相交
  532. moveWall.updateForAbsorbWallPoints()
  533. this.history.save()
  534. }
  535. }
  536. else{
  537. this.uiControl.showAttributes(focusItem);
  538. }
  539. break
  540. case LayerEvents.AddingWall:
  541. needAutoRedraw = true
  542. if (addWall.startInfo && addWall.startInfo.linkedPointId) {
  543. let addWallStartPoint = floorplanService.getPoint(addWall.startInfo.linkedPointId)
  544. if (addWall.endInfo.position && Object.keys(addWallStartPoint.parent).length > 1) {
  545. stateService.clearEventName()
  546. addWall.clear()
  547. elementService.hideAll()
  548. this.uiControl.clearUI();
  549. this.history.save()
  550. }
  551. }
  552. break
  553. case LayerEvents.MoveWall:
  554. if(focusItem == null){
  555. needAutoRedraw = true
  556. this.history.save()
  557. }
  558. else{
  559. this.uiControl.showAttributes(focusItem);
  560. }
  561. break
  562. case LayerEvents.AddingRectangle:
  563. needAutoRedraw = true
  564. if(mathUtil.getDistance(addRectangle.start,addRectangle.end)<Constant.minAdsorb){
  565. floorplanService.deleteRectangle(addRectangle.currentVectorId)
  566. }
  567. stateService.clearEventName()
  568. elementService.hideAll()
  569. addRectangle.clear();
  570. this.history.save()
  571. this.uiControl.clearUI();
  572. break
  573. case LayerEvents.MoveRectangle:
  574. if(focusItem == null){
  575. needAutoRedraw = true
  576. this.history.save()
  577. }
  578. else{
  579. this.uiControl.showAttributes(focusItem);
  580. }
  581. break
  582. case LayerEvents.MoveRectangleVertex:
  583. needAutoRedraw = true
  584. elementService.hideAll()
  585. this.history.save()
  586. break
  587. case LayerEvents.MoveRectangleSide:
  588. needAutoRedraw = true
  589. this.history.save()
  590. break
  591. case LayerEvents.AddingCircle:
  592. if(addCircle.end != null && addCircle.currentVectorId != null)
  593. {
  594. if(mathUtil.getDistance(addCircle.start,addCircle.end)<Constant.minAdsorb){
  595. floorplanService.deleteCircle(addCircle.currentVectorId)
  596. }
  597. needAutoRedraw = true
  598. }
  599. stateService.clearEventName()
  600. elementService.hideAll()
  601. addCircle.clear();
  602. this.history.save()
  603. this.uiControl.clearUI();
  604. break
  605. case LayerEvents.MoveCircle:
  606. if(focusItem == null){
  607. needAutoRedraw = true
  608. this.history.save()
  609. }
  610. else{
  611. this.uiControl.showAttributes(focusItem);
  612. }
  613. break
  614. case LayerEvents.MoveCircleVertex:
  615. needAutoRedraw = true
  616. this.history.save()
  617. break
  618. case LayerEvents.AddingIcon:
  619. if(addIcon.end != null && addIcon.currentVectorId != null){
  620. if(mathUtil.getDistance(addIcon.start,addIcon.end)<Constant.minAdsorb){
  621. floorplanService.deleteIcon(addIcon.currentVectorId)
  622. }
  623. needAutoRedraw = true
  624. }
  625. stateService.clearEventName()
  626. elementService.hideAll()
  627. addIcon.clear();
  628. this.history.save()
  629. this.uiControl.clearUI();
  630. break
  631. case LayerEvents.MoveIcon:
  632. if(focusItem == null){
  633. needAutoRedraw = true
  634. this.history.save()
  635. }
  636. else{
  637. this.uiControl.showAttributes(focusItem);
  638. }
  639. break
  640. case LayerEvents.MoveIconVertex:
  641. needAutoRedraw = true
  642. this.history.save()
  643. break
  644. case LayerEvents.AddingArrow:
  645. if(addArrow.start != null && addArrow.currentVectorId != null){
  646. if(mathUtil.getDistance(addArrow.start,addArrow.end)<Constant.minAdsorb){
  647. floorplanService.deleteArrow(addArrow.currentVectorId)
  648. }
  649. needAutoRedraw = true
  650. }
  651. stateService.clearEventName()
  652. elementService.hideAll()
  653. addArrow.clear();
  654. this.history.save()
  655. this.uiControl.clearUI();
  656. break
  657. case LayerEvents.MoveArrow:
  658. needAutoRedraw = true
  659. this.history.save()
  660. break
  661. case LayerEvents.MoveTag:
  662. if(focusItem == null){
  663. needAutoRedraw = true
  664. this.history.save()
  665. }
  666. else{
  667. this.uiControl.showAttributes(focusItem);
  668. }
  669. break
  670. case LayerEvents.AddTag:
  671. needAutoRedraw = true
  672. this.uiControl.showAttributes(focusItem)
  673. this.history.save()
  674. break
  675. case LayerEvents.MoveTable:
  676. if(focusItem == null){
  677. needAutoRedraw = true
  678. this.history.save()
  679. }
  680. else{
  681. this.uiControl.showAttributes(focusItem);
  682. }
  683. break
  684. case LayerEvents.AddTable:
  685. needAutoRedraw = true
  686. this.uiControl.showAttributes(focusItem)
  687. this.history.save()
  688. break
  689. case LayerEvents.AddSign:
  690. needAutoRedraw = true
  691. this.uiControl.clearUI();
  692. this.history.save()
  693. break
  694. case LayerEvents.MoveSign:
  695. needAutoRedraw = true
  696. if (focusItem != null && signService.isSign(focusItem.type)) {
  697. this.uiControl.selectUI = focusItem.type
  698. this.history.save()
  699. } else {
  700. this.history.save()
  701. }
  702. break
  703. }
  704. this.lastX = null;
  705. this.lastY = null;
  706. this.setEventName('mouseUp')
  707. stateService.clearDraggingItem()
  708. this.renderer.autoRedraw()
  709. }
  710. onWheel(e) {
  711. e.preventDefault()
  712. const type = e.type
  713. if (type == 'DOMMouseScroll' || type == 'mousewheel') {
  714. // 当在canvas用滚轮滚动时
  715. const delta = e.wheelDelta ? (e.wheelDelta / 120) * 2 : (-(e.detail || 0) / 3) * 2
  716. const zoom = coordinate.zoom + delta
  717. if (zoom < 14) {
  718. return
  719. }
  720. coordinate.updateZoom(zoom)
  721. this.renderer.autoRedraw()
  722. }
  723. }
  724. onKeydown(e) {
  725. if (!this.display) {
  726. return
  727. }
  728. if (e.ctrlKey && e.code == 'KeyZ') {
  729. this.revokeHistory()
  730. console.log('ctrl+z')
  731. } else if (e.ctrlKey && e.code == 'KeyY') {
  732. this.recoveryHistory()
  733. console.log('ctrl+y')
  734. } else if (e.code == 'Delete') {
  735. this.deleteItem()
  736. this.uiControl.clearUI();
  737. this.history.save()
  738. this.renderer.autoRedraw()
  739. console.log('Delete')
  740. } else if (e.code == 'Escape') {
  741. this.stopAddVector()
  742. this.renderer.autoRedraw()
  743. console.log('Esc')
  744. }
  745. }
  746. setEventName(eventType) {
  747. let eventName = stateService.getEventName()
  748. if (eventType == 'mouseDown') {
  749. if (eventName == null) {
  750. const selectItem = stateService.getSelectItem()
  751. if (selectItem == null) {
  752. stateService.setEventName(LayerEvents.PanBackGround)
  753. } else if (selectItem.type == VectorType.Wall) {
  754. stateService.setEventName(LayerEvents.MoveWall)
  755. } else if (selectItem.type == VectorType.WallCorner) {
  756. stateService.setEventName(LayerEvents.MoveWallPoint)
  757. } else if (selectItem.type == VectorType.Tag) {
  758. stateService.setEventName(LayerEvents.MoveTag)
  759. } else if (signService.isSign(selectItem.type)) {
  760. stateService.setEventName(LayerEvents.MoveSign)
  761. } else if (selectItem.type == VectorType.Rectangle) {
  762. if(selectItem.selectIndex == SelectState.All){
  763. stateService.setEventName(LayerEvents.MoveRectangle)
  764. }
  765. else if(selectItem.selectIndex.indexOf(SelectState.Vertex)>-1){
  766. stateService.setEventName(LayerEvents.MoveRectangleVertex)
  767. }
  768. else if(selectItem.selectIndex.indexOf(SelectState.Side)>-1){
  769. stateService.setEventName(LayerEvents.MoveRectangleSide)
  770. }
  771. } else if (selectItem.type == VectorType.Circle) {
  772. if(selectItem.selectIndex == SelectState.All){
  773. stateService.setEventName(LayerEvents.MoveCircle)
  774. }
  775. else if(selectItem.selectIndex.indexOf(SelectState.Vertex)>-1){
  776. stateService.setEventName(LayerEvents.MoveCircleVertex)
  777. }
  778. } else if (selectItem.type == VectorType.Icon) {
  779. if(selectItem.selectIndex == SelectState.All){
  780. stateService.setEventName(LayerEvents.MoveIcon)
  781. }
  782. else if(selectItem.selectIndex.indexOf(SelectState.Vertex)>-1){
  783. stateService.setEventName(LayerEvents.MoveIconVertex)
  784. }
  785. } else if (selectItem.type == VectorType.Arrow) {
  786. if(selectItem.selectIndex == SelectState.All){
  787. stateService.setEventName(LayerEvents.MoveArrow)
  788. }
  789. else{
  790. stateService.setEventName(LayerEvents.moveArrowVertex)
  791. }
  792. } else if (selectItem.type == VectorType.Table) {
  793. stateService.setEventName(LayerEvents.MoveTable)
  794. }
  795. }
  796. else if (eventName == LayerEvents.AddWall) {
  797. stateService.setEventName(LayerEvents.AddingWall)
  798. }
  799. } else if (eventType == 'mouseUp') {
  800. if (eventName == LayerEvents.AddTag)
  801. {
  802. stateService.clearEventName()
  803. }
  804. else if (eventName == LayerEvents.AddRectangle)
  805. {
  806. stateService.setEventName(LayerEvents.AddingRectangle)
  807. }
  808. else if (eventName == LayerEvents.AddCircle)
  809. {
  810. stateService.setEventName(LayerEvents.AddingCircle)
  811. }
  812. else if (eventName == LayerEvents.AddIcon)
  813. {
  814. stateService.setEventName(LayerEvents.AddingIcon)
  815. }
  816. else if (eventName == LayerEvents.AddArrow)
  817. {
  818. stateService.setEventName(LayerEvents.AddingArrow)
  819. }
  820. else if (eventName == LayerEvents.AddTable)
  821. {
  822. stateService.clearEventName()
  823. }
  824. else if (eventName != LayerEvents.AddWall && eventName != LayerEvents.AddingWall ) { //&& eventName != LayerEvents.AddRectangle && eventName != LayerEvents.AddingRectangle && eventName != LayerEvents.AddCircle && eventName != LayerEvents.AddingCircle && eventName != LayerEvents.AddArrow && eventName != LayerEvents.AddingArrow && eventName != LayerEvents.AddIcon && eventName != LayerEvents.AddingIcon
  825. stateService.clearEventName()
  826. }
  827. }
  828. }
  829. exit() {
  830. stateService.clearItems()
  831. stateService.clearEventName()
  832. this.uiControl.clearUI()
  833. }
  834. stopAddVector() {
  835. let eventName = stateService.getEventName()
  836. if (eventName != LayerEvents.AddingWall) {
  837. stateService.clearEventName()
  838. const draggingItem = stateService.getDraggingItem()
  839. if (eventName == LayerEvents.AddTag) {
  840. if (draggingItem && draggingItem.vectorId) {
  841. tagService.deleteTag(draggingItem.vectorId)
  842. this.uiControl.clearUI();
  843. }
  844. }
  845. else if (eventName == LayerEvents.AddTable) {
  846. if (draggingItem && draggingItem.vectorId) {
  847. tableService.deleteTable(draggingItem.vectorId)
  848. this.uiControl.clearUI();
  849. }
  850. }
  851. else if (eventName == LayerEvents.AddSign) {
  852. if (draggingItem && draggingItem.vectorId) {
  853. floorplanService.deleteSign(draggingItem.vectorId)
  854. }
  855. }
  856. } else {
  857. stateService.setEventName(LayerEvents.AddWall)
  858. }
  859. this.uiControl.clearUI()
  860. elementService.hideAll()
  861. }
  862. deleteItem() {
  863. let item = stateService.getFocusItem()
  864. if (item) {
  865. if (item.type == VectorType.Wall) {
  866. floorplanService.deleteWall(item.vectorId)
  867. } else if (componentService.isComponent(item.type)) {
  868. floorplanService.deleteComponent(item.vectorId)
  869. } else if (item.type == VectorType.Tag) {
  870. floorplanService.deleteTag(item.vectorId)
  871. } else if (item.type == VectorType.Table) {
  872. floorplanService.deleteTable(item.vectorId)
  873. } else if (signService.isSign(item.type)) {
  874. floorplanService.deleteSign(item.vectorId)
  875. } else if (item.type == VectorType.WallCorner) {
  876. wallService.deleteWallCorner(item.vectorId)
  877. }
  878. this.history.save()
  879. this.renderer.autoRedraw()
  880. }
  881. }
  882. }