ListenLayer.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  1. import { mathUtil } from "./Util/MathUtil";
  2. import { dataService } from "./Service/DataService.js";
  3. import { stateService } from "./Service/StateService.js";
  4. import { roadService } from "./Service/RoadService.js";
  5. import Constant from "./Constant.js";
  6. import VectorType from "./enum/VectorType.js";
  7. import SelectState from "./enum/SelectState.js";
  8. export default class ListenLayer {
  9. constructor() {
  10. this.roadInfo = {
  11. roadId: null,
  12. state: null, // 未选中null
  13. };
  14. this.pointInfo = {
  15. pointId: null,
  16. state: null,
  17. };
  18. this.tagInfo = {
  19. tagId: null,
  20. state: null,
  21. };
  22. this.modifyPoint = null;
  23. }
  24. //开始监听,exceptPointId表示不考虑的点,exceptRoadIds表示不考虑的墙
  25. start(position, exceptPointId, exceptRoadIds) {
  26. let nearest = this.getNearForVectors(
  27. position,
  28. exceptPointId,
  29. exceptRoadIds
  30. );
  31. if (
  32. nearest.modifyPoint &&
  33. (nearest.modifyPoint.hasOwnProperty("linkedPointId") ||
  34. nearest.modifyPoint.hasOwnProperty("linkedPointIdX") ||
  35. nearest.modifyPoint.hasOwnProperty("linkedPointIdY") ||
  36. nearest.modifyPoint.hasOwnProperty("linkedRoadId"))
  37. ) {
  38. this.modifyPoint = {
  39. x: nearest.modifyPoint.x,
  40. y: nearest.modifyPoint.y,
  41. };
  42. if (
  43. nearest.modifyPoint.hasOwnProperty("linkedPointId") &&
  44. nearest.modifyPoint.linkedPointId != null
  45. ) {
  46. this.modifyPoint.linkedPointId = nearest.modifyPoint.linkedPointId;
  47. } else if (
  48. nearest.modifyPoint.hasOwnProperty("linkedRoadId") &&
  49. nearest.modifyPoint.linkedRoadId != null
  50. ) {
  51. this.modifyPoint.linkedRoadId = nearest.modifyPoint.linkedRoadId;
  52. } else {
  53. if (
  54. nearest.modifyPoint.hasOwnProperty("linkedPointIdX") &&
  55. nearest.modifyPoint.linkedPointIdX != null
  56. ) {
  57. this.modifyPoint.linkedPointIdX = nearest.modifyPoint.linkedPointIdX;
  58. }
  59. if (
  60. nearest.modifyPoint.hasOwnProperty("linkedPointIdY") &&
  61. nearest.modifyPoint.linkedPointIdY != null
  62. ) {
  63. this.modifyPoint.linkedPointIdY = nearest.modifyPoint.linkedPointIdY;
  64. }
  65. }
  66. } else {
  67. this.modifyPoint = null;
  68. }
  69. const flag = this.updateSelectInfos(nearest, Constant.minAdsorbPix);
  70. this.updateSelectItem();
  71. return flag;
  72. }
  73. // 获得最近的墙面和墙角
  74. // 同时获得吸附的相关信息
  75. // 找到选中的symbol(只有选中了,才算是最近的)
  76. getNearForVectors(position, exceptPointId, exceptRoadIds) {
  77. let min1 = null; // 与墙角的距离
  78. let min2 = null; // 与墙面的距离
  79. // 纠正
  80. // 垂直,水平
  81. const modifyPoint = {};
  82. // 吸附在墙面上
  83. let _modifyPoint = null;
  84. const hasPointIds = [];
  85. if (exceptPointId) {
  86. hasPointIds.push(exceptPointId);
  87. }
  88. const roads = dataService.getRoads();
  89. for (const roadId in roads) {
  90. if (exceptRoadIds && exceptRoadIds.hasOwnProperty(roadId)) {
  91. continue;
  92. }
  93. const road = dataService.getRoad(roadId);
  94. const startPoint = dataService.getPoint(road.startId);
  95. const endPoint = dataService.getPoint(road.endId);
  96. let distance = null;
  97. const line = roadService.getMidLine(road);
  98. if (!line) {
  99. //debugger
  100. //删除墙
  101. dataService.deleteRoad(roadId);
  102. continue;
  103. console.error("getNearForVectors************************************");
  104. }
  105. const join = mathUtil.getJoinLinePoint(position, line);
  106. //先找端点
  107. if (hasPointIds.indexOf(road.startId) == -1) {
  108. hasPointIds.push(road.startId);
  109. distance = mathUtil.getDistance(position, startPoint);
  110. if (min1 == null || min1.distance > distance) {
  111. min1 = {
  112. distance: distance,
  113. pointId: road.startId,
  114. };
  115. //start部分找到了墙的端点
  116. if (
  117. (mathUtil.getDistance(join, position) < road.width / 2 &&
  118. mathUtil.getDistance(join, startPoint) < road.width / 2) ||
  119. min1.distance < road.width / 2
  120. ) {
  121. modifyPoint.linkedPointId = road.startId;
  122. modifyPoint.x = startPoint.x;
  123. modifyPoint.y = startPoint.y;
  124. delete modifyPoint.linkedPointIdX;
  125. delete modifyPoint.linkedPointIdY;
  126. break;
  127. }
  128. }
  129. // //start部分找到了与x接近的其他点
  130. // if (Math.abs(position.x - startPoint.x) < Constant.minAdsorbPix) {
  131. // if (!modifyPoint.linkedPointIdX) {
  132. // modifyPoint.x = startPoint.x;
  133. // modifyPoint.linkedPointIdX = road.startId;
  134. // } else {
  135. // const linkedPointX = dataService.getPoint(
  136. // modifyPoint.linkedPointIdX
  137. // );
  138. // if (
  139. // mathUtil.getDistance(position, linkedPointX) >
  140. // mathUtil.getDistance(position, startPoint)
  141. // ) {
  142. // modifyPoint.x = startPoint.x;
  143. // modifyPoint.linkedPointIdX = road.startId;
  144. // }
  145. // }
  146. // }
  147. // //start部分找到了与y接近的其他点
  148. // if (Math.abs(position.y - startPoint.y) < Constant.minAdsorbPix) {
  149. // if (!modifyPoint.linkedPointIdY) {
  150. // modifyPoint.y = startPoint.y;
  151. // modifyPoint.linkedPointIdY = road.startId;
  152. // } else {
  153. // const linkedPointY = dataService.getPoint(
  154. // modifyPoint.linkedPointIdY
  155. // );
  156. // if (
  157. // mathUtil.getDistance(position, linkedPointY) >
  158. // mathUtil.getDistance(position, startPoint)
  159. // ) {
  160. // modifyPoint.y = startPoint.y;
  161. // modifyPoint.linkedPointIdY = road.startId;
  162. // }
  163. // }
  164. // }
  165. }
  166. if (hasPointIds.indexOf(road.endId) == -1) {
  167. hasPointIds.push(road.endId);
  168. distance = mathUtil.getDistance(position, endPoint);
  169. if (min1 == null || min1.distance > distance) {
  170. min1 = {
  171. distance: distance,
  172. pointId: road.endId,
  173. };
  174. //end部分找到了墙的端点
  175. if (
  176. (mathUtil.getDistance(join, position) < road.width / 2 &&
  177. mathUtil.getDistance(join, endPoint) < road.width / 2) ||
  178. min1.distance < road.width / 2
  179. ) {
  180. modifyPoint.linkedPointId = road.endId;
  181. modifyPoint.x = endPoint.x;
  182. modifyPoint.y = endPoint.y;
  183. delete modifyPoint.linkedPointIdX;
  184. delete modifyPoint.linkedPointIdY;
  185. break;
  186. }
  187. }
  188. // //end部分找到了与x接近的其他点
  189. // if (Math.abs(position.x - endPoint.x) < Constant.minAdsorbPix) {
  190. // if (!modifyPoint.linkedPointIdX) {
  191. // modifyPoint.x = endPoint.x;
  192. // modifyPoint.linkedPointIdX = road.endId;
  193. // } else {
  194. // const linkedPointX = dataService.getPoint(
  195. // modifyPoint.linkedPointIdX
  196. // );
  197. // if (
  198. // mathUtil.getDistance(position, linkedPointX) >
  199. // mathUtil.getDistance(position, endPoint)
  200. // ) {
  201. // modifyPoint.x = endPoint.x;
  202. // modifyPoint.linkedPointIdX = road.endId;
  203. // }
  204. // }
  205. // }
  206. // //end部分找到了与y接近的其他点
  207. // if (Math.abs(position.y - endPoint.y) < Constant.minAdsorbPix) {
  208. // if (!modifyPoint.linkedPointIdY) {
  209. // modifyPoint.y = endPoint.y;
  210. // modifyPoint.linkedPointIdY = road.endId;
  211. // } else {
  212. // const linkedPointY = dataService.getPoint(
  213. // modifyPoint.linkedPointIdY
  214. // );
  215. // if (
  216. // mathUtil.getDistance(position, linkedPointY) >
  217. // mathUtil.getDistance(position, endPoint)
  218. // ) {
  219. // modifyPoint.y = endPoint.y;
  220. // modifyPoint.linkedPointIdY = road.endId;
  221. // }
  222. // }
  223. // }
  224. }
  225. distance = mathUtil.getDistance(position, join);
  226. //是否在墙上,可能在墙外
  227. const _flag = roadService.isContain(road, join);
  228. if (_flag && (min2 == null || min2.distance > distance)) {
  229. min2 = {
  230. distance: distance,
  231. roadId: roadId,
  232. };
  233. }
  234. if (_flag && mathUtil.getDistance(position, join) < road.width / 2) {
  235. _modifyPoint = join;
  236. _modifyPoint.linkedRoadId = roadId;
  237. }
  238. }
  239. const result = {
  240. minPoint: min1,
  241. minRoad: min2,
  242. tagInfo: {},
  243. };
  244. if (_modifyPoint != null) {
  245. result.modifyPoint = JSON.parse(JSON.stringify(_modifyPoint));
  246. } else if (
  247. modifyPoint.hasOwnProperty("x") &&
  248. modifyPoint.hasOwnProperty("y")
  249. ) {
  250. result.modifyPoint = JSON.parse(JSON.stringify(modifyPoint));
  251. } else if (modifyPoint.hasOwnProperty("x")) {
  252. result.modifyPoint = JSON.parse(JSON.stringify(modifyPoint));
  253. result.modifyPoint.x = modifyPoint.x;
  254. result.modifyPoint.y = position.y;
  255. } else if (modifyPoint.hasOwnProperty("y")) {
  256. result.modifyPoint = JSON.parse(JSON.stringify(modifyPoint));
  257. result.modifyPoint.x = position.x;
  258. result.modifyPoint.y = modifyPoint.y;
  259. }
  260. // const tags = dataService.getTags();
  261. // for (const tagId in tags) {
  262. // const tag = dataService.getTag(tagId);
  263. // const location = tag.isContain(position);
  264. // if (location) {
  265. // result.tagInfo = {
  266. // tagId: tagId,
  267. // state: "all",
  268. // };
  269. // break;
  270. // }
  271. // }
  272. return result;
  273. }
  274. // position用来判断是否在墙的symbol上
  275. updateSelectInfos(nearest, minDistance) {
  276. // 墙角状态是否改变
  277. let flag1 = false;
  278. if (nearest.minPoint != null) {
  279. if (nearest.minPoint.distance < minDistance) {
  280. flag1 = this.isChanged(nearest.minPoint.pointId, SelectState.Select, 1);
  281. this.pointInfo = {
  282. pointId: nearest.minPoint.pointId,
  283. state: SelectState.Select,
  284. };
  285. } else {
  286. flag1 = this.isChanged(nearest.minPoint.pointId, null, 1);
  287. this.pointInfo = {
  288. pointId: nearest.minPoint.pointId,
  289. state: null,
  290. };
  291. }
  292. } else {
  293. flag1 = this.isChanged(null, null, 1);
  294. this.pointInfo = {
  295. pointId: null,
  296. state: null,
  297. };
  298. }
  299. // 墙面状态是否改变
  300. let flag2 = false;
  301. if (nearest.minRoad != null) {
  302. if (nearest.minRoad.distance < minDistance) {
  303. flag2 = this.isChanged(nearest.minRoad.roadId, SelectState.Select, 2);
  304. this.roadInfo = {
  305. roadId: nearest.minRoad.roadId,
  306. state: SelectState.Select,
  307. };
  308. } else {
  309. flag2 = this.isChanged(nearest.minRoad.roadId, null, 2);
  310. this.roadInfo = {
  311. roadId: nearest.minRoad.roadId,
  312. state: null,
  313. };
  314. }
  315. } else {
  316. flag2 = this.isChanged(null, null, 2);
  317. this.roadInfo = {
  318. roadId: null,
  319. state: null,
  320. };
  321. }
  322. const flag5 = this.isChanged(
  323. nearest.tagInfo.tagId,
  324. nearest.tagInfo.state,
  325. 5
  326. );
  327. this.tagInfo = {
  328. tagId: nearest.tagInfo.tagId,
  329. state: nearest.tagInfo.state,
  330. };
  331. return flag1 || flag2 || flag5;
  332. }
  333. // type是1表示点,2表示墙,3表示symbol,4表示component, 5表示tag,6表示furniture
  334. isChanged(vectorId, state, type) {
  335. let flag = false;
  336. if (type == 1) {
  337. if (state == null && state == this.pointInfo.state) {
  338. flag = false;
  339. } else if (
  340. this.pointInfo.pointId == vectorId &&
  341. state == this.pointInfo.state
  342. ) {
  343. flag = false;
  344. } else {
  345. flag = true;
  346. }
  347. } else if (type == 2) {
  348. if (state == null && state == this.roadInfo.state) {
  349. flag = false;
  350. } else if (
  351. this.roadInfo.roadId == vectorId &&
  352. state == this.roadInfo.state
  353. ) {
  354. flag = false;
  355. } else {
  356. flag = true;
  357. }
  358. } else if (type == 5) {
  359. if (state == null && state == this.tagInfo.state) {
  360. flag = false;
  361. } else if (
  362. this.tagInfo.tagId == vectorId &&
  363. state == this.tagInfo.state
  364. ) {
  365. flag = false;
  366. } else {
  367. flag = true;
  368. }
  369. }
  370. return flag;
  371. }
  372. updateSelectItem() {
  373. if (this.tagInfo.tagId != null && this.tagInfo.state != null) {
  374. const tag = dataService.getTag(this.tagInfo.tagId);
  375. stateService.setSelectItem(
  376. this.tagInfo.tagId,
  377. tag.geoType,
  378. this.tagInfo.state
  379. );
  380. } else if (this.pointInfo.pointId != null && this.pointInfo.state != null) {
  381. stateService.setSelectItem(
  382. this.pointInfo.pointId,
  383. VectorType.RoadCorner,
  384. SelectState.Select
  385. );
  386. } else if (this.roadInfo.roadId != null && this.roadInfo.state != null) {
  387. stateService.setSelectItem(
  388. this.roadInfo.roadId,
  389. VectorType.Road,
  390. SelectState.Select
  391. );
  392. } else {
  393. stateService.clearSelectItem();
  394. }
  395. }
  396. clear() {
  397. this.roadInfo = {
  398. roadId: null,
  399. state: null,
  400. };
  401. this.pointInfo = {
  402. pointId: null,
  403. state: null,
  404. };
  405. this.modifyPoint = null;
  406. }
  407. }
  408. const listenLayer = new ListenLayer();
  409. export { listenLayer };