1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036 |
- /*
- * *********************************************************************************************************************
- *
- * !!
- * .F88X
- * X8888Y
- * .}888888N;
- * i888888N; .:! .I$WI:
- * R888888I .'N88~ i8}+8Y&8"l8i$8>8W~'>W8}8]KW+8IIN"8&
- * .R888888I .;N8888~ .X8' "8I.!,/8" !%NY8`"8I8~~8>,88I
- * +888888N; .8888888Y "&&8Y.}8,
- * ./888888N; .R888888Y .'}~ .>}'.`+> i}! "i' +/' .'i~ !11,.:">, .~]! .i}i
- * ~888888%: .I888888l .]88~`1/iY88Ii+1'.R$8$8]"888888888> Y8$ W8E X8E W8888'188Il}Y88$*
- * 18888888 E8888881 .]W%8$`R8X'&8%++N8i,8N%N8+l8%` .}8N:.R$RE%N88N%N$K$R 188,FE$8%~Y88I
- * .E888888I .i8888888' .:$8I;88+`E8R:/8N,.>881.`$8E/1/]N8X.Y8N`"KF&&FK!'88*."88K./$88%RN888+~
- * 8888888I .,N888888~ ~88i"8W,!N8*.I88.}888%F,i$88"F88" 888:E8X.>88!i88>`888*.}Fl1]*}1YKi'
- * i888888N' I888Y ]88;/EX*IFKFK88X K8R .l8W 88Y ~88}'88E&%8W.X8N``]88!.$8K .:W8I
- * .i888888N; I8Y .&8$ .X88! i881.:%888>I88 ;88] +88+.';;;;:.Y88X 18N.,88l .+88/
- * .:R888888I
- * .&888888I Copyright (c) 2009-2020. 博锐尚格科技股份有限公司
- * ~8888'
- * .!88~ All rights reserved.
- *
- * *********************************************************************************************************************
- */
- import { SGraphItem } from "./SGraphItem";
- import { SGraphLayoutType } from "./enums/SGraphLayoutType";
- import { SOrderSetType } from "./enums/SOrderSetType";
- import { SColor, SPainter, SPoint, SRect, SSize } from "@persagy-web/draw";
- import { SMathUtil } from "./utils/SMathUtil";
- import { SMouseEvent } from "@persagy-web/base";
- /**
- * 基本选择器
- *
- * @author 郝建龙 <haojianlong@sagacloud.cn>
- */
- export class SGraphSelectContainer extends SGraphItem {
- /** 选择对象 list */
- private _itemList: SGraphItem[] = [];
- get itemList(): SGraphItem[] {
- return this._itemList;
- }
- /** 外接点的 list */
- private pointList: SPoint[] = [];
- /** item 宽 */
- private _width: number = 0;
- get width(): number {
- return this._width;
- }
- set width(v: number) {
- if (v == this._width) {
- return;
- }
- this._width = v;
- this.update();
- }
- /** item 高 */
- private _height: number = 0;
- get height(): number {
- return this._height;
- }
- set height(v: number) {
- if (v == this._height) {
- return;
- }
- this._height = v;
- this.update();
- }
- /** 修改之前的宽 */
- private oldWidth: number = 0;
- /** 修改之前的高 */
- private oldHeight: number = 0;
- /** 最小宽 */
- minWidth: number = 0;
- /** 最大宽 */
- maxWidth: number = Number.MAX_SAFE_INTEGER;
- /** 最小高 */
- minHeight: number = 0;
- /** 最小高 */
- maxHeight: number = Number.MAX_SAFE_INTEGER;
- /** 边框颜色 */
- _strokeColor: SColor = new SColor("#82C7FC");
- /** 画笔颜色 */
- get strokeColor(): SColor {
- return this._strokeColor;
- }
- set strokeColor(v: SColor) {
- this._strokeColor = v;
- this.update();
- }
- /** 填充颜色 */
- _fillColor: SColor = new SColor("#ffffff");
- get fillColor(): SColor {
- return this._fillColor;
- }
- set fillColor(v: SColor) {
- this._fillColor = v;
- this.update();
- }
- /** 统计选中 item 的数量 */
- get count(): number {
- return this.itemList.length;
- }
- /** 置顶 zOrder 增加基数 */
- private radix: number = 0.001;
- /** 定时器 */
- private timer: number | undefined;
- /** 半径 */
- radius: number = 5;
- /** 全局灵敏度 */
- dis: number = 10;
- /** 灵敏度转换为场景长度 */
- private sceneDis: number = 10;
- /** 当前点索引 */
- private _curIndex: number = -1;
- get curIndex(): number {
- return this._curIndex;
- }
- set curIndex(v: number) {
- if (v === this._curIndex) {
- return;
- }
- this._curIndex = v;
- this.update();
- }
- /** 当前点索引 */
- private curPoint: SPoint | undefined;
- /** 鼠标样式 */
- static cursorList: string[] = [
- "nw-resize",
- "n-resize",
- "ne-resize",
- "e-resize",
- "se-resize",
- "s-resize",
- "sw-resize",
- "w-resize",
- "move",
- "default"
- ];
- /**
- * 构造体
- */
- constructor() {
- super();
- this.zOrder = 9999999;
- this.visible = false;
- this.moveable = true;
- }
- /**
- * 添加 item 到 list
- *
- * @param item 当前选中的 item
- */
- addItem(item: SGraphItem): void {
- for (let i = 0; i < this.itemList.length; i++) {
- if (this.itemList[i] == item) {
- return;
- }
- }
- item.selected = true;
- this.itemList.push(item);
- this.throttle(this.selectChange, 80);
- }
- /**
- * 清空再添加(事件+复制数组)
- *
- * @param item 当前选中的 item
- */
- setItem(item: SGraphItem): void {
- this.itemList.forEach((t: SGraphItem): void => {
- t.selected = false;
- });
- this.itemList.length = 0;
- item.selected = true;
- this.itemList.push(item);
- this.throttle(this.selectChange, 80);
- }
- /**
- * 清空选择对象
- */
- clear(): void {
- this.itemList.forEach((t: SGraphItem): void => {
- t.selected = false;
- });
- this.itemList.length = 0;
- this.throttle(this.selectChange, 80);
- }
- /**
- * 切换选择对象
- *
- * @param item 当前选中的 item
- */
- toggleItem(item: SGraphItem): void {
- for (let i = 0; i < this.itemList.length; i++) {
- if (this.itemList[i] == item) {
- this.itemList[i].selected = false;
- this.itemList.splice(i, 1);
- this.throttle(this.selectChange, 80);
- return;
- }
- }
- // 多选时,父级 item 需要一致
- if (this.itemList.length) {
- if (item.parent != this.itemList[0].parent) {
- return;
- }
- }
- item.selected = true;
- this.itemList.push(item);
- this.throttle(this.selectChange, 80);
- }
- /**
- * 对齐
- *
- * @param type 对齐方式
- */
- layout(type: SGraphLayoutType): void {
- if (this.itemList.length < 2) {
- return;
- }
- switch (type) {
- case SGraphLayoutType.Left:
- this.alignLeft();
- break;
- case SGraphLayoutType.Bottom:
- this.alignBottom();
- break;
- case SGraphLayoutType.Center:
- this.alignCenter();
- break;
- case SGraphLayoutType.Horizontal:
- this.alignHorizontal();
- break;
- case SGraphLayoutType.Middle:
- this.alignMiddle();
- break;
- case SGraphLayoutType.Right:
- this.alignRight();
- break;
- case SGraphLayoutType.Top:
- this.alignTop();
- break;
- case SGraphLayoutType.Vertical:
- this.alignVertical();
- break;
- default:
- console.log("对齐类型不存在");
- break;
- }
- }
- /**
- * 图层操作
- *
- * @param type 操作类型
- */
- setOrder(type: SOrderSetType): void {
- if (this.itemList.length < 1) {
- return;
- }
- switch (type) {
- case SOrderSetType.Top:
- this.setTop();
- break;
- case SOrderSetType.Bottom:
- this.setBottom();
- break;
- case SOrderSetType.After:
- this.setAfter();
- break;
- case SOrderSetType.Before:
- this.setBefore();
- break;
- default:
- console.log("图层操作类型不存在");
- break;
- }
- }
- /**
- * 左对齐
- */
- private alignLeft(): void {
- let standardItem = this.itemList[0];
- // 计算第一个外接矩阵左上角坐标在场景中的坐标
- let p = standardItem.mapToScene(
- standardItem.boundingRect().x,
- standardItem.boundingRect().y
- );
- for (let i = 1; i < this.itemList.length; i++) {
- let curItem = this.itemList[i];
- if (curItem.moveable) {
- // 将 p 转换为当前 item 中的坐标
- let p1 = curItem.mapFromScene(p.x, p.y);
- // 根据等式差值相等 newboundx - oldboundx = newposx - oldposx => newposx = newboundx - oldboundx + oldposx
- curItem.x =
- (p1.x - curItem.boundingRect().x) * curItem.inverseScale +
- curItem.x;
- }
- }
- }
- /**
- * 顶对齐
- */
- private alignTop(): void {
- let standardItem = this.itemList[0];
- let p = standardItem.mapToScene(
- standardItem.boundingRect().x,
- standardItem.boundingRect().y
- );
- for (let i = 1; i < this.itemList.length; i++) {
- let curItem = this.itemList[i];
- if (curItem.moveable) {
- let p1 = curItem.mapFromScene(p.x, p.y);
- curItem.y =
- (p1.y - curItem.boundingRect().y) * curItem.inverseScale +
- curItem.y;
- }
- }
- }
- /**
- * 右对齐
- */
- private alignRight(): void {
- let standardItem = this.itemList[0];
- let p = standardItem.mapToScene(
- standardItem.boundingRect().right,
- standardItem.boundingRect().bottom
- );
- for (let i = 1; i < this.itemList.length; i++) {
- let curItem = this.itemList[i];
- if (curItem.moveable) {
- let p1 = curItem.mapFromScene(p.x, p.y);
- curItem.x =
- (p1.x - curItem.boundingRect().right) *
- curItem.inverseScale +
- curItem.x;
- }
- }
- }
- /**
- * 底对齐
- */
- private alignBottom(): void {
- let standardItem = this.itemList[0];
- let p = standardItem.mapToScene(
- standardItem.boundingRect().right,
- standardItem.boundingRect().bottom
- );
- for (let i = 1; i < this.itemList.length; i++) {
- let curItem = this.itemList[i];
- if (curItem.moveable) {
- let p1 = curItem.mapFromScene(p.x, p.y);
- curItem.y =
- (p1.y - curItem.boundingRect().bottom) *
- curItem.inverseScale +
- curItem.y;
- }
- }
- }
- /**
- * 水平居中对齐
- */
- private alignCenter(): void {
- // 第一个选中的 item 即为基准对象
- let standardItem = this.itemList[0];
- // 基准对象的中心点
- let center = standardItem.boundingRect().center();
- // 中心点转换为场景坐标
- let p = standardItem.mapToScene(center.x, center.y);
- for (let i = 1; i < this.itemList.length; i++) {
- let curItem = this.itemList[i];
- if (curItem.moveable) {
- let p1 = curItem.mapFromScene(p.x, p.y);
- // 根据等式差值相等 newcenterx - oldcenterx = newposx - oldposx => newposx = newcenterx - oldcenterx + oldposx
- curItem.x =
- (p1.x - curItem.boundingRect().center().x) *
- curItem.inverseScale +
- curItem.x;
- }
- }
- }
- /**
- * 垂直居中对齐
- */
- private alignMiddle(): void {
- // 第一个选中的 item 即为基准对象
- let standardItem = this.itemList[0];
- // 基准对象的中心点
- let center = standardItem.boundingRect().center();
- // 中心点转换为场景坐标
- let p = standardItem.mapToScene(center.x, center.y);
- for (let i = 1; i < this.itemList.length; i++) {
- let curItem = this.itemList[i];
- if (curItem.moveable) {
- let p1 = curItem.mapFromScene(p.x, p.y);
- curItem.y =
- (p1.y - curItem.boundingRect().center().y) *
- curItem.inverseScale +
- curItem.y;
- }
- }
- }
- /**
- * 垂直分布
- */
- private alignVertical(): void {
- if (this.itemList.length < 3) {
- return;
- }
- for (let i = 0; i < this.itemList.length - 1; i++) {
- for (let j = 0; j < this.itemList.length - 1 - i; j++) {
- let curItemCenter = this.itemList[j].boundingRect().center();
- let nextItemCenter = this.itemList[j + 1]
- .boundingRect()
- .center();
- let curCenterY = this.itemList[j].mapToScene(
- curItemCenter.x,
- curItemCenter.y
- ).y;
- let nextCenterY = this.itemList[j + 1].mapToScene(
- nextItemCenter.x,
- nextItemCenter.y
- ).y;
- if (curCenterY > nextCenterY) {
- let temp = this.itemList[j];
- this.itemList[j] = this.itemList[j + 1];
- this.itemList[j + 1] = temp;
- }
- }
- }
- let top = this.itemList[0];
- let bottom = this.itemList[this.itemList.length - 1];
- let topCenter = top.boundingRect().center();
- let bottomCenter = bottom.boundingRect().center();
- let leftToRight =
- bottom.mapToScene(bottomCenter.x, bottomCenter.y).y -
- top.mapToScene(topCenter.x, topCenter.y).y;
- let average = leftToRight / (this.itemList.length - 1);
- for (let k = 1; k < this.itemList.length - 1; k++) {
- let curItem = this.itemList[k];
- if (curItem.moveable) {
- let p1 = top.mapToScene(
- top.boundingRect().center().x,
- top.boundingRect().center().y
- );
- let p2 = curItem.mapFromScene(p1.x, p1.y + average * k);
- curItem.y =
- (p2.y - curItem.boundingRect().center().y) *
- curItem.inverseScale +
- curItem.y;
- }
- }
- }
- /**
- * 水平分布
- */
- private alignHorizontal(): void {
- if (this.itemList.length < 3) {
- return;
- }
- for (let i = 0; i < this.itemList.length - 1; i++) {
- for (let j = 0; j < this.itemList.length - 1 - i; j++) {
- let curItemCenter = this.itemList[j].boundingRect().center();
- let nextItemCenter = this.itemList[j + 1]
- .boundingRect()
- .center();
- let curCenterX = this.itemList[j].mapToScene(
- curItemCenter.x,
- curItemCenter.y
- ).x;
- let nextCenterX = this.itemList[j + 1].mapToScene(
- nextItemCenter.x,
- nextItemCenter.y
- ).x;
- if (curCenterX > nextCenterX) {
- let temp = this.itemList[j];
- this.itemList[j] = this.itemList[j + 1];
- this.itemList[j + 1] = temp;
- }
- }
- }
- let left = this.itemList[0];
- let right = this.itemList[this.itemList.length - 1];
- let leftCenter = left.boundingRect().center();
- let rightCenter = right.boundingRect().center();
- let leftToRight =
- right.mapToScene(rightCenter.x, rightCenter.y).x -
- left.mapToScene(leftCenter.x, leftCenter.y).x;
- let average = leftToRight / (this.itemList.length - 1);
- for (let k = 1; k < this.itemList.length - 1; k++) {
- let curItem = this.itemList[k];
- if (curItem.moveable) {
- let p1 = left.mapToScene(
- left.boundingRect().center().x,
- left.boundingRect().center().y
- );
- let p2 = curItem.mapFromScene(p1.x + average * k, p1.y);
- // 根据等式 newposx - oldposx = newcenterx - oldcenterx
- curItem.x =
- (p2.x - curItem.boundingRect().center().x) *
- curItem.inverseScale +
- curItem.x;
- }
- }
- }
- // 修改层级操作步骤:
- // 排序(保持相对层级不变)
- // children 里边要取 整数部分相同的 点: children 是已经排好序的
- // 然后再取最大值
- /**
- * 置顶
- */
- private setTop(): void {
- const arr: SGraphItem[] = this.sortItemList(this.itemList, true);
- // 按顺序 zOrder 增加
- arr.forEach((it): void => {
- let max = it.zOrder;
- if (it.parent) {
- it.parent.children.forEach((item): void => {
- if (
- Number(max.toFixed()) == Number(item.zOrder.toFixed())
- ) {
- if (item.zOrder > max) {
- max = item.zOrder;
- }
- }
- });
- it.zOrder = max + this.radix;
- }
- });
- }
- /**
- * 置底
- */
- private setBottom(): void {
- const arr: SGraphItem[] = this.sortItemList(this.itemList, false);
- // 按顺序 zOrder 增加
- arr.forEach((it): void => {
- let min = it.zOrder;
- if (it.parent) {
- it.parent.children.forEach((item): void => {
- if (
- Number(min.toFixed()) == Number(item.zOrder.toFixed())
- ) {
- if (item.zOrder < min) {
- min = item.zOrder;
- }
- }
- });
- it.zOrder = min - this.radix;
- }
- });
- }
- /**
- * 下移一层 zOrder 减小
- */
- private setBefore(): void {
- const arr: SGraphItem[] = this.sortItemList(this.itemList, false);
- arr.forEach((it): void => {
- if (it.parent) {
- const min = it.zOrder;
- let temp = it.parent.children
- .map((child): number | undefined => {
- if (
- Number(child.zOrder.toFixed()) ==
- Number(min.toFixed())
- ) {
- return child.zOrder;
- }
- })
- .filter((c): number | undefined => c);
- temp = [...new Set(temp)];
- // 当前层有多个
- const index = temp.indexOf(it.zOrder);
- if (index <= 1) {
- // 当前 item 索引为1时 将当前 item 放至第一个前边
- // @ts-ignore
- it.zOrder = temp[0] - this.radix;
- } else if (index > 1) {
- // 当前 item 索引大于等于2,将当前 item 放至前两个中间
- // @ts-ignore
- it.zOrder = (temp[index - 1] + temp[index - 2]) / 2;
- }
- }
- });
- }
- /**
- * 上移一层 zOrder 增加
- */
- private setAfter(): void {
- const arr: SGraphItem[] = this.sortItemList(this.itemList, false);
- arr.forEach((it): void => {
- if (it.parent) {
- const max = it.zOrder;
- let temp = it.parent.children
- .map((child): number | undefined => {
- if (
- Number(child.zOrder.toFixed()) ==
- Number(max.toFixed())
- ) {
- return child.zOrder;
- }
- })
- .filter((c): number | undefined => c);
- temp = [...new Set(temp)].reverse();
- // 当前层有多个
- const index = temp.indexOf(it.zOrder);
- if (index <= 1) {
- // 当前 item 索引为1时 将当前 item 放至第一个前边
- // @ts-ignore
- it.zOrder = temp[0] + this.radix;
- } else if (index > 1) {
- // 当前 item 索引大于等于2,将当前 item 放至前两个中间
- // @ts-ignore
- it.zOrder = (temp[index - 1] + temp[index - 2]) / 2;
- }
- }
- });
- }
- /**
- * 数组排序
- *
- * @param arr 排序前的数组
- * @param flag 正序 or 逆序
- * @return 排序后的数组
- */
- sortItemList(arr: SGraphItem[], flag: boolean = true): SGraphItem[] {
- const list: SGraphItem[] = arr.map((t): SGraphItem => t);
- list.sort(this.compare("zOrder", flag));
- return list;
- }
- /**
- * 按照某个属性排序
- *
- * @param prop 属性
- * @param flag 默认正序
- */
- private compare(prop: string, flag: boolean): any {
- const f = flag ? 1 : -1;
- // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
- return (a: any, b: any): number => {
- a = a[prop];
- b = b[prop];
- if (a < b) {
- return f * -1;
- }
- if (a > b) {
- return f * 1;
- }
- return 0;
- };
- }
- /**
- * 防抖-防止快速多次触发 itemChange; 只执行最后一次
- */
- throttle(fn: Function, wait: number): void {
- if (this.timer) clearTimeout(this.timer);
- this.timer = setTimeout((): void => {
- this.selectChange();
- }, wait);
- }
- /**
- * 选中的 item 更新时更新自身边界
- */
- selectChange(): void {
- // 抛出事件
- this.$emit("listChange", this.itemList);
- if (this.count > 0) {
- // 重新计算边界
- this.getSize();
- // 计算边界 8 个点
- this.calExtreme();
- if (
- this.showSelect &&
- (this.count > 1 ||
- (this.count == 1 && this.itemList[0].showSelect))
- ) {
- this.visible = true;
- }
- } else {
- this.visible = false;
- this.curIndex = -1;
- }
- }
- /**
- * 计算选中 item 的外接矩形和
- *
- * @return 外接矩形的和
- */
- calItemBounding(): SRect {
- if (this.itemList.length) {
- const fir = this.itemList[0];
- let rect = fir.boundingRect().adjusted(fir.pos.x, fir.pos.y, 0, 0);
- for (let i = 1; i < this.itemList.length; i++) {
- const cur = this.itemList[i];
- rect = rect.unioned(
- cur.boundingRect().adjusted(cur.pos.x, cur.pos.y, 0, 0)
- );
- }
- return rect;
- } else {
- return new SRect();
- }
- }
- /**
- * 根据矩阵得到宽高,并将自己移动至矩阵左上角
- */
- getSize(): void {
- const r = this.calItemBounding();
- this.width = r.width;
- this.height = r.height;
- this.moveTo(r.left, r.top);
- }
- /**
- * 计算外接矩形 8 个端点
- */
- calExtreme(): void {
- this.pointList = [];
- this.pointList.push(new SPoint(0, 0));
- this.pointList.push(new SPoint(this.width / 2, 0));
- this.pointList.push(new SPoint(this.width, 0));
- this.pointList.push(new SPoint(this.width, this.height / 2));
- this.pointList.push(new SPoint(this.width, this.height));
- this.pointList.push(new SPoint(this.width / 2, this.height));
- this.pointList.push(new SPoint(0, this.height));
- this.pointList.push(new SPoint(0, this.height / 2));
- }
- /**
- * 计算点到哪个点
- */
- getNearestPoint(p: SPoint): void {
- let len = this.sceneDis;
- this.curIndex = -1;
- for (let i = 0; i < this.pointList.length; i++) {
- let dis = SMathUtil.pointDistance(
- p.x,
- p.y,
- this.pointList[i].x,
- this.pointList[i].y
- );
- if (dis < len) {
- len = dis;
- this.curIndex = i;
- }
- }
- }
- /**
- * Item 对象边界区域
- *
- * @return 对象边界区域
- */
- boundingRect(): SRect {
- if (this.visible) {
- return new SRect(
- -this.sceneDis,
- -this.sceneDis,
- this.width + this.sceneDis + this.sceneDis,
- this.height + this.sceneDis + this.sceneDis
- );
- } else {
- return new SRect();
- }
- }
- /**
- * 宽高发发生变化
- *
- * @param oldSize 改之前的大小
- * @param newSize 改之后大小
- */
- protected resize(oldSize: SSize, newSize: SSize): void {
- for (let v of this.itemList) {
- // @ts-ignore
- v.resize && v.resize(oldSize, newSize);
- const p = v.mapToScene(this.pos.x, this.pos.y);
- const p2 = v.mapFromScene(p.x, p.y);
- v.moveTo(p2.x - v.boundingRect().x, p2.y - v.boundingRect().y);
- }
- }
- /**
- * 鼠标按下事件
- *
- * @param event 保存事件参数
- * @return 是否处理事件
- */
- onMouseDown(event: SMouseEvent): boolean {
- if (event.buttons == 1) {
- this.getNearestPoint(new SPoint(event.x, event.y));
- if (this.curIndex < 0) {
- this.curPoint = undefined;
- return super.onMouseDown(event);
- } else {
- const cur = this.pointList[this.curIndex];
- this.curPoint = new SPoint(cur.x, cur.y);
- this.oldWidth = this.width;
- this.oldHeight = this.height;
- this.grabItem(this);
- }
- this.update();
- return true;
- }
- return super.onMouseDown(event);
- }
- /**
- * 鼠标按下事件
- *
- * @param event 保存事件参数
- * @return 是否处理事件
- */
- onMouseMove(event: SMouseEvent): boolean {
- this.setCursor(9);
- if (
- this.curIndex > -1 &&
- this.count == 1 &&
- this.itemList[0]._resizeable
- ) {
- this.setCursor(this.curIndex);
- // @ts-ignore
- const difX = event.x - this.curPoint.x;
- // @ts-ignore
- const difY = event.y - this.curPoint.y;
- const mp = this.toParentChange(difX, difY);
- const oldSize = new SSize(this.width, this.height);
- let resultX, resultY;
- switch (this.curIndex) {
- case 0:
- resultX = this.width - difX;
- resultY = this.height - difY;
- if (resultX > this.minWidth && resultX < this.maxWidth) {
- this.width = resultX;
- this.x = this.x + mp.x;
- }
- if (resultY > this.minHeight && resultY < this.maxHeight) {
- this.height = resultY;
- this.y = this.y + mp.y;
- }
- break;
- case 1:
- resultY = this.height - difY;
- if (resultY > this.minHeight && resultY < this.maxHeight) {
- this.height = resultY;
- this.y = this.y + mp.y;
- }
- break;
- case 2:
- resultX = this.oldWidth + difX;
- resultY = this.height - difY;
- if (resultX > this.minWidth && resultX < this.maxWidth) {
- this.width = resultX;
- }
- if (resultY > this.minHeight && resultY < this.maxHeight) {
- this.height = resultY;
- this.y = this.y + mp.y;
- }
- break;
- case 3:
- resultX = this.oldWidth + difX;
- if (resultX > this.minWidth && resultX < this.maxWidth) {
- this.width = resultX;
- }
- break;
- case 4:
- resultX = this.oldWidth + difX;
- resultY = this.oldHeight + difY;
- if (resultX > this.minWidth && resultX < this.maxWidth) {
- this.width = resultX;
- }
- if (resultY > this.minHeight && resultY < this.maxHeight) {
- this.height = resultY;
- }
- break;
- case 5:
- resultY = this.oldHeight + difY;
- if (resultY > this.minHeight && resultY < this.maxHeight) {
- this.height = resultY;
- }
- break;
- case 6:
- resultX = this.width - difX;
- resultY = this.oldHeight + difY;
- if (resultX > this.minWidth && resultX < this.maxWidth) {
- this.width = resultX;
- this.x = this.x + mp.x;
- }
- if (resultY > this.minHeight && resultY < this.maxHeight) {
- this.height = resultY;
- }
- break;
- case 7:
- resultX = this.width - difX;
- if (resultX > this.minWidth && resultX < this.maxWidth) {
- this.width = resultX;
- this.x = this.x + mp.x;
- }
- break;
- default:
- break;
- }
- const newSize = new SSize(this.width, this.height);
- this.resize(oldSize, newSize);
- this.calExtreme();
- } else {
- let flag = true;
- for (let v of this.itemList) {
- if (!v.moveable) {
- flag = false;
- }
- }
- if (flag) {
- super.onMouseMove(event);
- this.setCursor(8);
- if (this._isMoving) {
- const mp = this.toParentChange(
- event.x - this._mouseDownPos.x,
- event.y - this._mouseDownPos.y
- );
- this.itemList.forEach((t): void => {
- t.moveTo(t.x + mp.x, t.y + mp.y);
- });
- }
- }
- }
- return true;
- }
- /**
- * 设置鼠标样式
- *
- * @param n 样式的索引
- */
- private setCursor(n: number): void {
- this.cursor = SGraphSelectContainer.cursorList[n] || "default";
- if (this.scene && this.scene.view) {
- this.scene.view.cursor = this.cursor;
- }
- }
- /**
- * 鼠标按下事件
- *
- * @param event 保存事件参数
- * @return 是否处理事件
- */
- onMouseUp(event: SMouseEvent): boolean {
- this.curIndex = -1;
- super.onMouseUp(event);
- // this.itemList.forEach(t => {
- // t.moveToOrigin(t.x, t.y);
- // });
- return true;
- }
- /**
- * Item 绘制操作
- *
- * @param painter 绘制对象
- */
- onDraw(painter: SPainter): void {
- // 缓存场景长度
- this.sceneDis = painter.toPx(this.dis);
- // 多选时 或者 只选择一个并且是需要显示选择器时 绘制
- if (
- this.showSelect &&
- (this.count > 1 || (this.count == 1 && this.itemList[0].showSelect))
- ) {
- painter.pen.lineWidth = painter.toPx(1);
- painter.pen.color = SColor.White;
- painter.brush.color = SColor.Transparent;
- painter.drawRect(0, 0, this.width, this.height);
- painter.pen.color = this.strokeColor;
- painter.pen.lineDash = [painter.toPx(4), painter.toPx(4)];
- painter.drawRect(0, 0, this.width, this.height);
- const r = painter.toPx(this.radius);
- painter.pen.lineDash = [];
- this.pointList.forEach((t, i): void => {
- if (i == this.curIndex) {
- painter.brush.color = this.fillColor;
- } else {
- painter.brush.color = SColor.White;
- }
- painter.drawCircle(t.x, t.y, r);
- });
- }
- }
- }
|