141 lines
5.5 KiB
JavaScript
141 lines
5.5 KiB
JavaScript
import {createTransform, createBoundingRect} from './utils/parser';
|
|
|
|
class TransformHandle {
|
|
constructor(painter) {
|
|
this.$painter = painter;
|
|
|
|
this.foldLines = [];
|
|
|
|
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) {
|
|
// console.log(view);
|
|
return createBoundingRect(view).intersect(this.rect);
|
|
}
|
|
|
|
revisibleView(view) {
|
|
if (this.checkVisible(view)) {
|
|
view.show();
|
|
} else {
|
|
view.hide();
|
|
}
|
|
view.dirty();
|
|
}
|
|
setFoldLines(lines) {
|
|
this.foldLines = lines;
|
|
}
|
|
// 视图进行缩放/平移
|
|
transformView(view) {
|
|
if (view) {
|
|
view.transform = this.transform;
|
|
if (this.foldLines && this.foldLines.length) {
|
|
this.handleFoldShow(view);
|
|
}
|
|
view.decomposeTransform();
|
|
this.revisibleView(view);
|
|
}
|
|
}
|
|
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);
|
|
}
|
|
});
|
|
}
|
|
|
|
}
|
|
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
|
|
});
|
|
}
|
|
// 处理所有视图缩放/平移
|
|
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 };
|
|
if (opts.isUpdate) { this.revisibleAll(); }
|
|
}
|
|
|
|
// 遍历group执行回调
|
|
traverse(cb, context) {
|
|
this.parentLevel.eachChild(level => {
|
|
level.eachChild((view) => {
|
|
cb.call(context, view);
|
|
}, context);
|
|
}, context);
|
|
}
|
|
}
|
|
|
|
export default TransformHandle;
|