import {createTransform1, createBoundingRect} from './utils/parser'; class TransformHandle { constructor(painter) { this.$painter = painter; this.scale = ''; this.scaleIndex = 0; this.scaleList = [ 0.2, 0.4, 0.6, 0.8, 0.9, 1, 1.2, 1.4, 1.6, 1.8, 2, 2.2, 2.4, 2.6, 2.8, 3, 3.2, 3.4, 3.6, 3.8, 4, 4.2, 4.4, 4.6, 4.8, 5, 5.2, 5.4, 5.6, 5.8, 6, 6.2, 6.4, 6.6, 6.8, 7, 7.2, 7.4, 7.6, 7.8, 8 ]; this.parentLevel = painter.getParentLevel(); this.rect = { x: 0, y: 0, width: 0, height: 0 }; this.rectList = []; this.transformInit = []; this.transform = [createTransform1({ scaleRateX: 1, scaleRateY: 1, offsetX: 0, offsetY: 0 })]; } revisibleView(view) { view.show(); view.dirty(); } checkVisible(view, rect) { // return createBoundingRect(view).intersect(this.rect); // 判断是否相交 // return createBoundingRect(view).intersect(rect); // 判断是否相交 const rectCopy = createBoundingRect(view); const x = rectCopy.x + rectCopy.width; if (x <= rect.width) { return true; } else { return false; } } // 视图进行初始化 transformView(view) { if (view) { for (let i = 0; i < this.transform.length; i++) { const rect = this.rectList[i]; if (this.checkVisible(view, rect)) { view.transform = this.transform[i]; view.transformIndex = i; view.decomposeTransform(); // 修改 transform 后同步位置 if (view.screenShow) { view.screenShow(); } view.show(); view.dirty(); // 更新 return; } } } } // 视图进行平移 transformView1(view) { if (view) { view.transform = this.transform[view.transformIndex]; view.decomposeTransform(); if (view.screenShow) { view.screenShow(); } view.show(); view.dirty(); } } // 处理所有视图缩放/平移 transformAll1() { this.traverse(this.transformView1, this); } // 处理所有视图缩放/平移 transformAll() { this.traverse(this.transformView, this); } // 重新计算显示图形 revisibleAll() { this.traverse(this.transformView, this); } // 更新偏移量 updateTransform(list, rectList) { this.rectList = rectList; // this.scale = list[0].scaleRateX; this.transform = []; list.forEach(item => { this.transform.push(createTransform1(item)); }); this.transformInit = JSON.parse(JSON.stringify(this.transform)); this.transformAll(); } updataOffset(opt) { if (opt.scale) { if (opt.scale <= 0) { this.scaleIndex = this.scaleIndex <= 0 ? 0 : this.scaleIndex - 1; } else { this.scaleIndex = this.scaleIndex >= 40 ? 40 : this.scaleIndex + 1; } const {screenList, rectList} = this.$painter.updateScreenZoom(this.scaleList[this.scaleIndex]); this.rectList = rectList; this.transform = []; screenList.forEach(item => { this.transform.push(createTransform1(item)); }); } else { this.transform.forEach(item => { item[4] = item[4] + opt.dx; item[5] = item[5] + opt.dy; }); } this.transformAll1(); } updataOffsetNum(opts) { this.transform = JSON.parse(JSON.stringify(this.transformInit)); this.transform.forEach(item => { item[4] = item[4] - opts.dx; item[5] = item[5] - opts.dy; }); this.transformAll1(); } removeType(type) { this.parentLevel.eachChild(level => { level.eachChild((view) => { if (view) { view.transform = this.transform[view.transformIndex]; view.decomposeTransform(); if (view._type == type) { view.hide(); } else { if (view.screenShow) { view.screenShow(); } view.show(); } view.dirty(); } }); }); } // 更新画布尺寸 updateZrSize(opts) { this.rect = { x: 0, y: 0, width: opts.width, height: opts.height }; this.revisibleAll(); } // 遍历group执行回调 traverse(cb, context) { this.parentLevel.eachChild(level => { level.eachChild((view) => { cb.call(context, view); }, context); }, context); } } export default TransformHandle;