SGraphSelectContainer.ts 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  1. /*
  2. * *********************************************************************************************************************
  3. *
  4. * !!
  5. * .F88X
  6. * X8888Y
  7. * .}888888N;
  8. * i888888N; .:! .I$WI:
  9. * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
  10. * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
  11. * +888888N; .8888888Y "&&8Y.}8,
  12. * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
  13. * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
  14. * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
  15. * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
  16. * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
  17. * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
  18. * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
  19. * .:R888888I
  20. * .&888888I Copyright (c) 2009-2020. 博锐尚格科技股份有限公司
  21. * ~8888'
  22. * .!88~ All rights reserved.
  23. *
  24. * *********************************************************************************************************************
  25. */
  26. import { SGraphItem } from "./SGraphItem";
  27. import { SGraphLayoutType } from "./enums/SGraphLayoutType";
  28. import { SOrderSetType } from "./enums/SOrderSetType";
  29. import { SColor, SPainter, SPoint, SRect, SSize } from "@persagy-web/draw";
  30. import { SMathUtil } from "./utils/SMathUtil";
  31. import { SMouseEvent } from "@persagy-web/base";
  32. /**
  33. * 基本选择器
  34. *
  35. * @author 郝建龙 <haojianlong@sagacloud.cn>
  36. */
  37. export class SGraphSelectContainer extends SGraphItem {
  38. /** 选择对象 list */
  39. private _itemList: SGraphItem[] = [];
  40. get itemList(): SGraphItem[] {
  41. return this._itemList;
  42. }
  43. /** 外接点的 list */
  44. private pointList: SPoint[] = [];
  45. /** item 宽 */
  46. private _width: number = 0;
  47. get width(): number {
  48. return this._width;
  49. }
  50. set width(v: number) {
  51. if (v == this._width) {
  52. return;
  53. }
  54. this._width = v;
  55. this.update();
  56. }
  57. /** item 高 */
  58. private _height: number = 0;
  59. get height(): number {
  60. return this._height;
  61. }
  62. set height(v: number) {
  63. if (v == this._height) {
  64. return;
  65. }
  66. this._height = v;
  67. this.update();
  68. }
  69. /** 修改之前的宽 */
  70. private oldWidth: number = 0;
  71. /** 修改之前的高 */
  72. private oldHeight: number = 0;
  73. /** 最小宽 */
  74. minWidth: number = 0;
  75. /** 最大宽 */
  76. maxWidth: number = Number.MAX_SAFE_INTEGER;
  77. /** 最小高 */
  78. minHeight: number = 0;
  79. /** 最小高 */
  80. maxHeight: number = Number.MAX_SAFE_INTEGER;
  81. /** 边框颜色 */
  82. _strokeColor: SColor = new SColor("#82C7FC");
  83. /** 画笔颜色 */
  84. get strokeColor(): SColor {
  85. return this._strokeColor;
  86. }
  87. set strokeColor(v: SColor) {
  88. this._strokeColor = v;
  89. this.update();
  90. }
  91. /** 填充颜色 */
  92. _fillColor: SColor = new SColor("#ffffff");
  93. get fillColor(): SColor {
  94. return this._fillColor;
  95. }
  96. set fillColor(v: SColor) {
  97. this._fillColor = v;
  98. this.update();
  99. }
  100. /** 统计选中 item 的数量 */
  101. get count(): number {
  102. return this.itemList.length;
  103. }
  104. /** 置顶 zOrder 增加基数 */
  105. private radix: number = 0.001;
  106. /** 定时器 */
  107. private timer: number | undefined;
  108. /** 半径 */
  109. radius: number = 5;
  110. /** 全局灵敏度 */
  111. dis: number = 10;
  112. /** 灵敏度转换为场景长度 */
  113. private sceneDis: number = 10;
  114. /** 当前点索引 */
  115. private _curIndex: number = -1;
  116. get curIndex(): number {
  117. return this._curIndex;
  118. }
  119. set curIndex(v: number) {
  120. if (v === this._curIndex) {
  121. return;
  122. }
  123. this._curIndex = v;
  124. this.update();
  125. }
  126. /** 当前点索引 */
  127. private curPoint: SPoint | undefined;
  128. /** 鼠标样式 */
  129. static cursorList: string[] = [
  130. "nw-resize",
  131. "n-resize",
  132. "ne-resize",
  133. "e-resize",
  134. "se-resize",
  135. "s-resize",
  136. "sw-resize",
  137. "w-resize",
  138. "move",
  139. "default"
  140. ];
  141. /**
  142. * 构造体
  143. */
  144. constructor() {
  145. super();
  146. this.zOrder = 9999999;
  147. this.visible = false;
  148. this.moveable = true;
  149. }
  150. /**
  151. * 添加 item 到 list
  152. *
  153. * @param item 当前选中的 item
  154. */
  155. addItem(item: SGraphItem): void {
  156. for (let i = 0; i < this.itemList.length; i++) {
  157. if (this.itemList[i] == item) {
  158. return;
  159. }
  160. }
  161. item.selected = true;
  162. this.itemList.push(item);
  163. this.throttle(this.selectChange, 80);
  164. }
  165. /**
  166. * 清空再添加(事件+复制数组)
  167. *
  168. * @param item 当前选中的 item
  169. */
  170. setItem(item: SGraphItem): void {
  171. this.itemList.forEach((t: SGraphItem): void => {
  172. t.selected = false;
  173. });
  174. this.itemList.length = 0;
  175. item.selected = true;
  176. this.itemList.push(item);
  177. this.throttle(this.selectChange, 80);
  178. }
  179. /**
  180. * 清空选择对象
  181. */
  182. clear(): void {
  183. this.itemList.forEach((t: SGraphItem): void => {
  184. t.selected = false;
  185. });
  186. this.itemList.length = 0;
  187. this.throttle(this.selectChange, 80);
  188. }
  189. /**
  190. * 切换选择对象
  191. *
  192. * @param item 当前选中的 item
  193. */
  194. toggleItem(item: SGraphItem): void {
  195. for (let i = 0; i < this.itemList.length; i++) {
  196. if (this.itemList[i] == item) {
  197. this.itemList[i].selected = false;
  198. this.itemList.splice(i, 1);
  199. this.throttle(this.selectChange, 80);
  200. return;
  201. }
  202. }
  203. // 多选时,父级 item 需要一致
  204. if (this.itemList.length) {
  205. if (item.parent != this.itemList[0].parent) {
  206. return;
  207. }
  208. }
  209. item.selected = true;
  210. this.itemList.push(item);
  211. this.throttle(this.selectChange, 80);
  212. }
  213. /**
  214. * 对齐
  215. *
  216. * @param type 对齐方式
  217. */
  218. layout(type: SGraphLayoutType): void {
  219. if (this.itemList.length < 2) {
  220. return;
  221. }
  222. switch (type) {
  223. case SGraphLayoutType.Left:
  224. this.alignLeft();
  225. break;
  226. case SGraphLayoutType.Bottom:
  227. this.alignBottom();
  228. break;
  229. case SGraphLayoutType.Center:
  230. this.alignCenter();
  231. break;
  232. case SGraphLayoutType.Horizontal:
  233. this.alignHorizontal();
  234. break;
  235. case SGraphLayoutType.Middle:
  236. this.alignMiddle();
  237. break;
  238. case SGraphLayoutType.Right:
  239. this.alignRight();
  240. break;
  241. case SGraphLayoutType.Top:
  242. this.alignTop();
  243. break;
  244. case SGraphLayoutType.Vertical:
  245. this.alignVertical();
  246. break;
  247. default:
  248. console.log("对齐类型不存在");
  249. break;
  250. }
  251. }
  252. /**
  253. * 图层操作
  254. *
  255. * @param type 操作类型
  256. */
  257. setOrder(type: SOrderSetType): void {
  258. if (this.itemList.length < 1) {
  259. return;
  260. }
  261. switch (type) {
  262. case SOrderSetType.Top:
  263. this.setTop();
  264. break;
  265. case SOrderSetType.Bottom:
  266. this.setBottom();
  267. break;
  268. case SOrderSetType.After:
  269. this.setAfter();
  270. break;
  271. case SOrderSetType.Before:
  272. this.setBefore();
  273. break;
  274. default:
  275. console.log("图层操作类型不存在");
  276. break;
  277. }
  278. }
  279. /**
  280. * 左对齐
  281. */
  282. private alignLeft(): void {
  283. let standardItem = this.itemList[0];
  284. // 计算第一个外接矩阵左上角坐标在场景中的坐标
  285. let p = standardItem.mapToScene(
  286. standardItem.boundingRect().x,
  287. standardItem.boundingRect().y
  288. );
  289. for (let i = 1; i < this.itemList.length; i++) {
  290. let curItem = this.itemList[i];
  291. if (curItem.moveable) {
  292. // 将 p 转换为当前 item 中的坐标
  293. let p1 = curItem.mapFromScene(p.x, p.y);
  294. // 根据等式差值相等 newboundx - oldboundx = newposx - oldposx => newposx = newboundx - oldboundx + oldposx
  295. curItem.x =
  296. (p1.x - curItem.boundingRect().x) * curItem.inverseScale +
  297. curItem.x;
  298. }
  299. }
  300. }
  301. /**
  302. * 顶对齐
  303. */
  304. private alignTop(): void {
  305. let standardItem = this.itemList[0];
  306. let p = standardItem.mapToScene(
  307. standardItem.boundingRect().x,
  308. standardItem.boundingRect().y
  309. );
  310. for (let i = 1; i < this.itemList.length; i++) {
  311. let curItem = this.itemList[i];
  312. if (curItem.moveable) {
  313. let p1 = curItem.mapFromScene(p.x, p.y);
  314. curItem.y =
  315. (p1.y - curItem.boundingRect().y) * curItem.inverseScale +
  316. curItem.y;
  317. }
  318. }
  319. }
  320. /**
  321. * 右对齐
  322. */
  323. private alignRight(): void {
  324. let standardItem = this.itemList[0];
  325. let p = standardItem.mapToScene(
  326. standardItem.boundingRect().right,
  327. standardItem.boundingRect().bottom
  328. );
  329. for (let i = 1; i < this.itemList.length; i++) {
  330. let curItem = this.itemList[i];
  331. if (curItem.moveable) {
  332. let p1 = curItem.mapFromScene(p.x, p.y);
  333. curItem.x =
  334. (p1.x - curItem.boundingRect().right) *
  335. curItem.inverseScale +
  336. curItem.x;
  337. }
  338. }
  339. }
  340. /**
  341. * 底对齐
  342. */
  343. private alignBottom(): void {
  344. let standardItem = this.itemList[0];
  345. let p = standardItem.mapToScene(
  346. standardItem.boundingRect().right,
  347. standardItem.boundingRect().bottom
  348. );
  349. for (let i = 1; i < this.itemList.length; i++) {
  350. let curItem = this.itemList[i];
  351. if (curItem.moveable) {
  352. let p1 = curItem.mapFromScene(p.x, p.y);
  353. curItem.y =
  354. (p1.y - curItem.boundingRect().bottom) *
  355. curItem.inverseScale +
  356. curItem.y;
  357. }
  358. }
  359. }
  360. /**
  361. * 水平居中对齐
  362. */
  363. private alignCenter(): void {
  364. // 第一个选中的 item 即为基准对象
  365. let standardItem = this.itemList[0];
  366. // 基准对象的中心点
  367. let center = standardItem.boundingRect().center();
  368. // 中心点转换为场景坐标
  369. let p = standardItem.mapToScene(center.x, center.y);
  370. for (let i = 1; i < this.itemList.length; i++) {
  371. let curItem = this.itemList[i];
  372. if (curItem.moveable) {
  373. let p1 = curItem.mapFromScene(p.x, p.y);
  374. // 根据等式差值相等 newcenterx - oldcenterx = newposx - oldposx => newposx = newcenterx - oldcenterx + oldposx
  375. curItem.x =
  376. (p1.x - curItem.boundingRect().center().x) *
  377. curItem.inverseScale +
  378. curItem.x;
  379. }
  380. }
  381. }
  382. /**
  383. * 垂直居中对齐
  384. */
  385. private alignMiddle(): void {
  386. // 第一个选中的 item 即为基准对象
  387. let standardItem = this.itemList[0];
  388. // 基准对象的中心点
  389. let center = standardItem.boundingRect().center();
  390. // 中心点转换为场景坐标
  391. let p = standardItem.mapToScene(center.x, center.y);
  392. for (let i = 1; i < this.itemList.length; i++) {
  393. let curItem = this.itemList[i];
  394. if (curItem.moveable) {
  395. let p1 = curItem.mapFromScene(p.x, p.y);
  396. curItem.y =
  397. (p1.y - curItem.boundingRect().center().y) *
  398. curItem.inverseScale +
  399. curItem.y;
  400. }
  401. }
  402. }
  403. /**
  404. * 垂直分布
  405. */
  406. private alignVertical(): void {
  407. if (this.itemList.length < 3) {
  408. return;
  409. }
  410. for (let i = 0; i < this.itemList.length - 1; i++) {
  411. for (let j = 0; j < this.itemList.length - 1 - i; j++) {
  412. let curItemCenter = this.itemList[j].boundingRect().center();
  413. let nextItemCenter = this.itemList[j + 1]
  414. .boundingRect()
  415. .center();
  416. let curCenterY = this.itemList[j].mapToScene(
  417. curItemCenter.x,
  418. curItemCenter.y
  419. ).y;
  420. let nextCenterY = this.itemList[j + 1].mapToScene(
  421. nextItemCenter.x,
  422. nextItemCenter.y
  423. ).y;
  424. if (curCenterY > nextCenterY) {
  425. let temp = this.itemList[j];
  426. this.itemList[j] = this.itemList[j + 1];
  427. this.itemList[j + 1] = temp;
  428. }
  429. }
  430. }
  431. let top = this.itemList[0];
  432. let bottom = this.itemList[this.itemList.length - 1];
  433. let topCenter = top.boundingRect().center();
  434. let bottomCenter = bottom.boundingRect().center();
  435. let leftToRight =
  436. bottom.mapToScene(bottomCenter.x, bottomCenter.y).y -
  437. top.mapToScene(topCenter.x, topCenter.y).y;
  438. let average = leftToRight / (this.itemList.length - 1);
  439. for (let k = 1; k < this.itemList.length - 1; k++) {
  440. let curItem = this.itemList[k];
  441. if (curItem.moveable) {
  442. let p1 = top.mapToScene(
  443. top.boundingRect().center().x,
  444. top.boundingRect().center().y
  445. );
  446. let p2 = curItem.mapFromScene(p1.x, p1.y + average * k);
  447. curItem.y =
  448. (p2.y - curItem.boundingRect().center().y) *
  449. curItem.inverseScale +
  450. curItem.y;
  451. }
  452. }
  453. }
  454. /**
  455. * 水平分布
  456. */
  457. private alignHorizontal(): void {
  458. if (this.itemList.length < 3) {
  459. return;
  460. }
  461. for (let i = 0; i < this.itemList.length - 1; i++) {
  462. for (let j = 0; j < this.itemList.length - 1 - i; j++) {
  463. let curItemCenter = this.itemList[j].boundingRect().center();
  464. let nextItemCenter = this.itemList[j + 1]
  465. .boundingRect()
  466. .center();
  467. let curCenterX = this.itemList[j].mapToScene(
  468. curItemCenter.x,
  469. curItemCenter.y
  470. ).x;
  471. let nextCenterX = this.itemList[j + 1].mapToScene(
  472. nextItemCenter.x,
  473. nextItemCenter.y
  474. ).x;
  475. if (curCenterX > nextCenterX) {
  476. let temp = this.itemList[j];
  477. this.itemList[j] = this.itemList[j + 1];
  478. this.itemList[j + 1] = temp;
  479. }
  480. }
  481. }
  482. let left = this.itemList[0];
  483. let right = this.itemList[this.itemList.length - 1];
  484. let leftCenter = left.boundingRect().center();
  485. let rightCenter = right.boundingRect().center();
  486. let leftToRight =
  487. right.mapToScene(rightCenter.x, rightCenter.y).x -
  488. left.mapToScene(leftCenter.x, leftCenter.y).x;
  489. let average = leftToRight / (this.itemList.length - 1);
  490. for (let k = 1; k < this.itemList.length - 1; k++) {
  491. let curItem = this.itemList[k];
  492. if (curItem.moveable) {
  493. let p1 = left.mapToScene(
  494. left.boundingRect().center().x,
  495. left.boundingRect().center().y
  496. );
  497. let p2 = curItem.mapFromScene(p1.x + average * k, p1.y);
  498. // 根据等式 newposx - oldposx = newcenterx - oldcenterx
  499. curItem.x =
  500. (p2.x - curItem.boundingRect().center().x) *
  501. curItem.inverseScale +
  502. curItem.x;
  503. }
  504. }
  505. }
  506. // 修改层级操作步骤:
  507. // 排序(保持相对层级不变)
  508. // children 里边要取 整数部分相同的 点: children 是已经排好序的
  509. // 然后再取最大值
  510. /**
  511. * 置顶
  512. */
  513. private setTop(): void {
  514. const arr: SGraphItem[] = this.sortItemList(this.itemList, true);
  515. // 按顺序 zOrder 增加
  516. arr.forEach((it): void => {
  517. let max = it.zOrder;
  518. if (it.parent) {
  519. it.parent.children.forEach((item): void => {
  520. if (
  521. Number(max.toFixed()) == Number(item.zOrder.toFixed())
  522. ) {
  523. if (item.zOrder > max) {
  524. max = item.zOrder;
  525. }
  526. }
  527. });
  528. it.zOrder = max + this.radix;
  529. }
  530. });
  531. }
  532. /**
  533. * 置底
  534. */
  535. private setBottom(): void {
  536. const arr: SGraphItem[] = this.sortItemList(this.itemList, false);
  537. // 按顺序 zOrder 增加
  538. arr.forEach((it): void => {
  539. let min = it.zOrder;
  540. if (it.parent) {
  541. it.parent.children.forEach((item): void => {
  542. if (
  543. Number(min.toFixed()) == Number(item.zOrder.toFixed())
  544. ) {
  545. if (item.zOrder < min) {
  546. min = item.zOrder;
  547. }
  548. }
  549. });
  550. it.zOrder = min - this.radix;
  551. }
  552. });
  553. }
  554. /**
  555. * 下移一层 zOrder 减小
  556. */
  557. private setBefore(): void {
  558. const arr: SGraphItem[] = this.sortItemList(this.itemList, false);
  559. arr.forEach((it): void => {
  560. if (it.parent) {
  561. const min = it.zOrder;
  562. let temp = it.parent.children
  563. .map((child): number | undefined => {
  564. if (
  565. Number(child.zOrder.toFixed()) ==
  566. Number(min.toFixed())
  567. ) {
  568. return child.zOrder;
  569. }
  570. })
  571. .filter((c): number | undefined => c);
  572. temp = [...new Set(temp)];
  573. // 当前层有多个
  574. const index = temp.indexOf(it.zOrder);
  575. if (index <= 1) {
  576. // 当前 item 索引为1时 将当前 item 放至第一个前边
  577. // @ts-ignore
  578. it.zOrder = temp[0] - this.radix;
  579. } else if (index > 1) {
  580. // 当前 item 索引大于等于2,将当前 item 放至前两个中间
  581. // @ts-ignore
  582. it.zOrder = (temp[index - 1] + temp[index - 2]) / 2;
  583. }
  584. }
  585. });
  586. }
  587. /**
  588. * 上移一层 zOrder 增加
  589. */
  590. private setAfter(): void {
  591. const arr: SGraphItem[] = this.sortItemList(this.itemList, false);
  592. arr.forEach((it): void => {
  593. if (it.parent) {
  594. const max = it.zOrder;
  595. let temp = it.parent.children
  596. .map((child): number | undefined => {
  597. if (
  598. Number(child.zOrder.toFixed()) ==
  599. Number(max.toFixed())
  600. ) {
  601. return child.zOrder;
  602. }
  603. })
  604. .filter((c): number | undefined => c);
  605. temp = [...new Set(temp)].reverse();
  606. // 当前层有多个
  607. const index = temp.indexOf(it.zOrder);
  608. if (index <= 1) {
  609. // 当前 item 索引为1时 将当前 item 放至第一个前边
  610. // @ts-ignore
  611. it.zOrder = temp[0] + this.radix;
  612. } else if (index > 1) {
  613. // 当前 item 索引大于等于2,将当前 item 放至前两个中间
  614. // @ts-ignore
  615. it.zOrder = (temp[index - 1] + temp[index - 2]) / 2;
  616. }
  617. }
  618. });
  619. }
  620. /**
  621. * 数组排序
  622. *
  623. * @param arr 排序前的数组
  624. * @param flag 正序 or 逆序
  625. * @return 排序后的数组
  626. */
  627. sortItemList(arr: SGraphItem[], flag: boolean = true): SGraphItem[] {
  628. const list: SGraphItem[] = arr.map((t): SGraphItem => t);
  629. list.sort(this.compare("zOrder", flag));
  630. return list;
  631. }
  632. /**
  633. * 按照某个属性排序
  634. *
  635. * @param prop 属性
  636. * @param flag 默认正序
  637. */
  638. private compare(prop: string, flag: boolean): any {
  639. const f = flag ? 1 : -1;
  640. // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
  641. return (a: any, b: any): number => {
  642. a = a[prop];
  643. b = b[prop];
  644. if (a < b) {
  645. return f * -1;
  646. }
  647. if (a > b) {
  648. return f * 1;
  649. }
  650. return 0;
  651. };
  652. }
  653. /**
  654. * 防抖-防止快速多次触发 itemChange; 只执行最后一次
  655. */
  656. throttle(fn: Function, wait: number): void {
  657. if (this.timer) clearTimeout(this.timer);
  658. this.timer = setTimeout((): void => {
  659. this.selectChange();
  660. }, wait);
  661. }
  662. /**
  663. * 选中的 item 更新时更新自身边界
  664. */
  665. selectChange(): void {
  666. // 抛出事件
  667. this.$emit("listChange", this.itemList);
  668. if (this.count > 0) {
  669. // 重新计算边界
  670. this.getSize();
  671. // 计算边界 8 个点
  672. this.calExtreme();
  673. if (
  674. this.showSelect &&
  675. (this.count > 1 ||
  676. (this.count == 1 && this.itemList[0].showSelect))
  677. ) {
  678. this.visible = true;
  679. }
  680. } else {
  681. this.visible = false;
  682. this.curIndex = -1;
  683. }
  684. }
  685. /**
  686. * 计算选中 item 的外接矩形和
  687. *
  688. * @return 外接矩形的和
  689. */
  690. calItemBounding(): SRect {
  691. if (this.itemList.length) {
  692. const fir = this.itemList[0];
  693. let rect = fir.boundingRect().adjusted(fir.pos.x, fir.pos.y, 0, 0);
  694. for (let i = 1; i < this.itemList.length; i++) {
  695. const cur = this.itemList[i];
  696. rect = rect.unioned(
  697. cur.boundingRect().adjusted(cur.pos.x, cur.pos.y, 0, 0)
  698. );
  699. }
  700. return rect;
  701. } else {
  702. return new SRect();
  703. }
  704. }
  705. /**
  706. * 根据矩阵得到宽高,并将自己移动至矩阵左上角
  707. */
  708. getSize(): void {
  709. const r = this.calItemBounding();
  710. this.width = r.width;
  711. this.height = r.height;
  712. this.moveTo(r.left, r.top);
  713. }
  714. /**
  715. * 计算外接矩形 8 个端点
  716. */
  717. calExtreme(): void {
  718. this.pointList = [];
  719. this.pointList.push(new SPoint(0, 0));
  720. this.pointList.push(new SPoint(this.width / 2, 0));
  721. this.pointList.push(new SPoint(this.width, 0));
  722. this.pointList.push(new SPoint(this.width, this.height / 2));
  723. this.pointList.push(new SPoint(this.width, this.height));
  724. this.pointList.push(new SPoint(this.width / 2, this.height));
  725. this.pointList.push(new SPoint(0, this.height));
  726. this.pointList.push(new SPoint(0, this.height / 2));
  727. }
  728. /**
  729. * 计算点到哪个点
  730. */
  731. getNearestPoint(p: SPoint): void {
  732. let len = this.sceneDis;
  733. this.curIndex = -1;
  734. for (let i = 0; i < this.pointList.length; i++) {
  735. let dis = SMathUtil.pointDistance(
  736. p.x,
  737. p.y,
  738. this.pointList[i].x,
  739. this.pointList[i].y
  740. );
  741. if (dis < len) {
  742. len = dis;
  743. this.curIndex = i;
  744. }
  745. }
  746. }
  747. /**
  748. * Item 对象边界区域
  749. *
  750. * @return 对象边界区域
  751. */
  752. boundingRect(): SRect {
  753. if (this.visible) {
  754. return new SRect(
  755. -this.sceneDis,
  756. -this.sceneDis,
  757. this.width + this.sceneDis + this.sceneDis,
  758. this.height + this.sceneDis + this.sceneDis
  759. );
  760. } else {
  761. return new SRect();
  762. }
  763. }
  764. /**
  765. * 宽高发发生变化
  766. *
  767. * @param oldSize 改之前的大小
  768. * @param newSize 改之后大小
  769. */
  770. protected resize(oldSize: SSize, newSize: SSize): void {
  771. for (let v of this.itemList) {
  772. // @ts-ignore
  773. v.resize && v.resize(oldSize, newSize);
  774. const p = v.mapToScene(this.pos.x, this.pos.y);
  775. const p2 = v.mapFromScene(p.x, p.y);
  776. v.moveTo(p2.x - v.boundingRect().x, p2.y - v.boundingRect().y);
  777. }
  778. }
  779. /**
  780. * 鼠标按下事件
  781. *
  782. * @param event 保存事件参数
  783. * @return 是否处理事件
  784. */
  785. onMouseDown(event: SMouseEvent): boolean {
  786. if (event.buttons == 1) {
  787. this.getNearestPoint(new SPoint(event.x, event.y));
  788. if (this.curIndex < 0) {
  789. this.curPoint = undefined;
  790. return super.onMouseDown(event);
  791. } else {
  792. const cur = this.pointList[this.curIndex];
  793. this.curPoint = new SPoint(cur.x, cur.y);
  794. this.oldWidth = this.width;
  795. this.oldHeight = this.height;
  796. this.grabItem(this);
  797. }
  798. this.update();
  799. return true;
  800. }
  801. return super.onMouseDown(event);
  802. }
  803. /**
  804. * 鼠标按下事件
  805. *
  806. * @param event 保存事件参数
  807. * @return 是否处理事件
  808. */
  809. onMouseMove(event: SMouseEvent): boolean {
  810. this.setCursor(9);
  811. if (
  812. this.curIndex > -1 &&
  813. this.count == 1 &&
  814. this.itemList[0]._resizeable
  815. ) {
  816. this.setCursor(this.curIndex);
  817. // @ts-ignore
  818. const difX = event.x - this.curPoint.x;
  819. // @ts-ignore
  820. const difY = event.y - this.curPoint.y;
  821. const mp = this.toParentChange(difX, difY);
  822. const oldSize = new SSize(this.width, this.height);
  823. let resultX, resultY;
  824. switch (this.curIndex) {
  825. case 0:
  826. resultX = this.width - difX;
  827. resultY = this.height - difY;
  828. if (resultX > this.minWidth && resultX < this.maxWidth) {
  829. this.width = resultX;
  830. this.x = this.x + mp.x;
  831. }
  832. if (resultY > this.minHeight && resultY < this.maxHeight) {
  833. this.height = resultY;
  834. this.y = this.y + mp.y;
  835. }
  836. break;
  837. case 1:
  838. resultY = this.height - difY;
  839. if (resultY > this.minHeight && resultY < this.maxHeight) {
  840. this.height = resultY;
  841. this.y = this.y + mp.y;
  842. }
  843. break;
  844. case 2:
  845. resultX = this.oldWidth + difX;
  846. resultY = this.height - difY;
  847. if (resultX > this.minWidth && resultX < this.maxWidth) {
  848. this.width = resultX;
  849. }
  850. if (resultY > this.minHeight && resultY < this.maxHeight) {
  851. this.height = resultY;
  852. this.y = this.y + mp.y;
  853. }
  854. break;
  855. case 3:
  856. resultX = this.oldWidth + difX;
  857. if (resultX > this.minWidth && resultX < this.maxWidth) {
  858. this.width = resultX;
  859. }
  860. break;
  861. case 4:
  862. resultX = this.oldWidth + difX;
  863. resultY = this.oldHeight + difY;
  864. if (resultX > this.minWidth && resultX < this.maxWidth) {
  865. this.width = resultX;
  866. }
  867. if (resultY > this.minHeight && resultY < this.maxHeight) {
  868. this.height = resultY;
  869. }
  870. break;
  871. case 5:
  872. resultY = this.oldHeight + difY;
  873. if (resultY > this.minHeight && resultY < this.maxHeight) {
  874. this.height = resultY;
  875. }
  876. break;
  877. case 6:
  878. resultX = this.width - difX;
  879. resultY = this.oldHeight + difY;
  880. if (resultX > this.minWidth && resultX < this.maxWidth) {
  881. this.width = resultX;
  882. this.x = this.x + mp.x;
  883. }
  884. if (resultY > this.minHeight && resultY < this.maxHeight) {
  885. this.height = resultY;
  886. }
  887. break;
  888. case 7:
  889. resultX = this.width - difX;
  890. if (resultX > this.minWidth && resultX < this.maxWidth) {
  891. this.width = resultX;
  892. this.x = this.x + mp.x;
  893. }
  894. break;
  895. default:
  896. break;
  897. }
  898. const newSize = new SSize(this.width, this.height);
  899. this.resize(oldSize, newSize);
  900. this.calExtreme();
  901. } else {
  902. let flag = true;
  903. for (let v of this.itemList) {
  904. if (!v.moveable) {
  905. flag = false;
  906. }
  907. }
  908. if (flag) {
  909. super.onMouseMove(event);
  910. this.setCursor(8);
  911. if (this._isMoving) {
  912. const mp = this.toParentChange(
  913. event.x - this._mouseDownPos.x,
  914. event.y - this._mouseDownPos.y
  915. );
  916. this.itemList.forEach((t): void => {
  917. t.moveTo(t.x + mp.x, t.y + mp.y);
  918. });
  919. }
  920. }
  921. }
  922. return true;
  923. }
  924. /**
  925. * 设置鼠标样式
  926. *
  927. * @param n 样式的索引
  928. */
  929. private setCursor(n: number): void {
  930. this.cursor = SGraphSelectContainer.cursorList[n] || "default";
  931. if (this.scene && this.scene.view) {
  932. this.scene.view.cursor = this.cursor;
  933. }
  934. }
  935. /**
  936. * 鼠标按下事件
  937. *
  938. * @param event 保存事件参数
  939. * @return 是否处理事件
  940. */
  941. onMouseUp(event: SMouseEvent): boolean {
  942. this.curIndex = -1;
  943. super.onMouseUp(event);
  944. // this.itemList.forEach(t => {
  945. // t.moveToOrigin(t.x, t.y);
  946. // });
  947. return true;
  948. }
  949. /**
  950. * Item 绘制操作
  951. *
  952. * @param painter 绘制对象
  953. */
  954. onDraw(painter: SPainter): void {
  955. // 缓存场景长度
  956. this.sceneDis = painter.toPx(this.dis);
  957. // 多选时 或者 只选择一个并且是需要显示选择器时 绘制
  958. if (
  959. this.showSelect &&
  960. (this.count > 1 || (this.count == 1 && this.itemList[0].showSelect))
  961. ) {
  962. painter.pen.lineWidth = painter.toPx(1);
  963. painter.pen.color = SColor.White;
  964. painter.brush.color = SColor.Transparent;
  965. painter.drawRect(0, 0, this.width, this.height);
  966. painter.pen.color = this.strokeColor;
  967. painter.pen.lineDash = [painter.toPx(4), painter.toPx(4)];
  968. painter.drawRect(0, 0, this.width, this.height);
  969. const r = painter.toPx(this.radius);
  970. painter.pen.lineDash = [];
  971. this.pointList.forEach((t, i): void => {
  972. if (i == this.curIndex) {
  973. painter.brush.color = this.fillColor;
  974. } else {
  975. painter.brush.color = SColor.White;
  976. }
  977. painter.drawCircle(t.x, t.y, r);
  978. });
  979. }
  980. }
  981. }