Layer.js 36 KB

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