2019-11-29 12:51:58 +08:00
|
|
|
import {createTransform, createBoundingRect} from './utils/parser';
|
|
|
|
|
|
|
|
class TransformHandle {
|
|
|
|
constructor(painter) {
|
|
|
|
this.$painter = painter;
|
|
|
|
|
2021-02-05 15:12:57 +08:00
|
|
|
this.foldLines = [];
|
2021-01-25 17:21:24 +08:00
|
|
|
|
2019-11-29 12:51:58 +08:00
|
|
|
this.parentLevel = painter.getParentLevel();
|
|
|
|
|
|
|
|
this.rect = { x: 0, y: 0, width: 0, height: 0 };
|
|
|
|
|
|
|
|
this.transform = createTransform({ scaleRate: 1, offsetX: 0, offsetY: 0 });
|
|
|
|
}
|
|
|
|
|
|
|
|
checkVisible(view) {
|
2020-11-24 16:53:13 +08:00
|
|
|
// console.log(view);
|
2019-12-05 10:25:07 +08:00
|
|
|
return createBoundingRect(view).intersect(this.rect);
|
2019-11-29 12:51:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
revisibleView(view) {
|
|
|
|
if (this.checkVisible(view)) {
|
|
|
|
view.show();
|
|
|
|
} else {
|
|
|
|
view.hide();
|
|
|
|
}
|
|
|
|
view.dirty();
|
|
|
|
}
|
2021-02-05 15:12:57 +08:00
|
|
|
setFoldLines(lines) {
|
|
|
|
this.foldLines = lines;
|
2021-01-25 17:21:24 +08:00
|
|
|
}
|
2019-11-29 12:51:58 +08:00
|
|
|
// 视图进行缩放/平移
|
|
|
|
transformView(view) {
|
|
|
|
if (view) {
|
|
|
|
view.transform = this.transform;
|
2021-02-05 15:12:57 +08:00
|
|
|
if (this.foldLines && this.foldLines.length) {
|
|
|
|
this.handleFoldShow(view);
|
2021-01-25 17:21:24 +08:00
|
|
|
}
|
2019-11-29 12:51:58 +08:00
|
|
|
view.decomposeTransform();
|
|
|
|
this.revisibleView(view);
|
|
|
|
}
|
|
|
|
}
|
2021-02-05 15:12:57 +08:00
|
|
|
handleFoldShow(view) {
|
|
|
|
if (view.model && view.model.instance && !(view.model._type === 'Line' && view.model.type === '03')) {
|
|
|
|
const anchorPoint = view.model.instance.getAnchorPoint();
|
|
|
|
if (!anchorPoint) { return; }
|
|
|
|
this.foldLines.forEach(item => {
|
|
|
|
const conditionsList = [];
|
|
|
|
let index = 0; let minIndex = 0; let maxIndex = 0; let minValue; let maxValue;
|
|
|
|
for (let i = 0; i < item.points.length - 1; i++) {
|
|
|
|
if (item.points[i].y === item.points[i + 1].y) {
|
|
|
|
continue;
|
|
|
|
} else if (item.points[i].x === item.points[i + 1].x) {
|
|
|
|
const condition = {
|
|
|
|
minValue: Math.min(item.points[i].y, item.points[i + 1].y),
|
|
|
|
maxValue: Math.max(item.points[i].y, item.points[i + 1].y),
|
|
|
|
getStandardX: () => { return item.points[i].x; }
|
|
|
|
};
|
|
|
|
conditionsList.push(condition);
|
|
|
|
if ((minValue && minValue > condition.minValue) || !minValue) {
|
|
|
|
minValue = condition.minValue;
|
|
|
|
minIndex = conditionsList.length;
|
|
|
|
}
|
|
|
|
if ((maxValue && maxValue < condition.maxValue) || !maxValue) {
|
|
|
|
maxValue = condition.maxValue;
|
|
|
|
maxIndex = conditionsList.length;
|
|
|
|
}
|
|
|
|
if ((anchorPoint.y >= condition.minValue && anchorPoint.y <= condition.maxValue)) {
|
|
|
|
index = conditionsList.length;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const condition = {
|
|
|
|
minValue: Math.min(item.points[i].y, item.points[i + 1].y),
|
|
|
|
maxValue: Math.max(item.points[i].y, item.points[i + 1].y),
|
|
|
|
getStandardX: (val) => {
|
|
|
|
const m = (item.points[i].x - item.points[i + 1].x) / (item.points[i].y - item.points[i + 1].y);
|
|
|
|
const n = item.points[i].x - (item.points[i].y * m);
|
|
|
|
return val * m + n;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
conditionsList.push(condition);
|
|
|
|
if ((anchorPoint.y >= condition.minValue && anchorPoint.y <= condition.maxValue)) {
|
|
|
|
index = conditionsList.length;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (index) {
|
|
|
|
const standardX = conditionsList[index - 1].getStandardX(anchorPoint.y);
|
|
|
|
anchorPoint.x >= standardX && this.handleFoldViewTransform(view, item);
|
|
|
|
} else if ( anchorPoint.y > maxValue) {
|
|
|
|
const standardX = conditionsList[maxIndex - 1].getStandardX(anchorPoint.y);
|
|
|
|
anchorPoint.x >= standardX && this.handleFoldViewTransform(view, item);
|
|
|
|
} else if (anchorPoint.y < minValue) {
|
|
|
|
const standardX = conditionsList[minIndex - 1].getStandardX(anchorPoint.y);
|
|
|
|
anchorPoint.x >= standardX && this.handleFoldViewTransform(view, item);
|
|
|
|
}
|
2021-01-25 17:21:24 +08:00
|
|
|
});
|
|
|
|
}
|
2021-02-05 15:12:57 +08:00
|
|
|
|
|
|
|
}
|
|
|
|
handleFoldViewTransform(view, line) {
|
|
|
|
view.transform = createTransform({
|
|
|
|
scaleRate: this.$painter.$jmap.$options.scaleRate,
|
|
|
|
offsetX: this.$painter.$jmap.$options.offsetX + line.offsetX * this.$painter.$jmap.$options.scaleRate,
|
|
|
|
offsetY: this.$painter.$jmap.$options.offsetY - line.offsetY * this.$painter.$jmap.$options.scaleRate
|
|
|
|
});
|
2021-01-25 17:21:24 +08:00
|
|
|
}
|
2019-11-29 12:51:58 +08:00
|
|
|
// 处理所有视图缩放/平移
|
|
|
|
transformAll() {
|
|
|
|
this.traverse(this.transformView, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 重新计算显示图形
|
|
|
|
revisibleAll() {
|
|
|
|
this.traverse(this.revisibleView, this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新偏移量
|
|
|
|
updateTransform(opts) {
|
|
|
|
this.transform = createTransform(opts);
|
|
|
|
this.transformAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 更新画布尺寸
|
|
|
|
updateZrSize(opts) {
|
|
|
|
this.rect = { x: 0, y: 0, width: opts.width, height: opts.height };
|
2020-11-24 16:53:13 +08:00
|
|
|
if (opts.isUpdate) { this.revisibleAll(); }
|
2019-11-29 12:51:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 遍历group执行回调
|
|
|
|
traverse(cb, context) {
|
|
|
|
this.parentLevel.eachChild(level => {
|
|
|
|
level.eachChild((view) => {
|
|
|
|
cb.call(context, view);
|
|
|
|
}, context);
|
|
|
|
}, context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default TransformHandle;
|