UNPKG

@visactor/vrender-core

Version:

```typescript import { xxx } from '@visactor/vrender-core'; ```

259 lines (245 loc) 12.9 kB
import { Point } from "@visactor/vutils"; import { application } from "../application"; import { Graphic, NOWORK_ANIMATE_ATTR } from "./graphic"; import { getTheme, Theme } from "./theme"; import { UpdateTag, IContainPointMode } from "../common/enums"; import { GROUP_NUMBER_TYPE } from "./constants"; import { DefaultTransform } from "./config"; import { createGroupSharedStateScope, setSharedStateScopeLocalDefinitions, setSharedStateScopeParent } from "./state/shared-state-scope"; import { markScopeActiveDescendantsDirty } from "./state/shared-state-refresh"; export var GroupUpdateAABBBoundsMode; !function(GroupUpdateAABBBoundsMode) { GroupUpdateAABBBoundsMode[GroupUpdateAABBBoundsMode.LESS_GROUP = 0] = "LESS_GROUP", GroupUpdateAABBBoundsMode[GroupUpdateAABBBoundsMode.MORE_GROUP = 1] = "MORE_GROUP"; }(GroupUpdateAABBBoundsMode || (GroupUpdateAABBBoundsMode = {})); export class Group extends Graphic { constructor(params) { super(params), this.type = "group", this.parent = null, this.isContainer = !0, this.numberType = GROUP_NUMBER_TYPE, this._childUpdateTag = UpdateTag.UPDATE_BOUNDS; } get sharedStateDefinitions() { return this._sharedStateDefinitions; } set sharedStateDefinitions(value) { if (this._sharedStateDefinitions === value) return; const previousScope = this.sharedStateScope; this._sharedStateDefinitions = value, this.ensureSharedStateScopeBound(), this.sharedStateScope && (setSharedStateScopeLocalDefinitions(this.sharedStateScope, value), markScopeActiveDescendantsDirty(this.sharedStateScope, this.stage)), previousScope !== this.sharedStateScope && this.notifyChildrenSharedStateTreeChanged(); } setMode(mode) { "3d" === mode ? this.set3dMode() : this.set2dMode(); } set3dMode() { this.in3dMode = !0; } set2dMode() { this.in3dMode = !1; } setTheme(t) { return this.theme || (this.theme = new Theme), this.theme.setTheme(t, this); } createTheme() { this.theme || (this.theme = new Theme); } visibleAll(visible) { this.setAttribute("visible", visible), this.forEachChildren((item => { item.isContainer && item.visibleAll ? item.visibleAll(visible) : item.setAttribute("visible", visible); })); } hideAll() { this.visibleAll(!1); } showAll() { this.visibleAll(!0); } containsPoint(x, y, mode) { if (mode === IContainPointMode.GLOBAL) { const point = new Point(x, y); return this.parent && this.parent.globalTransMatrix.transformPoint(point, point), this.AABBBounds.contains(point.x, point.y); } return this.AABBBounds.contains(x, y); } shouldUpdateAABBBounds() { return !!super.shouldUpdateAABBBounds() || !!(this._childUpdateTag & UpdateTag.UPDATE_BOUNDS); } tryUpdateAABBBounds() { if (!this.shouldUpdateAABBBounds()) return this._AABBBounds; this.getGraphicService().beforeUpdateAABBBounds(this, this.stage, !0, this._AABBBounds); const selfChange = this.shouldSelfChangeUpdateAABBBounds(), bounds = this.doUpdateAABBBounds(); return this.addUpdateLayoutTag(), this.getGraphicService().afterUpdateAABBBounds(this, this.stage, this._AABBBounds, this, selfChange), "empty" === this.attribute.boundsMode && bounds.clear(), bounds; } doUpdateLocalMatrix() { const {x: x = DefaultTransform.x, y: y = DefaultTransform.y, dx: dx = DefaultTransform.dx, dy: dy = DefaultTransform.dy, scaleX: scaleX = DefaultTransform.scaleX, scaleY: scaleY = DefaultTransform.scaleY, angle: angle = DefaultTransform.angle, postMatrix: postMatrix} = this.attribute; if (0 !== x || 0 !== y || 0 !== dx || 0 !== dy || 1 !== scaleX || 1 !== scaleY || 0 !== angle || postMatrix) return super.doUpdateLocalMatrix(); this._transMatrix.reset(); } getGraphicTheme() { return getTheme(this).group; } updateAABBBounds(attribute, groupTheme, aabbBounds) { const originalAABBBounds = aabbBounds; aabbBounds = aabbBounds.clone(); const {width: width, height: height, path: path, clip: clip = groupTheme.clip} = attribute; if (path && path.length ? path.forEach((g => { aabbBounds.union(g.AABBBounds); })) : null != width && null != height && aabbBounds.set(0, 0, Math.max(0, width), Math.max(0, height)), !clip) { this.forEachChildren((node => { aabbBounds.union(node.AABBBounds); })); const {scrollX: scrollX = 0, scrollY: scrollY = 0} = attribute; aabbBounds.translate(scrollX, scrollY); } return application.graphicService.updateTempAABBBounds(aabbBounds), application.graphicService.transformAABBBounds(attribute, aabbBounds, groupTheme, !1, this), originalAABBBounds.copy(aabbBounds), originalAABBBounds; } doUpdateAABBBounds() { this.updateAABBBoundsStamp++; const bounds = super.doUpdateAABBBounds(); return this.parent && this.parent.addChildUpdateBoundTag(), this._emitCustomEvent("AAABBBoundsChange"), bounds; } clearUpdateBoundTag() { this._updateTag &= UpdateTag.CLEAR_BOUNDS, this._childUpdateTag &= UpdateTag.CLEAR_BOUNDS; } addUpdateBoundTag() { this._updateTag |= UpdateTag.UPDATE_BOUNDS, this.parent && this.parent.addChildUpdateBoundTag(); } addChildUpdateBoundTag() { this._childUpdateTag & UpdateTag.UPDATE_BOUNDS || (this._childUpdateTag |= UpdateTag.UPDATE_BOUNDS, this.parent && this.parent.addChildUpdateBoundTag()); } getTheme() { return this.theme.getTheme(this); } incrementalAppendChild(node) { const data = super.appendChild(node); return data && this.syncChildSharedStateTreeBinding(data), this.addUpdateBoundTag(), this.getGraphicService().onAddIncremental(node, this, this.stage), data; } incrementalClearChild() { super.removeAllChild(), this.addUpdateBoundTag(), this.getGraphicService().onClearIncremental(this, this.stage); } _updateChildToStage(child) { return child && this.syncChildSharedStateTreeBinding(child), this.addUpdateBoundTag(), child; } appendChild(node, addStage = !0) { const data = super.appendChild(node); return data && addStage && this.syncChildSharedStateTreeBinding(data), this.addUpdateBoundTag(), data; } insertBefore(newNode, referenceNode) { return this._updateChildToStage(super.insertBefore(newNode, referenceNode)); } insertAfter(newNode, referenceNode) { return this._updateChildToStage(super.insertAfter(newNode, referenceNode)); } insertInto(newNode, idx) { return this._updateChildToStage(super.insertInto(newNode, idx)); } removeChild(child, highPerformance = !1) { const data = super.removeChild(child); return data ? highPerformance ? (child.detachStageForRelease(), data) : (this.getGraphicService().onRemove(child), child.setStage(null, null), this.addUpdateBoundTag(), data) : data; } removeAllChild(deep = !1) { const children = this.children.slice(); this.forEachChildren((child => { this.getGraphicService().onRemove(child), deep && child.isContainer && child.removeAllChild(deep); })), super.removeAllChild(), children.forEach((child => { child.setStage(null, null); })), this.addUpdateBoundTag(); } setStage(stage, layer) { var _a, _b, _c, _d, _e, _f; const graphicService = null !== (_c = null !== (_a = null == stage ? void 0 : stage.graphicService) && void 0 !== _a ? _a : null === (_b = this.stage) || void 0 === _b ? void 0 : _b.graphicService) && void 0 !== _c ? _c : application.graphicService, needsSharedStateTreeSync = this.hasSharedStateDefinitions() || this.sharedStateScope || (null === (_d = this.currentStates) || void 0 === _d ? void 0 : _d.length) || this.boundSharedStateScope || (null === (_e = this.registeredActiveScopes) || void 0 === _e ? void 0 : _e.size) || this.sharedStateDirty; if (this.stage !== stage) return this.stage = stage, this.layer = layer, needsSharedStateTreeSync && (this.ensureSharedStateScopeBound(), this.syncSharedStateScopeBindingOnTreeChange(!0)), this.setStageToShadowRoot(stage, layer), this._onSetStage && this._onSetStage(this, stage, layer), null === (_f = null == graphicService ? void 0 : graphicService.onSetStage) || void 0 === _f || _f.call(graphicService, this, stage), void this.notifyChildrenSharedStateTreeChanged(); const layerChanged = this.layer !== layer; layerChanged && (this.layer = layer), needsSharedStateTreeSync ? (this.ensureSharedStateScopeBound(), this.syncSharedStateScopeBindingOnTreeChange(!0), this.notifyChildrenSharedStateTreeChanged()) : layerChanged && this.notifyChildrenSharedStateTreeChanged(); } addUpdatePositionTag() { super.addUpdatePositionTag(), this.forEachChildren((g => { g.isContainer && g.addUpdateGlobalPositionTag(); })); } addUpdateGlobalPositionTag() { super.addUpdateGlobalPositionTag(), this.forEachChildren((g => { g.isContainer && g.addUpdateGlobalPositionTag(); })); } tryUpdateGlobalTransMatrix(clearTag = !0) { if (this.shouldUpdateGlobalMatrix()) { if (this._globalTransMatrix) { if (this.parent) { const m = this.parent.globalTransMatrix; this._globalTransMatrix.setValue(m.a, m.b, m.c, m.d, m.e, m.f); } } else this._globalTransMatrix = this.parent ? this.parent.globalTransMatrix.clone() : this.transMatrix.clone(); this.doUpdateGlobalMatrix(), clearTag && this.clearUpdateGlobalPositionTag(); } return this._globalTransMatrix; } shouldUpdateGlobalMatrix() { return !!(this._updateTag & UpdateTag.UPDATE_GLOBAL_MATRIX); } _getChildByName(name, deep) { return this.find((node => node.name === name), deep); } createOrUpdateChild(graphicName, attributes, graphicType) { let graphic = this._getChildByName(graphicName); return graphic ? graphic.setAttributes(attributes) : (graphic = application.graphicService.creator[graphicType](attributes), graphic.name = graphicName, this.add(graphic)), graphic; } clone() { return new Group(Object.assign({}, this.attribute)); } getNoWorkAnimateAttr() { return Group.NOWORK_ANIMATE_ATTR; } release(all) { all && this.forEachChildren((g => { g.release(all); })), super.release(); } detachStageForRelease() { super.detachStageForRelease(), this.sharedStateScope = void 0, this.forEachChildren((item => { item.detachStageForRelease(); })); } ensureSharedStateScopeBound() { var _a, _b, _c; if (!this.hasSharedStateDefinitions()) return void (this.sharedStateScope = void 0); const parentScope = null !== (_b = null === (_a = this.parent) || void 0 === _a ? void 0 : _a.sharedStateScope) && void 0 !== _b ? _b : null === (_c = this.stage) || void 0 === _c ? void 0 : _c.rootSharedStateScope; this.sharedStateScope ? (this.sharedStateScope.ownerStage = this.stage, setSharedStateScopeParent(this.sharedStateScope, parentScope), this.sharedStateScope.localStateDefinitions !== this._sharedStateDefinitions && setSharedStateScopeLocalDefinitions(this.sharedStateScope, this._sharedStateDefinitions)) : this.sharedStateScope = createGroupSharedStateScope(this, parentScope, this._sharedStateDefinitions); } hasSharedStateDefinitions() { return !!this._sharedStateDefinitions && Object.keys(this._sharedStateDefinitions).length > 0; } notifyChildrenSharedStateTreeChanged() { this.forEachChildren((item => { this.syncChildSharedStateTreeBinding(item); })); } syncChildSharedStateTreeBinding(child) { child.onParentSharedStateTreeChanged(this.stage, this.layer); } onParentSharedStateTreeChanged(stage, layer) { var _a; this.stage === stage && this.layer === layer ? (this.ensureSharedStateScopeBound(), this.syncSharedStateScopeBindingOnTreeChange(!!(null === (_a = this.currentStates) || void 0 === _a ? void 0 : _a.length)), this.notifyChildrenSharedStateTreeChanged()) : this.setStage(stage, layer); } } Group.NOWORK_ANIMATE_ATTR = NOWORK_ANIMATE_ATTR; export function createGroup(attributes) { return new Group(attributes); } //# sourceMappingURL=group.js.map