修改代码逻辑

This commit is contained in:
ival 2021-04-02 18:02:06 +08:00
parent cb9aa77312
commit 0c87981d43
12 changed files with 72 additions and 86 deletions

View File

@ -1,11 +1,12 @@
import * as graphic from '../core/graphic'; import * as graphic from '../core/graphic';
import Group from 'zrender/src/container/Group';
import shapeRender from '../constant/shapeRender'; import shapeRender from '../constant/shapeRender';
function shapeStyleBuilder(model) { function shapeStyleBuilder(model) {
return { return {
subType: '__hover__', subType: '__hover__',
...shapeRender, ...shapeRender,
...model, code: model.code,
z: 9998, z: 9998,
cursor: 'pointer', cursor: 'pointer',
shape: { shape: {
@ -23,12 +24,13 @@ function shapeStyleBuilder(model) {
} }
// 图形抽象层 // 图形抽象层
class AbstractShape { class AbstractShape extends Group {
constructor({model, shapeType, shapeFactory}) { constructor({model, shapeType, shapeFactory}) {
super({...shapeRender, code: model.code, type: model.type});
this.model = model; this.model = model;
this.shapeType = shapeType; this.shapeType = shapeType;
this.shapeFactory = shapeFactory; this.shapeFactory = shapeFactory;
this.hightLightInstance = new graphic.Rect(shapeStyleBuilder(this.model)); this.instanceHightLight = new graphic.Rect(shapeStyleBuilder(this.model));
} }
// 拖动 // 拖动
@ -53,15 +55,9 @@ class AbstractShape {
// 解除绑定 // 解除绑定
uncouple() {} uncouple() {}
// 获取包围框
getBoundingRect() {}
// 获取依赖图形 // 获取依赖图形
getDepShapes() {} getDepShapes() {}
// 修改属性
attr() {}
// 设置状态 // 设置状态
setState(state) {} setState(state) {}
} }

View File

@ -97,7 +97,7 @@ export default class ImageDraggable extends Group {
this.handle.e.target && this.handle.e.target &&
this.offset) { this.offset) {
if (!eventTool.isMiddleOrRightButtonOnMouseUpDown(e)) { if (!eventTool.isMiddleOrRightButtonOnMouseUpDown(e)) {
const type = this.handle.e.target.model.type; const type = this.handle.e.target.type;
const dx = Math.round(e.offsetX-this.offset.x); const dx = Math.round(e.offsetX-this.offset.x);
const dy = Math.round(e.offsetY-this.offset.y); const dy = Math.round(e.offsetY-this.offset.y);
@ -113,7 +113,7 @@ export default class ImageDraggable extends Group {
if (this.target && if (this.target &&
this.handle.e.target && this.handle.e.target &&
this.offset) { this.offset) {
const type = this.handle.e.target.model.type; const type = this.handle.e.target.type;
const dx = Math.round(e.offsetX-this.offset.x); const dx = Math.round(e.offsetX-this.offset.x);
const dy = Math.round(e.offsetY-this.offset.y); const dy = Math.round(e.offsetY-this.offset.y);
this.setShape(...this.handle.normalizedDiff(dx, dy)); this.setShape(...this.handle.normalizedDiff(dx, dy));

View File

@ -14,15 +14,14 @@ class Compose extends AbstractShape {
create() { create() {
this.instance = new Group({ this.instance = new Group({
...shapeRender, ...shapeRender,
...this.option,
...this.model ...this.model
}); });
this.model.elementCodes.forEach(code => { this.model.elementCodes.forEach(code => {
const el = this.shapeFactory.getShapeByCode(code); const el = this.shapeFactory.getShapeByCode(code);
if (el.instance) { if (el) {
this.shapeFactory.removeFromLayer(el.model.type, el.instance); this.shapeFactory.removeFromLayer(el.type, el);
this.instance.add(el.instance); this.instance.add(el);
} }
}) })
this.instance.scale = this.model.scale; this.instance.scale = this.model.scale;
@ -30,6 +29,7 @@ class Compose extends AbstractShape {
this.instance.position = this.model.position; this.instance.position = this.model.position;
this.instance.origin = utils.createOrigin(this.instance); this.instance.origin = utils.createOrigin(this.instance);
this.instance.transform = utils.createTransform({scale: this.model.scale, position: this.model.position, rotation: this.model.rotation}); this.instance.transform = utils.createTransform({scale: this.model.scale, position: this.model.position, rotation: this.model.rotation});
this.add(this.instance);
} }
// 拖动 // 拖动
@ -67,8 +67,8 @@ class Compose extends AbstractShape {
const el = this.shapeFactory.getShapeByCode(code); const el = this.shapeFactory.getShapeByCode(code);
if (el && if (el &&
el.model) { el.model) {
this.shapeFactory.removeFromLayer(el.model.type, el.instance); this.shapeFactory.removeFromLayer(el.type, el);
this.instance.add(el.instance); this.instance.add(el);
el.model.composeCode = this.model.code; el.model.composeCode = this.model.code;
el.attr(el.model); el.attr(el.model);
} }
@ -84,8 +84,8 @@ class Compose extends AbstractShape {
if (el && if (el &&
el.model && el.model &&
el.model.composeCode) { el.model.composeCode) {
this.instance.remove(el.instance); this.instance.remove(el);
this.shapeFactory.addToLayer(el.model.type, el.instance); this.shapeFactory.addToLayer(el.type, el);
el.model.composeCode = ''; el.model.composeCode = '';
el.attr(el.model); el.attr(el.model);
} }
@ -102,11 +102,6 @@ class Compose extends AbstractShape {
getDepShapes() { getDepShapes() {
return this.model.elementCodes.reduce((shapes, code) => shapes.concat(this.shapeFactory.getShapeByCode(code).getDepShapes()), [this]); return this.model.elementCodes.reduce((shapes, code) => shapes.concat(this.shapeFactory.getShapeByCode(code).getDepShapes()), [this]);
} }
// 获取包围框
getBoundingRect() {
return utils.createBoundingRect(this.instance);
}
} }
export default Compose; export default Compose;

View File

@ -32,7 +32,6 @@ class Element extends AbstractShape {
this.instance = new elementBuilder({ this.instance = new elementBuilder({
...shapeRender, ...shapeRender,
...this.option,
...this.model, ...this.model,
onmouseover, onmouseover,
onmousemove, onmousemove,
@ -44,6 +43,7 @@ class Element extends AbstractShape {
this.instance.position = this.model.position; this.instance.position = this.model.position;
this.instance.origin = utils.createOrigin(this.instance); this.instance.origin = utils.createOrigin(this.instance);
this.instance.transform = utils.createTransform({scale: this.model.scale, position: this.model.position, rotation: this.model.rotation}); this.instance.transform = utils.createTransform({scale: this.model.scale, position: this.model.position, rotation: this.model.rotation});
this.add(this.instance);
} }
} }
@ -85,6 +85,8 @@ class Element extends AbstractShape {
const index = compose.model.elementCodes.findIndex(this.model.code); const index = compose.model.elementCodes.findIndex(this.model.code);
if (index < 0) { if (index < 0) {
compose.model.elementCodes.push(this.model.code); compose.model.elementCodes.push(this.model.code);
this.shapeFactory.removeFormLayer(el.type, this);
compose.add(this);
} }
} }
} }
@ -100,6 +102,8 @@ class Element extends AbstractShape {
const index = compose.model.elementCodes.findIndex(this.model.code); const index = compose.model.elementCodes.findIndex(this.model.code);
if (index >= 0) { if (index >= 0) {
compose.model.elementCodes.splice(index, 1); compose.model.elementCodes.splice(index, 1);
compose.remove(this);
this.shapeFactory.addToLayer(el.type, this);
} }
} }
} }
@ -113,12 +117,6 @@ class Element extends AbstractShape {
getDepShapes() { getDepShapes() {
return [this]; return [this];
} }
// 获取包围框
getBoundingRect() {
return utils.createBoundingRect(this.instance);
}
} }
export default Element; export default Element;

View File

@ -130,17 +130,18 @@ class ShapeFactory extends Eventful {
} }
storageShape(shape) { storageShape(shape) {
if (shape && shape.model) { console.log(shape);
this.mapShape[shape.model.code] = shape; if (shape && shape.code) {
this.mapShape[shape.code] = shape;
} }
return shape; return shape;
} }
removeShape(shape) { removeShape(shape) {
if (shape && shape.model) { if (shape && shape.code) {
this.hideHightLight(shape); this.hideHightLight(shape);
shape.uncouple(shape); shape.uncouple(shape);
delete this.mapShape[shape.model.code]; delete this.mapShape[shape.code];
} }
return shape; return shape;
} }
@ -155,9 +156,9 @@ class ShapeFactory extends Eventful {
showHightLight(shape) { showHightLight(shape) {
const target = this.$controller.getTarget(); const target = this.$controller.getTarget();
if (shape.hightLightInstance) { if (shape.instanceHightLight) {
shape.hightLightInstance.setShape(shape.instance.getBoundingRect()); shape.instanceHightLight.setShape(shape.getBoundingRect())
this.$painter.addToLayer(shapeLayer.HightLight)(shape.hightLightInstance); this.$painter.addToLayer(shapeLayer.HightLight)(shape.instanceHightLight);
} }
if (target == shape) { if (target == shape) {
@ -171,8 +172,8 @@ class ShapeFactory extends Eventful {
hideHightLight(shape) { hideHightLight(shape) {
const target = this.$controller.getTarget(); const target = this.$controller.getTarget();
if (shape.hightLightInstance) { if (shape.instanceHightLight) {
this.$painter.removeFromLayer(shapeLayer.HightLight)(shape.hightLightInstance); this.$painter.removeFromLayer(shapeLayer.HightLight)(shape.instanceHightLight);
} }
if (target != shape) { if (target != shape) {

View File

@ -147,10 +147,9 @@ class JMap {
render(list=[]) { render(list=[]) {
list.forEach(({model, action}) => { list.forEach(({model, action}) => {
const updateModel = this.isDrawing() ? this.$shapeFactory.updateSource(model, action): model; let updateModel = this.isDrawing() ? this.$shapeFactory.updateSource(model, action): model;
const shape = this.$shapeFactory.getShapeByCode(updateModel.code); let curShape = this.$shapeFactory.getShapeByCode(updateModel.code);
const deps = shape.getDepShapes(); let deps = null;
let oldShape = null let oldShape = null
let newShape = null; let newShape = null;
@ -163,21 +162,26 @@ class JMap {
this.$painter.add(newShape); this.$painter.add(newShape);
break; break;
case orders.DELETE: case orders.DELETE:
oldShape = this.$shapeFactory.removeShape(shape); deps = curShape.getDepShapes();
deps.forEach(shape => { deps.forEach(el => {
this.$shapeFactory.updateSource(shape.model, {...action, shapeType: shape.option.shapeType}); updateModel = this.isDrawing() ? this.$shapeFactory.updateSource(el, {...action, shapeType: el.shapeType}): el;
this.$painter.remove(shape); if (updateModel) {
curShape = this.$shapeFactory.getShapeByCode(updateModel.code);
oldShape = this.$shapeFactory.removeShape(curShape);
this.$painter.remove(oldShape);
}
}); });
break; break;
case orders.UPDATE: case orders.UPDATE:
oldShape = this.$shapeFactory.removeShape(shape); updateModel = this.isDrawing() ? this.$shapeFactory.updateSource(model, action): model;
oldShape = this.$shapeFactory.removeShape(curShape);
newShape = this.$shapeFactory.createShape(updateModel, action.shapeType); newShape = this.$shapeFactory.createShape(updateModel, action.shapeType);
this.$painter.remove(oldShape); this.$painter.remove(oldShape);
this.$shapeFactory.storageShape(newShape) this.$shapeFactory.storageShape(newShape)
this.$painter.add(newShape); this.$painter.add(newShape);
break; break;
case orders.UNBINDING: case orders.UNBINDING:
oldShape = this.$shapeFactory.removeShape(shape); oldShape = this.$shapeFactory.removeShape(curShape);
this.$painter.remove(oldShape); this.$painter.remove(oldShape);
break; break;
} }

View File

@ -37,7 +37,6 @@ class Painter extends Group {
Object.keys(graphic).forEach(key => { Object.keys(graphic).forEach(key => {
this.mapShapeLayer[key] = new Group({ this.mapShapeLayer[key] = new Group({
name: `__${key}__` name: `__${key}__`
// matrix.identity(matrix.create())
}); });
}) })
@ -48,19 +47,19 @@ class Painter extends Group {
} }
add(shape) { add(shape) {
if (shape && shape.instance) { if (shape) {
this.addToLayer(shape.model.type)(shape.instance); this.addToLayer(shape.type)(shape);
} }
} }
remove(shape) { remove(shape) {
if (shape && shape.instance) { if (shape) {
this.removeFromLayer(shape.model.type)(shape.instance); this.removeFromLayer(shape.type)(shape);
} }
} }
update(shape) { update(shape) {
if (shape && shape.instance) { if (shape) {
// //
} }
} }

View File

@ -17,7 +17,7 @@ export default class SelectHandle {
if (e.target) { if (e.target) {
this.e = {...e}; this.e = {...e};
if (['Control'].includes(this.$controller.getKeyStr())) { if (['Control'].includes(this.$controller.getKeyStr())) {
if (this.$controller.isSelected(e.target.model.code)) { if (this.$controller.isSelected(e.target.code)) {
this.$controller.setTarget(null); this.$controller.setTarget(null);
this.delSelected(e.target); this.delSelected(e.target);
} else { } else {
@ -31,13 +31,13 @@ export default class SelectHandle {
} }
addSelected(target) { addSelected(target) {
this.$controller.storage.set(target.model.code, target); this.$controller.storage.set(target.code, target);
target.shapeFactory.showHightLight(target); target.shapeFactory.showHightLight(target);
} }
delSelected(target) { delSelected(target) {
target.shapeFactory.hideHightLight(target);; target.shapeFactory.hideHightLight(target);;
this.$controller.storage.delete(target.model.code); this.$controller.storage.delete(target.code);
} }
clear() { clear() {
@ -48,7 +48,7 @@ export default class SelectHandle {
createSelected(target) { createSelected(target) {
if (this.$map.draggle) { if (this.$map.draggle) {
switch (target.model.type) { switch (target.type) {
case graphic.Line: case graphic.Line:
return new LineDraggable(this); return new LineDraggable(this);
case graphic.Image: case graphic.Image:

View File

@ -68,7 +68,7 @@ export default class SelectingHandle {
} }
setSelected(target) { setSelected(target) {
this.$controller.storage.set(target.model.code, target); this.$controller.storage.set(target.code, target);
target.shapeFactory.showHightLight(target); target.shapeFactory.showHightLight(target);
} }

View File

@ -15,7 +15,6 @@ export default class TransformHandle {
// 检查显隐 // 检查显隐
checkVisible(view) { checkVisible(view) {
console.log(utils.createBoundingRectCheckVisible(view, this.transform).intersect(this.rect));
return utils.createBoundingRectCheckVisible(view, this.transform).intersect(this.rect); return utils.createBoundingRectCheckVisible(view, this.transform).intersect(this.rect);
} }
@ -40,7 +39,7 @@ export default class TransformHandle {
// 更新偏移量 // 更新偏移量
updateTransform(opt) { updateTransform(opt) {
this.transform = utils.createTransform({scale: [opt.scaleRate, opt.scaleRate], position: [-opt.offsetX, -opt.offsetY], rotation: 0}); this.transform = utils.createTransform({scale: [opt.scaleRate, opt.scaleRate], position: [-opt.offsetX/opt.scaleRate, -opt.offsetY/opt.scaleRate], rotation: 0});
this.transformAll(); this.transformAll();
} }

View File

@ -66,28 +66,19 @@ export function createBoundingRect(view) {
rect.y = rect.y * scaleY + offsetY; rect.y = rect.y * scaleY + offsetY;
rect.width = rect.width * scaleX; rect.width = rect.width * scaleX;
rect.height = rect.height * scaleY; rect.height = rect.height * scaleY;
// let x1, y1, x2, y2;
// [x1, y1] = view.transformCoordToLocal(rect.x, rect.y);
// [x2, y2] = view.transformCoordToLocal(rect.x + rect.width, rect.y + rect.height);
// return new BoundingRect(x1, y1, x2-x1, y2-y1);
return rect; return rect;
} }
// 计算变化计算包围框 // 计算变化计算包围框
export function createBoundingRectCheckVisible(view, transform) { export function createBoundingRectCheckVisible(view, transform) {
const rect = view.getBoundingRect(); const rect = view.getBoundingRect();
const position = view.position;
const dx = position[0];
const dy = position[1];
const scaleX = transform[0]; const scaleX = transform[0];
const scaleY = transform[3]; const scaleY = transform[3];
const offsetX = transform[4]; const offsetX = transform[4];
const offsetY = transform[5]; const offsetY = transform[5];
rect.x = (rect.x + dx) * scaleX + offsetX; rect.x = rect.x * scaleX + offsetX;
rect.y = (rect.y + dy) * scaleY + offsetY; rect.y = rect.y * scaleY + offsetY;
rect.width = rect.width * scaleX; rect.width = rect.width * scaleX;
rect.height = rect.height * scaleY; rect.height = rect.height * scaleY;
return rect; return rect;

View File

@ -151,7 +151,7 @@ export default {
name: 'b', name: 'b',
type: 'Circle', type: 'Circle',
scale: [1, 1], scale: [1, 1],
position: [100, 0], position: [0, 0],
rotation: 0, rotation: 0,
shape: { shape: {
cx: 100, cx: 100,
@ -245,8 +245,8 @@ export default {
name: 'a', name: 'a',
type: 'Rect', type: 'Rect',
scale: [1, 1], scale: [1, 1],
position: [0, 0], position: [100, 0],
rotation: Math.PI/4, rotation: Math.PI/2,
shape: { shape: {
x: 100, x: 100,
y: 100, y: 100,
@ -268,23 +268,26 @@ export default {
scale: [0.5, 0.5], scale: [0.5, 0.5],
position: [100, 100], position: [100, 100],
rotation: Math.PI/2, rotation: Math.PI/2,
// composeCode: '1000', composeCode: '1000',
}, },
{ {
code: '101', code: '101',
type: 'Device', type: 'Device',
elementCodes: ['3', '4'], elementCodes: ['3', '4'],
scale: [0.5, 0.5], scale: [1, 1],
position: [200, 0], position: [200, 0],
rotation: 0, rotation: 0,
// composeCode: '1000' composeCode: '1000'
}, },
// { {
// code: '1000', code: '1000',
// type: 'Combine', type: 'Device',
// elementCodes: ['100', '101'], scale: [1, 1],
// composeCode: '' position: [0, 0],
// } rotation: 0,
elementCodes: ['100', '101'],
composeCode: ''
}
] ]
}, { }, {
panEnable: true, panEnable: true,