@visactor/vrender-core
Version:
```typescript import { xxx } from '@visactor/vrender-core'; ```
126 lines (117 loc) • 7.58 kB
JavaScript
import { AABBBounds, Matrix, Point } from "@visactor/vutils";
import { foreach } from "../common/sort";
import { getTheme } from "../graphic/theme";
import { DefaultAttribute } from "../graphic/config";
import { mat3Tomat4, multiplyMat4Mat4 } from "../common/matrix";
import { mat4Allocate, matrixAllocate } from "../allocator/matrix-allocate";
import { application } from "../application";
const EMPTY_PICK_ITEM_INTERCEPTOR_PROVIDER = {
getContributions: () => []
}, EMPTY_PICK_SERVICE_INTERCEPTOR_PROVIDER = {
getContributions: () => []
};
export class DefaultPickService {
constructor(pickItemInterceptorContributions = EMPTY_PICK_ITEM_INTERCEPTOR_PROVIDER, pickServiceInterceptorContributions = EMPTY_PICK_SERVICE_INTERCEPTOR_PROVIDER, deps = {}) {
var _a, _b, _c, _d, _e;
this.pickItemInterceptorContributions = pickItemInterceptorContributions, this.pickServiceInterceptorContributions = pickServiceInterceptorContributions,
this.type = "default", this.global = null !== (_a = deps.global) && void 0 !== _a ? _a : application.global,
this.pickerRegistry = deps.pickerRegistry, this.pickItemInterceptorContributions = null !== (_c = null !== (_b = deps.pickItemInterceptorContributions) && void 0 !== _b ? _b : this.pickItemInterceptorContributions) && void 0 !== _c ? _c : EMPTY_PICK_ITEM_INTERCEPTOR_PROVIDER,
this.pickServiceInterceptorContributions = null !== (_e = null !== (_d = deps.pickServiceInterceptorContributions) && void 0 !== _d ? _d : this.pickServiceInterceptorContributions) && void 0 !== _e ? _e : EMPTY_PICK_SERVICE_INTERCEPTOR_PROVIDER,
this.pickerMap = new Map;
}
reInit() {
this._init();
}
_init() {
this.pickerRegistry && (this.pickerMap.clear(), this.pickerRegistry.getAll().forEach((item => {
this.pickerMap.set(item.numberType, item);
}))), this.InterceptorContributions = this.pickItemInterceptorContributions.getContributions().sort(((a, b) => a.order - b.order)),
this.pickerServiceInterceptorContributions = this.pickServiceInterceptorContributions.getContributions().sort(((a, b) => a.order - b.order));
}
pick(graphics, point, params) {
let result = {
graphic: null,
group: null
};
params.pickerService = this;
const w = params.bounds.width(), h = params.bounds.height();
if (!(new AABBBounds).setValue(0, 0, w, h).containsPoint(point)) return result;
this.pickContext && (this.pickContext.inuse = !0), params.pickContext = this.pickContext,
this.pickContext && this.pickContext.clearMatrix(!0, 1);
const parentMatrix = new Matrix(1, 0, 0, 1, 0, 0);
let group;
for (let i = graphics.length - 1; i >= 0 && (result = graphics[i].isContainer ? this.pickGroup(graphics[i], point, parentMatrix, params) : this.pickItem(graphics[i], point, parentMatrix, params),
!result.graphic); i--) group || (group = result.group);
if (result.graphic || (result.group = group), this.pickContext && (this.pickContext.inuse = !1),
this.pickerServiceInterceptorContributions.length) for (let i = 0; i < this.pickerServiceInterceptorContributions.length; i++) {
const drawContribution = this.pickerServiceInterceptorContributions[i];
drawContribution.afterPickItem && (result = drawContribution.afterPickItem(result, this, point, params, {
parentMatrix: parentMatrix
}));
}
return result;
}
containsPoint(graphic, point, params) {
var _a;
return !!(null === (_a = this.pickItem(graphic, point, null, null != params ? params : {
pickContext: this.pickContext,
pickerService: this
})) || void 0 === _a ? void 0 : _a.graphic);
}
pickGroup(group, point, parentMatrix, params) {
let result = {
group: null,
graphic: null
};
if (!1 === group.attribute.visibleAll) return result;
const context = params.pickContext, lastMatrix = context.modelMatrix;
if (context.camera) {
const m = group.transMatrix, matrix = mat4Allocate.allocate();
if (mat3Tomat4(matrix, m), lastMatrix) {
if (matrix) {
const m = mat4Allocate.allocate();
context.modelMatrix = multiplyMat4Mat4(m, lastMatrix, matrix), mat4Allocate.free(matrix);
}
} else mat3Tomat4(matrix, group.globalTransMatrix), context.modelMatrix = matrix;
}
if (this.InterceptorContributions.length) for (let i = 0; i < this.InterceptorContributions.length; i++) {
const drawContribution = this.InterceptorContributions[i];
if (drawContribution.beforePickItem) {
const result = drawContribution.beforePickItem(group, this, point, params, {
parentMatrix: parentMatrix
});
if (result) return context.modelMatrix !== lastMatrix && mat4Allocate.free(context.modelMatrix),
context.modelMatrix = lastMatrix, result;
}
}
const transMatrix = group.transMatrix, currentGroupMatrix = matrixAllocate.allocateByObj(parentMatrix), newPoint = new Point(point.x, point.y);
currentGroupMatrix.transformPoint(newPoint, newPoint);
const insideGroup = group.AABBBounds.containsPoint(newPoint);
if (!insideGroup && !group.stage.camera) return result;
const pickedItem = this.pickItem(group, newPoint.clone(), parentMatrix, params);
pickedItem && pickedItem.graphic && (result.graphic = pickedItem.graphic, result.params = pickedItem.params);
const groupPicked = !1 !== group.attribute.pickable && insideGroup;
return currentGroupMatrix.multiply(transMatrix.a, transMatrix.b, transMatrix.c, transMatrix.d, transMatrix.e, transMatrix.f),
!1 === group.attribute.childrenPickable || pickedItem && pickedItem.graphic || foreach(group, DefaultAttribute.zIndex, (graphic => {
if (graphic.isContainer) {
const newPoint = new Point(point.x, point.y), theme = getTheme(group).group, {scrollX: scrollX = theme.scrollX, scrollY: scrollY = theme.scrollY} = group.attribute;
newPoint.x -= scrollX, newPoint.y -= scrollY, result = this.pickGroup(graphic, newPoint, currentGroupMatrix, params);
} else {
const newPoint = new Point(point.x, point.y);
currentGroupMatrix.transformPoint(newPoint, newPoint);
const theme = getTheme(group).group, {scrollX: scrollX = theme.scrollX, scrollY: scrollY = theme.scrollY} = group.attribute;
newPoint.x -= scrollX, newPoint.y -= scrollY;
const pickedItem = this.pickItem(graphic, newPoint, parentMatrix, params);
pickedItem && pickedItem.graphic && (result.graphic = pickedItem.graphic, result.params = pickedItem.params);
}
return !!result.graphic || !!result.group;
}), !0, !!context.camera), context.modelMatrix !== lastMatrix && mat4Allocate.free(context.modelMatrix),
context.modelMatrix = lastMatrix, result.graphic || result.group || !groupPicked || group.stage.camera || (result.group = group),
matrixAllocate.free(currentGroupMatrix), result;
}
selectPicker(graphic) {
const picker = this.pickerMap.get(graphic.numberType);
return picker || null;
}
}
//# sourceMappingURL=picker-service.js.map